React Shiki Syntax Highlighter

0.9.3 · active · verified Tue Apr 21

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.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the `ShikiHighlighter` component to display and dynamically update syntax-highlighted TypeScript code with a specified theme.

import ShikiHighlighter from "react-shiki";
import { useState } from 'react';

function CodeExample() {
  const [code, setCode] = useState(`function greet(name: string): string {
  return \`Hello, ${name}!\`;
}

const message = greet("World");
console.log(message);
`);

  return (
    <div>
      <label htmlFor="code-input">Edit code:</label>
      <textarea
        id="code-input"
        value={code}
        onChange={(e) => setCode(e.target.value)}
        rows={10}
        cols={50}
        style={{ width: '100%', minHeight: '150px' }}
      />
      <h2>Highlighted Code:</h2>
      <ShikiHighlighter
        language="typescript"
        theme="ayu-dark"
        showLanguage={true}
        wrap={true}
      >
        {code.trim()}
      </ShikiHighlighter>
    </div>
  );
}

view raw JSON →