Skip to content
Ginga UI
Ginga UI
日本語

Getting started

  • Overview
  • Quick start
  • Theme generation
  • Custom rendering
  • All components

Forms

  • Button
  • Checkbox
  • Input
  • Radio
  • Questionnaire
  • SegmentedControl
  • Select
  • ComboBox
  • NumberField
  • SearchField
  • Slider
  • Switch
  • FormControl

Display

  • Accordion
  • Alert
  • AlertDialog
  • Badge
  • CodeBlock
  • Card
  • Dialog
  • Drawer
  • Image
  • List
  • Popover
  • Menu
  • Tooltip
  • Table
  • Tab
  • Toast
  • Trace

Typography

  • Heading
  • Paragraph

Navigation

  • Anchor

Layout

  • Box

Resources

  • Changelog
  • GitHub (opens in a new tab)

Logo: Twemoji (opens in a new tab) (CC-BY 4.0)

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/core
code-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-react
code-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

NameTypeDefaultDescription
codestring""Code to display and copy.
childrenReactNode-Content shown instead of code, such as syntax-highlighted elements. Its text is copied when code is empty.
filenamestring-File name shown in the header. The header is hidden when omitted.
filenameIconReactNode-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.
filesLabelstring"Files"Accessible name of the file tab list.
copyLabelstring"Copy"Label of the copy button.
copiedLabelstring"Copied!"Label shown for two seconds after copying.
copyIconReactNode-Icon of the copy button.
copiedIconReactNode-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.