Skip to content
BigBang

Popular

    ↑↓Navigate↵OpenEscClose
    TR

    Components

    CodeBlock

    Shows code in the mono type with an optional header, line numbers and a copy button.

    Storybook
    Storybook
    Source
    Source

    CodeBlock draws a figure; the header is its figcaption. The code is in pre and code. The scroll region has the region role and is named by title, or “Code sample” without it. Line numbers are hidden with aria-hidden.

    Key Behaviour
    Tab Focus the copy button, then the scroll region.
    Left, Right, Up, Down Scroll the code in the scroll region.
    Enter, Space Copy the code from the copy button.

    The scroll region draws a 2 px color-focus-ring outline inside its edge with focus-visible; the copy button uses the Button focus.

    Example Output
    Region “Save.tsx, region”
    Copy button “Copy code, button”; then “Copied”

    Code text has 4.5:1 contrast on the layer 1 surface, and line numbers 4.5:1 with color-text-subtle. A highlighted line keeps the text colour.

    Criterion How CodeBlock meets it What the product team does
    1.3.1 Info and relationships figure, figcaption, pre, code. Explain the code in text.
    2.1.1 Keyboard A focusable scroll region. Do not nest it in another focusable container.
    1.4.10 Reflow Sideways scrolling inside the region; breaking with wrap. Use wrap for long commands.