Code block
Displays long identifiers and code with a copy action.
Preview
Local preview. It doesn't affect real accounts, environments, or run data.
Compare the same example at each size and density. Switching keeps input values and selection state. Size guidance and comparisons
npm install @surflane/uiUsage
Show code, configuration, and long identifiers with whitespace and line breaks preserved, ready to select and copy. The copy button shows its result in place: Copied, or Copy unavailable. Content scrolls horizontally by default. In documentation, set wrap so only long lines break.
- Don't let syntax highlighting pass for syntax validation or an execution result. Report those separately instead.
- Don't inject code as HTML or execute it. Pass a plain source string to code instead.
- Don't treat a rejected clipboard write as success. Let a custom onCopy throw or reject on failure, so the button shows Copy unavailable.
Size: SizeProvider size="small" resizes the whole composition, including portals. The default size is Default. Size guidance and comparisons
Anatomy
Root → toolbar(label + language + CopyButton, which also shows the result) + labeled ScrollArea(pre/code). CopyButton also stands alone beside any value.
Keyboard and accessibility
- Tab reaches the copy button and the scroll area.
- Enter or Space copies. The result replaces the button label in place, so nothing else moves: Copied on success, Copy unavailable on failure.
- On failure, the source is selected for the keyboard copy shortcut.
- Changing the source clears the result, and late responses don't overwrite the new source's state.
- The label is a polite live region and returns to the action after 2 seconds.
Give each control a name that describes its purpose. Connect error messages to their controls, and signal state with more than color. Automated checks don't replace manual keyboard and assistive technology testing.
Example code
Shared components take their styles from design tokens. Each example sets its own composition and accessibility attributes. The calling screen defines callbacks, refs, and product data.
Key API
The table lists the common wrapper's main props. See the package type declarations for the full types.