{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-code-snippet",
  "type": "registry:ui",
  "title": "react-tw/code-snippet.tsx",
  "description": "Carbon `CodeSnippet` as a React + Tailwind component, the look and rules of `<ui-code-snippet>`. Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [
    "@purpurabog/ui@^2.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/core-position",
    "@kanok-ui/tw-copy-button",
    "@kanok-ui/tw-i18n",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-tooltip",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/code-snippet.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/code-snippet.tsx",
      "content": "import type { ReactNode, HTMLAttributes, Ref, ComponentProps } from 'react';\nimport type { Placement } from '../core/position.ts';\nimport { CodeSnippet as SnippetPrimitive, Copy as CopyPrimitive } from '@purpurabog/ui/headless';\nimport { CopyButton } from './copy-button.tsx';\nimport { useTranslation } from './lib/i18n.ts';\nimport { cn } from './lib/utils.ts';\nimport { Tooltip } from './tooltip.tsx';\n\nexport type CodeSnippetType = 'single' | 'multi' | 'inline';\n\nexport interface CodeSnippetProps extends Omit<HTMLAttributes<HTMLElement>, 'type' | 'text' | 'label' | 'copyLabel' | 'feedback' | 'feedbackTimeout' | 'onCopy' | 'placement' | 'hideCopyButton' | 'wrapText' | 'disabled' | 'expanded' | 'defaultExpanded' | 'onToggle' | 'showMoreText' | 'showLessText' | 'maxCollapsedRows' | 'maxExpandedRows' | 'minCollapsedRows' | 'minExpandedRows' | 'lang' | 'className' | 'children'> {\n    /** The snippet's outer element. */\n    ref?: Ref<HTMLElement>;\n    type?: CodeSnippetType;\n    /** What gets copied instead of the code shown. */\n    text?: string;\n    /** Accessible name of the read-only textbox (single / multi); inline: the copy button's tooltip and description (the button is named by its code, WCAG 2.5.3). */\n    label?: string;\n    /** Tooltip of the copy button (single / multi). */\n    copyLabel?: string;\n    feedback?: string;\n    feedbackTimeout?: number;\n    /** Called before copying; return false to copy it yourself (the feedback still shows). */\n    onCopy?: (text: string) => boolean | void;\n    placement?: Placement;\n    hideCopyButton?: boolean;\n    /** Multi: wrap long lines instead of scrolling sideways. */\n    wrapText?: boolean;\n    disabled?: boolean;\n    /** Multi: showing every row (controlled). */\n    expanded?: boolean;\n    defaultExpanded?: boolean;\n    onToggle?: (expanded: boolean) => void;\n    showMoreText?: string;\n    showLessText?: string;\n    /** Rows (16px) visible while collapsed; 0 = no limit. */\n    maxCollapsedRows?: number;\n    /** Rows visible while expanded; 0 = no limit. */\n    maxExpandedRows?: number;\n    minCollapsedRows?: number;\n    minExpandedRows?: number;\n    lang?: string;\n    className?: string;\n    /** The code. As in `<pre>`, one line break right at the start is dropped. */\n    children: ReactNode;\n}\n\nconst codeType = 'm-0 text-ink type-code';\nconst forcedOutline = 'forced-colors:outline-1 forced-colors:outline-[CanvasText] forced-colors:outline-solid';\n\nconst chevron = (expanded: boolean, disabled: boolean) => (\n    <svg\n        viewBox=\"0 0 16 16\"\n        aria-hidden=\"true\"\n        className={cn(\n            'ms-2 size-4 fill-icon-primary',\n            // Carbon: opening turns it with $transition-expansion (300ms) and the browser's ease, closing in moderate-01\n            expanded ? '[transform:rotate(180deg)] transition-[transform] duration-300 ease-[ease] motion-reduce:transition-none' : 'transition-all duration-(--ui-duration-moderate-01) ease-carbon',\n            disabled && 'fill-icon-disabled',\n        )}\n    >\n        <path d=\"M8 11 3 6l.7-.7L8 9.6l4.3-4.3.7.7z\" />\n    </svg>\n);\n\n/**\n * Carbon `CodeSnippet` as a React + Tailwind component, the look and rules of `<ui-code-snippet>`.\n * `single`: one 40px line that scrolls sideways, a read-only textbox, copy button at the end.\n * `multi`: a block collapsed to at most `maxCollapsedRows` (15 × 16px) with \"Show more / Show less\"\n * when longer, copy button top-right, optional `wrapText`. `inline`: a `<code>` chip that is itself\n * the copy button (or plain with `hideCopyButton`). The copied text is `text`, else the code shown.\n * The behaviour is the headless CodeSnippet (and Copy for the inline chip).\n */\nexport function CodeSnippet(props: Readonly<CodeSnippetProps>) {\n    const { type = 'single', text = '', expanded, defaultExpanded, onToggle, maxCollapsedRows, maxExpandedRows, minCollapsedRows, minExpandedRows } = props;\n    // the expanded state is the multi-line snippet's only\n    const multi = type === 'multi';\n    return (\n        <SnippetPrimitive.Root\n            text={text}\n            expanded={multi ? expanded : undefined}\n            defaultExpanded={multi ? defaultExpanded : undefined}\n            onExpandedChange={multi ? onToggle : undefined}\n            maxCollapsedRows={maxCollapsedRows}\n            maxExpandedRows={maxExpandedRows}\n            minCollapsedRows={minCollapsedRows}\n            minExpandedRows={minExpandedRows}\n        >\n            <Snippet {...props} />\n        </SnippetPrimitive.Root>\n    );\n}\n\n/** The snippet of its type, inside the primitive (it follows the code's copy text and the expanded state). */\nfunction Snippet(props: Readonly<CodeSnippetProps>) {\n    // what isn't the snippet's own (the rest) goes to its outer element\n    const { type = 'single', text, label, copyLabel, feedback, feedbackTimeout = 2000, onCopy, placement = 'bottom', hideCopyButton = false, disabled = false, lang, className, children, wrapText, expanded, defaultExpanded, onToggle, showMoreText, showLessText, maxCollapsedRows, maxExpandedRows, minCollapsedRows, minExpandedRows, ref, ...attributes } = props;\n    const t = useTranslation(lang).copy;\n    const { copyText } = SnippetPrimitive.useCodeSnippetContext();\n    const shown = typeof children === 'string' ? children.replace(/^\\n/, '') : children;\n\n    if (type === 'inline') return <InlineSnippet {...props} ref={undefined} root={attributes} rootRef={ref} copyText={copyText} shown={shown} />;\n\n    const copyButton = hideCopyButton ? null : (\n        <CopyButton\n            className={cn('absolute end-0 top-0 z-10', type === 'multi' && 'end-2 top-2')}\n            size={type === 'multi' ? 'sm' : 'md'}\n            text={copyText}\n            label={copyLabel || t.copy}\n            feedback={feedback || t.copied}\n            feedbackTimeout={feedbackTimeout}\n            onCopy={onCopy}\n            placement={placement}\n            disabled={disabled}\n            lang={lang}\n        />\n    );\n    const textbox = { role: 'textbox', tabIndex: disabled ? undefined : 0, 'aria-label': label || t.copy, 'aria-readonly': true } as const;\n    const focusRing = 'outline-none focus-visible:outline-[length:var(--ui-focus-width,2px)] focus-visible:outline-focus focus-visible:outline-solid';\n\n    if (type === 'multi') return <MultiSnippet {...props} ref={undefined} root={attributes} rootRef={ref} shown={shown} textbox={textbox} focusRing={focusRing} copyButton={copyButton} />;\n\n    return (\n        <div {...attributes} ref={ref as Ref<HTMLDivElement> | undefined} className={cn('relative block w-full max-w-[768px] font-sans', className)}>\n            {/* --ui-code-background: the snippet's fill (Carbon: the layer's) */}\n            <div className={cn('box-border flex h-10 items-center rounded-[var(--ui-code-radius,0px)] bg-[color:var(--ui-code-background,var(--ui-surface-1))] pe-10', hideCopyButton && 'p-0', disabled && 'cursor-not-allowed text-ink-disabled', forcedOutline)}>\n                <div {...textbox} className={cn('relative flex h-full w-full items-center overflow-x-auto ps-4', focusRing, 'focus-visible:-outline-offset-2 focus-visible:rounded-[var(--ui-code-radius,0px)]')}>\n                    <pre className={cn(codeType, 'pe-2 whitespace-pre', disabled && 'text-ink-disabled')}>\n                        <SnippetPrimitive.Code className={cn(codeType, disabled && 'text-ink-disabled')}>{shown}</SnippetPrimitive.Code>\n                    </pre>\n                </div>\n                {copyButton}\n            </div>\n        </div>\n    );\n}\n\ninterface PartsProps extends CodeSnippetProps {\n    shown: ReactNode;\n    /** The user's HTML attributes and ref, for the outer element. */\n    root: HTMLAttributes<HTMLElement>;\n    rootRef?: Ref<HTMLElement>;\n}\n\nfunction MultiSnippet({ root, rootRef, shown, textbox, focusRing, copyButton, wrapText = false, disabled = false, hideCopyButton = false, showMoreText, showLessText, lang, className }: PartsProps & { textbox: Record<string, unknown>; focusRing: string; copyButton: ReactNode }) {\n    const t = useTranslation(lang).copy;\n    const { expanded } = SnippetPrimitive.useCodeSnippetContext();\n\n    return (\n        <div {...root} ref={rootRef as Ref<HTMLDivElement> | undefined} className={cn('relative block w-full max-w-[768px] font-sans', className)}>\n            <div className={cn('relative box-border flex rounded-[var(--ui-code-radius,0px)] bg-[color:var(--ui-code-background,var(--ui-surface-1))] p-4', disabled && 'cursor-not-allowed text-ink-disabled', forcedOutline)}>\n                <SnippetPrimitive.Content className={cn('relative order-1 w-full overflow-y-auto transition-[max-height] duration-(--ui-duration-moderate-01) ease-carbon', expanded && 'pb-4')}>\n                    <pre {...textbox} aria-multiline=\"true\" className={cn(codeType, 'pe-10 whitespace-pre', focusRing, 'focus-visible:outline-offset-0 focus-visible:rounded-[var(--ui-link-focus-radius,0px)]', hideCopyButton && 'pe-0', wrapText && 'whitespace-pre-wrap [overflow-wrap:break-word]', disabled && 'text-ink-disabled')}>\n                        <SnippetPrimitive.Code className={cn(codeType, 'block overflow-hidden', disabled && 'text-ink-disabled')}>{shown}</SnippetPrimitive.Code>\n                    </pre>\n                </SnippetPrimitive.Content>\n                {copyButton}\n                <SnippetPrimitive.Toggle\n                    disabled={disabled}\n                    className={cn(\n                        'absolute end-0 bottom-0 z-10 inline-flex h-8 cursor-pointer items-center rounded-ee-[var(--ui-code-radius,0px)] border-0 bg-[color:var(--ui-code-background,var(--ui-surface-1))] px-4 py-2 text-ink type-body-sm leading-[18px]! outline-none',\n                        'hover:bg-ghost-hover active:bg-ghost-active focus-visible:outline-[length:var(--ui-focus-width,2px)] focus-visible:[outline-offset:var(--ui-focus-offset,-2px)] focus-visible:outline-focus focus-visible:outline-solid',\n                        // Carbon .cds--btn: the fill and the ring change in fast-01 with the entrance easing\n                        'transition-[background-color,outline] duration-(--ui-duration-fast) ease-(--ui-easing-entrance)',\n                        disabled && 'cursor-not-allowed text-ink-disabled hover:bg-[color:var(--ui-code-background,var(--ui-surface-1))]',\n                    )}\n                >\n                    <span className=\"relative -top-px\">{expanded ? showLessText || t.showLess : showMoreText || t.showMore}</span>\n                    {chevron(expanded, disabled)}\n                </SnippetPrimitive.Toggle>\n            </div>\n        </div>\n    );\n}\n\nfunction InlineSnippet({ root, rootRef, copyText, shown, label, feedback, feedbackTimeout, onCopy, placement = 'bottom', hideCopyButton = false, disabled = false, lang, className }: PartsProps & { copyText: string }) {\n    const chip = 'relative box-border h-5 min-h-5 rounded-[4px] border border-solid border-transparent bg-[color:var(--ui-code-background,var(--ui-surface-1))] p-0 text-ink type-code outline-none';\n    const codeEl = <SnippetPrimitive.Code className={cn(codeType, 'px-2 whitespace-pre', disabled && 'text-ink-disabled')}>{shown}</SnippetPrimitive.Code>;\n    const box = cn('relative inline font-sans', className);\n    if (hideCopyButton) {\n        return (\n            <span {...root} ref={rootRef} className={box}>\n                <span className={cn(chip, 'inline-block cursor-auto', disabled && 'cursor-not-allowed text-ink-disabled', forcedOutline)}>{codeEl}</span>\n            </span>\n        );\n    }\n    return (\n        <span {...root} ref={rootRef} className={box}>\n            <CopyPrimitive.Root text={copyText} onCopy={onCopy} feedbackTimeout={feedbackTimeout}>\n                <InlineCopy label={label} feedback={feedback} placement={placement} disabled={disabled} lang={lang} chip={chip}>\n                    {codeEl}\n                </InlineCopy>\n            </CopyPrimitive.Root>\n        </span>\n    );\n}\n\n/** The inline chip as the copy button (named by its code), its tooltip and live region, inside the copy primitive. */\nfunction InlineCopy({ label, feedback, placement, disabled, lang, chip, children }: Readonly<{ label?: string; feedback?: string; placement: Placement; disabled: boolean; lang?: string; chip: string; children: ReactNode }>) {\n    const t = useTranslation(lang).copy;\n    const { copied } = CopyPrimitive.useCopyContext();\n    const said = feedback || t.copied;\n    return (\n        <>\n            <Tooltip text={copied ? said : label || t.copy} small placement={placement} open={copied || undefined}>\n                <CopyPrimitive.Trigger\n                    disabled={disabled}\n                    focusableWhenDisabled\n                    className={cn(chip, 'inline cursor-pointer hover:bg-ghost-hover focus-visible:border-focus active:bg-ghost-active', disabled && 'cursor-not-allowed text-ink-disabled', forcedOutline)}\n                >\n                    {children}\n                </CopyPrimitive.Trigger>\n            </Tooltip>\n            <CopyPrimitive.Status className=\"sr-only\">{said}</CopyPrimitive.Status>\n        </>\n    );\n}\n\n/** Carbon `CodeSnippetSkeleton`: a layer box, 56px with one bar (`single`) or 98px with three lines at 100 / 85 / 95% (`multi`). Hidden from assistive tech. */\nexport function CodeSnippetSkeleton({ type = 'single', className, ...rest }: Readonly<ComponentProps<'div'> & { type?: 'single' | 'multi' }>) {\n    const multi = type === 'multi';\n    return (\n        <div {...rest} aria-hidden=\"true\" className={cn('block max-w-[768px]', className)}>\n            <div className={cn('box-border bg-surface-1', multi ? 'flex h-[98px] flex-col gap-2 p-4' : 'h-14 ps-4 pe-10 pt-5')}>\n                <span className=\"skeleton block h-4\" />\n                {multi ? (\n                    <>\n                        <span className=\"skeleton block h-4 w-[85%]\" />\n                        <span className=\"skeleton block h-4 w-[95%]\" />\n                    </>\n                ) : null}\n            </div>\n        </div>\n    );\n}\n"
    }
  ]
}
