{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "cut",
  "title": "Cut",
  "description": "Text cut short shows whole: a label that truncates is marked `data-cut`, and the control around it shows the cut text in a Tooltip, by pointer and keyboard, only while it is cut. Select, Combobox, Breadcrumb, Table, Sidebar and the charts share it.",
  "dependencies": [
    "@ui-sdk/react",
    "@ui-sdk/core"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/scroll.json",
    "https://kinoui.com/r/swap.json",
    "https://kinoui.com/r/tooltip.json"
  ],
  "files": [
    {
      "path": "registry/lib/cut.tsx",
      "content": "'use client'\n\nimport {\n  Tooltip,\n  TooltipContent,\n  type TooltipContentProps,\n  TooltipTrigger,\n} from '@/registry/components/kino/tooltip'\nimport { overflows } from '@/registry/lib/scroll'\nimport { SwapTextTitleContext } from '@/registry/lib/swap'\nimport { mergeProps, type RenderProp } from '@ui-sdk/react/primitive'\nimport { cloneElement, type ReactElement, useEffect, useState } from 'react'\n\n/*\n * Text cut short shows whole, one way: a label that truncates is marked\n * `data-cut`, and the control it sits in shows the cut labels' text in a\n * Tooltip, by pointer and by keyboard focus, only while one is cut.\n */\n\n/** The labels whose text can be cut short, as a selector. */\nconst cutLabels = '[data-cut]'\n\n/** A label that says whether it is cut, such as a `SwapText`. */\nconst said = '[data-cut=cut], [data-cut=whole]'\n\n/**\n * Whether a label's text is cut short. A label that knows it, such as a\n * `SwapText`, says so with `data-cut='cut'` or `'whole'`; any other is cut\n * while its text is larger than its box.\n */\nfunction isCut(label: {\n  dataset: DOMStringMap\n  scrollWidth: number\n  clientWidth: number\n  scrollHeight: number\n  clientHeight: number\n}) {\n  const said = label.dataset.cut\n  if (said === 'cut') return true\n  if (said === 'whole') return false\n  return overflows(label)\n}\n\n/**\n * Whether a part inside a label is cut short across. An inline part, such as\n * a `<bdi>` around a name, has no box of its own: Firefox gives it its text's\n * width to scroll and none to show, which would read as cut.\n */\nfunction cutsAcross(part: { scrollWidth: number; clientWidth: number }, display: string) {\n  if (display === 'inline' || display === 'contents') return false\n  return part.scrollWidth - part.clientWidth > 1\n}\n\n/** The text of each label marked `data-cut` in or on `owner`, while any of them is cut; otherwise none. */\nfunction cutLines(owner: HTMLElement, selector = cutLabels): readonly string[] {\n  // A count beside a label, such as a Badge's, isn't part of what it names, and a copy\n  // drawn for the eye only, such as a Text shimmer's light, would say it twice.\n  const labels = [\n    ...(owner.matches(selector) ? [owner] : []),\n    ...owner.querySelectorAll<HTMLElement>(selector),\n  ].filter((label) => {\n    const hidden = label.closest('[aria-hidden=true]')\n    return !label.closest('[data-slot=badge]') && !(hidden && owner.contains(hidden))\n  })\n  // A label may hold what cuts across, such as a name that truncates beside\n  // an icon; one that says whether it is cut is taken at its word.\n  const cut = (label: HTMLElement) =>\n    isCut(label) ||\n    (!label.matches(said) &&\n      [...label.querySelectorAll<HTMLElement>('*')].some(\n        (part) => !part.closest(said) && cutsAcross(part, getComputedStyle(part).display),\n      ))\n  if (!labels.some(cut)) return []\n  return labels.map((label) => label.textContent?.trim() ?? '').filter(Boolean)\n}\n\n/**\n * The text of the labels cut short in `owner`, each on its own line, such as\n * a name over an email; none while they all fit. It follows each label's size\n * and its text, so a value changed in place is checked again.\n */\nfunction useCut(owner: HTMLElement | null, selector = cutLabels): readonly string[] {\n  const [lines, setLines] = useState<readonly string[]>([])\n  useEffect(() => {\n    if (!owner) return\n    const sizes = new ResizeObserver(() => measure())\n    const watch = () => {\n      sizes.disconnect()\n      sizes.observe(owner)\n      for (const label of owner.querySelectorAll(selector)) sizes.observe(label)\n    }\n    const measure = () => {\n      const next = cutLines(owner, selector)\n      setLines((current) => (sameLines(current, next) ? current : next))\n    }\n    const changes = new MutationObserver(() => {\n      watch()\n      measure()\n    })\n    changes.observe(owner, {\n      subtree: true,\n      childList: true,\n      characterData: true,\n      attributes: true,\n      attributeFilter: ['data-cut'],\n    })\n    watch()\n    measure()\n    return () => {\n      sizes.disconnect()\n      changes.disconnect()\n    }\n  }, [owner, selector])\n  return lines\n}\n\nfunction sameLines(a: readonly string[], b: readonly string[]) {\n  return a.length === b.length && a.every((line, index) => line === b[index])\n}\n\n/** The cut labels' text in a tooltip, each on its own line. */\nfunction CutLines({ lines }: { lines: readonly string[] }) {\n  return lines.map((line, index) => (\n    // oxlint-disable-next-line react/no-array-index-key -- lines repeat, so their place is their key\n    <span key={index} dir='auto' className='block wrap-anywhere'>\n      {line}\n    </span>\n  ))\n}\n\n/**\n * `render`, such as a Button or a Select's trigger, with a Tooltip that shows\n * whole the labels marked `data-cut` inside it, while any of them is cut\n * short. It opens on hover and on keyboard focus, so `render` should be what\n * takes focus. The trigger isn't described by the tooltip: its name already\n * holds the whole text.\n */\nfunction CutTooltip({\n  render,\n  labels = cutLabels,\n  text,\n  disabled = false,\n  side,\n  align,\n}: {\n  render: ReactElement\n  /** The labels that can be cut short, as a selector, where they can't be marked `data-cut`. */\n  labels?: string\n  /** What the tooltip shows, where it isn't the cut labels' text. */\n  text?: string\n  /** Keeps it closed while the labels can't be seen, such as in a sidebar's icon rail. */\n  disabled?: boolean\n} & Pick<TooltipContentProps, 'side' | 'align'>) {\n  const [owner, setOwner] = useState<HTMLElement | null>(null)\n  const cut = useCut(owner, labels)\n  const lines = cut.length > 0 && text !== undefined ? [text] : cut\n  // What it said last, which it keeps showing as it closes once the text fits.\n  const [said, setSaid] = useState(lines)\n  if (lines.length > 0 && !sameLines(said, lines)) setSaid(lines)\n  return (\n    <SwapTextTitleContext value={false}>\n      <Tooltip\n        disabled={disabled || lines.length === 0}\n        // Measured again as it opens, since a value just changed may not be measured yet.\n        onOpenChange={(open, details) => {\n          if (open && owner && cutLines(owner, labels).length === 0) details.cancel()\n        }}\n      >\n        <TooltipTrigger ref={setOwner} render={undescribed(render)} />\n        <TooltipContent side={side} align={align}>\n          <CutLines lines={lines.length > 0 ? lines : said} />\n        </TooltipContent>\n      </Tooltip>\n    </SwapTextTitleContext>\n  )\n}\n\n/**\n * `render` as a tooltip's trigger that the tooltip doesn't describe, for one\n * whose name already holds the whole text the tooltip shows: described too,\n * it would be read twice.\n */\nfunction undescribed(render: ReactElement): RenderProp {\n  return (props) => {\n    const own = { ...props }\n    delete own['aria-describedby']\n    return cloneElement(render, mergeProps(own, render.props as Record<string, unknown>))\n  }\n}\n\nexport { CutLines, CutTooltip, cutLines, cutsAcross, isCut, undescribed, useCut }\n",
      "type": "registry:lib",
      "target": "@lib/cut.tsx"
    }
  ],
  "type": "registry:lib"
}