{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "code-block",
  "title": "CodeBlock",
  "description": "Code or a command on a muted surface in the theme's monospace font, with a caption and a button that copies it.",
  "registryDependencies": [
    "@ouro/copy-button",
    "@ouro/emoji",
    "@ouro/lib-class-name-check",
    "@ouro/lib-cn",
    "@ouro/lib-env",
    "@ouro/lib-refs",
    "@ouro/lib-scroll-fade",
    "@ouro/lib-use-dev-messages"
  ],
  "files": [
    {
      "path": "registry/ouro/components/ouro/code-block/code-block.tsx",
      "content": "\"use client\";\n\nimport { useCallback, useRef, type HTMLAttributes, type JSX, type ReactNode, type Ref, type RefCallback } from \"react\";\nimport { useAppearanceClassNameCheck } from \"@/registry/ouro/lib/ouro/class-name-check\";\nimport { cn } from \"@/registry/ouro/lib/ouro/cn\";\nimport { useMergedRef } from \"@/registry/ouro/lib/ouro/refs\";\nimport { scrollFadeClass, useScrollFade } from \"@/registry/ouro/lib/ouro/scroll-fade\";\nimport { useDevWarnings } from \"@/registry/ouro/lib/ouro/use-dev-messages\";\nimport { CopyButton } from \"@/registry/ouro/components/ouro/copy-button/copy-button\";\nimport { Emoji, type EmojiCatalogue } from \"@/registry/ouro/components/ouro/emoji/emoji\";\nimport { splitEmojiText } from \"@/registry/ouro/components/ouro/emoji/emoji-styles\";\n\n/** The props CodeBlock adds to the `<figure>` attributes it passes through. */\nexport interface CodeBlockOwnProps {\n  /**\n   * The code, as plain text. It shows exactly as given, whitespace and all, and is what the copy button copies.\n   *\n   * @claim \"It shows exactly as given, whitespace\" registry/ouro/components/ouro/code-block/code-block.test.tsx verify:testExists(registry/ouro/components/ouro/code-block/code-block.test.tsx::CodeBlock > copies the code exactly with its copy button)\n   * @claim \"It shows exactly as given, whitespace\" registry/ouro/components/ouro/code-block/code-block.test.tsx verify:testExists(registry/ouro/components/ouro/code-block/code-block.test.tsx::CodeBlock > shows the code as given on the muted surface in the monospace font, under a caption naming its language)\n   */\n  code: string;\n  /**\n   * The language, shown in the block's caption when there is no `title` and set as\n   * `data-language` on the block for a highlighter of your own: `language=\"bash\"`.\n   * @claim \"The language, shown in the block's\" registry/ouro/components/ouro/code-block/code-block.test.tsx verify:testExists(registry/ouro/components/ouro/code-block/code-block.test.tsx::CodeBlock > captions with the title rather than the language)\n   * @claim \"The language, shown in the block's\" registry/ouro/components/ouro/code-block/code-block.test.tsx verify:fileLine(registry/ouro/components/ouro/code-block/code-block.test.tsx, /getAttribute\\(\"data-language\"\\)\\)\\.toBe\\(\"bash\"\\)/)\n   */\n  language?: string | undefined;\n  /** A caption above the code, such as its file name: `title=\"vite.config.ts\"`. */\n  title?: ReactNode;\n  /**\n   * The copy button in the bar above the code. Turn it off for code nobody should paste, such as\n   * output. @default true\n   */\n  copy?: boolean | undefined;\n  /** The copy button's name for screen readers. @default \"Copy code\" */\n  copyLabel?: string | undefined;\n  /**\n   * Wraps long lines instead of scrolling them sideways. Scrolling keeps code as written,\n   * so leave it off for code and turn it on for prose-like text such as a long command.\n   */\n  wrap?: boolean | undefined;\n  /**\n   * An image emoji catalogue: emoji characters in the code that it lists show as its pictures,\n   * 1em square, over the characters themselves, which stay in place unseen. The lines keep\n   * their length and wrapping, and copying or selecting gives the code's own characters.\n   * `:shortcode:` text stays text, since in code it means something else.\n   *\n   * @claim \"1em square, over the characters themselves\" registry/ouro/components/ouro/code-block/code-block.test.tsx verify:testExists(registry/ouro/components/ouro/code-block/code-block.test.tsx::CodeBlock emoji > draws the catalogue's emoji as 1em pictures over the code's own characters, without moving a line)\n   * @claim \"copying or selecting gives the code's own characters\" registry/ouro/components/ouro/code-block/code-block.test.tsx verify:testExists(registry/ouro/components/ouro/code-block/code-block.test.tsx::CodeBlock emoji > copies and selects the code's own characters, and leaves shortcode text as it is)\n   */\n  catalogue?: EmojiCatalogue | undefined;\n  /** Layout only: margins, width, a maximum height that scrolls (`max-h-80`). Appearance classes warn in development. */\n  className?: string | undefined;\n  ref?: Ref<HTMLElement> | undefined;\n\n  /** Not a prop. The code is `code`, a string: `<CodeBlock code={source} />`. */\n  children?: { 'Use <CodeBlock code={source} />': never };\n  /** Not a prop. Ouro UI uses `language`: `<CodeBlock code={source} language=\"tsx\" />`. */\n  lang?: { 'Use <CodeBlock language=\"tsx\" />': never };\n  /** Not a prop. The caption is `title`: `<CodeBlock code={source} title=\"app.tsx\" />`. */\n  filename?: { 'Use <CodeBlock title=\"app.tsx\" />': never };\n  /** Not a prop. The copy button is on by default; `copy={false}` turns it off. */\n  showCopyButton?: { 'Use copy={false} to hide the copy button; it shows by default': never };\n}\n\n/** Props for {@link CodeBlock}: its own props and the `<figure>` attributes it passes through. */\nexport type CodeBlockProps = Omit<HTMLAttributes<HTMLElement>, keyof CodeBlockOwnProps> & CodeBlockOwnProps;\n\n/**\n * Makes the code a tab stop while it overflows either way, so a keyboard can scroll it (WCAG 2.1.1).\n *\n * @claim \"Makes the code a tab stop\" registry/ouro/components/ouro/code-block/code-block.test.tsx verify:testExists(registry/ouro/components/ouro/code-block/code-block.test.tsx::CodeBlock > scrolls a long line sideways and becomes a tab stop only while it overflows)\n * @claim \"Makes the code a tab stop\" registry/ouro/components/ouro/code-block/code-block.test.tsx verify:testExists(registry/ouro/components/ouro/code-block/code-block.test.tsx::CodeBlock > scrolls tall code within a maximum height the app sets, as a tab stop)\n * @claim \"Makes the code a tab stop\" https://www.w3.org/TR/WCAG22/#keyboard verify:urlOk(https://www.w3.org/TR/WCAG22/#keyboard)\n */\nfunction useScrollableCode(): RefCallback<HTMLPreElement> {\n  return useCallback((element: HTMLPreElement | null) => {\n    // React 19 calls a ref that returns a cleanup with the element only; the cleanup replaces the call with null.\n    /* istanbul ignore if @preserve -- never called with null */\n    if (!element) return;\n    const update = () => {\n      const overflows = element.scrollWidth > element.clientWidth || element.scrollHeight > element.clientHeight;\n      if (overflows) element.setAttribute(\"tabindex\", \"0\");\n      // Removing the focused block's tab stop would drop focus to <body>; it goes once focus leaves.\n      else if (element !== document.activeElement) element.removeAttribute(\"tabindex\");\n    };\n    const observer = new ResizeObserver(update);\n    observer.observe(element);\n    observer.observe(element.firstElementChild!);\n    element.addEventListener(\"blur\", update);\n    return () => {\n      observer.disconnect();\n      element.removeEventListener(\"blur\", update);\n    };\n  }, []);\n}\n\n/**\n * The code with each emoji character the catalogue lists drawn as its picture: the characters\n * stay in the text, unseen, so the line keeps its length and a selection copies them, and the\n * picture sits over them, 1em square.\n *\n * @claim \"so the line keeps its length\" registry/ouro/components/ouro/code-block/code-block.test.tsx verify:testExists(registry/ouro/components/ouro/code-block/code-block.test.tsx::CodeBlock emoji > draws the catalogue's emoji as 1em pictures over the code's own characters, without moving a line)\n * @claim \"a selection copies them\" registry/ouro/components/ouro/code-block/code-block.test.tsx verify:testExists(registry/ouro/components/ouro/code-block/code-block.test.tsx::CodeBlock emoji > copies and selects the code's own characters, and leaves shortcode text as it is)\n */\nfunction CodeWithEmoji({ code, catalogue }: { code: string; catalogue: EmojiCatalogue }): ReactNode {\n  return splitEmojiText(code, catalogue, false).map((part, index) =>\n    typeof part === \"string\" ? (\n      part\n    ) : (\n      <span key={index} data-ouro-code-emoji=\"\" className=\"relative\">\n        <span className=\"text-transparent [forced-color-adjust:none]\">{part.source}</span>\n        <span aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 flex items-center justify-center\">\n          <Emoji item={part.item} skinTone={part.tone} play={false} alt=\"\" className=\"size-[1em] align-baseline\" />\n        </span>\n      </span>\n    ),\n  );\n}\n\n/**\n * Code or a command on a muted surface in the theme's monospace font, with a caption\n * and a button that copies it. The code shows exactly as given and scrolls sideways\n * rather than wrapping (unless `wrap`), fading out at the side where a long line goes on, and\n * the block becomes a tab stop while it overflows, so a keyboard can scroll it. Copying says\n * \"Copied\" to screen readers.\n *\n * It does not color the code; to highlight it, set colors on the `data-language` block\n * from a highlighter of your own, or pass highlighted code to your own `<pre>`. With\n * `catalogue`, the emoji characters in the code show as an image catalogue's pictures.\n *\n * Use `Kbd` instead for a key or shortcut in a sentence, `<code>` for a short name in\n * running text, and `CopyButton` for one value without a block around it.\n *\n * @group Display\n * @see https://ui.ouroboros.bot/docs/code-block\n *\n * @claim \"fading out at the side where a long line goes on\" registry/ouro/components/ouro/code-block/code-block.test.tsx verify:testExists(registry/ouro/components/ouro/code-block/code-block.test.tsx::CodeBlock > fades out at the side where a long line goes on, with its focus ring outside the fade)\n * @example\n * <CodeBlock language=\"bash\" code=\"npx shadcn@latest add button\" />\n * <CodeBlock title=\"vite.config.ts\" code={config} className=\"max-h-80\" />\n */\nexport function CodeBlock(props: CodeBlockProps): JSX.Element {\n  const {\n    code,\n    language,\n    title,\n    copy = true,\n    copyLabel = \"Copy code\",\n    wrap,\n    catalogue,\n    className,\n    ref,\n    // Guard props (hint types), dropped here so they never reach the DOM.\n    children,\n    lang,\n    filename,\n    showCopyButton,\n    ...rest\n  } = props;\n  useAppearanceClassNameCheck(\n    \"CodeBlock\",\n    className,\n    process.env.NODE_ENV !== \"production\"\n      ? (token: string) => `remove \"${token}\"; CodeBlock sets its own look from the theme`\n      : /* istanbul ignore next @preserve -- production only */ \"\",\n  );\n  useDevWarnings(\n    \"CodeBlock\",\n    process.env.NODE_ENV !== \"production\"\n      ? () => [\n          children !== undefined && [\"CodeBlock takes its code as code, a string\", \"<CodeBlock code={source} />\"],\n          lang !== undefined && [\"lang is not a prop\", `<CodeBlock language=\"${typeof lang === \"string\" ? lang : \"tsx\"}\" />`],\n          filename !== undefined && [\"filename is not a prop\", `<CodeBlock title=\"${typeof filename === \"string\" ? filename : \"app.tsx\"}\" />`],\n          showCopyButton !== undefined && [\"showCopyButton is not a prop; the copy button shows by default\", \"<CodeBlock code={source} copy={false} />\"],\n        ]\n      : /* istanbul ignore next @preserve -- production only */ undefined,\n  );\n  const fadeRef = useRef<HTMLPreElement>(null);\n  useScrollFade(fadeRef);\n  const scrollable = useMergedRef(useScrollableCode(), fadeRef);\n  const caption = title ?? language;\n  const bar = caption != null || copy;\n  // The focus ring is the figure's outline, not an element over the code: axe reads an element over\n  // a line of text as hiding its background, so it could not judge the text's contrast and measured each\n  // line from a screenshot. The code's mask would also hide an outline on the code itself.\n  // The caption and the copy button share the bar above the code as siblings in the grid: a\n  // <figcaption> names the figure, so the button stays out of it.\n  return (\n    <figure\n      {...rest}\n      ref={ref}\n      data-ouro=\"code-block\"\n      data-language={language}\n      className={cn(\n        \"[@layer_ouro-reset]:m-0 grid min-w-0 grid-cols-[minmax(0,1fr)_auto] overflow-hidden rounded-ouro-lg border border-solid border-border bg-muted text-foreground outline-ring outline-offset-2 has-[>[data-ouro-part=code]:focus-visible]:outline-2 has-[>[data-ouro-part=code]:focus-visible]:outline-solid\",\n        bar ? \"grid-rows-[minmax(calc(var(--spacing)*11),auto)_minmax(0,1fr)]\" : \"grid-rows-[minmax(0,1fr)]\",\n        className,\n      )}\n    >\n      {caption != null ? (\n        <figcaption\n          data-slot=\"caption\"\n          className=\"col-start-1 row-start-1 flex min-w-0 items-center ps-4 pe-2 text-xs leading-5 font-medium text-[color-mix(in_oklab,var(--muted-foreground)_80%,var(--foreground))]\"\n        >\n          <span className=\"min-w-0 truncate\">{caption}</span>\n        </figcaption>\n      ) : null}\n      {copy ? (\n        <CopyButton\n          data-ouro-control=\"code-block\"\n          data-ouro-part=\"copy\"\n          value={code}\n          label={copyLabel}\n          hideLabel\n          variant=\"plain\"\n          size=\"sm\"\n          className=\"col-start-2 row-start-1 self-center me-1.5\"\n        />\n      ) : null}\n      {bar ? (\n        // The code's fade would fade a hairline drawn on the code, so it sits beside the code, under its transparent top border (#530).\n        <span aria-hidden=\"true\" data-slot=\"rule\" className=\"pointer-events-none col-span-2 col-start-1 row-start-2 h-px self-start border-0 border-t border-solid border-border\" />\n      ) : null}\n      <pre\n        ref={scrollable}\n        data-ouro-control=\"code-block\"\n        data-ouro-part=\"code\"\n        className={cn(\n          \"col-span-2 col-start-1 m-0 min-h-0 overflow-auto p-4 font-mono text-sm leading-6 outline-none\",\n          scrollFadeClass,\n          bar ? \"row-start-2 border-0 border-t border-solid border-transparent\" : \"row-start-1\",\n          wrap ? \"whitespace-pre-wrap break-words\" : \"whitespace-pre\",\n        )}\n      >\n        <code className={cn(\"[font-family:inherit]\", wrap ? \"block\" : \"block w-max min-w-full\")}>{catalogue ? <CodeWithEmoji code={code} catalogue={catalogue} /> : code}</code>\n      </pre>\n    </figure>\n  );\n}\n",
      "type": "registry:ui",
      "target": "@components/ouro/code-block/code-block.tsx"
    }
  ],
  "docs": "Code or a command on a muted surface in the theme's monospace font, with a caption and a button that copies it. Import: import { CodeBlock } from \"@/components/ouro/code-block/code-block\", with @/components replaced by your components alias (aliases.components in components.json, such as ~/components). Also installed: button, copy-button, emoji, the helpers they import in lib/ouro. Needs ouro-base in your stylesheet: add that item once per app, from the same place as this one, if you have not. Docs: https://ui.ouroboros.bot/docs/code-block.md Ouro UI v1.3.257.",
  "type": "registry:ui"
}