React Syntax Highlighter: Themes, Line Numbers (v16)

November 11, 2026 · 10 min read

React Syntax Highlighter: Setup, Themes, and Line Numbers

To use React Syntax Highlighter, install the react-syntax-highlighter package and pass a code string to its component, which colours it with Prism or highlight.js grammars. This tutorial covers version 16: picking a build, applying a theme, numbering and highlighting lines, and keeping the bundle small.

Install react-syntax-highlighter and Render One Code Block

Install the package, plus the community type definitions if you use TypeScript. The package doesn't ship its own .d.ts files, so the README recommends @types/react-syntax-highlighter.

npm install react-syntax-highlighter
npm install --save-dev @types/react-syntax-highlighter

Then render a block. The code goes in as children, and language tells the grammar how to tokenise it.

import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { oneDark } from 'react-syntax-highlighter/dist/esm/styles/prism';

const snippet = `function greet(name) {
  return 'Hello, ' + name;
}`;

export function Example() {
  return (
    <SyntaxHighlighter language="javascript" style={oneDark}>
      {snippet}
    </SyntaxHighlighter>
  );
}

This renders a pre element with the One Dark background, and every keyword, string and operator sits in its own coloured span. The colours are inline styles, so you don't load a stylesheet. Pass language="text" to get plain monospaced text with no tokenising.

The package publishes both dist/esm and dist/cjs folders. Use the esm paths with Vite, Next.js or webpack 5, and keep the cjs paths for older tooling that expects CommonJS. Versions 16.1.1 and 16.1.2 added .js extensions that some ESM files were missing, so update if strict ESM tooling reports unresolved imports.

Language names follow the grammar's own identifiers, which mostly match the words you type after a Markdown fence. Our code block languages guide lists the common ones.

Should You Use the Prism or highlight.js Build?

The package wraps two engines. The default export uses highlight.js through lowlight. The Prism export uses Prism grammars through refractor, which moved to version 5 in react-syntax-highlighter 16.0.0 for security fixes. Both builds accept the same props, so switching later costs you one import line and a new theme.

Here are the builds the README documents. Names that start with Prism use Prism, and the rest use highlight.js.

ImportWhat you get
default exportEvery bundled hljs language. A quick start when you don't need JSX.
PrismEvery bundled Prism language, including jsx and tsx. Suits React or TypeScript code.
Light, PrismLightOnly the languages you register. Suits a known language list where size matters.
LightAsync, PrismAsyncLightLanguages loaded on demand. Suits code blocks below the fold.

We prefer the Prism build for React projects. Prism ships dedicated jsx and tsx grammars, and the README singles out JSX as the main benefit of the Prism build. The hljs list in the package has no jsx entry. Pick the default build if you mostly show languages like SQL or YAML and want highlight.js themes.

React Syntax Highlighter Themes: Prism and hljs Style Folders

Themes are plain JavaScript objects in two folders: dist/esm/styles/prism and dist/esm/styles/hljs. The theme must match the build. Give an hljs theme to the Prism build and you get the theme's background with uncoloured tokens, because the class names don't line up.

// Prism build: oneDark, oneLight, vscDarkPlus, duotoneDark ...
import { vscDarkPlus } from 'react-syntax-highlighter/dist/esm/styles/prism';

// highlight.js build: atomOneDark, docco, github ...
import { atomOneDark } from 'react-syntax-highlighter/dist/esm/styles/hljs';

The full lists live in the repo's AVAILABLE_STYLES_PRISM.MD and its hljs counterpart. To adjust spacing without editing a theme, use customStyle, which merges into the outer pre element. codeTagProps passes props to the inner code element.

<SyntaxHighlighter
  language="tsx"
  style={vscDarkPlus}
  customStyle={{ borderRadius: 8, padding: '1.25rem', fontSize: 14 }}
>
  {snippet}
</SyntaxHighlighter>

If you'd rather style tokens with your own CSS, set useInlineStyles={false}. The component then outputs class names such as hljs-keyword (or Prism's token keyword) and leaves token colours to your stylesheet. On the Prism build, the inner code element still gets the theme's base styles inline, so pass your own codeTagProps if your CSS should own those too.

Add Line Numbers with showLineNumbers

Set showLineNumbers and each line gets a number. Since version 14, showInlineLineNumbers defaults to true, so the numbers render inside each line rather than in a separate column. Inline numbers have user-select: none, so copying the code doesn't copy them.

<SyntaxHighlighter
  language="javascript"
  style={oneDark}
  showLineNumbers
  showInlineLineNumbers={false}
  startingLineNumber={40}
  lineNumberStyle={(n) => ({ color: n === 42 ? '#e5c07b' : '#5c6370' })}
>
  {snippet}
</SyntaxHighlighter>

Here numbering starts at 40, which helps when you quote an excerpt from a longer file. lineNumberStyle accepts an object or a function of the line number, so line 42 gets a gold number and the rest stay grey.

Why showInlineLineNumbers={false}? Inline numbers carry Prism's comment class, so a Prism theme's comment colour overrides any color you set (we confirmed this with oneDark). The separate column takes your colours as written, but it doesn't add user-select: none.

How Do You Highlight Specific Lines in react-syntax-highlighter?

Line-level styling needs two props together. wrapLines wraps every line in its own span, and lineProps returns the props for that span. Without wrapLines, the component ignores lineProps.

const highlighted = new Set([2, 3]);

<SyntaxHighlighter
  language="javascript"
  style={oneDark}
  showLineNumbers
  wrapLines
  lineProps={(n) => ({
    style: {
      display: 'block',
      background: highlighted.has(n) ? 'rgba(255, 255, 255, 0.08)' : undefined,
    },
  })}
>
  {snippet}
