Skip to content
العربية
Data display

CodeBlock

Code with line numbers, a file name and a copy button, always left to right. For syntax colours, pass highlighted children (for example from Shiki); copy still copies the plain code.

import { CodeBlock } from "@percentsa/ui";

Examples

app/root.tsx
import { PercentProvider } from "@percentsa/ui";

export function App({ lang, children }: { lang: "en" | "ar"; children: React.ReactNode }) {
  return <PercentProvider lang={lang}>{children}</PercentProvider>;
}
Code
export function CodeBlockExample() {
  return <CodeBlock code={ROOT} filename="app/root.tsx" highlight={[4]} className="w-full max-w-xl" />;
}

// Highlighted children (for example Shiki tokens rendered on the server). Copy still copies `code`, without the "$ ".
bash
$ bun add @percentsa/ui
Code
export function CodeBlockHighlightedExample() {
  return (
    <CodeBlock code="bun add @percentsa/ui" language="bash" className="w-full max-w-xl">
      <span className="text-fg-3">$ </span>
      <span className="font-semibold text-fg">bun</span> <span className="text-fg-2">add @percentsa/ui</span>
    </CodeBlock>
  );
}