Prism

Code highlight component with copy button. This is a custom component in @tidbcloud/uikit built on top of prism-react-renderer.

Import

import { Prism } from '@tidbcloud/uikit'

Basic Usage

import { Prism } from '@tidbcloud/uikit'
 
const code = `function greet(name: string) {
  console.log('Hello, ' + name + '!')
}
 
greet('World')`
 
function Demo() {
  return <Prism language="tsx">{code}</Prism>
}

With Line Numbers

import { Prism } from '@tidbcloud/uikit'
 
const code = `const numbers = [1, 2, 3, 4, 5]
const doubled = numbers.map(n => n * 2)
console.log(doubled)`
 
function Demo() {
  return (
    <Prism language="javascript" withLineNumbers>
      {code}
    </Prism>
  )
}

Highlight Lines

import { Prism } from '@tidbcloud/uikit'
 
const code = `function sum(a: number, b: number) {
  // This line is highlighted
  return a + b
}
 
const result = sum(1, 2)`
 
function Demo() {
  return (
    <Prism
      language="tsx"
      withLineNumbers
      highlightLines={{
        2: { color: 'yellow', label: 'Important' },
        3: { color: 'green' }
      }}
    >
      {code}
    </Prism>
  )
}

Without Copy Button

import { Prism } from '@tidbcloud/uikit'
 
function Demo() {
  return (
    <Prism language="sql" noCopy>
      SELECT * FROM users WHERE id = 1
    </Prism>
  )
}

Force Color Scheme

import { Prism } from '@tidbcloud/uikit'
 
function Demo() {
  return (
    <Prism language="json" colorScheme="dark">
      {JSON.stringify({ name: 'test', value: 123 }, null, 2)}
    </Prism>
  )
}

Supported Languages

Prism supports many languages including: javascript, typescript, tsx, jsx, json, css, html, sql, bash, python, go, rust, and many more.

Props

PropTypeDefaultDescription
childrenstring-Code to highlight (required)
languageLanguage-Programming language for syntax highlighting
noCopybooleanfalseRemove copy to clipboard button
copyLabelstring'Copy code'Copy button tooltip
copiedLabelstring'Copied'Copy button tooltip in copied state
withLineNumbersbooleanfalseDisplay line numbers
highlightLinesRecord<number, { color: MantineColor; label?: string }>{}Highlight specific lines with color and label
colorScheme'dark' | 'light'-Force color scheme
trimbooleantrueTrim whitespace from code
radiusMantineRadius-Border radius
scrollAreaComponentReact.ComponentTypeScrollAreaCustom scroll area component
getPrismTheme(theme: MantineTheme, colorScheme: 'light' | 'dark') => PrismTheme-Custom theme function