# Code block

Readable source code with a filename, optional line numbers, wrapping and a scrollable viewport.

Access: pro. Category: Essentials. Kind: composite.

## When to use

- Use it when a page needs readable source code with a filename, optional line numbers, wrapping and a scrollable viewport.
- Choose it when the host application can supply the required state and keep business logic outside the presentation component.

## Alternatives

- Choose tidal-button instead when press, loading, success and disabled states is the actual interface need, rather than code block.

## Setup for agents

Use React 19 and TypeScript. Initialize shadcn for your framework, then merge this exact entry into components.json:

```json
{
  "registries": {
    "@tidal-pro": {
      "url": "https://tidalui.com/r/pro/{name}.json",
      "headers": {
        "Authorization": "Bearer ${TIDALUI_TOKEN}"
      }
    }
  }
}
```

The namespace must be named **@tidal-pro** because transitive Pro dependencies use that namespace. Obtain an agent key from your active Pro account at https://tidalui.com/account. Keep TIDALUI_TOKEN in your local environment, never in components.json, client code, Git or logs.

```sh
export TIDALUI_TOKEN='your-private-agent-key'
npx shadcn@latest add @tidal-pro/waitlist-form
npx shadcn@latest add https://tidalui.com/r/tidal-button.json
```

Free command: `npx shadcn@latest add https://tidalui.com/r/<name>.json`.
Pro command: `npx shadcn@latest add @tidal-pro/<name>`.

Import the base stylesheet and every stylesheet listed on the installed item's metadata. For example:

```tsx
import "./components/ui/tidal.css";
import "./components/ui/hero-section.css";
// Place installed components inside this wrapper:
<div className="tidal-scope" data-theme="dark">...</div>
```

Omit data-theme for the default light theme. Put the attribute on the same tidal-scope element for dark mode. Every component installs the shared free tidal-base dependency. After upgrading, run `npx shadcn@latest add https://tidalui.com/r/tidal-base.json --overwrite` to receive current tokens. Review local modifications before overwriting.

