Prism code highlight
Installation
Package depends on @mantine/core
and @mantine/hooks
.
Install with yarn:
Install with npm:
Usage
Use Prism component to highlight code with Mantine theme styles. Component uses prism-react-renderer under the hood and supports light and dark theme, it is used in Mantine docs to display all code examples.
Line numbers
Set withLineNumbers
prop to display line numbers:
Lines highlight
To highlight individual lines use highlightLines
prop with object containing
line numbers as keys and highlight options as values. Highlight options include
color from theme.colors
and label which replaces line number:
Copy button
To remove copy button set noCopy
prop.
Copy button labels can be changed with copyLabel
and copiedLabel
props:
Native scrollbars
By default, Prism uses ScrollArea component to handle overflow content,
to replace it with native scrollbars set scrollAreaComponent="div"
:
<p>Long code that will force Prism to have a horizontal scrollbar, by default, scroll behavior is handled by ScrollArea component, but it can be changed to native browser scrollbars</p>
With tabs
To display multiple files use Prism.Tabs
component, it supports the same props as Tabs component:
Force dark/light theme
You can force dark/light color scheme by setting colorScheme
prop:
Custom themes
Use getPrismTheme
prop to change prism colors. You can use premade themes from prism-react-renderer
package or build your own using Mantine theme colors. getPrismTheme
prop accepts
a function with two arguments: first is Mantine theme object, second is color scheme (light
or dark
).
The function must return prism theme object:
Languages
Component supports all languages which are supported by prism-react-renderer:
Load additional languages
View Java language example on codesandbox
By default, prism-react-renderer
only includes an arbitrary subset of the languages that Prism supports.
You can add support for more by including their definitions from the main prismjs
package:
Prism component props
Name | Type | Description |
---|---|---|
children * | string | Code which will be highlighted |
colorScheme | "light" | "dark" | Force color scheme, defaults to theme.colorScheme |
copiedLabel | string | Copy button tooltip in copied state |
copyLabel | string | Copy button tooltip |
getPrismTheme | (theme: MantineTheme, colorScheme: "light" | "dark") => PrismTheme | Provide custom color scheme |
highlightLines | Record<string, { color: DefaultMantineColor; label?: string; }> | Highlight line at given line number with color from theme.colors |
language * | "c" | "go" | "markup" | "bash" | "clike" | "cpp" | "css" | "javascript" | "jsx" | "coffeescript" | "actionscript" | "css-extr" | "diff" | "git" | "graphql" | "handlebars" | "json" | "less" | "makefile" | "markdown" | "objectivec" | "ocaml" | "python" | "reason" | "sass" | "scss" | "sql" | "stylus" | "tsx" | "typescript" | "wasm" | "yaml" | Programming language that should be highlighted |
noCopy | boolean | True to remove copy to clipboard button |
radius | number | "xs" | "sm" | "md" | "lg" | "xl" | Key of theme.radius or any valid CSS value to set border-radius, theme.defaultRadius by default |
scrollAreaComponent | any | Change scroll area component |
trim | boolean | Defines whether the code should be trimmed, defaults to true |
withLineNumbers | boolean | Display line numbers |
Prism component Styles API
Name | Static selector | Description |
---|---|---|
root | .mantine-Prism-root | Root element |
scrollArea | .mantine-Prism-scrollArea | ScrollArea component wrapping pre element |
code | .mantine-Prism-code | Pre element which contains code |
copy | .mantine-Prism-copy | Copy button wrapper |
line | .mantine-Prism-line | Line wrapper, contains line number and content |
lineNumber | .mantine-Prism-lineNumber | Line number |
lineContent | .mantine-Prism-lineContent | Line content |