{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-breadcrumb",
  "type": "registry:ui",
  "title": "react-tw/breadcrumb.tsx",
  "description": "Carbon `Breadcrumb` / `BreadcrumbItem` as React + Tailwind components, the look of 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": [],
  "registryDependencies": [
    "@kanok-ui/core-overflow",
    "@kanok-ui/tw-dom",
    "@kanok-ui/tw-i18n",
    "@kanok-ui/tw-menu",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/breadcrumb.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/breadcrumb.tsx",
      "content": "import { Children, createContext, isValidElement, useContext, useLayoutEffect, useMemo, useRef, useState, type ComponentProps, type ReactNode } from 'react';\nimport { visibleCount } from '../core/overflow.ts';\nimport { useTranslation } from './lib/i18n.ts';\nimport { naturalWidth, revealForMeasure } from './lib/dom.ts';\nimport { cn } from './lib/utils.ts';\nimport { MenuItem, OverflowMenu } from './menu.tsx';\n\n/**\n * Carbon `Breadcrumb` / `BreadcrumbItem` as React + Tailwind components, the look of\n * `<ui-breadcrumb>`: a `<nav aria-label=\"Breadcrumb\">` holding an ordered list of items, each a\n * link or — `current` — the page the user is on (plain text, `aria-current=\"page\"`), with a \"/\"\n * after every item; `noTrailingSlash` drops the last one, `size=\"sm\"` uses label type. Route a\n * client-side app through each item's `onClick`.\n */\nexport type BreadcrumbSize = 'sm' | 'md';\n\nconst BreadcrumbContext = createContext<{ size: BreadcrumbSize; noTrailingSlash: boolean }>({ size: 'md', noTrailingSlash: false });\n\n/** A crumb gone into the trail's overflow menu (`overflow`). */\nconst CrumbHiddenContext = createContext(false);\n\n/** Carbon's OverflowMenuHorizontal (16px), the \"…\" of a collapsed trail. */\nconst moreIcon = (\n    <svg viewBox=\"0 0 32 32\" aria-hidden=\"true\">\n        <circle cx=\"8\" cy=\"16\" r=\"2\" />\n        <circle cx=\"16\" cy=\"16\" r=\"2\" />\n        <circle cx=\"24\" cy=\"16\" r=\"2\" />\n    </svg>\n);\n\nexport interface BreadcrumbProps extends Omit<ComponentProps<'nav'>, 'label' | 'noTrailingSlash' | 'size' | 'lang' | 'className' | 'children'> {\n    /** Accessible name of the navigation landmark (default: the translation's \"Breadcrumb\"). */\n    label?: string;\n    noTrailingSlash?: boolean;\n    size?: BreadcrumbSize;\n    /**\n     * On one line, the crumbs that don't fit go — from the end, the first ones stay — into a \"…\" overflow menu just\n     * before the last crumb (the page, which always stays), as `<ui-breadcrumb overflow>` (Carbon for IBM Products'\n     * page header breadcrumbs); choosing one follows its link.\n     */\n    overflow?: boolean;\n    /** The overflow menu's name (default: the translation's \"More breadcrumbs\"). */\n    overflowLabel?: string;\n    lang?: string;\n    className?: string;\n    /** `<BreadcrumbItem>`s. */\n    children: ReactNode;\n}\n\nexport function Breadcrumb({ label, noTrailingSlash = false, size = 'md', overflow = false, overflowLabel, lang, className, children, ...rest }: Readonly<BreadcrumbProps>) {\n    const t = useTranslation(lang).common;\n    const context = useMemo(() => ({ size, noTrailingSlash }), [size, noTrailingSlash]);\n    const gap = size === 'sm' ? 'gap-x-1' : 'gap-x-2';\n    return (\n        <nav {...rest} aria-label={label || t.breadcrumb} className={cn('block font-sans', className)}>\n            <BreadcrumbContext.Provider value={context}>\n                {overflow ? (\n                    <OverflowTrail size={size} label={overflowLabel ?? t.moreBreadcrumbs} lang={lang}>\n                        {children}\n                    </OverflowTrail>\n                ) : (\n                    // the space after each item (Carbon margin-inline-end) as the list's gap\n                    <ol className={cn('m-0 inline-flex max-w-full list-none items-center p-0 [flex-wrap:var(--ui-breadcrumb-wrap,wrap)]', gap)}>{children}</ol>\n                )}\n            </BreadcrumbContext.Provider>\n        </nav>\n    );\n}\n\n/**\n * The trail of an `overflow` breadcrumb: which crumbs fit (core/overflow, as Carbon's createOverflowHandler), every one\n * measured at its natural width; the last one is kept — up to half the trail, as it shrinks with an ellipsis (the page\n * header's title crumb) — the \"…\" just before it.\n */\nfunction OverflowTrail({ size, label, lang, children }: Readonly<{ size: BreadcrumbSize; label: string; lang?: string; children: ReactNode }>) {\n    const list = useRef<HTMLOListElement>(null);\n    const items = Children.toArray(children).filter(isValidElement);\n    const lead = items.slice(0, -1);\n    const [visible, setVisible] = useState(lead.length);\n    const [names, setNames] = useState<string[]>([]);\n    useLayoutEffect(() => {\n        const el = list.current;\n        if (!el) return;\n        let frame = 0;\n        const measure = () => {\n            const more = el.querySelector<HTMLElement>(':scope > [data-more]');\n            const crumbs = [...el.children].filter((li): li is HTMLElement => li instanceof HTMLElement && li !== more);\n            const last = crumbs.pop();\n            if (!more || !last) return;\n            const gap = size === 'sm' ? 4 : 8;\n            const restore = revealForMeasure([more, ...crumbs]);\n            const sizes = crumbs.map((crumb) => naturalWidth(crumb) + gap);\n            const container = el.parentElement?.clientWidth ?? el.clientWidth;\n            const fixed = Math.min(naturalWidth(last), container / 2);\n            const offset = naturalWidth(more) + gap;\n            restore();\n            setVisible(visibleCount({ container, sizes, fixedSizes: [fixed], offset, gap, keepFirst: false }));\n            setNames(crumbs.map((crumb) => crumb.textContent?.trim() ?? ''));\n        };\n        const schedule = () => {\n            cancelAnimationFrame(frame);\n            frame = requestAnimationFrame(measure);\n        };\n        const observer = new ResizeObserver(schedule);\n        observer.observe(el.parentElement ?? el);\n        schedule();\n        return () => {\n            observer.disconnect();\n            cancelAnimationFrame(frame);\n        };\n    }, [items.length, size]);\n    const pick = (index: number) => list.current?.querySelectorAll<HTMLElement>(':scope > li:not([data-more])')[index]?.querySelector<HTMLElement>('a')?.click();\n    const small = size === 'sm';\n    return (\n        <ol ref={list} className={cn('m-0 inline-flex max-w-full list-none flex-nowrap items-center p-0', small ? 'gap-x-1' : 'gap-x-2')}>\n            {lead.map((item, index) => (\n                <CrumbHiddenContext.Provider key={item.key ?? index} value={index >= visible}>\n                    {item}\n                </CrumbHiddenContext.Provider>\n            ))}\n            <li\n                data-more=\"\"\n                data-hidden={visible >= lead.length || undefined}\n                className={cn(\n                    \"inline-flex flex-none items-center data-hidden:hidden after:text-ink after:content-['/'] forced-colors:after:text-[CanvasText]\",\n                    small ? 'type-caption after:ms-1' : 'type-body-sm after:ms-2',\n                )}\n            >\n                <OverflowMenu\n                    size=\"sm\"\n                    placement=\"bottom-start\"\n                    label={label}\n                    lang={lang}\n                    icon={moreIcon}\n                    // Carbon breadcrumb-item overflow menu: a 16 × 18 link-coloured \"…\", a 12px line under it on hover / focus\n                    triggerClassName={cn(\n                        'relative w-4 bg-transparent text-link shadow-none hover:bg-transparent hover:text-link-hover active:text-icon-primary aria-expanded:bg-transparent aria-expanded:shadow-none',\n                        small ? 'h-4' : 'h-[18px]',\n                        'focus-visible:outline-1 focus-visible:outline-offset-0',\n                        'after:absolute after:start-0 after:bottom-0.5 after:h-px after:w-3 after:bg-focus after:opacity-0 after:transition-opacity after:duration-(--ui-duration-fast) after:ease-carbon after:content-[\"\"] motion-reduce:after:transition-none',\n                        'hover:after:bg-link-hover hover:after:opacity-100 focus-visible:after:opacity-100 active:after:bg-ink',\n                    )}\n                >\n                    {lead.slice(visible).map((item, offset) => (\n                        <MenuItem key={item.key ?? visible + offset} label={names[visible + offset] ?? ''} onClick={() => pick(visible + offset)} />\n                    ))}\n                </OverflowMenu>\n            </li>\n            {items.at(-1)}\n        </ol>\n    );\n}\n\nexport interface BreadcrumbItemProps extends Omit<ComponentProps<'a'>, 'children'> {\n    /** The page the user is on: plain text with `aria-current=\"page\"`. */\n    current?: boolean;\n    children: ReactNode;\n}\n\nexport function BreadcrumbItem({ href, current = false, className, children, ...rest }: Readonly<BreadcrumbItemProps>) {\n    const { size, noTrailingSlash } = useContext(BreadcrumbContext);\n    const hidden = useContext(CrumbHiddenContext);\n    const small = size === 'sm';\n    return (\n        <li data-hidden={hidden || undefined} className={cn('group/crumb inline-flex items-center font-sans data-hidden:hidden', small ? 'type-caption' : 'type-body-sm', className)}>\n            {current || !href ? (\n                <span aria-current={current ? 'page' : undefined} className=\"whitespace-nowrap text-ink\">\n                    {children}\n                </span>\n            ) : (\n                // tabindex: as Carbon's cds-breadcrumb-link, so Safari focuses it on click and Tab reaches it\n                <a\n                    {...rest}\n                    href={href}\n                    tabIndex={0}\n                    className={cn(\n                        'whitespace-nowrap rounded-[var(--ui-link-focus-radius,0px)] text-link no-underline outline-[length:var(--ui-focus-width,2px)] outline-solid outline-offset-1 outline-transparent transition-[color] duration-(--ui-duration-fast) ease-carbon',\n                        'visited:text-link hover:text-link-hover hover:underline active:text-ink focus-visible:outline-focus',\n                        'forced-colors:focus-visible:outline-[Highlight]',\n                    )}\n                >\n                    {children}\n                </a>\n            )}\n            <span\n                aria-hidden=\"true\"\n                className={cn(\n                    \"after:text-ink after:content-['/'] forced-colors:after:text-[CanvasText]\",\n                    small ? 'after:ms-1' : 'after:ms-2',\n                    // Carbon keeps the last item's slash; a design can drop it (--ui-breadcrumb-trailing-slash: none)\n                    noTrailingSlash ? 'group-last/crumb:hidden' : 'group-last/crumb:[display:var(--ui-breadcrumb-trailing-slash,inline)]',\n                )}\n            />\n        </li>\n    );\n}\n\n/** Carbon `BreadcrumbSkeleton`: `items` bars of 100×16, each followed by the trail's \"/\" (not after the last with `noTrailingSlash`). Hidden from assistive tech. */\nexport function BreadcrumbSkeleton({ items = 3, noTrailingSlash = false, size = 'md', className, ...rest }: Readonly<ComponentProps<'div'> & { items?: number; noTrailingSlash?: boolean; size?: 'sm' | 'md' }>) {\n    const sm = size === 'sm';\n    return (\n        <div {...rest} aria-hidden=\"true\" className={cn('flex flex-wrap', sm ? 'gap-x-1 type-caption' : 'gap-x-2 type-body-sm', className)}>\n            {Array.from({ length: Math.max(1, items) }, (_, i) => (\n                <span key={i} className={cn(\"inline-flex items-center leading-[18px]! after:text-ink after:content-['/']\", sm ? 'after:ms-1' : 'after:ms-2', noTrailingSlash && 'last:after:content-none')}>\n                    <span className=\"skeleton block h-4 w-[100px]\" />\n                </span>\n            ))}\n        </div>\n    );\n}\n"
    }
  ]
}
