Skip to Content
Cristal

Content

Code block

Code to read and copy in one click.

<cr-code-block>CrCodeBlock

Example

cr-code-block shows code (code) as is, in a monospace font, with a Copy button. label adds a title above: file name, language or "Terminal". A line that is too long scrolls the block sideways instead of wrapping.

'use client';

import { CrBox, CrCodeBlock } from '@cristal-ui/react';

const config = `{
  "name": "my-app",
  "dependencies": {
    "@cristal-ui/react": "^1.0.0"
  }
}`;

export default function CodeBlockBasic() {
  return (
    <CrBox display="flex" direction="column" gap="lg">
      <CrCodeBlock label="Terminal" code="pnpm add @cristal-ui/react" />
      <CrCodeBlock label="package.json" code={config} />
    </CrBox>
  );
}

Copying

The button copies the code to the clipboard and turns into "Copied" for two seconds. If the browser refuses the copy (insecure page, permission denied), a message asks to select the code.

The block adds no syntax highlighting.

Accessibility

  • The button is named after the block title ("Copy package.json"): several blocks on a page stay distinct.
  • The copy is announced to screen readers ("Copied"), without moving focus.
  • The code scrolls with the keyboard: the block takes focus with Tab, then the arrows scroll it.

API

Properties

PropertyTypeDefaultDescription
code (obligatoire)string—Le code à afficher, tel quel (obligatoire).
labelstring—Titre affiché au-dessus du code : nom de fichier, langage ou « Terminal ». Il complète aussi le nom du bouton (« Copier package.json »).

CSS parts

To style it from outside: cr-code-block::part(name) { … }

PartDescription
codeLe bloc pre.
copyLe bouton Copier.
headerLa barre du haut, avec le titre et le bouton.

On this page