React component for code syntax highlighting.
Why another code syntax highlighting package?
- This package is simple to use. It is very lightweight, it essentially just ships a single React component.
- The
highlight.jsdependency is loaded from CDN asynchronously. highlight.jsis run in a Web Worker, so it doesn't block the main thread.- No layout shifts before the code is highlighted. While the code is being highlighted in the Web Worker, the component renders plain text with the correct dimensions.
npm i code-colors-reactimport {ColorTokens} from 'code-colors-react';
<pre>
<ColorTokens code={"console.log('hello world');"} lang={'js'} />
</pre>code— The code to highlight.lang— The language of the code. If omitted, it will try to autodetect the language.prefix— The prefix to use for the CSS classes. Defaults tohljs-.as— The root element to render. Defaults to"code".
To style the highlighted code, you can use any of the pre-defined themes from
highlight.js.
You can do it in JavaScript like so:
import {loadCss} from 'thingies/lib/loadCss';
const theme = 'github';
loadCss(`https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.8.0/build/styles/${theme}.min.css`, 'hljs');