Skip to content
Haze UI

Code preview

Docs-style panel with a Preview and a Code pane: a live example in the slot, its source in a scrolling pre, and a copy button.

pnpm dlx shadcn@latest add @haze/code-preview
Open preview in a new tab
Livesmall script

When to use

  • Documenting a component with a live example and the exact source beside it
  • Letting readers check an example at narrower widths by dragging the stage edge
  • Showing the same snippet either rendered or as text, one pane at a time

When not to use

  • Source only, with no rendered example; use code-block
  • Install commands in several package managers; use code-tabs
  • Showing a page or app inside a device or browser frame; use browser-window

Installation

Add the Haze registry to components.json once, then install Code preview with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.

pnpm dlx shadcn@latest add @haze/code-preview
First time? Registry and theme setup

1. Add to components.json:

{
  "registries": {
    "@haze": "http://localhost:4329/r/{name}.json"
  }
}

2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";

API reference

PropTypeDefaultDescription
id*string–Unique on the page. The panes get ids `${id}-preview` and `${id}-code`.
code*string–Source shown in the Code pane. Kept verbatim in <pre><code>; the pane scrolls inside itself.
titlestring–Optional plain-text line above the tabs.
languagestring–Small language label beside the copy button, for example "astro".
defaultView"preview" | "code""preview"Pane shown first, with and without script.
stage"default" | "muted" | "dots""default"Surface behind the example. dots is an SVG dot pattern filled with the border token.
align"center" | "start""center"Where the example sits in the stage.
resizablebooleanfalseNative horizontal resize handle on the example, with a minimum width, to test responsive behavior.
resizeHintstring"Drag the bottom-right corner to resize the example."Visible hint shown under a resizable stage.
previewLabelstring"Preview"Text of the first tab.
codeLabelstring"Code"Text of the second tab and name of the code region.
viewsLabelstring"Example view"Accessible name of the tab list.
copyLabelstring"Copy code"Accessible name of the copy button.
copiedLabelstring"Copied"Text announced after a successful copy.
failedLabelstring"Copy failed"Text announced when the browser refuses the copy.
classstring–Extra classes on the outer panel.
default slot*slot–The live example shown in the Preview pane.

Keyboard interactions

LeftRight
Switch between the Preview and Code tabs
HomeEnd
Select the first or last tab
Tab
Move from the selected tab to the copy button, then into the pane
EnterSpace
Copy the code when the copy button has focus

Accessibility

  • Without script the two tabs are in-page links and :target shows the matching pane; the first pane shows until a link targets the other.
  • The script adds tablist, tab and tabpanel roles, aria-selected and a roving tabindex. The static markup never claims them.
  • The code region is focusable and named "Code", so keyboard users can scroll long or wide source (WCAG 2.1.1).
  • The copy button appears only with script and announces "Copied" or "Copy failed" through a role="status" region.
  • The resize handle is a native pointer feature (resize-x): it has no keyboard or touch alternative, so do not hide information that only a narrow width reveals. The example stays reachable at any width because the stage scrolls instead of clipping.

Notes for AI

Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.

  • Install and use

    Install the Haze UI component "Code preview" with `npx shadcn@latest add @haze/code-preview`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import CodePreview from "@/components/haze/ui/code-preview.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/code-preview.md

  • Customize with real content

    Adapt the Haze code-preview ui for our product. Replace the demo values of these props with our real content: id, code, title, language, defaultView, stage, align, resizable, resizeHint, previewLabel, codeLabel, viewsLabel, copyLabel, copiedLabel, failedLabel, class, default slot. Keep the layout, the heading level rules and these accessibility details: Without script the two tabs are in-page links and :target shows the matching pane; the first pane shows until a link targets the other. The script adds tablist, tab and tabpanel roles, aria-selected and a roving tabindex. The static markup never claims them. The code region is focusable and named "Code", so keyboard users can scroll long or wide source (WCAG 2.1.1). The copy button appears only with script and announces "Copied" or "Copy failed" through a role="status" region. The resize handle is a native pointer feature (resize-x): it has no keyboard or touch alternative, so do not hide information that only a narrow width reveals. The example stays reachable at any width because the stage scrolls instead of clipping. Do not add raw color classes; use the Haze tokens.

  • Choose the right block

    Before using code-preview, check it fits: use it when documenting a component with a live example and the exact source beside it; letting readers check an example at narrower widths by dragging the stage edge; showing the same snippet either rendered or as text, one pane at a time. Avoid it when source only, with no rendered example; use code-block; install commands in several package managers; use code-tabs; showing a page or app inside a device or browser frame; use browser-window. Alternatives: @haze/code-tabs, @haze/code-block, @haze/browser-window.

Dependencies

  • npm: @lucide/astro
  • @haze/utils
  • @haze/theme (one-time setup)

Source · Original Haze UI work.

Catalog

↑↓ moveEnter openEsc close

Keyboard shortcuts

CtrlK
Command palette
/
Focus search
←↑→↓
Move between cards
jk
Next / previous card
ci
Copy code / install command (detail page)
[]
Previous / next entry
?
This help
Esc
Close dialog