{"name":"code-block","dependencies":["class-variance-authority","lucide-react"],"registryDependencies":["@herocn/button","@herocn/use-highlighted-code"],"files":[{"path":"code-block.tsx","content":"\"use client\";\n\nimport { cva, type VariantProps } from \"class-variance-authority\";\nimport { cn } from \"cn\";\nimport { CheckIcon, CopyIcon } from \"lucide-react\";\nimport * as React from \"react\";\nimport { Button } from \"@/components/ui/button\";\n\nconst codeBlockVariants = cva(\n\t\"group/code-block has-[>[data-slot=code-block-content]:focus-visible]:focus-ring relative flex w-full min-w-0 flex-col overflow-hidden rounded-lg bg-surface-secondary text-sm data-[size=sm]:rounded-md data-[size=sm]:text-xs\",\n\t{\n\t\tvariants: {\n\t\t\tvariant: {\n\t\t\t\tdefault: \"border border-surface-secondary text-foreground\",\n\t\t\t\tdestructive: \"border border-destructive/70 text-destructive\",\n\t\t\t},\n\t\t},\n\t\tdefaultVariants: {\n\t\t\tvariant: \"default\",\n\t\t},\n\t},\n);\n\nfunction CodeBlock({\n\tclassName,\n\tvariant = \"default\",\n\tsize = \"default\",\n\t...props\n}: React.ComponentProps<\"div\"> &\n\tVariantProps<typeof codeBlockVariants> & { size?: \"default\" | \"sm\" }) {\n\treturn (\n\t\t<div\n\t\t\tdata-slot=\"code-block\"\n\t\t\tdata-variant={variant}\n\t\t\tdata-size={size}\n\t\t\tclassName={cn(codeBlockVariants({ variant }), className)}\n\t\t\t{...props}\n\t\t/>\n\t);\n}\n\nfunction CodeBlockHeader({ className, ...props }: React.ComponentProps<\"div\">) {\n\treturn (\n\t\t<div\n\t\t\tdata-slot=\"code-block-header\"\n\t\t\tclassName={cn(\n\t\t\t\t\"flex min-w-0 shrink-0 items-center gap-1 px-2 pt-2 text-muted-foreground *:has-[>[data-slot=code-block-title]]:min-w-0 group-data-[size=sm]/code-block:px-1.5 group-data-[size=sm]/code-block:pt-1.5 group-data-[variant=destructive]/code-block:text-destructive\",\n\t\t\t\tclassName,\n\t\t\t)}\n\t\t\t{...props}\n\t\t/>\n\t);\n}\n\nfunction CodeBlockTitle({ className, ...props }: React.ComponentProps<\"div\">) {\n\treturn (\n\t\t<div\n\t\t\tdata-slot=\"code-block-title\"\n\t\t\tclassName={cn(\n\t\t\t\t\"min-w-0 truncate font-medium font-sans text-sm group-data-[size=sm]/code-block:text-xs group-data-[variant=destructive]/code-block:text-destructive [&_svg:not([class*='size-'])]:size-4 group-data-[size=sm]/code-block:[&_svg:not([class*='size-'])]:size-3.5 [&_svg]:pointer-events-none [&_svg]:inline-block [&_svg]:align-middle *:[svg]:me-1\",\n\t\t\t\tclassName,\n\t\t\t)}\n\t\t\t{...props}\n\t\t/>\n\t);\n}\n\nfunction CodeBlockActions({\n\tclassName,\n\t...props\n}: React.ComponentProps<\"div\">) {\n\treturn (\n\t\t<div\n\t\t\tdata-slot=\"code-block-actions\"\n\t\t\tclassName={cn(\n\t\t\t\t\"-my-1 ms-auto -me-1 flex shrink-0 items-center gap-0.5 [[data-slot=code-block]>&]:absolute [[data-slot=code-block]>&]:end-2 [[data-slot=code-block]>&]:top-2 [[data-slot=code-block]>&]:z-10 [[data-slot=code-block]>&]:m-0 [[data-slot=code-block]>&]:opacity-0 [[data-slot=code-block]>&]:pointer-coarse:opacity-100 [[data-slot=code-block]>&]:transition-opacity [[data-slot=code-block]>&]:group-focus-within/code-block:opacity-100 [[data-slot=code-block]>&]:group-hover/code-block:opacity-100 group-data-[size=sm]/code-block:[[data-slot=code-block]>&]:end-1.5 group-data-[size=sm]/code-block:[[data-slot=code-block]>&]:top-1.5 [[data-slot=code-block]>&]:*:[button]:bg-background [[data-slot=code-block]>&]:*:[button]:ring-1 [[data-slot=code-block]>&]:*:[button]:ring-foreground/10\",\n\t\t\t\tclassName,\n\t\t\t)}\n\t\t\t{...props}\n\t\t/>\n\t);\n}\n\nfunction CodeBlockCopyButton({\n\tvalue,\n\tvariant = \"ghost\",\n\tsize = \"icon-xs\",\n\tclassName,\n\tonClick,\n\t...props\n}: React.ComponentProps<typeof Button> & { value: string }) {\n\tconst [copied, setCopied] = React.useState(false);\n\n\tReact.useEffect(() => {\n\t\tif (!copied) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst timeout = setTimeout(() => setCopied(false), 2000);\n\t\treturn () => clearTimeout(timeout);\n\t}, [copied]);\n\n\treturn (\n\t\t<>\n\t\t\t<Button\n\t\t\t\tdata-slot=\"code-block-copy-button\"\n\t\t\t\tdata-copied={copied || undefined}\n\t\t\t\tvariant={variant}\n\t\t\t\tsize={size}\n\t\t\t\taria-label={copied ? \"Copied\" : \"Copy\"}\n\t\t\t\tclassName={cn(className)}\n\t\t\t\tonClick={(event) => {\n\t\t\t\t\tonClick?.(event);\n\t\t\t\t\tnavigator.clipboard\n\t\t\t\t\t\t.writeText(value)\n\t\t\t\t\t\t.then(() => setCopied(true))\n\t\t\t\t\t\t.catch(() => {});\n\t\t\t\t}}\n\t\t\t\t{...props}\n\t\t\t>\n\t\t\t\t{copied ? <CheckIcon /> : <CopyIcon />}\n\t\t\t</Button>\n\t\t\t<span aria-live=\"polite\" className=\"sr-only\">\n\t\t\t\t{copied ? \"Copied\" : \"\"}\n\t\t\t</span>\n\t\t</>\n\t);\n}\n\nfunction CodeBlockContent({\n\tclassName,\n\tchildren,\n\tstyle,\n\twrap = false,\n\tlineNumbers = false,\n\tstartLine = 1,\n\t...props\n}: React.ComponentProps<\"pre\"> & {\n\twrap?: boolean;\n\tlineNumbers?: boolean;\n\tstartLine?: number;\n}) {\n\treturn (\n\t\t<pre\n\t\t\tdata-slot=\"code-block-content\"\n\t\t\tdata-wrap={wrap || undefined}\n\t\t\tdata-numbered={lineNumbers || undefined}\n\t\t\tdir=\"ltr\"\n\t\t\ttabIndex={0}\n\t\t\tstyle={{ counterReset: `line ${startLine - 1}`, ...style }}\n\t\t\tclassName={cn(\n\t\t\t\t\"scroll-fade-x scrollbar-thin data-wrap:scroll-fade-y data-wrap:wrap-break-word min-h-0 min-w-0 overflow-auto p-2 text-start font-mono leading-relaxed outline-none group-has-data-[slot=code-block-header]/code-block:pt-1.5 group-has-data-[slot=code-block-footer]/code-block:pb-1.5 data-wrap:whitespace-pre-wrap group-data-[size=sm]/code-block:px-1.5 group-data-[size=sm]/code-block:py-1.5 group-data-[size=sm]/code-block:group-has-data-[slot=code-block-header]/code-block:pt-1 group-data-[size=sm]/code-block:group-has-data-[slot=code-block-footer]/code-block:pb-1\",\n\t\t\t\tclassName,\n\t\t\t)}\n\t\t\t{...props}\n\t\t>\n\t\t\t<code\n\t\t\t\tdata-slot=\"code-block-code\"\n\t\t\t\tclassName=\"grid min-h-lh in-data-wrap:min-w-0 min-w-max in-data-numbered:grid-cols-[auto_1fr] content-start\"\n\t\t\t>\n\t\t\t\t{lineNumbers && typeof children === \"string\"\n\t\t\t\t\t? children\n\t\t\t\t\t\t\t.replace(/\\n$/, \"\")\n\t\t\t\t\t\t\t.split(\"\\n\")\n\t\t\t\t\t\t\t.map((line, index) => (\n\t\t\t\t\t\t\t\t<CodeBlockLine key={index}>{line}</CodeBlockLine>\n\t\t\t\t\t\t\t))\n\t\t\t\t\t: children}\n\t\t\t</code>\n\t\t</pre>\n\t);\n}\n\nconst codeBlockLineVariants = cva(\n\t\"relative in-data-numbered:col-span-2 -mx-2 block in-data-numbered:grid min-h-lh in-data-numbered:grid-cols-subgrid in-data-numbered:gap-x-2 px-2 in-data-numbered:[counter-increment:line] before:min-w-6 in-data-numbered:before:select-none before:text-end before:text-muted-foreground in-data-numbered:before:content-[counter(line)] after:absolute after:start-1 after:top-0 after:select-none after:text-muted-foreground group-data-[size=sm]/code-block:-mx-1.5 group-data-[size=sm]/code-block:px-1.5 in-data-numbered:supports-[content:counter(line)_/_'']:before:content-[counter(line)_/_'']\",\n\t{\n\t\tvariants: {\n\t\t\tvariant: {\n\t\t\t\tdefault: \"\",\n\t\t\t\tadded: \"bg-muted after:content-['+']\",\n\t\t\t\tremoved: \"bg-destructive/20 after:text-destructive after:content-['−']\",\n\t\t\t\thighlighted: \"bg-muted\",\n\t\t\t},\n\t\t},\n\t\tdefaultVariants: {\n\t\t\tvariant: \"default\",\n\t\t},\n\t},\n);\n\nfunction CodeBlockLine({\n\tclassName,\n\tvariant = \"default\",\n\tchildren,\n\t...props\n}: React.ComponentProps<\"span\"> & VariantProps<typeof codeBlockLineVariants>) {\n\t// An empty line holds a newline so copying a selection keeps blank lines.\n\tconst empty =\n\t\tchildren === undefined ||\n\t\tchildren === null ||\n\t\tchildren === \"\" ||\n\t\t(Array.isArray(children) && children.length === 0);\n\n\treturn (\n\t\t<span\n\t\t\tdata-slot=\"code-block-line\"\n\t\t\tdata-variant={variant}\n\t\t\tclassName={cn(codeBlockLineVariants({ variant }), className)}\n\t\t\t{...props}\n\t\t>\n\t\t\t<span data-slot=\"code-block-line-content\" className=\"min-w-0\">\n\t\t\t\t{empty ? \"\\n\" : children}\n\t\t\t</span>\n\t\t</span>\n\t);\n}\n\nfunction CodeBlockFooter({ className, ...props }: React.ComponentProps<\"div\">) {\n\treturn (\n\t\t<div\n\t\t\tdata-slot=\"code-block-footer\"\n\t\t\tclassName={cn(\n\t\t\t\t\"flex min-w-0 shrink-0 items-center gap-1 px-2 pb-1 text-muted-foreground group-data-[size=sm]/code-block:px-1.5 group-data-[size=sm]/code-block:pb-0.5\",\n\t\t\t\tclassName,\n\t\t\t)}\n\t\t\t{...props}\n\t\t/>\n\t);\n}\n\nexport {\n\tCodeBlock,\n\tCodeBlockActions,\n\tCodeBlockContent,\n\tCodeBlockCopyButton,\n\tCodeBlockFooter,\n\tCodeBlockHeader,\n\tCodeBlockLine,\n\tCodeBlockTitle,\n};\n","type":"registry:ui"}],"cssVars":{"theme":{"--color-surface-secondary":"var(--surface-secondary)"},"light":{"--surface-secondary":"oklch(0.9524 0.0013 286.37)"},"dark":{"--surface-secondary":"oklch(0.257 0.0037 286.14)"}},"css":{"@utility focus-ring":{"@apply ring-2 ring-ring ring-offset-2 ring-offset-background outline-none":{}}},"type":"registry:ui"}