Vite: Use React + TypeScript. Map @/* to ./src/* in paths and Vite resolve.alias. Import tidal.css and each component stylesheet in your application entry.

Next.js 15 App Router: Next.js App Router: import tidal.css and component CSS in app/layout.tsx. Interactive examples belong in a client component with "use client". Map @/* to ./* or ./src/* to match your project.

TypeScript 6: use paths without baseUrl:

```json
{
  "compilerOptions": {
    "paths": {
      "@/*": [
        "./src/*"
      ]
    }
  }
}
```

Pro registry requests are server/CLI-only. Use the same-origin account interface in a browser; cross-origin Authorization preflights are intentionally unsupported.


## Install Code block

```sh
npx shadcn@latest add @tidal-pro/code-block
```

Stylesheets: `components/ui/tidal.css`, `components/ui/code-block.css`.

## Minimal example

```tsx
import"./components/ui/tidal.css";
import"./components/ui/code-block.css";
import{TidalCodeBlock}from"./components/ui/code-block";
export default()=><div className="tidal-scope"><TidalCodeBlock code={"value"}/></div>;
```

## Structured props

- `TidalCodeBlock.code: string`: Source text is rendered as text, never evaluated or interpreted as HTML.
- `TidalCodeBlock.actions?: ReactNode`: Supply a TidalCopyButton or any other native action.
- `TidalCodeBlock.caption?: ReactNode`: Optional caption value accepted by this exported component.
- `TidalCodeBlock.codeLabel?: string | undefined`: Optional code label value accepted by this exported component.
- `TidalCodeBlock.filename?: string | undefined`: Optional filename value accepted by this exported component.
- `TidalCodeBlock.language?: string | undefined`: Optional language value accepted by this exported component.
- `TidalCodeBlock.lineNumbers?: boolean | undefined` (default true): The number gutter is omitted while wrapping so it cannot mislabel visual lines.
- `TidalCodeBlock.maxHeight?: number | undefined` (default 400): Maximum scrolling body height in pixels. Omit for the 400px default.
- `TidalCodeBlock.wrap?: boolean | undefined` (default false): Optional wrap value accepted by this exported component.

## Complete usage example

[Source](https://tidalui.com/examples/code-block.tsx) | [Manifest](https://tidalui.com/examples/code-block.json). Install the listed additional dependencies. Consumer fixture: TypeScript pass; Vite pass. This fixture check is distinct from the actual shadcn install receipt.

```tsx
"use client";

import "./components/ui/tidal.css";

import { useId, useState } from "react";

import { TidalCodeBlock } from "./components/ui/code-block";

import { TidalCopyButton } from "./components/ui/copy-button";

import "./components/ui/code-block.css";

import "./components/ui/copy-button.css";



const tidalExampleStyles = ".code-block-demo {\n  width: min(100%, 680px);\n  min-width: 0;\n}\n.code-block-demo .code-block-demo-copy {\n  min-width: 88px;\n  padding-inline: 12px;\n}\n.code-block-demo-controls {\n  display: flex;\n  align-items: center;\n  justify-content: space-between;\n  gap: 16px;\n  margin-bottom: 16px;\n  color: var(--t-ink);\n  font: 500 12px/1.5 var(--t-font, sans-serif);\n}\n.code-block-demo-controls fieldset {\n  display: flex;\n  gap: 4px;\n  min-width: 0;\n  border: 0;\n  padding: 0;\n  margin: 0;\n}\n.code-block-demo-controls label {\n  display: inline-flex;\n  align-items: center;\n  gap: 6px;\n  min-height: 44px;\n  cursor: pointer;\n}\n.code-block-demo-controls fieldset label {\n  position: relative;\n}\n.code-block-demo-controls input[type=\"radio\"] {\n  position: absolute;\n  opacity: 0;\n  width: 1px;\n  height: 1px;\n}\n.code-block-demo-controls fieldset span {\n  display: grid;\n  align-items: center;\n  min-height: 44px;\n  padding: 0 14px;\n  border-radius: 9px;\n  color: var(--t-muted);\n  transition:\n    color 160ms,\n    background 160ms;\n}\n.code-block-demo-controls input:checked + span {\n  color: var(--t-ink);\n  background: var(--t-wash);\n}\n.code-block-demo-controls input:focus-visible + span {\n  outline: 2px solid var(--t-accent);\n  outline-offset: 2px;\n}\n.code-block-demo-wrap {\n  flex: 0 0 auto;\n  padding-inline: 4px;\n}\n.code-block-demo-wrap input {\n  accent-color: var(--t-accent);\n  width: 16px;\n  height: 16px;\n}\n.code-block-demo-sr {\n  position: absolute;\n  width: 1px;\n  height: 1px;\n  padding: 0;\n  overflow: hidden;\n  clip-path: inset(50%);\n  white-space: nowrap;\n}\n@media (prefers-reduced-motion: reduce) {\n  .code-block-demo-controls fieldset span {\n    transition: none;\n  }\n}";

const sources = {
  React: `import { useState } from "react";

export function SaveButton() {
  const [saved, setSaved] = useState(false);

  return (
    <button type="button" onClick={() => setSaved(true)}>
      {saved ? "Saved" : "Save changes"}
    </button>
  );
}`,
  CSS: `.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 18px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--foreground);
}

.button:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 3px;
}`,
};

export function CodeBlockDemo() {
  const id = useId();
  const [source, setSource] = useState<keyof typeof sources>("React");
  const [wrap, setWrap] = useState(false);
  return (
    <div className="code-block-demo">
      <div className="code-block-demo-controls">
        <fieldset>
          <legend className="code-block-demo-sr">Example language</legend>
          {(["React", "CSS"] as const).map((value) => (
            <label key={value}>
              <input
                type="radio"
                name={`${id}-code-language`}
                checked={source === value}
                onChange={() => setSource(value)}
              />
              <span>{value}</span>
            </label>
          ))}
        </fieldset>
        <label className="code-block-demo-wrap">
          <input
            type="checkbox"
            checked={wrap}
            onChange={(event) => setWrap(event.target.checked)}
          />
          <span>Wrap lines</span>
        </label>
      </div>
      <TidalCodeBlock
        code={sources[source]}
        filename={source === "React" ? "save-button.tsx" : "button.css"}
        language={source === "React" ? "TSX" : "CSS"}
        wrap={wrap}
        actions={
          <TidalCopyButton
            text={sources[source]}
            aria-label="Copy code"
            className="code-block-demo-copy"
          />
        }
        caption="Select the code or scroll to read more."
      />
    </div>
  );
}

export default function AgentExample() {
  return (
    <div className="tidal-scope" data-theme="light" dir="ltr">
      <style>{tidalExampleStyles}</style>
      <CodeBlockDemo />
    </div>
  );
}

```

## Gotchas

- Install the declared registry dependencies and render inside a tidal-scope with the foundation stylesheet loaded.

## Theme and accessibility

{"tokens":["--t-accent","--t-border","--t-border-strong","--t-canvas","--t-danger","--t-display","--t-ease-out","--t-ease-spring","--t-faint","--t-font","--t-ink","--t-line","--t-muted","--t-primary","--t-primary-contrast","--t-radius-card","--t-radius-control","--t-radius-overlay","--t-radius-panel","--t-ring","--t-shadow-lg","--t-shadow-soft","--t-soft","--t-subtle","--t-surface","--t-wash"],"darkMode":true}

{"aria":"The component wires labels and relationships from consumer-provided visible text or ARIA props; preserve those labels in production.","notes":"Verify focus visibility, text alternatives, contrast, and reduced motion in the final composition."}

Required code is rendered as plain text and never evaluated. Optional filename, language, caption, codeLabel, actions, wrap, lineNumbers (default true), maxHeight (default 400px, finite and at least 80px), native figure attributes/ref/className/style. The native focusable code region supports horizontal/vertical keyboard scrolling. Direction is LTR for source text even in an RTL page. Number gutter is decorative, excluded from selection and hidden when wrapping to avoid misleading line numbers. Use the actions slot for a separate TidalCopyButton; clipboard behavior remains the action's responsibility. Empty strings, tabs, Unicode and trailing newlines are preserved. No syntax parser, code execution, network or storage is introduced.
