{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "waffle-chart",
  "title": "Waffle chart",
  "description": "Shares of a whole as a grid of squares, a hundred by default, dealt out so they always fill the grid and any share takes at least one. A total larger than the values leaves the rest as remaining room. The share under the pointer, the keys or the legend stays bright while the rest step back, and new data turns each square to its new share in a wave along the fill.",
  "dependencies": [],
  "registryDependencies": [
    "https://kinoui.com/r/chart.json",
    "https://kinoui.com/r/chart-cells.json",
    "https://kinoui.com/r/chart-core.json",
    "https://kinoui.com/r/chart-root.json",
    "https://kinoui.com/r/chart-math.json",
    "https://kinoui.com/r/number-format.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/waffle-chart.tsx",
      "content": "'use client'\n\nimport {\n  type CategoryChartProps,\n  figureProps,\n  pickedName,\n  shareColor,\n  ShareTooltipContent,\n  shareValue,\n  useChartActive,\n  useChartLife,\n  useChartLoad,\n  useChartPick,\n  writeDatum,\n} from '@/registry/lib/chart-base'\nimport {\n  CellGridCell,\n  cellLayerVariables,\n  CellGridPlaceholder,\n  CellGridSizer,\n} from '@/registry/lib/chart-cells'\nimport { ListChartFrame } from '@/registry/lib/chart-list'\nimport { listReader, type ListStop } from '@/registry/lib/chart-list-reader'\nimport { type ChartDatum, gridCellAt } from '@/registry/lib/chart-math'\nimport {\n  type ChartPoint,\n  type ChartRootState,\n  forcedSeriesColor,\n  patternImage,\n  plotLayerClassName,\n  stepBackLayerClassName,\n  swatchClassName,\n  useChartRoot,\n  useChartRootValue,\n} from '@/registry/lib/chart-root'\nimport { indexOfKey } from '@/registry/lib/chart-state'\nimport { numberFormatter } from '@/registry/lib/number-format'\nimport { createContext, type CSSProperties, memo, use, useState } from 'react'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * The Waffle chart: shares of a whole as a grid of squares, a hundred by\n * default, so each square is a percent and a share can be counted. The\n * squares are dealt out so they always add up to the grid, and any share\n * above nothing gets at least one. A `total` larger than the values leaves\n * the squares past them as the remaining room, such as the free part of a\n * plan. Its shares are a list of stops on the list reader\n * (`chart-list-reader`), the remaining room last, and it is drawn on the\n * cells' squares (`chart-cells`): the share under\n * the pointer, the keys or the legend stays bright while the rest step back,\n * and new data turns each square to its new share in a wave from the first.\n */\n\ninterface WaffleChartProps extends Omit<\n  CategoryChartProps,\n  'domain' | 'tickCount' | 'tickValues' | 'allowDecimals' | 'ordered'\n> {\n  /** The key of each share's value in the data. The config's keys are the shares, under `index`. */\n  dataKey: string\n  /** What the values are, such as \"Visitors\": the data table's column. */\n  valueLabel?: string\n  /** The whole the values are shares of; the sum of the values by default. A larger total leaves the squares past them as the remaining room. */\n  total?: number\n  /** What the squares past the values are, with `total`, such as \"Free\": in the tooltip, the data table and for screen readers. */\n  remainingLabel?: string\n  /** How many rows of squares the grid has. */\n  rows?: number\n  /** How many squares each row has. */\n  columns?: number\n  /** How the shares fill the grid from its bottom start corner: along each row, as a rising bar does, or up each column, as a bar across does. */\n  fill?: 'rows' | 'columns'\n  /**\n   * How the squares arrive the first time the chart comes into view: turning\n   * to their shares in a wave from the first, or simply there, the default,\n   * since a page that loads doesn't animate into place.\n   */\n  entrance?: 'draw' | 'none'\n}\n\nconst waffleChartKeys = [\n  'dataKey',\n  'valueLabel',\n  'total',\n  'remainingLabel',\n  'rows',\n  'columns',\n  'fill',\n  'entrance',\n]\n\n/** The remaining room's key, for `selected`: a share can't take it. Its stop holds no row of the data. */\nconst WAFFLE_REMAINING = 'remaining'\n\n/** The most squares a side, so a grid stays one a pointer can find its squares in. */\nconst MOST_LINES = 40\n\n/** A square: where it sits, and the share it shows, by its place among the chart's categories, or none. */\ninterface WaffleSquare {\n  column: number\n  row: number\n  owner: number | null\n}\n\ninterface WaffleContextValue {\n  rows: number\n  columns: number\n  squares: readonly WaffleSquare[]\n  /** The shares still in the data, and the remaining room last, which holds no row. */\n  stops: readonly ListStop[]\n  /** The entrance has played, or wasn't asked for. */\n  entered: boolean\n}\n\nconst WaffleContext = createContext<WaffleContextValue | null>(null)\n\nfunction useWaffleChart() {\n  const waffle = use(WaffleContext)\n  if (!waffle) throw new Error('Waffle chart parts go inside a `WaffleChart`.')\n  return waffle\n}\n\n/** A count of squares a side: a whole number from 1 to `MOST_LINES`. */\nfunction lineCount(value: number | undefined, fallback: number) {\n  if (value === undefined || !Number.isFinite(value)) return fallback\n  return Math.min(MOST_LINES, Math.max(1, Math.round(value)))\n}\n\n/**\n * The chart's root: it deals the squares out to the shares, and lays out the\n * frame its legend and tooltip draw in, with square cells from the first\n * paint.\n */\nfunction WaffleChart(props: WaffleChartProps) {\n  const {\n    data,\n    index,\n    dataKey,\n    valueLabel,\n    total,\n    remainingLabel = 'Remaining',\n    fill = 'rows',\n    entrance = 'none',\n    messages,\n    description,\n    empty,\n    className,\n    ref,\n  } = props\n  const rows = lineCount(props.rows, 10)\n  const columns = lineCount(props.columns, 10)\n  const cells = rows * columns\n  const whole = total !== undefined && Number.isFinite(total) ? Math.max(0, total) : null\n  // Every value, the hidden ones too: they still take their part of a total.\n  const all = data.reduce((sum, row) => sum + shareValue(row, dataKey), 0)\n  const left = whole === null ? 0 : Math.max(0, whole - all)\n  const state = useChartRoot(\n    {\n      ...props,\n      messages: {\n        series: 'Shares',\n        empty: 'No data yet',\n        allHidden: 'Every share is hidden',\n        showAll: (count) => `Show all ${count} shares`,\n        ...messages,\n      },\n    },\n    { entrance: entrance !== 'none', heightFromData: true },\n  )\n  const load = useChartLoad(props)\n  const loaded = !load.placeholder && !load.failed\n  const shares = loaded ? data : NO_ROWS\n  const drawn = shares.filter((row) => !state.hidden.includes(String(row[index])))\n  // What a total leaves is a stop of its own after the shares, so the pointer, the keys, the\n  // tooltip and the table reach it as they reach a share; it holds no row.\n  const remaining = loaded && left > 0\n  const stops: ListStop[] = drawn.map((datum, at) => ({\n    key: String(datum[index]),\n    value: datum[index],\n    datum,\n    index: at,\n  }))\n  if (remaining) {\n    stops.push({\n      key: WAFFLE_REMAINING,\n      value: WAFFLE_REMAINING,\n      datum: null,\n      index: drawn.length,\n    })\n  }\n  const shown = state.series.filter((key) => !state.hidden.includes(key))\n  const { status, entered } = useChartLife(\n    props,\n    state,\n    load,\n    {\n      rows: shares.length + (remaining ? 1 : 0),\n      shown: shown.length,\n      drawn: stops.length,\n    },\n    { entrance: entrance !== 'none', cascade: null },\n  )\n  const keys = stops.map((stop) => stop.key)\n  const pick = useChartPick(props, keys)\n  const { messages: words, direction, plotSize } = state\n  // A share is named by its config's label, as its legend entry is.\n  const nameOf = (stop: ListStop) => {\n    if (stop.datum === null) return remainingLabel\n    if (props.categoryFormat) return props.categoryFormat(stop.value)\n    const named = props.config[String(stop.value)]?.label\n    return typeof named === 'string' ? named : String(stop.value)\n  }\n  const label = valueLabel ?? words.total\n  const values = stops.map((stop) =>\n    stop.datum === null ? left : shareValue(stop.datum, dataKey),\n  )\n  const shownSum = values.reduce((sum, value) => sum + value, 0)\n  // Against a total, a hidden share keeps its squares, as nobody's, before the remaining room;\n  // with none, the shares shown take every square.\n  const hidden = whole === null ? 0 : Math.max(0, all + left - shownSum)\n  const before = remaining ? stops.length - 1 : stops.length\n  const counts = waffleCounts(\n    [...values.slice(0, before), hidden, ...values.slice(before)],\n    cells,\n  )\n  const squares = waffleSquares(counts, columns, rows, fill, (part) =>\n    part < before ? part : part === before ? null : part - 1,\n  )\n  const counted = whole === null ? shownSum : Math.max(whole, all)\n  const percent = numberFormatter(state.locale, {\n    style: 'percent',\n    maximumFractionDigits: 1,\n  })\n  const shareOf = (at: number) => (counted === 0 ? 0 : (values[at] ?? 0) / counted)\n  // A share's own value as written, so a missing one reads as missing, as a Donut's does; the\n  // remaining room's is the total's room left.\n  const valueText = (stop: ListStop) =>\n    stop.datum === null\n      ? state.valueText(left)\n      : writeDatum(stop.datum[dataKey], state.valueText, words.noValue)\n\n  const reader = listReader({\n    current: stops,\n    walk: { kind: 'straight', orientation: 'vertical' },\n    series: { kind: 'none' },\n    direction,\n    hit: (point) => {\n      const box = waffleBox(point.width, point.height, columns, rows, direction)\n      if (box.width === 0 || box.height === 0) return null\n      const x = (point.x - box.left) / box.width\n      const y = (point.y - box.top) / box.height\n      if (x < 0 || x > 1 || y < 0 || y > 1) return null\n      const { column, row } = gridCellAt({ x, y }, columns, rows, direction)\n      return squares[waffleOrder(column, row, columns, rows, fill)]?.owner ?? null\n    },\n    // The tooltip sits over the middle of the share's top row of squares.\n    anchor: (stop) => {\n      const point = waffleAnchor(\n        squares,\n        stop.index,\n        waffleStart(counts, before, stop.index),\n        columns,\n        rows,\n        fill,\n        direction,\n      )\n      const box = waffleBox(plotSize.width, plotSize.height, columns, rows, direction)\n      if (box.width === 0 || box.height === 0) return point\n      return {\n        x: (box.left + point.x * box.width) / plotSize.width,\n        y: (box.top + point.y * box.height) / plotSize.height,\n        side: 'top',\n      }\n    },\n    words: {\n      describe: (stop) =>\n        `${pickedName(nameOf(stop), pick.picked === stop.key, words)}: ${label} ${valueText(stop)}, ${percent.format(shareOf(stop.index))}`,\n      tooltip: (stop) => (\n        <ShareTooltipContent\n          chart={state}\n          category={stop}\n          name={nameOf(stop)}\n          rows={[\n            {\n              label: null,\n              value: valueText(stop),\n              share: percent.format(shareOf(stop.index)),\n            },\n          ]}\n          swatch={\n            stop.datum === null ? (\n              <span aria-hidden className={cn(swatchClassName, remainingClassName)} />\n            ) : undefined\n          }\n        />\n      ),\n      table: {\n        header: words.category,\n        columns: [label, words.share],\n        rows: stops.map((stop) => ({\n          key: stop.key,\n          name: nameOf(stop),\n          cells: [valueText(stop), percent.format(shareOf(stop.index))],\n        })),\n        footer: [state.valueText(counted), percent.format(counted === 0 ? 0 : 1)],\n      },\n    },\n  })\n\n  const boxStyle = waffleBoxStyle(columns, rows)\n  return (\n    <WaffleContext value={{ rows, columns, squares, stops, entered }}>\n      <ListChartFrame\n        chart={{\n          state,\n          status,\n          drawn,\n          selectable: pick.selectable,\n          picked: pick.picked,\n          togglePicked: (key) => {\n            if (indexOfKey(keys, key) !== null) pick.toggle(key)\n          },\n          activeProps: {\n            activeIndex: props.activeIndex,\n            defaultActiveIndex: props.defaultActiveIndex ?? null,\n            onActiveIndexChange: props.onActiveIndexChange,\n          },\n        }}\n        reader={reader}\n        ref={ref}\n        slot='waffle-chart'\n        description={description}\n        empty={empty}\n        measuring={false}\n        className={cn('h-auto', className)}\n        placeholder={\n          load.placeholder && (\n            <CellGridPlaceholder\n              slot='waffle-chart'\n              shape='square'\n              columns={columns}\n              rows={rows}\n              className={waffleBoxClassName}\n              style={boxStyle}\n            />\n          )\n        }\n        {...figureProps(props, waffleChartKeys)}\n      >\n        <CellGridSizer shape='square' columns={columns} rows={rows} />\n        <WaffleSquares />\n        {props.children}\n      </ListChartFrame>\n    </WaffleContext>\n  )\n}\n\n/** The shares of a chart that loads or failed: one array, so what compares them sees no change. */\nconst NO_ROWS: readonly ChartDatum[] = []\n\n/** The remaining room's look: the page's muted fill, and a dashed outline in forced colours. */\nconst remainingClassName =\n  'bg-(--muted) forced-colors:bg-transparent forced-colors:outline-1 forced-colors:-outline-offset-1 forced-colors:outline-dashed forced-colors:outline-[GrayText]'\n\n/** The box the squares take: from the plot's start, as wide as square cells make it. */\nconst waffleBoxClassName = 'absolute inset-y-0 start-0 [container-type:size]'\n\nfunction waffleBoxStyle(columns: number, rows: number): CSSProperties {\n  return { width: `min(100cqw, 100cqh * ${columns / rows})` }\n}\n\n/** What a square shows: its share's colour, pattern and forced colour, or the remaining room's. */\ninterface SquareLook {\n  /** The square's colour, or none for a square nobody holds, drawn as a dashed outline. */\n  color: string | null\n  image?: string\n  forced?: string\n}\n\n/** A square nobody holds, such as a hidden share's against a total, or every square with nothing to share. */\nconst NOBODY: SquareLook = { color: null }\n\nfunction lookOf(\n  chart: Pick<ChartRootState, 'series' | 'patternOf'>,\n  stop: ListStop | undefined,\n): SquareLook {\n  if (!stop || stop.datum === null) {\n    return { color: 'var(--muted)', forced: remainingClassName }\n  }\n  const slot = chart.series.indexOf(stop.key)\n  return {\n    color: shareColor(chart, stop),\n    image: slot === -1 ? undefined : patternImage(chart.patternOf(stop.key)) || undefined,\n    forced: forcedSeriesColor(slot === -1 ? stop.index : slot),\n  }\n}\n\n/**\n * The squares: one layer of every square, in its share's colour, that steps\n * back as one while a share is lit, a single filter whatever the number of\n * squares; over it, each share's squares again, bright, faded in while it is\n * lit and out as another is, so moving from share to share crossfades.\n */\nfunction WaffleSquares() {\n  const chart = useChartRootValue()\n  const waffle = useWaffleChart()\n  const { stops } = waffle\n  const { spotlit } = useChartActive()\n  // Which share holds each square, by its key, now and before the data last changed, so a lit\n  // share's copies turn in the same wave as the squares below.\n  const owners = waffle.squares\n    .map((square) => (square.owner === null ? '' : (stops[square.owner]?.key ?? '')))\n    .join(OWNER_SEPARATOR)\n  const [held, setHeld] = useState({ now: owners, was: owners })\n  if (held.now !== owners) setHeld({ now: owners, was: held.now })\n  if (chart.status === 'empty') return null\n  const litNow = spotlit === null ? undefined : stops[spotlit]\n  const lit = litNow ? litNow.key : chart.emphasis\n  const looks = stops.map((stop) => lookOf(chart, stop))\n  const rest = lookOf(chart, undefined)\n  // Until the chart enters, every square waits in the remaining room's look, then turns.\n  const waiting = !waffle.entered\n  return (\n    <>\n      <SquareLayer\n        dim={lit !== null}\n        squares={waffle.squares}\n        looks={looks}\n        waiting={waiting}\n        rest={rest}\n        columns={waffle.columns}\n        rows={waffle.rows}\n      />\n      <div\n        data-slot='waffle-chart-lit'\n        aria-hidden\n        className={cn(plotLayerClassName, 'z-11')}\n        style={cellLayerVariables('waffle-chart')}\n      >\n        <div\n          className={waffleBoxClassName}\n          style={waffleBoxStyle(waffle.columns, waffle.rows)}\n        >\n          {stops.map((stop) => (\n            <LitShare\n              key={stop.key}\n              on={lit === stop.key}\n              share={stop.key}\n              look={looks[stop.index]!}\n              squares={waffle.squares}\n              owners={held.now}\n              was={held.was}\n              columns={waffle.columns}\n              rows={waffle.rows}\n            />\n          ))}\n        </div>\n      </div>\n    </>\n  )\n}\n\n/**\n * Every square, by its place in the fill, so a square that changes share\n * turns its colour where it is. Memoised, so lighting a share redraws none\n * of them.\n */\nconst SquareLayer = memo(function SquareLayer({\n  dim,\n  squares,\n  looks,\n  waiting,\n  rest,\n  columns,\n  rows,\n}: {\n  dim: boolean\n  /** Every square shows the remaining room's look until the chart enters. */\n  waiting: boolean\n  squares: readonly WaffleSquare[]\n  looks: readonly SquareLook[]\n  rest: SquareLook\n  columns: number\n  rows: number\n}) {\n  const last = Math.max(1, squares.length - 1)\n  return (\n    <div\n      data-slot='waffle-chart-squares'\n      data-dim={dim ? '' : undefined}\n      aria-hidden\n      className={cn(plotLayerClassName, 'z-10', stepBackLayerClassName)}\n      style={cellLayerVariables('waffle-chart')}\n    >\n      <div className={waffleBoxClassName} style={waffleBoxStyle(columns, rows)}>\n        {squares.map((square, order) => {\n          const look = waiting\n            ? rest\n            : square.owner === null\n              ? NOBODY\n              : (looks[square.owner] ?? NOBODY)\n          return (\n            <CellGridCell\n              // oxlint-disable-next-line react/no-array-index-key -- a square is its place in the fill\n              key={order}\n              slot='waffle-chart'\n              shape='square'\n              column={square.column}\n              row={square.row}\n              columns={columns}\n              rows={rows}\n              color={look.color}\n              strength={1}\n              leaving={false}\n              along={order / last}\n              image={look.image}\n              forced={look.forced}\n            />\n          )\n        })}\n      </div>\n    </div>\n  )\n})\n\n/** What separates the shares' keys in a list of the squares' owners. */\nconst OWNER_SEPARATOR = '\\u0001'\n\n/** A square of a lit share's copy that its share no longer holds: clear, in forced colours too. */\nconst CLEAR: SquareLook = {\n  color: 'transparent',\n  forced: 'forced-colors:bg-transparent',\n}\n\n/**\n * One share's squares, bright over the rest, shown while it is lit and\n * fading on `layout`. Each turns in the same wave as the square below when\n * new data comes: one it gains fades in, and one it loses clears, so a lit\n * share refills from where it is too.\n */\nconst LitShare = memo(function LitShare({\n  on,\n  share,\n  look,\n  squares,\n  owners,\n  was,\n  columns,\n  rows,\n}: {\n  on: boolean\n  share: string\n  look: SquareLook\n  squares: readonly WaffleSquare[]\n  /** Each square's share's key, now and before the data last changed, joined by `OWNER_SEPARATOR`. */\n  owners: string\n  was: string\n  columns: number\n  rows: number\n}) {\n  const now = owners.split(OWNER_SEPARATOR)\n  const before = was.split(OWNER_SEPARATOR)\n  const last = Math.max(1, squares.length - 1)\n  return (\n    <div\n      data-off={on ? undefined : ''}\n      className='absolute inset-0 transition-opacity duration-(--motion-layout-duration) ease-(--motion-layout-fade-easing) data-off:opacity-0'\n    >\n      {squares.map((square, order) => {\n        const holds = now[order] === share\n        if (!holds && before[order] !== share) return null\n        const shown = holds ? look : CLEAR\n        return (\n          <CellGridCell\n            // oxlint-disable-next-line react/no-array-index-key -- a square is its place in the fill\n            key={order}\n            slot='waffle-chart'\n            shape='square'\n            column={square.column}\n            row={square.row}\n            columns={columns}\n            rows={rows}\n            color={shown.color}\n            strength={1}\n            leaving={false}\n            along={order / last}\n            image={shown.image}\n            forced={shown.forced}\n          />\n        )\n      })}\n    </div>\n  )\n})\n\n/**\n * How many of `cells` squares each value takes: its share of their sum, by\n * largest remainder, so the counts always add up to `cells`. A value above\n * zero takes at least one, from the value whose count is furthest over its\n * share, while there is one to give; a value at or below zero takes none.\n */\nfunction waffleCounts(values: readonly number[], cells: number): number[] {\n  const positive = values.map((value) =>\n    Number.isFinite(value) ? Math.max(0, value) : 0,\n  )\n  const sum = positive.reduce((all, value) => all + value, 0)\n  if (sum === 0 || cells <= 0) return positive.map(() => 0)\n  const exact = positive.map((value) => (value / sum) * cells)\n  const counts = exact.map(Math.floor)\n  let left = cells - counts.reduce((all, count) => all + count, 0)\n  // The biggest remainders first; a tie goes to the bigger value, then to the earlier.\n  const byRemainder = exact\n    .map((share, at) => ({ at, remainder: share - Math.floor(share), share }))\n    .sort((a, b) => b.remainder - a.remainder || b.share - a.share || a.at - b.at)\n  for (const { at } of byRemainder) {\n    if (left === 0) break\n    counts[at]! += 1\n    left -= 1\n  }\n  // The smallest values left with none, biggest first, each take one where one can be spared.\n  const unseen = positive\n    .map((value, at) => ({ value, at }))\n    .filter(({ value, at }) => value > 0 && counts[at] === 0)\n    .sort((a, b) => b.value - a.value || a.at - b.at)\n  for (const { at } of unseen) {\n    let donor = -1\n    for (let other = 0; other < counts.length; other += 1) {\n      if (counts[other]! < 2) continue\n      const over = counts[other]! - exact[other]!\n      const best = donor === -1 ? -Infinity : counts[donor]! - exact[donor]!\n      if (over > best) donor = other\n    }\n    if (donor === -1) break\n    counts[donor]! -= 1\n    counts[at] = 1\n  }\n  return counts\n}\n\n/**\n * Where the square at place `order` in the fill sits, counted from the grid's\n * bottom start corner: along each row and then up, or up each column and\n * then along. Rows are counted from the top.\n */\nfunction waffleCellAt(\n  order: number,\n  columns: number,\n  rows: number,\n  fill: 'rows' | 'columns',\n): { column: number; row: number } {\n  if (fill === 'columns') {\n    return { column: Math.floor(order / rows), row: rows - 1 - (order % rows) }\n  }\n  return {\n    column: order % columns,\n    row: rows - 1 - Math.floor(order / columns),\n  }\n}\n\n/** The place in the fill of the square at `column` and `row`, the reverse of `waffleCellAt`. */\nfunction waffleOrder(\n  column: number,\n  row: number,\n  columns: number,\n  rows: number,\n  fill: 'rows' | 'columns',\n) {\n  const up = rows - 1 - row\n  return fill === 'columns' ? column * rows + up : up * columns + column\n}\n\n/**\n * Every square of a grid `columns` by `rows`, in the order they fill, each\n * given in turn to the owner of its part of `counts`: the part's own place by\n * default, or none.\n */\nfunction waffleSquares(\n  counts: readonly number[],\n  columns: number,\n  rows: number,\n  fill: 'rows' | 'columns',\n  ownerOf: (part: number) => number | null = (part) => part,\n): WaffleSquare[] {\n  const owners: (number | null)[] = []\n  counts.forEach((count, part) => {\n    for (let at = 0; at < count; at += 1) owners.push(ownerOf(part))\n  })\n  return Array.from({ length: columns * rows }, (_, order) => ({\n    ...waffleCellAt(order, columns, rows, fill),\n    owner: owners[order] ?? null,\n  }))\n}\n\n/**\n * The box the squares take in a plot `width` by `height` px: as large as\n * square cells allow, from the top and the start, under the legend's start.\n * Before the plot is measured, nothing.\n */\nfunction waffleBox(\n  width: number,\n  height: number,\n  columns: number,\n  rows: number,\n  direction: 'ltr' | 'rtl',\n) {\n  if (width <= 0 || height <= 0) return { left: 0, top: 0, width: 0, height: 0 }\n  const side = Math.min(width / columns, height / rows)\n  const boxWidth = side * columns\n  return {\n    left: direction === 'rtl' ? width - boxWidth : 0,\n    top: 0,\n    width: boxWidth,\n    height: side * rows,\n  }\n}\n\n/**\n * Where share `owner`'s squares start in the fill: the squares of the parts\n * before it in `counts`, where a hidden share's part sits at `gap`.\n */\nfunction waffleStart(counts: readonly number[], gap: number, owner: number) {\n  const part = owner < gap ? owner : owner + 1\n  return counts.slice(0, part).reduce((all, count) => all + count, 0)\n}\n\n/**\n * Where the tooltip for share `owner` sits, as fractions of the squares' box:\n * over the middle of the share's top row of squares, the one nearest the top.\n * A share with no squares points where the fill reaches it, at its place\n * `from`, or at the last square when the grid is full.\n */\nfunction waffleAnchor(\n  squares: readonly WaffleSquare[],\n  owner: number,\n  from: number,\n  columns: number,\n  rows: number,\n  fill: 'rows' | 'columns',\n  direction: 'ltr' | 'rtl',\n): ChartPoint {\n  const own = squares.filter((square) => square.owner === owner)\n  if (own.length === 0) {\n    const at = waffleCellAt(\n      Math.max(0, Math.min(from, columns * rows - 1)),\n      columns,\n      rows,\n      fill,\n    )\n    const middle = (at.column + 0.5) / columns\n    return {\n      x: direction === 'rtl' ? 1 - middle : middle,\n      y: at.row / rows,\n      side: 'top',\n    }\n  }\n  const top = Math.min(...own.map((square) => square.row))\n  const across = own.filter((square) => square.row === top).map((square) => square.column)\n  const middle = (Math.min(...across) + Math.max(...across) + 1) / 2 / columns\n  return {\n    x: direction === 'rtl' ? 1 - middle : middle,\n    y: top / rows,\n    side: 'top',\n  }\n}\n\nexport {\n  WAFFLE_REMAINING,\n  waffleStart,\n  WaffleChart,\n  type WaffleChartProps,\n  waffleAnchor,\n  waffleBox,\n  waffleCellAt,\n  waffleCounts,\n  waffleOrder,\n  waffleSquares,\n}\n",
      "type": "registry:component",
      "target": "@components/kino/waffle-chart.tsx"
    }
  ],
  "type": "registry:component"
}