|
| 1 | +Enhance your React-Notion-X projects with a versatile code block component. |
| 2 | +This component offers out-of-the-box support for multiple programming languages and automatically adapts to light and dark themes, |
| 3 | +powered by [Shiki](https://github.com/shikijs/shiki). |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +You can install the react-notion-x-code-block package using npm, yarn, or pnpm: |
| 8 | + |
| 9 | +```shell |
| 10 | +npm install react-notion-x-code-block |
| 11 | + |
| 12 | +yarn add react-notion-x-code-block |
| 13 | + |
| 14 | +pnpm install react-notion-x-code-block |
| 15 | +``` |
| 16 | + |
| 17 | +## How to use |
| 18 | + |
| 19 | +To use the component, import Code from the package and include it in your NotionRenderer components object: |
| 20 | + |
| 21 | +```tsx |
| 22 | +import { Code } from "react-notion-x-code-block"; |
| 23 | + |
| 24 | +<NotionRenderer |
| 25 | + // ... |
| 26 | + components={{ |
| 27 | + Code |
| 28 | + }} |
| 29 | +/>; |
| 30 | +``` |
| 31 | + |
| 32 | +### Adapting to Theme Changes |
| 33 | + |
| 34 | +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. |
| 35 | + |
| 36 | +```css |
| 37 | +/* file: style.css */ |
| 38 | +@media (prefers-color-scheme: dark) { |
| 39 | + .shiki, |
| 40 | + .shiki span { |
| 41 | + color: var(--shiki-dark) !important; |
| 42 | + background-color: var(--shiki-dark-bg) !important; |
| 43 | + /* Optional, if you also want font styles */ |
| 44 | + font-style: var(--shiki-dark-font-style) !important; |
| 45 | + font-weight: var(--shiki-dark-font-weight) !important; |
| 46 | + text-decoration: var(--shiki-dark-text-decoration) !important; |
| 47 | + } |
| 48 | +``` |
| 49 | + |
| 50 | +Class-based Dark Mode |
| 51 | + |
| 52 | +```css |
| 53 | +/* file: style.css */ |
| 54 | +html.dark .shiki, |
| 55 | +html.dark .shiki span { |
| 56 | + color: var(--shiki-dark) !important; |
| 57 | + background-color: var(--shiki-dark-bg) !important; |
| 58 | + /* Optional, if you also want font styles */ |
| 59 | + font-style: var(--shiki-dark-font-style) !important; |
| 60 | + font-weight: var(--shiki-dark-font-weight) !important; |
| 61 | + text-decoration: var(--shiki-dark-text-decoration) !important; |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +And then import it to the page: |
| 66 | + |
| 67 | +```tsx |
| 68 | +import { Code } from "react-notion-x-code-block"; |
| 69 | +import "./style.css"; |
| 70 | + |
| 71 | +<NotionRenderer |
| 72 | + // ... |
| 73 | + components={{ |
| 74 | + Code |
| 75 | + }} |
| 76 | +/>; |
| 77 | +``` |
0 commit comments