Skip to Content

CodeBlock

A code display component with syntax highlighting, copy functionality, and optional folding/collapsing capabilities.

Import

import { CodeBlock, CopyText } from '@tidbcloud/uikit/biz'

Basic Usage

import { CodeBlock } from '@tidbcloud/uikit/biz' const code = `SELECT * FROM users WHERE id = 1;` function Demo() { return <CodeBlock language="sql">{code}</CodeBlock> }

Different Languages

import { CodeBlock } from '@tidbcloud/uikit/biz' function Demo() { return ( <> <CodeBlock language="javascript"> {`const greeting = 'Hello, World!'; console.log(greeting);`} </CodeBlock> <CodeBlock language="python"> {`def greet(name): return f"Hello, {name}!"`} </CodeBlock> </> ) }

Foldable Code Block

import { CodeBlock } from '@tidbcloud/uikit/biz' const longCode = `// Long code content... line 1 line 2 line 3 ...` function Demo() { return ( <CodeBlock language="javascript" defaultFoldHeight={100} showFoldIcon foldStateKey="my-code-block"> {longCode} </CodeBlock> ) }

Without Copy Button

import { CodeBlock } from '@tidbcloud/uikit/biz' function Demo() { return ( <CodeBlock language="bash" showCopy={false}> npm install @tidbcloud/uikit </CodeBlock> ) }

Without Border

import { CodeBlock } from '@tidbcloud/uikit/biz' function Demo() { return ( <CodeBlock language="json" showBorder={false}> {JSON.stringify({ name: 'test' }, null, 2)} </CodeBlock> ) }

Custom Copy Content

import { CodeBlock } from '@tidbcloud/uikit/biz' function Demo() { return ( <CodeBlock language="bash" copyContent="npm install @tidbcloud/uikit" onCopy={() => console.log('Copied!')}> $ npm install @tidbcloud/uikit </CodeBlock> ) }

Props

PropTypeDefaultDescription
childrenstring-The code content to display
languagestring'bash'Programming language for syntax highlight
copyContentstring-Custom content to copy (defaults to children)
onCopy() => void-Callback when copy is clicked
codeRenderer(code: string) => ReactNode-Custom code renderer
prismPropsPrismProps-Props for Prism highlighter
defaultFoldHeightnumber-Default height when folded
foldStateKeystring-Key for persisting fold state
showFoldIconboolean-Show fold/expand icon
onFoldIconClick(folded: boolean) => void-Fold icon click callback
showBorderbooleantrueShow border
showCopybooleantrueShow copy button

CopyText

A component that displays text with a copy button.

Import

import { CopyText } from '@tidbcloud/uikit/biz'

Basic Usage

import { CopyText } from '@tidbcloud/uikit/biz' function Demo() { return <CopyText value="Text to copy">Click to copy this text</CopyText> }

Props

PropTypeDescription
valuestringThe value to copy
childrenReactNodeDisplay content
Last updated on