{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "code-block",
  "title": "Code block",
  "description": "Code with its colours, under a header that names the file or holds a tab for each one, with a Copy button. Line numbers, highlighted, added and removed lines, and a long file that shows its first lines until expanded.",
  "dependencies": [],
  "registryDependencies": [
    "https://kinoui.com/r/copy-button.json",
    "https://kinoui.com/r/tooltip.json",
    "https://kinoui.com/r/tabs.json",
    "https://kinoui.com/r/code-lines.json",
    "https://kinoui.com/r/syntax.json",
    "https://kinoui.com/r/controllable.json",
    "https://kinoui.com/r/resize.json",
    "https://kinoui.com/r/fold.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/code-block.tsx",
      "content": "'use client'\n\nimport { CopyButton } from '@/registry/components/kino/copy-button'\nimport { Tabs, TabsPanel, TabsList, TabsTab } from '@/registry/components/kino/tabs'\nimport type { TooltipContentProps } from '@/registry/components/kino/tooltip'\nimport {\n  type CodeBlockProps,\n  type CodeFile,\n  CodeFoldButton,\n  CodeLines,\n  CodeScroller,\n  CodeTitle,\n  codeCardClassName,\n  codeHeaderClassName,\n  collapses,\n  fileValue,\n  useCodeTokens,\n} from '@/registry/lib/code-lines'\nimport { useControllable } from '@/registry/lib/controllable'\nimport { bringTopBack } from '@/registry/lib/fold'\nimport { followHeight } from '@/registry/lib/resize'\nimport { codeLines, languageName } from '@/registry/lib/syntax'\nimport { useEffect, useId, useRef, type ReactNode } from 'react'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * Code in a panel: a header names the file, or holds a tab for each one,\n * with the Copy button at its end, and the lines sit under it. A long file\n * shows its first lines and a footer that shows all of them.\n */\n\nfunction CodeBody({\n  file,\n  lineNumbers,\n  labelledBy,\n  collapsed,\n  collapsedLines,\n  id,\n}: {\n  file: CodeFile\n  lineNumbers: boolean\n  labelledBy?: string\n  collapsed: boolean\n  collapsedLines?: number\n  id: string\n}) {\n  const lines = useCodeTokens(file.code, file.language, file.tokens)\n  return (\n    <CodeScroller\n      id={id}\n      label={file.label ?? `${file.title ?? languageName(file.language)} code`}\n      labelledBy={file.label ? undefined : labelledBy}\n      collapsed={collapsed}\n      collapsedLines={collapsedLines}\n      className='bg-muted/50'\n    >\n      <CodeLines\n        lines={lines}\n        lineNumbers={lineNumbers}\n        startLine={file.startLine}\n        highlighted={file.highlighted}\n        added={file.added}\n        removed={file.removed}\n      />\n    </CodeScroller>\n  )\n}\n\n/** Tabs when there are several files; a single file is just its header and lines. */\nfunction FileSwitch({\n  tabbed,\n  value,\n  onValueChange,\n  children,\n}: {\n  tabbed: boolean\n  value: string\n  onValueChange: (value: string) => void\n  children: ReactNode\n}) {\n  if (!tabbed) return <div className='flex min-w-0 flex-col'>{children}</div>\n  return (\n    <Tabs\n      value={value}\n      onValueChange={(next) => onValueChange(String(next))}\n      className='gap-0'\n    >\n      {children}\n    </Tabs>\n  )\n}\n\n/** A file's tab panel, when there are tabs. The lines scroll as their own region: no second stop for the keyboard. */\nfunction FilePanel({\n  tabbed,\n  value,\n  className,\n  children,\n}: {\n  tabbed: boolean\n  value: string\n  className?: string\n  children: ReactNode\n}) {\n  if (!tabbed) return children\n  return (\n    <TabsPanel value={value} tabIndex={-1} className={className}>\n      {children}\n    </TabsPanel>\n  )\n}\n\nfunction CodeBlock({\n  files,\n  value: valueProp,\n  defaultValue,\n  onValueChange,\n  lineNumbers = false,\n  collapsedLines,\n  expanded: expandedProp,\n  defaultExpanded = false,\n  onExpandedChange,\n  label = 'Files',\n  tooltipProps,\n  onCopy,\n  streaming = false,\n  className,\n}: CodeBlockProps & {\n  /** The code is still arriving: each line shows as it lands, with no glide, and Copy waits until it has all of it. */\n  streaming?: boolean\n  /** Props for the Copy button's tooltip, as the Copy button takes them. */\n  tooltipProps?: Omit<TooltipContentProps, 'children'>\n  /** Called when the Copy button is pressed, with the file it copies. */\n  onCopy?: (file: CodeFile) => void\n}) {\n  const id = useId()\n  const [value, setValue] = useControllable(\n    valueProp,\n    defaultValue ?? fileValue(files[0]),\n    onValueChange,\n  )\n  const [expanded, setExpanded] = useControllable(\n    expandedProp,\n    defaultExpanded,\n    onExpandedChange,\n  )\n  const active = files.find((file) => fileValue(file) === value) ?? files[0]\n  const lineCount = codeLines(active.code).length\n  const collapsible = collapses(lineCount, collapsedLines)\n  const collapsed = collapsible && !expanded\n  const tabbed = files.length > 1\n\n  const rootRef = useRef<HTMLDivElement>(null)\n  const bodyRef = useRef<HTMLDivElement>(null)\n  // A file longer or shorter than the last moves the block to its height.\n  useEffect(() => {\n    const body = bodyRef.current\n    const inner = body?.firstElementChild\n    if (!body || !(inner instanceof HTMLElement)) return\n    // A fold moves the lines' own box: the body takes each of its frames as they come.\n    // Code still arriving lands at once, or a glide would hide lines that have come.\n    return followHeight(body, inner, {\n      instant: () =>\n        body.querySelector('[data-moving]') !== null ||\n        rootRef.current?.hasAttribute('data-streaming') === true,\n    })\n  }, [])\n\n  const toggle = () => {\n    const root = rootRef.current\n    if (expanded && root) bringTopBack(root)\n    setExpanded(!expanded)\n  }\n\n  const scrollerId = `${id}-${fileValue(active)}`\n  const titleId = `${id}-title`\n\n  return (\n    <div\n      ref={rootRef}\n      data-slot='code-block'\n      data-streaming={streaming ? '' : undefined}\n      className={cn(codeCardClassName, className)}\n    >\n      <FileSwitch tabbed={tabbed} value={fileValue(active)} onValueChange={setValue}>\n        <div data-slot='code-block-header' className={codeHeaderClassName}>\n          {tabbed ? (\n            <TabsList\n              variant='line'\n              aria-label={label}\n              className='min-w-0 flex-1 self-end'\n            >\n              {files.map((file) => (\n                <TabsTab\n                  key={fileValue(file)}\n                  value={fileValue(file)}\n                  className={cn('text-xs', file.title !== undefined && 'font-mono')}\n                >\n                  {file.title ?? languageName(file.language)}\n                </TabsTab>\n              ))}\n            </TabsList>\n          ) : (\n            <CodeTitle title={active.title} language={active.language} id={titleId} />\n          )}\n          <CopyButton\n            value={active.code}\n            size='icon-sm'\n            // Named after its code, so a page of several blocks names each one apart.\n            aria-label={`Copy ${active.title ?? languageName(active.language)} code`}\n            tooltipProps={tooltipProps}\n            onClick={() => onCopy?.(active)}\n            disabled={streaming}\n            className='shrink-0'\n          />\n        </div>\n        <div\n          ref={bodyRef}\n          data-slot='code-block-body'\n          className='min-w-0 overflow-clip transition-[height] duration-(--motion-layout-duration) ease-(--motion-layout-easing) motion-reduce:transition-none'\n        >\n          <div>\n            {files.map((file) => (\n              <FilePanel\n                key={fileValue(file)}\n                tabbed={tabbed}\n                value={fileValue(file)}\n                className='rounded-none'\n              >\n                <CodeBody\n                  file={file}\n                  id={`${id}-${fileValue(file)}`}\n                  lineNumbers={lineNumbers}\n                  labelledBy={tabbed ? undefined : titleId}\n                  collapsed={collapsed}\n                  collapsedLines={collapsible ? collapsedLines : undefined}\n                />\n              </FilePanel>\n            ))}\n          </div>\n        </div>\n      </FileSwitch>\n      {collapsible ? (\n        <CodeFoldButton\n          expanded={expanded}\n          lines={lineCount}\n          controls={scrollerId}\n          onToggle={toggle}\n        />\n      ) : null}\n    </div>\n  )\n}\n\nexport { CodeBlock }\n",
      "type": "registry:component",
      "target": "@components/kino/code-block.tsx"
    }
  ],
  "css": {
    "@property --code-fade": {
      "syntax": "'<length>'",
      "inherits": "false",
      "initial-value": "0px"
    },
    ":root": {
      "--code-keyword": "oklch(0.47 0.19 330)",
      "--code-string": "oklch(0.46 0.12 155)",
      "--code-string-expression": "oklch(0.46 0.12 155)",
      "--code-function": "oklch(0.47 0.17 260)",
      "--code-link": "oklch(0.47 0.17 260)",
      "--code-constant": "oklch(0.48 0.13 50)",
      "--code-parameter": "oklch(0.48 0.15 20)",
      "--code-comment": "oklch(0.47 0 0)",
      "--code-punctuation": "oklch(0.42 0 0)",
      "--code-added": "oklch(0.55 0.14 155)",
      "--code-removed": "oklch(0.56 0.2 25)"
    },
    ".dark": {
      "--code-keyword": "oklch(0.78 0.14 330)",
      "--code-string": "oklch(0.8 0.13 155)",
      "--code-string-expression": "oklch(0.8 0.13 155)",
      "--code-function": "oklch(0.78 0.12 255)",
      "--code-link": "oklch(0.78 0.12 255)",
      "--code-constant": "oklch(0.82 0.12 65)",
      "--code-parameter": "oklch(0.8 0.11 25)",
      "--code-comment": "oklch(0.7 0 0)",
      "--code-punctuation": "oklch(0.75 0 0)",
      "--code-added": "oklch(0.72 0.15 155)",
      "--code-removed": "oklch(0.7 0.17 25)"
    }
  },
  "type": "registry:component"
}