</SyntaxHighlighter>

Lines 2 and 3 get a faint band across the full width. The display: 'block' matters, because a background on an inline span only covers the text. Keep showLineNumbers on, too: without it, lineProps receives false instead of a line number. The same function can draw a diff view by reading the first character of each line.

const lines = diff.split('\n');

lineProps={(n) => {
  const first = lines[n - 1]?.[0];
  const bg = first === '+' ? 'rgba(46, 160, 67, 0.2)'
    : first === '-' ? 'rgba(248, 81, 73, 0.2)'
    : undefined;
  return { style: { display: 'block', background: bg } };
}}

Here diff is the same string you pass as children. The lookup assumes showLineNumbers is on and numbering starts at 1, because n is the displayed line number.

For long lines, wrapLongLines sets white-space: pre-wrap on the code element and turns on wrapLines for you. That's enough with hljs themes. Prism themes set their own white-space: pre on that element, which wins, so also pass lineProps={{ style: { whiteSpace: 'pre-wrap' } }}. Leave both off if you want long lines to scroll sideways.

Shrink the Bundle with the Light and Async Builds

The Prism and default imports include every language. For a docs site that shows four languages, that's mostly dead weight, and it's the main reason React Syntax Highlighter gets called heavy. The light builds start empty, and you register only the grammars you use.

import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter';
import tsx from 'react-syntax-highlighter/dist/esm/languages/prism/tsx';
import bash from 'react-syntax-highlighter/dist/esm/languages/prism/bash';
import oneDark from 'react-syntax-highlighter/dist/esm/styles/prism/one-dark';

SyntaxHighlighter.registerLanguage('tsx', tsx);
SyntaxHighlighter.registerLanguage('bash', bash);

Importing the theme from its own file keeps the other themes out too. On PrismLight, an unregistered language doesn't throw: the block renders as plain text, a common reason a block shows up without colour. The hljs Light build instead guesses among the grammars you did register, so it can colour code as the wrong language. The light builds also have no default theme, so always pass style.

Not sure a grammar exists? The full builds expose a supportedLanguages array, so Prism.supportedLanguages.includes('graphql') returns true before you go looking for the language file to register.

PrismAsyncLight and LightAsync go further and load the engine and grammars through dynamic import(). The README notes this needs a bundler that supports dynamic imports. The trade-off is visible: until the chunks arrive, your code shows as plain text, with line numbers if you turned them on.

One limitation to plan for: with inline styles, a light/dark theme switch means passing a different style object from React state. CSS media queries can't reach it unless you turn inline styles off.

A Reusable CodeBlock Component for Next.js and Plain React

Most apps want one wrapper that fixes the theme and language list, then adds line highlighting and a copy button. This version uses the light build from the previous section.

'use client';

import { useState } from 'react';
import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter';
import tsx from 'react-syntax-highlighter/dist/esm/languages/prism/tsx';
import oneDark from 'react-syntax-highlighter/dist/esm/styles/prism/one-dark';

SyntaxHighlighter.registerLanguage('tsx', tsx);

type Props = { code: string; language?: string; highlight?: number[] };

export function CodeBlock({ code, language = 'tsx', highlight = [] }: Props) {
  const [copied, setCopied] = useState(false);
  const marked = new Set(highlight);

  async function copy() {
    await navigator.clipboard.writeText(code);
    setCopied(true);
    setTimeout(() => setCopied(false), 1500);
  }

  return (
    <div style={{ position: 'relative' }}>
      <button onClick={copy} style={{ position: 'absolute', top: 8, right: 8 }}>
        {copied ? 'Copied' : 'Copy'}
      </button>
      <SyntaxHighlighter
        language={language}
        style={oneDark}
        showLineNumbers
        wrapLines
        lineProps={(n) => ({
          style: { display: 'block', background: marked.has(n) ? '#2c313a' : undefined },
        })}
        customStyle={{ margin: 0, borderRadius: 8 }}
      >
        {code.trimEnd()}
      </SyntaxHighlighter>
    </div>
  );
}

Use it as <CodeBlock code={source} highlight={[3, 4]} />. The 'use client' line is there because the copy button needs state and a click handler, and the React docs on use client explain where that boundary goes. The async builds are class components that load grammars after mount, so they also belong on the client side of that boundary.

In plain React with Vite, drop the directive and nothing else changes. To render Markdown fences through this component, pass it to react-markdown's components prop, as shown in our react-markdown guide. MDX accepts a similar components prop, which our MDX post introduces. On mobile, React Native Markdown display libraries style fenced code with native components instead.

Is react-syntax-highlighter Still Maintained? Alternatives Worth Knowing

Yes. Version 16.0.0 landed in October 2025 with the refractor 5 upgrade, and 16.1.2 followed on 8 October 2026 with an ESM import fix, per the release notes. The highlight.js dependency is still pinned to the 10.x line, so newer highlight.js grammars and themes won't appear in the default build.

If that matters to you, prism-react-renderer gives you a render-prop API over Prism. Shiki, also available for React through react-shiki, uses VS Code's TextMate grammars and suits build-time highlighting. Each makes different trade-offs on bundle size and theming, so test with your own code samples. For client-rendered code blocks with line-level control, react-syntax-highlighter is still the one we reach for first. For the engines themselves, our syntax highlighter comparison puts highlight.js, Prism and Shiki side by side.

React Syntax Highlighter comes down to three decisions: Prism grammars for JSX and TSX, a light build with only the languages you register, and wrapLines for highlighted lines. Write your snippets as Markdown fences in the editor, where Ctrl+E (Cmd+E on Mac) inserts a fenced block. Its preview shows code blocks without colour. For a static HTML snippet with line numbers and highlight.js themes, try the code block generator.