Skip to content

Code Block

Display highlighted code with a file name or language, optional line numbers and a copy button.

import { CodeBlock, CodeTokens } from '@youj/react'

Installation & usage

npm i @youj/react

For an app already set up with npx @youj/cli init, which installs the peers (@youj/core, @youj/motion, motion and Base UI) and wires the provider. New app? Start at Installation.

Use a code block to show code someone will read or copy: an install command, a config file, a snippet in a changelog or an empty state. It's the same block the Chat renders for fenced code in a model's answer. For code inside a sentence, use Typography's TypographyInlineCode.

import { CodeBlock } from '@youj/react'

export function InstallCommand() {
  return <CodeBlock code="pnpm dlx @youj/cli@latest init" language="sh" />
}

The header shows filename when you pass one, with the language value beside it as written (ts, sh), or else that value alone, or text. Long lines scroll sideways instead of wrapping, so indentation stays true; the scrollbar appears only when it's needed. lineNumbers adds a gutter that holds still while the code scrolls.

Reference

Propscode, language, filename… (8)

Props

CodeBlock props
PropType
codeRequiredstring

The source to show and copy.

–
languagestring

Language for highlighting and the header label: ts, tsx, js, json, css, html, sh, py, go, rust, sql, yaml, diff… Anything else is plain text.

default ''

''
filenamestring

A file name for the header, in place of the language.

–
lineNumbersboolean

Number the lines in a gutter that stays put while the code scrolls.

default false

false
copyLabelstring

The copy button's name and tooltip.

default "Copy code"

"Copy code"
actionsReactNode

Replaces the header's copy button, e.g. with your own actions.

–
tailReactNode

Drawn at the end of the code, inside it: the caret of a streaming answer, say.

–
soundboolean

With sound on (YoujProvider’s sound), the copy button plays tap and a copy success. false keeps it silent.

default true

true
Other props pass through: extends ComponentProps<'figure'>.
MotionCopied, back to rest · reduced motion

Motion

Motion tokens
MomentTokens
Copiedthe back sheet tucks behind the front one and a check is written on it
no token

the copy icon’s own choreography

the copy icon’s own choreography
Back to restafter a beat
no token

3 × deliberate

3 × deliberate

Reduced motion

The icon swaps to the check at once and back again, with no drawing.

Reduced motion

The copy icon swaps straight to its check and back. Everything else is the same.

AccessibilityKeyboard, ARIA, announcements

Accessibility

  • The block is a figure whose figcaption holds the file name or language and the copy button.
  • The code sits in a scroll region named by the file name, or "TypeScript code" and so on, so a keyboard user can scroll a long line.
  • The copy button is named by copyLabel. Its tooltip says "Copied" after a copy, and the result is announced politely. A clipboard that refuses (no permission, an insecure page) is announced as "Couldn’t copy", not faked.
  • Line numbers are decorative (aria-hidden) and can't be selected, so copying by hand takes only the code.