{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "code-block-story",
  "title": "Code Block Story",
  "author": "Lloyd Richards <lloyd.d.richards@gmail.com>",
  "description": "Interactive Storybook stories demonstrating code block usage and variants",
  "dependencies": [
    "react-aria-components"
  ],
  "registryDependencies": [
    "code-block"
  ],
  "files": [
    {
      "path": "registry/ui/code-block-story/code-block-aria.stories.tsx",
      "content": "import type { Meta, StoryObj } from \"@storybook/nextjs-vite\";\nimport { expect, fn, userEvent, waitFor, within } from \"storybook/test\";\nimport {\n  CodeBlock,\n  CodeBlockActions,\n  CodeBlockContent,\n  CodeBlockCopyButton,\n  CodeBlockFooter,\n  CodeBlockHeader,\n  CodeBlockLine,\n  CodeBlockTitle,\n} from \"@/components/ui/code-block\";\nimport { useHighlightedCode } from \"@/hooks/use-highlighted-code\";\n\nconst source = 'const message = \"Hello, world!\";\\nconsole.log(message);';\n\n/** Display source code, terminal output, and diffs, with optional copying and syntax highlighting. */\nconst meta = {\n  id: \"ui-aria-codeblock\",\n  title: \"ui/CodeBlock\",\n  component: CodeBlock,\n  tags: [\"autodocs\"],\n  parameters: { layout: \"padded\" },\n  args: { className: \"w-full\" },\n  decorators: [\n    (Story) => (\n      <div className=\"mx-auto w-full min-w-0 max-w-xl\">\n        <Story />\n      </div>\n    ),\n  ],\n  render: (args) => (\n    <CodeBlock {...args}>\n      <CodeBlockHeader>\n        <CodeBlockTitle>hello.ts</CodeBlockTitle>\n        <CodeBlockActions>\n          <CodeBlockCopyButton value={source} className=\"size-11\" />\n        </CodeBlockActions>\n      </CodeBlockHeader>\n      <CodeBlockContent\n        role=\"region\"\n        aria-label=\"TypeScript source\"\n        wrap\n        className=\"data-wrap:wrap-anywhere\"\n      >\n        {source}\n      </CodeBlockContent>\n    </CodeBlock>\n  ),\n} satisfies Meta<typeof CodeBlock>;\nexport default meta;\ntype Story = StoryObj<typeof meta>;\n\n/** Show a plain code snippet with a title and copy action. */\nexport const Default: Story = {};\n\n/** Add line numbers and choose the first number for a source excerpt. */\nexport const LineNumbers: Story = {\n  render: (args) => (\n    <CodeBlock {...args}>\n      <CodeBlockContent\n        role=\"region\"\n        aria-label=\"Numbered TypeScript source\"\n        lineNumbers\n        startLine={12}\n      >\n        {source}\n      </CodeBlockContent>\n    </CodeBlock>\n  ),\n};\n\n/** Wrap long output within the container on narrow screens. */\nexport const Wrapped: Story = {\n  decorators: [\n    (Story) => (\n      <div className=\"w-full min-w-0 max-w-72\">\n        <Story />\n      </div>\n    ),\n  ],\n  render: (args) => (\n    <CodeBlock {...args}>\n      <CodeBlockContent\n        role=\"region\"\n        aria-label=\"Request log\"\n        wrap\n        className=\"data-wrap:wrap-anywhere\"\n      >\n        {\n          \"GET /api/components?registry=base&category=storybook&include=dependencies&format=json returned 200 OK after loading the requested component examples.\"\n        }\n      </CodeBlockContent>\n    </CodeBlock>\n  ),\n  play: async ({ canvas }) => {\n    const output = canvas.getByRole(\"region\", { name: \"Request log\" });\n    await expect(output.scrollWidth).toBeLessThanOrEqual(\n      output.clientWidth + 1,\n    );\n  },\n};\n\n/** Present a terminal command and its deterministic output. */\nexport const TerminalOutput: Story = {\n  render: (args) => (\n    <CodeBlock {...args}>\n      <CodeBlockHeader>\n        <CodeBlockTitle>Terminal</CodeBlockTitle>\n      </CodeBlockHeader>\n      <CodeBlockContent\n        role=\"region\"\n        aria-label=\"Build output\"\n        wrap\n        className=\"data-wrap:wrap-anywhere\"\n      >\n        {\"$ bun run build\\nCompiled successfully\\nGenerated static pages\"}\n      </CodeBlockContent>\n      <CodeBlockFooter>Process exited with code 0</CodeBlockFooter>\n    </CodeBlock>\n  ),\n};\n\n/** Mark removed and added lines when reviewing a change. */\nexport const Diff: Story = {\n  render: (args) => (\n    <CodeBlock {...args}>\n      <CodeBlockHeader>\n        <CodeBlockTitle>greeting.ts</CodeBlockTitle>\n      </CodeBlockHeader>\n      <CodeBlockContent role=\"region\" aria-label=\"Code changes\" lineNumbers>\n        <CodeBlockLine>{\"function greet(name: string) {\"}</CodeBlockLine>\n        <CodeBlockLine variant=\"removed\">\n          {'  return \"Hello \" + name;'}\n        </CodeBlockLine>\n        <CodeBlockLine variant=\"added\">\n          {\"  return `Hello, ${name}!`;\"}\n        </CodeBlockLine>\n        <CodeBlockLine>{\"}\"}</CodeBlockLine>\n      </CodeBlockContent>\n    </CodeBlock>\n  ),\n};\n\nfunction HighlightedExample() {\n  const lines = useHighlightedCode(source, \"typescript\");\n  return (\n    <CodeBlock className=\"w-full\">\n      <CodeBlockHeader>\n        <CodeBlockTitle>hello.ts</CodeBlockTitle>\n      </CodeBlockHeader>\n      <CodeBlockContent\n        role=\"region\"\n        aria-label=\"Highlighted TypeScript source\"\n        lineNumbers\n      >\n        {lines.map((line, index) => (\n          <CodeBlockLine key={index}>{line}</CodeBlockLine>\n        ))}\n      </CodeBlockContent>\n    </CodeBlock>\n  );\n}\n\n/** Load the optional Shiki highlighter, retaining plain text while the language loads. */\nexport const SyntaxHighlighted: Story = {\n  render: () => <HighlightedExample />,\n  play: async ({ canvasElement }) => {\n    const code = canvasElement.querySelector(\"code\");\n    if (!code) throw new Error(\"Highlighted code is missing\");\n    await expect(code).toHaveTextContent('const message = \"Hello, world!\";');\n    await expect(code).toHaveTextContent(\"console.log(message);\");\n    await waitFor(\n      () => {\n        const tokens = Array.from(code.querySelectorAll(\"span[style]\"));\n        const coloredTokens = tokens.filter((token) => {\n          const style = (token as HTMLElement).style;\n          return (\n            style.getPropertyValue(\"--shiki-light\") &&\n            style.getPropertyValue(\"--shiki-dark\")\n          );\n        });\n        expect(coloredTokens.length).toBeGreaterThan(0);\n        expect(\n          coloredTokens.some((token) => token.textContent?.includes(\"const\")),\n        ).toBe(true);\n      },\n      { timeout: 10000 },\n    );\n  },\n};\n\n/** Use the destructive variant for an error message or failed command. */\nexport const ErrorOutput: Story = {\n  args: { variant: \"destructive\" },\n  render: (args) => (\n    <CodeBlock {...args}>\n      <CodeBlockContent\n        role=\"region\"\n        aria-label=\"Error output\"\n        wrap\n        className=\"data-wrap:wrap-anywhere\"\n      >\n        {\n          'Error: Missing registry component \"example\".\\nCheck the component name and try again.'\n        }\n      </CodeBlockContent>\n    </CodeBlock>\n  ),\n};\n\n/** Use the small size for compact snippets. */\nexport const Small: Story = { args: { size: \"sm\" } };\n\n/** Fit a narrow container with readable wrapped output and a large copy target. */\nexport const NarrowScreen: Story = {\n  decorators: [\n    (Story) => (\n      <div className=\"w-full min-w-0 max-w-72\">\n        <Story />\n      </div>\n    ),\n  ],\n  render: (args) => (\n    <CodeBlock {...args}>\n      <CodeBlockHeader>\n        <CodeBlockTitle className=\"whitespace-normal break-all\">\n          app/api/components/registry/route.ts\n        </CodeBlockTitle>\n        <CodeBlockActions>\n          <CodeBlockCopyButton value={source} className=\"size-11\" />\n        </CodeBlockActions>\n      </CodeBlockHeader>\n      <CodeBlockContent\n        role=\"region\"\n        aria-label=\"Wrapped TypeScript source\"\n        wrap\n        className=\"data-wrap:wrap-anywhere\"\n      >\n        {source}\n      </CodeBlockContent>\n    </CodeBlock>\n  ),\n  play: async ({ canvas }) => {\n    const output = canvas.getByRole(\"region\", {\n      name: \"Wrapped TypeScript source\",\n    });\n    const copy = canvas.getByRole(\"button\", { name: \"Copy\" });\n    const block = output.parentElement;\n    if (!block?.parentElement)\n      throw new Error(\"Code block container is missing\");\n    await expect(block.getBoundingClientRect().width).toBeLessThanOrEqual(\n      block.parentElement.getBoundingClientRect().width + 1,\n    );\n    await expect(output.scrollWidth).toBeLessThanOrEqual(\n      output.clientWidth + 1,\n    );\n    await expect(copy.getBoundingClientRect().width).toBeGreaterThanOrEqual(44);\n    await expect(copy.getBoundingClientRect().height).toBeGreaterThanOrEqual(\n      44,\n    );\n    await userEvent.click(copy);\n    await userEvent.tab();\n    await expect(output).toHaveFocus();\n  },\n};\n\n/** Copy exact source text and announce success after the clipboard write resolves. */\nexport const ShouldCopySource: Story = {\n  tags: [\"!dev\", \"!autodocs\"],\n  play: async ({ canvasElement }) => {\n    const canvas = within(canvasElement);\n    const clipboard =\n      canvasElement.ownerDocument.defaultView?.navigator.clipboard;\n    if (!clipboard) throw new Error(\"Clipboard API is unavailable\");\n    const original = Object.getOwnPropertyDescriptor(clipboard, \"writeText\");\n    const writeText =\n      fn<(value: string) => Promise<void>>().mockResolvedValue(undefined);\n    Object.defineProperty(clipboard, \"writeText\", {\n      configurable: true,\n      value: writeText,\n    });\n    try {\n      await userEvent.click(canvas.getByRole(\"button\", { name: \"Copy\" }));\n      await expect(writeText).toHaveBeenCalledTimes(1);\n      await expect(writeText).toHaveBeenCalledWith(source);\n      await expect(\n        await canvas.findByRole(\"button\", { name: \"Copied\" }),\n      ).toBeVisible();\n      await expect(canvas.getByText(\"Copied\")).toHaveAttribute(\n        \"aria-live\",\n        \"polite\",\n      );\n    } finally {\n      if (original) Object.defineProperty(clipboard, \"writeText\", original);\n      else Reflect.deleteProperty(clipboard, \"writeText\");\n    }\n  },\n};\n",
      "type": "registry:component",
      "target": "@ui/code-block.stories.tsx"
    }
  ],
  "categories": [
    "ui",
    "storybook",
    "code-block"
  ],
  "type": "registry:component"
}