CodeBlock
A block that displays code with a copy button. Supports showing a filename and switching between multiple files with tabs.
View in Storybook (opens in a new tab)
Installation
import { CodeBlock } from "@ginga-ui/core";Basic usage
Displays the string passed to code, which can be copied to the clipboard with the copy button.
pnpm add @ginga-ui/corecode-block.tsx
import { CodeBlock } from "@ginga-ui/core";
import { Copy } from "lucide-react";
export function BasicExample() {
return (
<CodeBlock
code={`pnpm add @ginga-ui/core`}
copyLabel="Copy"
copiedLabel="Copied"
/>
);
}Filename
Specify filename to show the file name above the code.
app.tsx
import { Button } from "@ginga-ui/core";
export const App = () => <Button>Click</Button>;code-block.tsx
import { CodeBlock } from "@ginga-ui/core";
import { Copy } from "lucide-react";
export function FilenameExample() {
return (
<CodeBlock
filename="app.tsx"
code={`import { Button } from "@ginga-ui/core";
export const App = () => <Button>Click</Button>;`}
copyLabel="Copy"
copiedLabel="Copied"
/>
);
}Multiple files
Pass multiple files to files to switch between them with tabs.
app.tsx
app.css
import "./app.css";
export const App = () => <p className="message">Hello</p>;code-block.tsx
import { CodeBlock } from "@ginga-ui/core";
export function FilesExample() {
return (
<CodeBlock
files={[
{
filename: "app.tsx",
code: `import "./app.css";
export const App = () => <p className="message">Hello</p>;`,
},
{
filename: "app.css",
code: `.message {
color: var(--color-primary);
}File icons
Use icon on each entry in files, or filenameIcon with filename, to show an icon next to the file name.
app.tsx
app.css
import "./app.css";
export const App = () => <p className="message">Hello</p>;code-block.tsx
import { CodeBlock } from "@ginga-ui/core";
import { FileCode, Paintbrush } from "lucide-react";
export function FileIconExample() {
return (
<CodeBlock
files={[
{
filename: "app.tsx",
icon: <FileCode />,
code: `import "./app.css";
export const App = () => <p className="message">Hello</p>;`,
},
{
filename: "app.css",
icon: <Paintbrush />,
code: `.message {
color: var(--color-primary);
}Copy button with icons
Use copyIcon and copiedIcon to show icons in the copy button.
pnpm add lucide-reactcode-block.tsx
import { CodeBlock } from "@ginga-ui/core";
import { Check, Copy } from "lucide-react";
export function WithIconExample() {
return (
<CodeBlock
code={`pnpm add lucide-react`}
copyLabel="Copy"
copiedLabel="Copied"
copyIcon={<Copy />}
copiedIcon={<Check />}
/>
);
}Props
CodeBlock
| Name | Type | Default | Description |
|---|---|---|---|
code | string | "" | Code to display and copy. |
children | ReactNode | - | Content shown instead of code, such as syntax-highlighted elements. Its text is copied when code is empty. |
filename | string | - | File name shown in the header. The header is hidden when omitted. |
filenameIcon | ReactNode | - | Icon shown before the file name. |
files | { filename: string; code: string; icon?: ReactNode; content?: ReactNode }[] | - | Multiple files switched with tabs. When given, code, filename, and children are ignored. |
filesLabel | string | "Files" | Accessible name of the file tab list. |
copyLabel | string | "Copy" | Label of the copy button. |
copiedLabel | string | "Copied!" | Label shown for two seconds after copying. |
copyIcon | ReactNode | - | Icon of the copy button. |
copiedIcon | ReactNode | - | Icon shown after copying. |
render | (props) => ReactElement | - | Replaces the root element. Return an element that receives the given props, such as next/link. |
Also accepts the attributes of the HTML div (opens in a new tab) element.