Skip to content

← All components

Display and feedback

CodeBlock

Readable source code with an optional language label and copy action.

Live example

typescript
          const preference = "system";
applyTheme(preference);
        

Usage

Each example includes the complete adapter-specific parameter reference. Copy it as a starting point, then remove options your use case does not need.

React

tsx
import { CodeBlock } from "@cofob/design-system-react";

/**
 * Complete CodeBlock parameter reference.
 * @param code
 *   type: string
 *   required
 *   example: "const ready = true;"
 * @param language
 *   type: string
 *   default: —
 *   example: "typescript"
 * @param showLanguage
 *   type: boolean
 *   default: true when language is set
 *   example: true
 * @param copyable
 *   type: boolean
 *   default: true
 *   example: true
 * @param copyLabel
 *   type: string
 *   default: "Copy"
 *   example: "Accessible label"
 * @param copiedLabel
 *   type: string
 *   default: "Copied"
 *   example: "Accessible label"
 * @param copyErrorLabel
 *   type: string
 *   default: "Try again"
 *   example: "Accessible label"
 * @param copyResetAfter
 *   type: number (milliseconds)
 *   default: 1800
 *   example: 1800
 * @param div attributes
 *   type: native element attributes
 *   default: —
 *   example: class / style / aria-* / data-*
 */
export function CodeBlockExample() {
  return (
    <CodeBlock code={source} language="typescript" />
  );
}

Svelte

svelte
<script lang="ts">
  import { CodeBlock } from "@cofob/design-system-svelte";

  // Complete CodeBlock parameter reference.
  // code
  //   type: string
  //   required
  //   example: "const ready = true;"
  // language
  //   type: string
  //   default: —
  //   example: "typescript"
  // showLanguage
  //   type: boolean
  //   default: true when language is set
  //   example: true
  // copyable
  //   type: boolean
  //   default: true
  //   example: true
  // copyLabel
  //   type: string
  //   default: "Copy"
  //   example: "Accessible label"
  // copiedLabel
  //   type: string
  //   default: "Copied"
  //   example: "Accessible label"
  // copyErrorLabel
  //   type: string
  //   default: "Try again"
  //   example: "Accessible label"
  // copyResetAfter
  //   type: number (milliseconds)
  //   default: 1800
  //   example: 1800
  // div attributes
  //   type: native element attributes
  //   default: —
  //   example: class / style / aria-* / data-*
</script>

<CodeBlock code={source} language="typescript" />

HTML

html
<!--
  Complete CodeBlock parameter reference.
  code
    type: string
    required
    example: "const ready = true;"
  language
    type: string
    default: —
    example: "typescript"
  showLanguage
    type: boolean
    default: true when language is set
    example: true
  copyable
    type: boolean
    default: true
    example: true
  copyLabel
    type: string
    default: "Copy"
    example: "Accessible label"
  copiedLabel
    type: string
    default: "Copied"
    example: "Accessible label"
  copyErrorLabel
    type: string
    default: "Try again"
    example: "Accessible label"
  copyResetAfter
    type: number (milliseconds)
    default: 1800
    example: 1800
  div attributes
    type: native element attributes
    default: —
    example: class / style / aria-* / data-*
-->
<div class="cf-code-block" data-cf-copy-scope><button data-cf-copy-button>Copy</button><pre><code data-cf-copy-source>const value = 1;</code></pre></div>

<script type="module">
  import { initDesignSystem } from "@cofob/design-system-css";

  const designSystem = initDesignSystem(document);
  window.addEventListener("pagehide", () => designSystem.destroy(), { once: true });
</script>

Parameters

Adapter differences are explicit. Native element attributes are forwarded in React and Svelte and can be written directly in HTML.

ParameterTypeDefaultAdaptersExampleDescription
coderequiredstringrequiredReact · Svelte · HTML"const ready = true;"Exact source text to render and copy.
languagestringReact · Svelte · HTML"typescript"Syntax language identifier and optional visible label.
showLanguagebooleantrue when language is setReact · Svelte · HTMLtrueControls whether the language label is visible.
copyablebooleantrueReact · Svelte · HTMLtrueEnables the clipboard action.
copyLabelstring"Copy"React · Svelte · HTML"Accessible label"Initial accessible copy button text.
copiedLabelstring"Copied"React · Svelte · HTML"Accessible label"Temporary success label after copying.
copyErrorLabelstring"Try again"React · Svelte · HTML"Accessible label"Error label when clipboard access fails.
copyResetAfternumber (milliseconds)1800React · Svelte · HTML1800Delay before the copy action returns to its initial label.
div attributesnative element attributesReact · Svelte · HTMLclass / style / aria-* / data-*Forwards native class/style, accessibility, event, and data attributes to the root element.