npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-notion-x-code-block

v0.4.0

Published

Enhance your React-Notion-X projects with a code block component. This component offers out-of-the-box support for multiple programming languages and automatically adapts to light and dark themes

Downloads

49

Readme

Enhance your React-Notion-X projects with a versatile code block component. This component offers out-of-the-box support for multiple programming languages and automatically adapts to light and dark themes, powered by Shiki.

Install

You can install the react-notion-x-code-block package using npm, yarn, or pnpm:

npm install react-notion-x-code-block

yarn add react-notion-x-code-block

pnpm install react-notion-x-code-block

How to use

To use the component, import Code from the package and include it in your NotionRenderer components object:

import { Code } from "react-notion-x-code-block";
import { NotionRenderer } from "react-notion-x";

<NotionRenderer
  // ...
  components={{
    Code
  }}
/>;

Adapting to Theme Changes

To ensure the code block styles automatically adjust to your theme mode (light or dark), define CSS style according to the method you use to achieve dark mode.

/* file: style.css */
@media (prefers-color-scheme: dark) {
  .shiki,
  .shiki span {
    color: var(--shiki-dark) !important;
    background-color: var(--shiki-dark-bg) !important;
    /* Optional, if you also want font styles */
    font-style: var(--shiki-dark-font-style) !important;
    font-weight: var(--shiki-dark-font-weight) !important;
    text-decoration: var(--shiki-dark-text-decoration) !important;
  }
}

Class-based Dark Mode

/* file: style.css */
html.dark .shiki,
html.dark .shiki span {
  color: var(--shiki-dark) !important;
  background-color: var(--shiki-dark-bg) !important;
  /* Optional, if you also want font styles */
  font-style: var(--shiki-dark-font-style) !important;
  font-weight: var(--shiki-dark-font-weight) !important;
  text-decoration: var(--shiki-dark-text-decoration) !important;
}

And then import it to the page:

import { Code } from "react-notion-x-code-block";
import { NotionRenderer } from "react-notion-x";

import "./style.css";

<NotionRenderer
  // ...
  components={{
    Code
  }}
/>;

Personalization settings

Since NotionRenderer will only accept react components as props, we need to wrapper Code component and set specific settings.

Specific theme

import { type CodeBlock, ExtendedRecordMap } from "notion-types";
import { Code } from "react-notion-x-code-block";

function PersonalizedCode({ block }: { block: CodeBlock }) {
  return (
    <Code
      block={block}
      themes={{
        light: "material-theme-lighter",
        dark: "material-theme-ocean"
      }}
    />
  );
}

Hide copy code button

import { type CodeBlock, ExtendedRecordMap } from "notion-types";
import { Code } from "react-notion-x-code-block";

function PersonalizedCode({ block }: { block: CodeBlock }) {
  return <Code block={block} showCopy={false} />;
}

API

| Property | Description | Type | Default | | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | --------- | -------------------------------------------- | | block | Receives render code content from NotionRenderer | CodeBlock | - | | className | Additional class for Code | string | - | | defaultLanguage | Default programming language if not specified in block | string | typescript | | themes | Themes for rendering code | object | {light: "catppuccin-latte", dark: "dracula"} | | IntersectionObserverOptions | Manage the conditions under which the highlighting of a code block should be triggered (Need lazyLoading property to be true) | object | {rootMargin: "0px",threshold: 0.1} | | showCopy | Whether to show the copy button on the top right corner | boolean | true | | showLangLabel | Whether to show the language type label on the top left corner | boolean | true | | lazyLoading | Whether to run highlighting rendering when a code block is within viewport | boolean | true |

Run the Example

  1. Install dependencies pnpm i
  2. Build the package by running pnpm build.
  3. Navigate to the example package directory with cd example.
  4. Start the example application using pnpm dev.
  5. Open your web browser and go to http://localhost:5173 to view the application.

Development

  1. Execute the command pnpm watch. This initiates continuous monitoring of file modifications, enabling automatic compilation.
  2. Navigate to the example folder and execute pnpm dev. This starts the Vite React project, which automatically refreshes upon any changes made in the main packages.