react-shiki is a performant client-side syntax highlighting component and hook for React, leveraging the Shiki library. As of version 0.9.3, it offers flexible output options, including React elements to avoid `dangerouslySetInnerHTML` or raw HTML strings for improved performance. The package provides multiple bundle choices (Full, Web, Core) to optimize bundle size depending on the languages and themes required. It fully supports custom TextMate themes, languages, and Shiki transformers. Key features include dynamic language and theme imports for optimal performance, optional line numbers, and performance optimizations like throttling for real-time highlighting. The project maintains a regular patch release cadence, with minor versions introducing features such as output format selection and regex engine customization, ensuring active development and feature expansion.
npm install react-shikiVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use the `ShikiHighlighter` component to display and dynamically update syntax-highlighted TypeScript code with a specified theme.
Update your custom CSS to use the new `rs-` prefixed classnames and CSS variables (e.g., `rs-line`, `rs-line-number`, `--rs-line-number-color`). Consider utilizing CSS `@layer base` for better specificity management.
For smaller bundles, import from `react-shiki/web` (~695KB gzipped) or `react-shiki/core` (minimal bundle) and explicitly load only the required languages and themes using the `loadLanguage` and `loadTheme` configuration options.
For offline use or controlled environments, preload languages and themes during application initialization using Shiki's API or configure your bundler to include them. Ensure your CSP allows network requests to the Shiki assets if not self-hosted.
If performance is critical for large code blocks and you trust the input, set the `outputFormat` prop on `ShikiHighlighter` or the `outputFormat` option in `useShikiHighlighter` to `'html'`.
Ensure the `language` prop matches a supported Shiki language ID. If it's a custom language, load it using `loadLanguage` or provide it via the `languages` prop.
Verify the import path is correct (`react-shiki/web` or `react-shiki/core`). Ensure your `tsconfig.json` or bundler configuration is set up to resolve module paths correctly for sub-path exports. Restarting your IDE might also help.
Ensure you have imported the minimal default styles (if desired) and that your component props (`code`, `language`, `theme`) are correctly passed. Check browser developer tools for CSS conflicts or missing styles. Review the `warnings` section for potential CSS class name changes.