@zonsh/ui 0.2.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (180) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +19 -13
  3. package/dist/components/alert-dialog.js +1 -1
  4. package/dist/components/ascii-field.d.ts +1 -1
  5. package/dist/components/bottom-nav.d.ts +86 -0
  6. package/dist/components/bottom-nav.js +175 -0
  7. package/dist/components/breadcrumb.d.ts +9 -0
  8. package/dist/components/breadcrumb.js +6 -1
  9. package/dist/components/button-group.d.ts +1 -1
  10. package/dist/components/button-group.js +2 -2
  11. package/dist/components/calendar.d.ts +1 -1
  12. package/dist/components/calendar.js +18 -3
  13. package/dist/components/carousel.d.ts +22 -3
  14. package/dist/components/carousel.js +25 -7
  15. package/dist/components/chart.d.ts +9 -0
  16. package/dist/components/chart.js +40 -3
  17. package/dist/components/code-block.d.ts +20 -10
  18. package/dist/components/code-block.js +54 -12
  19. package/dist/components/collapsible.js +2 -2
  20. package/dist/components/color-picker.d.ts +30 -8
  21. package/dist/components/color-picker.js +48 -27
  22. package/dist/components/combobox.d.ts +21 -3
  23. package/dist/components/combobox.js +79 -7
  24. package/dist/components/command.d.ts +23 -3
  25. package/dist/components/command.js +61 -16
  26. package/dist/components/conversation.d.ts +10 -1
  27. package/dist/components/conversation.js +14 -4
  28. package/dist/components/copy-button.d.ts +15 -6
  29. package/dist/components/copy-button.js +20 -5
  30. package/dist/components/data-table.d.ts +66 -6
  31. package/dist/components/data-table.js +376 -23
  32. package/dist/components/date-picker.d.ts +73 -4
  33. package/dist/components/date-picker.js +138 -7
  34. package/dist/components/dialog.d.ts +10 -1
  35. package/dist/components/dialog.js +6 -3
  36. package/dist/components/drawer.d.ts +10 -1
  37. package/dist/components/drawer.js +6 -3
  38. package/dist/components/feature-list.js +2 -2
  39. package/dist/components/field.d.ts +27 -8
  40. package/dist/components/field.js +49 -4
  41. package/dist/components/file-drop.d.ts +26 -4
  42. package/dist/components/file-drop.js +125 -64
  43. package/dist/components/filter-bar.d.ts +50 -3
  44. package/dist/components/filter-bar.js +71 -33
  45. package/dist/components/form.d.ts +4 -2
  46. package/dist/components/form.js +24 -2
  47. package/dist/components/graph.d.ts +41 -0
  48. package/dist/components/graph.js +84 -45
  49. package/dist/components/illustration.js +1 -1
  50. package/dist/components/input-group.d.ts +16 -5
  51. package/dist/components/input-group.js +35 -15
  52. package/dist/components/input.d.ts +2 -2
  53. package/dist/components/input.js +3 -3
  54. package/dist/components/kbd.d.ts +17 -0
  55. package/dist/components/kbd.js +8 -1
  56. package/dist/components/lightbox.d.ts +100 -0
  57. package/dist/components/lightbox.js +439 -0
  58. package/dist/components/locale.d.ts +44 -0
  59. package/dist/components/locale.js +37 -0
  60. package/dist/components/logo.js +1 -1
  61. package/dist/components/menu.js +3 -1
  62. package/dist/components/message.d.ts +3 -3
  63. package/dist/components/message.js +10 -7
  64. package/dist/components/meter.js +3 -1
  65. package/dist/components/money-input.d.ts +80 -0
  66. package/dist/components/money-input.js +253 -0
  67. package/dist/components/number-field.d.ts +21 -2
  68. package/dist/components/number-field.js +27 -16
  69. package/dist/components/otp-field.d.ts +14 -1
  70. package/dist/components/otp-field.js +10 -2
  71. package/dist/components/pagination.d.ts +13 -7
  72. package/dist/components/pagination.js +18 -7
  73. package/dist/components/pdf-viewer.d.ts +67 -2
  74. package/dist/components/pdf-viewer.js +88 -21
  75. package/dist/components/phone-input.d.ts +79 -0
  76. package/dist/components/phone-input.js +282 -0
  77. package/dist/components/popover.js +1 -1
  78. package/dist/components/progress.js +3 -1
  79. package/dist/components/prompt-input.d.ts +34 -9
  80. package/dist/components/prompt-input.js +28 -16
  81. package/dist/components/rating.d.ts +73 -0
  82. package/dist/components/rating.js +124 -0
  83. package/dist/components/reasoning.d.ts +16 -1
  84. package/dist/components/reasoning.js +10 -7
  85. package/dist/components/resizable.d.ts +10 -1
  86. package/dist/components/resizable.js +4 -1
  87. package/dist/components/response.d.ts +14 -3
  88. package/dist/components/response.js +36 -8
  89. package/dist/components/scroll-area.d.ts +1 -1
  90. package/dist/components/select.js +14 -3
  91. package/dist/components/separator.js +1 -1
  92. package/dist/components/sidebar.d.ts +20 -4
  93. package/dist/components/sidebar.js +34 -18
  94. package/dist/components/site-header.d.ts +46 -12
  95. package/dist/components/site-header.js +73 -19
  96. package/dist/components/slider.js +3 -1
  97. package/dist/components/sortable.d.ts +178 -0
  98. package/dist/components/sortable.js +1258 -0
  99. package/dist/components/sources.d.ts +17 -2
  100. package/dist/components/sources.js +11 -4
  101. package/dist/components/spinner.d.ts +13 -1
  102. package/dist/components/spinner.js +5 -2
  103. package/dist/components/stat.d.ts +91 -0
  104. package/dist/components/stat.js +141 -0
  105. package/dist/components/stepper.d.ts +71 -0
  106. package/dist/components/stepper.js +101 -0
  107. package/dist/components/switch.d.ts +2 -2
  108. package/dist/components/switch.js +2 -2
  109. package/dist/components/table.d.ts +12 -2
  110. package/dist/components/table.js +176 -39
  111. package/dist/components/tags-input.d.ts +18 -2
  112. package/dist/components/tags-input.js +65 -33
  113. package/dist/components/theme.d.ts +16 -2
  114. package/dist/components/theme.js +15 -7
  115. package/dist/components/time-field.d.ts +22 -3
  116. package/dist/components/time-field.js +19 -6
  117. package/dist/components/timeline.d.ts +18 -2
  118. package/dist/components/timeline.js +13 -4
  119. package/dist/components/tippy.d.ts +48 -0
  120. package/dist/components/tippy.js +648 -0
  121. package/dist/components/toast.d.ts +18 -2
  122. package/dist/components/toast.js +12 -3
  123. package/dist/components/tool-call.d.ts +19 -8
  124. package/dist/components/tool-call.js +18 -7
  125. package/dist/components/toolbar.js +6 -1
  126. package/dist/components/tour.d.ts +127 -0
  127. package/dist/components/tour.js +648 -0
  128. package/dist/components/tree.js +17 -4
  129. package/dist/components/video-player.d.ts +10 -2
  130. package/dist/components/video-player.js +73 -33
  131. package/dist/illustration.d.ts +2 -0
  132. package/dist/illustration.js +7 -0
  133. package/dist/index.d.ts +11 -3
  134. package/dist/index.js +11 -3
  135. package/dist/lib/code-highlight.d.ts +3 -16
  136. package/dist/lib/code-highlight.js +160 -10
  137. package/dist/lib/code-theme.js +3 -3
  138. package/dist/lib/digit-roll.js +8 -8
  139. package/dist/lib/field-errors.d.ts +28 -0
  140. package/dist/lib/field-errors.js +24 -0
  141. package/dist/lib/field-value.d.ts +13 -0
  142. package/dist/lib/field-value.js +44 -0
  143. package/dist/lib/graph-layout.js +11 -2
  144. package/dist/lib/money.d.ts +73 -0
  145. package/dist/lib/money.js +343 -0
  146. package/dist/lib/phone-countries.d.ts +1 -0
  147. package/dist/lib/phone-countries.js +281 -0
  148. package/dist/lib/phone.d.ts +90 -0
  149. package/dist/lib/phone.js +286 -0
  150. package/dist/lib/platform.d.ts +8 -3
  151. package/dist/lib/platform.js +13 -6
  152. package/dist/lib/row-index.d.ts +15 -0
  153. package/dist/lib/row-index.js +136 -0
  154. package/dist/lib/table-fit.js +23 -4
  155. package/dist/lib/theme-script.d.ts +15 -2
  156. package/dist/lib/theme-script.js +23 -3
  157. package/dist/lib/tippy/pixels.d.ts +47 -0
  158. package/dist/lib/tippy/pixels.js +187 -0
  159. package/dist/lib/tippy/sprite.d.ts +86 -0
  160. package/dist/lib/tippy/sprite.js +753 -0
  161. package/dist/messages/de.d.ts +12 -0
  162. package/dist/messages/de.js +326 -0
  163. package/dist/styles/expressive.css +20 -5
  164. package/dist/styles/index.css +3 -2
  165. package/dist/styles/material.css +7 -3
  166. package/dist/styles/parts/ai.css +2 -1
  167. package/dist/styles/parts/controls.css +62 -13
  168. package/dist/styles/parts/feedback.css +68 -1
  169. package/dist/styles/parts/foundations.css +1 -1
  170. package/dist/styles/parts/navigation.css +194 -2
  171. package/dist/styles/parts/overlays.css +7 -7
  172. package/dist/styles/tokens.css +12 -3
  173. package/dist/vite.d.ts +0 -50
  174. package/dist/vite.js +0 -100
  175. package/package.json +28 -33
  176. package/dist/components/code-highlight.d.ts +0 -13
  177. package/dist/components/code-highlight.js +0 -107
  178. package/dist/components/whiteboard.d.ts +0 -80
  179. package/dist/components/whiteboard.js +0 -389
  180. package/dist/styles/parts/whiteboard.css +0 -855
@@ -68,8 +68,21 @@ function StaticTable({ dense = false, variant = "card", containerProps, classNam
68
68
  const scrolls = useScrolls(containerRef);
69
69
  return (_jsx(TableDenseContext.Provider, { value: dense, children: _jsx("div", { "data-slot": "table-container", "data-variant": variant, tabIndex: scrolls ? 0 : undefined, role: scrolls ? "region" : undefined, "aria-label": scrolls ? props["aria-label"] : undefined, "aria-labelledby": scrolls ? props["aria-labelledby"] : undefined, ...containerProps, ref: containerRefs, className: cn("relative w-full overflow-x-auto focus-ring", variant === "card" && "surface-card [--card-padding:0] [--scrollbar-inset:var(--radius-card)]", containerProps?.className), children: _jsx("table", { "data-slot": "table", "data-dense": dense ? "" : undefined, "data-variant": variant, className: cn("w-full caption-bottom border-collapse text-start font-body text-sm", className), ...props }) }) }));
70
70
  }
71
- /** Card padding, carried by the rows in a fitting table: 16px, 24px from a 40rem container. */
72
- const FIT_PAD = "[--table-pad:--spacing(4)] @min-[40rem]:[--table-pad:--spacing(6)]";
71
+ /**
72
+ * Card padding, carried by the rows in a fitting table: 16px, 24px from a 40rem container; and the
73
+ * gap between columns, 12–28px with the container's width. The fit sets both as it measures the
74
+ * width (`fitSpacing`). Not a container query and `cqi`: WebKit restyles every cell of a size
75
+ * container on each resize, which made a fitting table drag as Safari resized it.
76
+ */
77
+ const FIT_SPACING = "[--table-pad:--spacing(4)] in-data-wide:[--table-pad:--spacing(6)] gap-x-(--fit-gap,0.75rem)";
78
+ /** The container's padding (`data-wide`) and column gap (`--fit-gap`) at its current width. */
79
+ function fitSpacing(container, body, rem) {
80
+ const width = container.clientWidth;
81
+ container.toggleAttribute("data-wide", width >= 40 * rem);
82
+ const gap = Math.min(Math.max(0.046 * width - 0.45 * rem, 0.75 * rem), 1.75 * rem);
83
+ body.style.setProperty("--fit-gap", `${gap}px`);
84
+ return gap;
85
+ }
73
86
  /**
74
87
  * A table that fits its columns to its container. It is one table at every width — `role="table"`
75
88
  * on a CSS grid whose groups and rows are subgrids — so a column is as wide as its widest cell
@@ -140,9 +153,9 @@ function FitTable({ dense = false, variant = "card", containerProps, className,
140
153
  if (!metrics)
141
154
  return;
142
155
  const { step, order, columns, drawn, title } = latest.current;
156
+ const gap = fitSpacing(container, body, rem());
143
157
  const row = body.querySelector("[data-slot=table-row]");
144
158
  const edge = row ? parseFloat(getComputedStyle(row).paddingInlineStart) : 16;
145
- const gap = parseFloat(getComputedStyle(body).columnGap) || 0;
146
159
  const available = body.clientWidth;
147
160
  const next = fitStep({
148
161
  available,
@@ -273,7 +286,7 @@ function FitTable({ dense = false, variant = "card", containerProps, className,
273
286
  const containerRefs = React.useMemo(() => mergeRefs(containerRef, containerProps?.ref), [containerProps?.ref]);
274
287
  const bodyRefs = React.useMemo(() => mergeRefs(bodyRef, ref), [ref]);
275
288
  const scrolls = useScrolls(bodyRef);
276
- return (_jsx(TableFitContext.Provider, { value: value, children: _jsx(TableDenseContext.Provider, { value: dense, children: _jsxs("div", { "data-slot": "table-container", "data-variant": variant, "data-fit": "", "data-tier": order.length - layout.step, "data-final": layout.final ? "" : undefined, ...containerProps, ref: containerRefs, className: cn("@container relative flex w-full max-w-full min-w-0 flex-col overflow-hidden", variant === "card" && "surface-card [--card-padding:0] [--scrollbar-inset:var(--radius-card)]", containerProps?.className), children: [_jsx("div", { tabIndex: scrolls ? 0 : undefined, ...props, ref: bodyRefs, role: "table", "data-slot": "table", "data-fit": "", "data-dense": dense ? "" : undefined, "data-variant": variant, className: cn("relative grid min-h-0 flex-1 content-start gap-x-[clamp(0.75rem,calc(4.6cqi_-_0.45rem),1.75rem)] overflow-auto text-start font-body text-sm focus-visible:outline-none", FIT_PAD, className), style: { gridTemplateColumns: template, ...style }, children: children }), _jsx("div", { ref: layerRef, "aria-hidden": true, "data-slot": "table-ghosts", className: "pointer-events-none absolute inset-0 z-3 overflow-hidden font-body text-sm" })] }) }) }));
289
+ return (_jsx(TableFitContext.Provider, { value: value, children: _jsx(TableDenseContext.Provider, { value: dense, children: _jsxs("div", { "data-slot": "table-container", "data-variant": variant, "data-fit": "", "data-tier": order.length - layout.step, "data-final": layout.final ? "" : undefined, ...containerProps, ref: containerRefs, className: cn("relative flex w-full max-w-full min-w-0 flex-col overflow-hidden", variant === "card" && "surface-card [--card-padding:0] [--scrollbar-inset:var(--radius-card)]", containerProps?.className), children: [_jsx("div", { tabIndex: scrolls ? 0 : undefined, ...props, ref: bodyRefs, role: "table", "data-slot": "table", "data-fit": "", "data-dense": dense ? "" : undefined, "data-variant": variant, className: cn("relative grid min-h-0 flex-1 content-start overflow-auto text-start font-body text-sm focus-visible:outline-none", FIT_SPACING, className), style: { gridTemplateColumns: template, ...style }, children: children }), _jsx("div", { ref: layerRef, "aria-hidden": true, "data-slot": "table-ghosts", className: "pointer-events-none absolute inset-0 z-3 overflow-hidden font-body text-sm" })] }) }) }));
277
290
  }
278
291
  /* ── Sections ──────────────────────────────────────────────────────────────── */
279
292
  export function TableHeader({ className, ...props }) {
@@ -349,7 +362,9 @@ function FitCell({ place, fit, kind, className, style, children, colSpan, scope:
349
362
  return (_jsx("div", { role: role, className: cn("min-w-0", (colSpan ?? 1) > 1 && "col-span-full", className), style: style, ...props, children: children }));
350
363
  if (index === fit.title) {
351
364
  const inline = fit.inlined && !head;
352
- return (_jsx("div", { role: role, "data-title": "", className: cn(inline ? "inline" : cn("flex min-w-0", head ? "items-center" : "min-h-7.5 items-start py-1"), className), style: inline ? style : { gridColumn: fit.track.get(index), ...style }, ...props, children: _jsx("span", { "data-slot": "table-title-text", className: fit.final && !head ? "min-w-0 text-pretty break-words" : "whitespace-nowrap", children: children }) }));
365
+ return (_jsx("div", { role: role, "data-title": "", className: cn(inline
366
+ ? "inline"
367
+ : cn("flex min-w-0", head ? "items-center" : "min-h-(--control-h-sm) items-start py-1"), className), style: inline ? style : { gridColumn: fit.track.get(index), ...style }, ...props, children: _jsx("span", { "data-slot": "table-title-text", className: fit.final && !head ? "min-w-0 text-pretty break-words" : "whitespace-nowrap", children: children }) }));
353
368
  }
354
369
  const end = fit.align[index] === "end";
355
370
  const column = fit.modes[index] ?? "column";
@@ -358,18 +373,24 @@ function FitCell({ place, fit, kind, className, style, children, colSpan, scope:
358
373
  return (_jsx("div", { role: role, "data-column": index, "data-head": head ? "" : undefined, "data-mode": mode, "data-align": end ? "end" : "start", "data-flip": "piece", "data-flip-key": `${rowKey}:${index}`, className: cn("min-w-0 items-center whitespace-nowrap", mode === "column" && "flex",
359
374
  // Cells are 30px, the height of a dense control, so a button sits in a 46px row; text
360
375
  // centres on the title's first line.
361
- mode === "column" && !head && "min-h-7.5", mode === "column" && (end ? "justify-self-end text-end" : "justify-self-start"),
376
+ mode === "column" && !head && "min-h-(--control-h-sm)", mode === "column" && (end ? "justify-self-end text-end" : "justify-self-start"),
362
377
  // Inline cells flow right after the title's last word, and wrap like words.
363
378
  mode === "inline" && "inline-flex min-h-5.5 align-top",
364
379
  // In the final layout, where titles wrap, one longer than the line (a link) wraps inside
365
380
  // the title cell too, instead of running out of the table.
366
381
  mode === "inline" && fit.final && "max-w-full wrap-anywhere whitespace-normal", mode === "hidden" && "sr-only", className), style: mode === "column" ? { gridColumn: fit.track.get(index), ...style } : style, ...props, children: children }));
367
382
  }
368
- /** A column header, set as an eyebrow: 12px mono, uppercase, 0.18em tracking, muted. */
383
+ /**
384
+ * A header cell. In the `TableHeader` it is a column header, set as an eyebrow: 12px mono,
385
+ * uppercase, 0.18em tracking, muted. In a body or footer row it is that row's header — a
386
+ * `<th scope="row">` set like a cell in the foreground ink, not as an eyebrow: use it for the
387
+ * cell that names the row (a feature in a comparison, a month in a report).
388
+ */
369
389
  export function TableHead({ numeric = false, align, width, priority, inline, minWidth, titleColumn, className, style, ...props }) {
370
390
  const dense = React.useContext(TableDenseContext);
371
391
  const fit = React.useContext(TableFitContext);
372
392
  const slot = React.useContext(TableCellSlotContext);
393
+ const row = React.useContext(TableSectionContext) === "body";
373
394
  const end = (align ?? (numeric ? "end" : "start")) === "end";
374
395
  const register = fit?.register;
375
396
  const index = slot?.head ? slot.index : -1;
@@ -380,6 +401,8 @@ export function TableHead({ numeric = false, align, width, priority, inline, min
380
401
  }, [register, index, priority, inline, minWidth, width, titleColumn, end]);
381
402
  if (fit && slot)
382
403
  return (_jsx(FitCell, { place: slot, fit: fit, kind: "head", "data-slot": "table-head", className: cn(slot.head ? "eyebrow font-normal" : "text-start font-normal text-foreground", className), style: style, ...props }));
404
+ if (row)
405
+ return (_jsx("th", { "data-slot": "table-head", scope: "row", className: cn("px-4 text-start align-middle font-normal text-foreground first:ps-6 last:pe-6", dense ? "h-(--control-h-sm) py-1 text-[13px]" : "h-(--control-h-lg) py-2", end && "text-end", numeric && "font-mono text-[13px] whitespace-nowrap tabular-nums", className), style: width ? { width, ...style } : style, ...props }));
383
406
  return (_jsx("th", { "data-slot": "table-head", scope: "col", className: cn("px-4 text-start align-middle eyebrow font-normal whitespace-nowrap first:ps-6 last:pe-6", dense ? "h-(--control-h-sm)" : "h-(--control-h)", end && "text-end", className), style: width ? { width, ...style } : style, ...props }));
384
407
  }
385
408
  export function TableCell({ numeric = false, className, ...props }) {
@@ -454,6 +477,10 @@ function useVirtualRow(ref) {
454
477
  const FIRST_ROWS = 40;
455
478
  /** The height of the view assumed until the scroller is measured. */
456
479
  const FIRST_VIEW = 1000;
480
+ /** The most a virtual body is drawn tall, under every browser's limit: more rows share it. */
481
+ const MAX_HEIGHT = 15_000_000;
482
+ /** A compressed body keeps the scroll position as rows are measured: a pixel there isn't one here. */
483
+ const noAdjust = () => false;
457
484
  const isWindow = (value) => typeof window !== "undefined" && value === window;
458
485
  /**
459
486
  * The nearest ancestor that scrolls vertically — the table's own box when it has a height, a
@@ -492,7 +519,7 @@ const VirtualRow = React.memo(function VirtualRow({ index, rowIndex, measure, ro
492
519
  * Use it from a few hundred rows on. The rule people break: reaching for it for a page of rows —
493
520
  * rows that are not rendered can't be found with the browser's search or printed.
494
521
  */
495
- export function TableVirtualBody({ rows, groups, renderRow, getRowKey, sizeRows, overscan = 8, scrollResetKey, }) {
522
+ export function TableVirtualBody({ rows, groups, renderRow, getRowKey, sizeRows, overscan = 8, scrollResetKey, onRangeChange, }) {
496
523
  const fit = React.useContext(TableFitContext);
497
524
  const dense = React.useContext(TableDenseContext);
498
525
  /** The sizer rows' group, of no height: the rows start at its top. */
@@ -515,12 +542,24 @@ export function TableVirtualBody({ rows, groups, renderRow, getRowKey, sizeRows,
515
542
  group.rows.forEach((row, at) => items.push({ header: false, key: `r:${String(group.key)}:${keyOf(row, at)}`, group: g, row, at }));
516
543
  spans.push({ start, end: items.length, open });
517
544
  });
518
- else
519
- rows?.forEach((row, at) => items.push({ header: false, key: `r:${keyOf(row, at)}`, group: -1, row, at }));
520
545
  return { items, spans };
521
- }, [rows, groups, toggled]);
546
+ }, [groups, toggled]);
547
+ // Without groups, each row is an entry of its own, made when it is asked for: a million rows
548
+ // need no list of a million entries, and rows filled in where they were (a server's, as they
549
+ // load) are read as they are now.
550
+ const count = groups ? items.length : (rows?.length ?? 0);
551
+ const itemAt = React.useCallback((i) => {
552
+ if (groups)
553
+ return items[i];
554
+ if (!rows || i < 0 || i >= rows.length)
555
+ return undefined;
556
+ const row = rows[i];
557
+ return { header: false, key: `r:${String(keyRef.current?.(row, i) ?? i)}`, group: -1, row, at: i };
558
+ }, [groups, items, rows]);
522
559
  // The rows that size the columns, with their index (renderRow's second argument).
523
560
  const sized = React.useMemo(() => {
561
+ if (!groups && rows && !sizeRows)
562
+ return widestRows(rows, fieldsOf).map((at) => ({ row: rows[at], at }));
524
563
  const all = [];
525
564
  if (groups)
526
565
  for (const group of groups)
@@ -532,25 +571,37 @@ export function TableVirtualBody({ rows, groups, renderRow, getRowKey, sizeRows,
532
571
  const index = new Map(all.map((entry) => [entry.row, entry.at]));
533
572
  return sizeRows.map((row) => ({ row, at: index.get(row) ?? -1 }));
534
573
  }, [rows, groups, sizeRows]);
535
- // Rows are 46px (30px dense) and group headers 38px until they are drawn and measured.
536
- const estimate = React.useCallback((i) => (items[i]?.header ? (dense ? 30 : 38) : dense ? 30 : 46), [items, dense]);
537
- const getItemKey = React.useCallback((i) => items[i]?.key ?? i, [items]);
574
+ // Rows are 46px (30px dense) and group headers 38px until they are drawn; from then on, as tall
575
+ // as most of the drawn ones (`drawn`, below). An estimate off by a pixel makes every row drawn
576
+ // move every row after it, which with a million rows is most of each frame.
577
+ const [drawnSize, setDrawnSize] = React.useState(null);
578
+ const estimate = React.useCallback((i) => groups && items[i]?.header
579
+ ? (drawnSize?.header ?? (dense ? 30 : 38))
580
+ : (drawnSize?.row ?? (dense ? 30 : 46)), [items, dense, drawnSize]);
581
+ const getItemKey = React.useCallback((i) => itemAt(i)?.key ?? i, [itemAt]);
538
582
  // The last row to take the focus stays rendered wherever it scrolls, with the rows on either side,
539
583
  // so Tab and Shift+Tab carry on from it to the next row, and a menu it opened can give the focus
540
584
  // back to it.
541
585
  const [focusKey, setFocusKey] = React.useState(null);
542
- const focusIndex = React.useMemo(() => (focusKey == null ? -1 : items.findIndex((entry) => entry.key === focusKey)), [focusKey, items]);
586
+ const focusIndex = React.useMemo(() => {
587
+ if (focusKey == null)
588
+ return -1;
589
+ for (let i = 0; i < count; i++)
590
+ if (itemAt(i).key === focusKey)
591
+ return i;
592
+ return -1;
593
+ }, [focusKey, itemAt, count]);
543
594
  const rangeExtractor = React.useCallback((range) => {
544
595
  const shown = new Set(defaultRangeExtractor(range));
545
596
  // The group of the first row in view keeps its header, so a sticky one stays at the top.
546
- const first = items[range.startIndex];
597
+ const first = groups ? items[range.startIndex] : undefined;
547
598
  if (first && first.group >= 0)
548
599
  shown.add(spans[first.group].start);
549
600
  if (focusIndex >= 0)
550
601
  for (let i = Math.max(0, focusIndex - 1); i <= Math.min(range.count - 1, focusIndex + 1); i++)
551
602
  shown.add(i);
552
603
  return [...shown].sort((a, b) => a - b);
553
- }, [items, spans, focusIndex]);
604
+ }, [groups, items, spans, focusIndex]);
554
605
  // What scrolls the rows — the table's own box, an ancestor or the window — and where in it they
555
606
  // start. Both are known only once the table is in the page: before that (on the server, and in
556
607
  // the first render here) the first rows are drawn, the same on both.
@@ -558,8 +609,15 @@ export function TableVirtualBody({ rows, groups, renderRow, getRowKey, sizeRows,
558
609
  const scrollerRef = React.useRef(null);
559
610
  const [margin, setMargin] = React.useState(0);
560
611
  const live = scroller != null;
612
+ // Set at each render, read as the scroller moves (see `compress`).
613
+ const realOffsetRef = React.useRef(0);
614
+ const compressRef = React.useRef(1);
615
+ const marginRef = React.useRef(0);
616
+ const toRows = (offset) => offset <= marginRef.current
617
+ ? offset
618
+ : marginRef.current + (offset - marginRef.current) * compressRef.current;
561
619
  const virtualizer = useVirtualizer({
562
- count: items.length,
620
+ count,
563
621
  enabled: live,
564
622
  getScrollElement: () => scroller,
565
623
  estimateSize: estimate,
@@ -568,14 +626,30 @@ export function TableVirtualBody({ rows, groups, renderRow, getRowKey, sizeRows,
568
626
  scrollMargin: margin,
569
627
  rangeExtractor,
570
628
  initialRect: { width: 0, height: FIRST_VIEW },
571
- initialOffset: () => (!scroller ? 0 : isWindow(scroller) ? scroller.scrollY : scroller.scrollTop),
629
+ initialOffset: () => toRows(!scroller ? 0 : isWindow(scroller) ? scroller.scrollY : scroller.scrollTop),
572
630
  observeElementRect: (instance, cb) => isWindow(instance.scrollElement)
573
631
  ? observeWindowRect(instance, cb)
574
632
  : observeElementRect(instance, cb),
575
- observeElementOffset: (instance, cb) => isWindow(instance.scrollElement)
576
- ? observeWindowOffset(instance, cb)
577
- : observeElementOffset(instance, cb),
633
+ // The scroller's offset, in the rows' own space: past where they start, `compress` times as far.
634
+ observeElementOffset: (instance, cb) => {
635
+ const report = (offset, scrolling) => {
636
+ realOffsetRef.current = offset;
637
+ cb(toRows(offset), scrolling);
638
+ };
639
+ return isWindow(instance.scrollElement)
640
+ ? observeWindowOffset(instance, report)
641
+ : observeElementOffset(instance, report);
642
+ },
578
643
  });
644
+ // Rows taller in all than a browser can draw (about 33M px in Chrome and Safari, 17M in Firefox)
645
+ // are drawn in at most MAX_HEIGHT: the space between them shrinks, and the rows in view sit
646
+ // where they would, at their own height.
647
+ const viewHeight = virtualizer.scrollRect?.height ?? FIRST_VIEW;
648
+ const virtualTotal = virtualizer.getTotalSize();
649
+ const compress = live && virtualTotal > MAX_HEIGHT ? (virtualTotal - viewHeight) / (MAX_HEIGHT - viewHeight) : 1;
650
+ compressRef.current = compress;
651
+ marginRef.current = margin;
652
+ virtualizer.shouldAdjustScrollPositionOnItemSizeChange = compress > 1 ? noAdjust : undefined;
579
653
  /** Looks for the scroller again: when the rows change, or the table or the window resize. */
580
654
  const findScroller = React.useCallback(() => {
581
655
  const anchor = anchorRef.current;
@@ -600,7 +674,7 @@ export function TableVirtualBody({ rows, groups, renderRow, getRowKey, sizeRows,
600
674
  React.useLayoutEffect(() => {
601
675
  findScroller();
602
676
  measureMargin();
603
- }, [findScroller, measureMargin, items.length]);
677
+ }, [findScroller, measureMargin, count]);
604
678
  React.useEffect(() => {
605
679
  const anchor = anchorRef.current;
606
680
  if (!anchor)
@@ -636,14 +710,14 @@ export function TableVirtualBody({ rows, groups, renderRow, getRowKey, sizeRows,
636
710
  ];
637
711
  setHeadRows(heads.length);
638
712
  heads.forEach((row, i) => row.setAttribute("aria-rowindex", String(i + 1)));
639
- table.setAttribute("aria-rowcount", String(heads.length + items.length));
713
+ table.setAttribute("aria-rowcount", String(heads.length + count));
640
714
  return () => {
641
715
  table.removeAttribute("aria-rowcount");
642
716
  heads.forEach((row) => row.removeAttribute("aria-rowindex"));
643
717
  };
644
- }, [items.length]);
645
- const itemsRef = React.useRef(items);
646
- itemsRef.current = items;
718
+ }, [count]);
719
+ const itemsRef = React.useRef(itemAt);
720
+ itemsRef.current = itemAt;
647
721
  React.useEffect(() => {
648
722
  const table = anchorRef.current?.closest("table, [role=table]");
649
723
  if (!table)
@@ -654,7 +728,7 @@ export function TableVirtualBody({ rows, groups, renderRow, getRowKey, sizeRows,
654
728
  : null;
655
729
  if (!row || row.closest("table, [role=table]") !== table)
656
730
  return;
657
- const entry = itemsRef.current[Number(row.getAttribute("data-index"))];
731
+ const entry = itemsRef.current(Number(row.getAttribute("data-index")));
658
732
  if (entry)
659
733
  setFocusKey(entry.key);
660
734
  };
@@ -683,37 +757,100 @@ export function TableVirtualBody({ rows, groups, renderRow, getRowKey, sizeRows,
683
757
  const measure = virtualizer.measureElement;
684
758
  const shown = live
685
759
  ? virtualizer.getVirtualIndexes()
686
- : Array.from({ length: Math.min(FIRST_ROWS, items.length) }, (_, i) => i);
760
+ : Array.from({ length: Math.min(FIRST_ROWS, count) }, (_, i) => i);
687
761
  let starts = null;
688
762
  if (!live) {
689
- starts = new Float64Array(items.length + 1);
690
- for (let i = 0; i < items.length; i++)
763
+ starts = new Float64Array(count + 1);
764
+ for (let i = 0; i < count; i++)
691
765
  starts[i + 1] = starts[i] + estimate(i);
692
766
  }
693
767
  const startOf = (i) => (starts ? starts[i] : virtualizer.measurementsCache[i].start - margin);
694
768
  const endOf = (i) => (starts ? starts[i + 1] : virtualizer.measurementsCache[i].end - margin);
695
- const total = starts ? starts[items.length] : virtualizer.getTotalSize();
769
+ const total = starts ? starts[count] : compress > 1 ? MAX_HEIGHT : virtualTotal;
770
+ // Where a point of the rows is drawn. Compressed, the view's top (`seen`, the rows' own space)
771
+ // is drawn where the scroller is (`at`), the rows around it at their height, and the rest of the
772
+ // rows shrink into the space on either side.
773
+ let place = (y) => y;
774
+ if (compress > 1) {
775
+ const at = realOffsetRef.current - margin;
776
+ const seen = at > 0 ? at * compress : at;
777
+ const near = viewHeight * 2;
778
+ const from = Math.max(0, seen - Math.min(near, Math.max(0, at)));
779
+ const to = Math.min(virtualTotal, seen + Math.min(near, Math.max(0, total - at)));
780
+ const fromAt = at - (seen - from);
781
+ const toAt = at + (to - seen);
782
+ place = (y) => y < from
783
+ ? from > 0
784
+ ? (y / from) * fromAt
785
+ : 0
786
+ : y > to
787
+ ? toAt + ((y - to) / Math.max(1, virtualTotal - to)) * (total - toAt)
788
+ : at + (y - seen);
789
+ }
790
+ // The estimate follows the rows drawn: most of them are one height (and a theme can change it).
791
+ React.useLayoutEffect(() => {
792
+ const table = anchorRef.current?.closest("table, [role=table]");
793
+ if (!live || !table)
794
+ return;
795
+ const common = (selector) => {
796
+ const count = new Map();
797
+ for (const el of table.querySelectorAll(selector)) {
798
+ const h = Math.round(el.getBoundingClientRect().height * 2) / 2;
799
+ if (h > 0)
800
+ count.set(h, (count.get(h) ?? 0) + 1);
801
+ }
802
+ let best;
803
+ for (const [h, n] of count)
804
+ if (best === undefined || n > count.get(best))
805
+ best = h;
806
+ return best;
807
+ };
808
+ const row = common(":scope [data-slot=table-row][data-index]");
809
+ const header = common(":scope [data-slot=table-group-header][data-index]");
810
+ setDrawnSize((old) => {
811
+ const next = {
812
+ row: row ?? old?.row ?? (dense ? 30 : 46),
813
+ header: header ?? old?.header ?? (dense ? 30 : 38),
814
+ };
815
+ return old && old.row === next.row && old.header === next.header ? old : next;
816
+ });
817
+ });
696
818
  const spacer = (key, height, outer = false) => fit ? (_jsx("div", { "aria-hidden": true, "data-slot": "table-spacer", className: "col-span-full", style: { height } }, key)) : outer ? (_jsx("tbody", { "aria-hidden": true, "data-slot": "table-spacer", children: _jsx("tr", { children: _jsx("td", { style: { height, padding: 0 } }) }) }, key)) : (_jsx("tr", { "aria-hidden": true, "data-slot": "table-spacer", children: _jsx("td", { style: { height, padding: 0 } }) }, key));
697
819
  const entry = (i) => {
698
- const item = items[i];
820
+ const item = itemAt(i);
699
821
  const rowIndex = headRows + i + 1;
700
822
  if (item.header)
701
823
  return (_jsx(VirtualRow, { index: i, rowIndex: rowIndex, measure: measure, row: groups[item.group].header, at: -1, render: renderHeader }, item.key));
702
824
  return (_jsx(VirtualRow, { index: i, rowIndex: rowIndex, measure: measure, row: item.row, at: item.at, render: renderRow }, item.key));
703
825
  };
826
+ // The rows rendered, for a body that loads them as they come into view.
827
+ // The rows in view and `overscan` beyond — not a far-off row kept for the focus.
828
+ const range = live ? virtualizer.range : null;
829
+ const firstRow = groups || !range ? -1 : Math.max(0, range.startIndex - overscan);
830
+ const lastRow = groups || !range ? -1 : Math.min(count - 1, range.endIndex + overscan);
831
+ const rangeRef = React.useRef(onRangeChange);
832
+ rangeRef.current = onRangeChange;
833
+ React.useEffect(() => {
834
+ if (lastRow >= 0)
835
+ rangeRef.current?.(firstRow, lastRow + 1);
836
+ }, [firstRow, lastRow]);
837
+ /** Where the last `run` ended. */
838
+ let runEnd = 0;
704
839
  /** The shown entries `from` up to `to`, spaced out between `top` and `bottom`. */
705
840
  const run = (from, to, top, bottom) => {
706
841
  const out = [];
707
842
  let y = top;
708
843
  for (let j = from; j < to; j++) {
709
844
  const i = shown[j];
710
- if (startOf(i) > y + 0.5)
711
- out.push(spacer(`gap-${i}`, startOf(i) - y));
845
+ const start = place(startOf(i));
846
+ if (start > y + 0.5)
847
+ out.push(spacer(`gap-${i}`, start - y));
712
848
  out.push(entry(i));
713
- y = endOf(i);
849
+ y = Math.max(y, start) + endOf(i) - startOf(i);
714
850
  }
715
851
  if (bottom > y + 0.5)
716
852
  out.push(spacer("end", bottom - y));
853
+ runEnd = Math.max(y, bottom);
717
854
  return out;
718
855
  };
719
856
  // Rendered once per change of the rows, not as the table scrolls.
@@ -728,8 +865,8 @@ export function TableVirtualBody({ rows, groups, renderRow, getRowKey, sizeRows,
728
865
  for (let j = 0; j < shown.length;) {
729
866
  const g = items[shown[j]].group;
730
867
  const { start: first, end, open } = spans[g];
731
- const top = startOf(first);
732
- const bottom = endOf(end - 1);
868
+ const top = Math.max(y, place(startOf(first)));
869
+ const bottom = place(endOf(end - 1));
733
870
  let k = j;
734
871
  while (k < shown.length && items[shown[k]].group === g)
735
872
  k++;
@@ -741,7 +878,7 @@ export function TableVirtualBody({ rows, groups, renderRow, getRowKey, sizeRows,
741
878
  setToggled((map) => new Map(map).set(group.key, next));
742
879
  group.onOpenChange?.(next);
743
880
  }, collapsible: group.collapsible, children: run(j, k, top, bottom) }, `g:${String(group.key)}`));
744
- y = bottom;
881
+ y = runEnd;
745
882
  j = k;
746
883
  }
747
884
  if (total > y + 0.5)
@@ -3,6 +3,17 @@ declare const groupSizes: {
3
3
  readonly sm: "min-h-(--control-h-sm) p-0.5";
4
4
  readonly default: "min-h-(--control-h) p-[3px]";
5
5
  };
6
+ export interface TagsInputMessages {
7
+ /** Each chip's remove button. */
8
+ remove: (tag: string) => string;
9
+ /** Read after a tag that `validate` turned down. */
10
+ invalid: string;
11
+ }
12
+ declare module "./locale" {
13
+ interface Messages {
14
+ tagsInput: TagsInputMessages;
15
+ }
16
+ }
6
17
  export interface TagsInputProps extends Omit<React.ComponentProps<"input">, "value" | "defaultValue" | "onChange" | "size" | "max" | "type" | "className"> {
7
18
  value?: string[];
8
19
  defaultValue?: string[];
@@ -21,8 +32,13 @@ export interface TagsInputProps extends Omit<React.ComponentProps<"input">, "val
21
32
  * and marks the `Field` invalid.
22
33
  */
23
34
  required?: boolean;
24
- /** The remove button's name. Default: "Remove {tag}". */
35
+ /**
36
+ * The remove button's name. Default: "Remove {tag}".
37
+ * @deprecated Use `labels.remove`.
38
+ */
25
39
  removeLabel?: (tag: string) => string;
40
+ /** What screen readers hear on the chips; a LocaleProvider sets it for every TagsInput. */
41
+ labels?: Partial<TagsInputMessages>;
26
42
  /** Classes for the visible field, not the bare `<input>`. */
27
43
  className?: string;
28
44
  }
@@ -36,5 +52,5 @@ export interface TagsInputProps extends Omit<React.ComponentProps<"input">, "val
36
52
  * (letter case aside). Pass `validate` to mark the tags that don't fit, and say why under the
37
53
  * field. The rule people break: colouring tags by kind — they are raised secondary surfaces.
38
54
  */
39
- export declare function TagsInput({ value: valueProp, defaultValue, onValueChange, size, validate, max, name, removeLabel, className, placeholder, disabled, readOnly, required, id, ref, onKeyDown, onPaste, onFocus, onBlur, ...props }: TagsInputProps): React.JSX.Element;
55
+ export declare function TagsInput({ value: valueProp, defaultValue, onValueChange, size, validate, max, name, removeLabel, labels, className, placeholder, disabled, readOnly, required, id, ref, onKeyDown, onPaste, onFocus, onBlur, ...props }: TagsInputProps): React.JSX.Element;
40
56
  export {};
@@ -1,29 +1,35 @@
1
1
  "use client";
2
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
- import { useDirection } from "@base-ui/react/direction-provider";
2
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
4
3
  import { Field as BaseField } from "@base-ui/react/field";
5
4
  import { CircleAlertIcon, XIcon } from "lucide-react";
6
5
  import * as React from "react";
7
6
  import { cn } from "../lib/cn.js";
8
7
  import { mergeRefs } from "../lib/merge-refs.js";
8
+ import { useMessages } from "./locale.js";
9
9
  /* The field and chips are ComboboxChips' own: the same group, the same raised 30px chips with 7px
10
10
  corners and a round remove button, so a multiple Combobox and a TagsInput read as one family. */
11
11
  const groupSizes = {
12
12
  sm: "min-h-(--control-h-sm) p-0.5",
13
13
  default: "min-h-(--control-h) p-[3px]",
14
14
  };
15
+ // `leading-none` after the text size, which would drop it: a chip as small as a compact theme's
16
+ // dense field has no room for a line box.
15
17
  const chipSizes = {
16
- sm: "min-h-(--control-inset-sm) pe-0.5 ps-2 text-xs",
17
- default: "min-h-(--control-h-sm) pe-[3px] ps-2.5 text-[13px]",
18
+ sm: "min-h-(--control-inset-sm) pe-0.5 ps-2 text-xs leading-none",
19
+ default: "min-h-(--control-h-sm) pe-[3px] ps-2.5 text-[13px] leading-none",
18
20
  };
19
21
  const removeSizes = {
20
- sm: "size-[18px] [&_svg]:size-3",
22
+ sm: "size-[min(18px,calc(var(--control-inset-sm)-2px))] [&_svg]:size-3",
21
23
  default: "size-(--control-inset-sm) [&_svg]:size-3.5",
22
24
  };
23
25
  const inputSizes = {
24
26
  sm: "min-h-(--control-inset-sm) text-[13px]",
25
27
  default: "min-h-(--control-h-sm) text-base sm:text-[15px]",
26
28
  };
29
+ const defaultMessages = {
30
+ remove: (tag) => `Remove ${tag}`,
31
+ invalid: "(invalid)",
32
+ };
27
33
  /** Split typed or pasted text into tags: commas, semicolons, tabs and new lines. */
28
34
  const splitTags = (text) => text
29
35
  .split(/[,;\t\n]+/)
@@ -39,7 +45,8 @@ const splitTags = (text) => text
39
45
  * (letter case aside). Pass `validate` to mark the tags that don't fit, and say why under the
40
46
  * field. The rule people break: colouring tags by kind — they are raised secondary surfaces.
41
47
  */
42
- export function TagsInput({ value: valueProp, defaultValue, onValueChange, size = "default", validate, max, name, removeLabel = (tag) => `Remove ${tag}`, className, placeholder, disabled, readOnly, required, id, ref, onKeyDown, onPaste, onFocus, onBlur, ...props }) {
48
+ export function TagsInput({ value: valueProp, defaultValue, onValueChange, size = "default", validate, max, name, removeLabel, labels, className, placeholder, disabled, readOnly, required, id, ref, onKeyDown, onPaste, onFocus, onBlur, ...props }) {
49
+ const messages = useMessages("tagsInput", defaultMessages, labels);
43
50
  const [valueState, setValueState] = React.useState(defaultValue ?? []);
44
51
  const tags = valueProp ?? valueState;
45
52
  const [text, setText] = React.useState("");
@@ -52,16 +59,20 @@ export function TagsInput({ value: valueProp, defaultValue, onValueChange, size
52
59
  // Disabled by the prop or by a disabled Field, which Base UI sets on the input.
53
60
  const locked = () => !!input.current?.disabled || !!readOnly;
54
61
  // The chips run before the input: to its left, or to its right in a right-to-left page.
55
- const rtl = useDirection() === "rtl";
56
- const towardChips = rtl ? "ArrowRight" : "ArrowLeft";
57
- const awayFromChips = rtl ? "ArrowLeft" : "ArrowRight";
62
+ // The chips run the way the field is laid out — a DirectionProvider, or only dir="rtl" on the
63
+ // page — so the arrows are read from the element's own direction when pressed, as Tree does.
64
+ const arrows = (el) => getComputedStyle(el).direction === "rtl"
65
+ ? { towardChips: "ArrowRight", awayFromChips: "ArrowLeft" }
66
+ : { towardChips: "ArrowLeft", awayFromChips: "ArrowRight" };
58
67
  // Tags are unique, but a value passed in may repeat one: a repeat gets a key of its own.
59
- const counts = new Map();
60
- const keys = tags.map((tag) => {
61
- const n = counts.get(tag) ?? 0;
62
- counts.set(tag, n + 1);
63
- return n ? `${tag}\u0000${n}` : tag;
64
- });
68
+ const keys = React.useMemo(() => {
69
+ const counts = new Map();
70
+ return tags.map((tag) => {
71
+ const n = counts.get(tag) ?? 0;
72
+ counts.set(tag, n + 1);
73
+ return n ? `${tag}\u0000${n}` : tag;
74
+ });
75
+ }, [tags]);
65
76
  /* A form's reset puts back the tags it started with, as it does a native input's text. */
66
77
  const initial = React.useRef(defaultValue);
67
78
  initial.current = defaultValue;
@@ -147,7 +158,9 @@ export function TagsInput({ value: valueProp, defaultValue, onValueChange, size
147
158
  else if (event.key === ",") {
148
159
  event.preventDefault();
149
160
  }
150
- else if ((event.key === "Backspace" || event.key === towardChips) &&
161
+ else if (
162
+ // Backspace only from an empty input, so it never jumps away while editing the text's start.
163
+ ((event.key === "Backspace" && text === "") || event.key === arrows(el).towardChips) &&
151
164
  el.selectionStart === 0 &&
152
165
  el.selectionEnd === 0 &&
153
166
  tags.length > 0) {
@@ -155,10 +168,24 @@ export function TagsInput({ value: valueProp, defaultValue, onValueChange, size
155
168
  chips.current[tags.length - 1]?.focus();
156
169
  }
157
170
  };
171
+ // The chips' handlers, current at each render, for the chips that are not drawn again.
172
+ const actions = React.useRef({
173
+ keyDown: (_index, _event) => { },
174
+ remove: (_index) => { },
175
+ });
176
+ React.useLayoutEffect(() => {
177
+ actions.current.keyDown = (index, event) => handleChipKeyDown(index)(event);
178
+ actions.current.remove = (index) => {
179
+ if (locked())
180
+ return;
181
+ remove(index, "input");
182
+ };
183
+ });
158
184
  const handleChipKeyDown = (index) => (event) => {
159
185
  if (event.target !== event.currentTarget)
160
186
  return;
161
187
  const { key } = event;
188
+ const { towardChips, awayFromChips } = arrows(event.currentTarget);
162
189
  if (key === towardChips) {
163
190
  event.preventDefault();
164
191
  chips.current[Math.max(0, index - 1)]?.focus();
@@ -198,7 +225,7 @@ export function TagsInput({ value: valueProp, defaultValue, onValueChange, size
198
225
  // validity; a native form gets one hidden input per tag under the name, ours or the Field's.
199
226
  render: (control) => {
200
227
  const { ref: fieldRef, id: controlId, name: formName, value: formValue, disabled: off, onChange: fieldChange, onFocus: fieldFocus, onBlur: fieldBlur, onKeyDown: _onKeyDown, className: _className, defaultValue: _defaultValue, autoFocus: _autoFocus, "aria-invalid": fieldInvalid, ...field } = control;
201
- return (_jsxs("div", { "data-slot": "tags-input", "data-size": size, "data-readonly": readOnly ? "" : undefined, "data-full": full ? "" : undefined, className: cn("group/tags-input flex w-full material-field cursor-text items-center rounded-control font-body", groupSizes[size], "has-[[data-slot=tags-input-input][aria-invalid=true]]:border-destructive has-[[data-slot=tags-input-input][aria-invalid=true]]:shadow-invalid", "has-[[data-slot=tags-input-input]:disabled]:cursor-not-allowed has-[[data-slot=tags-input-input]:disabled]:opacity-55", className), onMouseDown: (event) => {
228
+ return (_jsxs("div", { "data-slot": "tags-input", "data-size": size, "data-readonly": readOnly ? "" : undefined, "data-full": full ? "" : undefined, "data-disabled": off ? "" : undefined, className: cn("group/tags-input flex w-full material-field cursor-text items-center rounded-control font-body", groupSizes[size], "has-[[data-slot=tags-input-input][aria-invalid=true]]:border-destructive has-[[data-slot=tags-input-input][aria-invalid=true]]:shadow-invalid", "data-disabled:cursor-not-allowed data-disabled:opacity-55", className), onMouseDown: (event) => {
202
229
  // A press on the field's empty space puts the caret in the input, as on a text field.
203
230
  if (event.target.closest("input, button, [data-slot=tags-input-chip]"))
204
231
  return;
@@ -213,22 +240,7 @@ export function TagsInput({ value: valueProp, defaultValue, onValueChange, size
213
240
  fieldBlur?.({
214
241
  currentTarget: fieldInput.current,
215
242
  });
216
- }, children: [_jsxs("div", { "data-slot": "tags-input-chips", role: tags.length > 0 ? "toolbar" : undefined, className: "flex w-full flex-wrap items-center gap-[3px]", children: [tags.map((tag, index) => {
217
- const invalid = validate ? !validate(tag) : false;
218
- return (_jsxs("div", { ref: (el) => {
219
- chips.current[index] = el;
220
- }, "data-slot": "tags-input-chip", "data-invalid": invalid ? "" : undefined, "data-repeat": repeat === tag ? "" : undefined, tabIndex: -1, onKeyDown: handleChipKeyDown(index), className: cn("inline-flex max-w-full cursor-default items-center gap-1 rounded-item border border-transparent leading-none font-medium text-foreground", "bg-control-surface bg-linear-to-b from-control-inner-edge to-transparent shadow-raised", "transition-[box-shadow,border-color,color] duration-(--duration) ease-(--ease)", "group-has-[[data-slot=tags-input-input]:disabled]/tags-input:pointer-events-none", chipSizes[size], readOnly && "pe-2.5 group-data-[size=sm]/tags-input:pe-2",
221
- // Not valid: a 1px red edge, red ink and an alert icon, read out as "invalid".
222
- "data-invalid:border-destructive/60 data-invalid:text-destructive",
223
- // The tag that was typed again is marked with a current item's edge for a moment.
224
- "data-repeat:shadow-raised-current"), children: [invalid && _jsx(CircleAlertIcon, { "aria-hidden": true, className: "size-3.5 shrink-0" }), _jsxs("span", { className: "truncate", children: [tag, invalid && _jsx("span", { className: "sr-only", children: " (invalid)" })] }), !readOnly && (_jsx("button", { type: "button", tabIndex: -1, "data-slot": "tags-input-chip-remove", "aria-label": removeLabel(tag),
225
- // Keep focus where it is: a click removes the tag, then returns to the input.
226
- onMouseDown: (event) => event.preventDefault(), onClick: () => {
227
- if (locked())
228
- return;
229
- remove(index, "input");
230
- }, className: cn("inline-flex shrink-0 cursor-pointer items-center justify-center rounded-full text-muted-foreground transition-[background-color,color] duration-(--duration) ease-brand hover:bg-control-hover hover:text-foreground", "group-has-[[data-slot=tags-input-input]:disabled]/tags-input:pointer-events-none", removeSizes[size]), children: _jsx(XIcon, {}) }))] }, keys[index]));
231
- }), _jsx("input", { ...field, ...props, ref: refs, id: controlId, "data-slot": "tags-input-input", value: text, onChange: (event) => setText(event.currentTarget.value), onKeyDown: handleInputKeyDown, onPaste: handlePaste, onFocus: onFocus, onBlur: (event) => {
243
+ }, children: [_jsxs("div", { "data-slot": "tags-input-chips", role: tags.length > 0 ? "toolbar" : undefined, className: "flex w-full flex-wrap items-center gap-[3px]", children: [_jsx(TagChips, { tags: tags, keys: keys, repeat: repeat, size: size, readOnly: readOnly, validate: validate, removeLabel: removeLabel ?? messages.remove, invalidLabel: messages.invalid, chips: chips, actions: actions }), _jsx("input", { ...field, ...props, ref: refs, id: controlId, "data-slot": "tags-input-input", value: text, onChange: (event) => setText(event.currentTarget.value), onKeyDown: handleInputKeyDown, onPaste: handlePaste, onFocus: onFocus, onBlur: (event) => {
232
244
  onBlur?.(event);
233
245
  // What was typed and not yet added is added on leaving, so a submit never loses it.
234
246
  if (text.trim() && !locked())
@@ -237,3 +249,23 @@ export function TagsInput({ value: valueProp, defaultValue, onValueChange, size
237
249
  tags.map((tag, index) => (_jsx("input", { type: "hidden", name: formName, value: tag, form: props.form, disabled: off }, keys[index])))] }), _jsx("input", { ref: fieldRef, value: formValue ?? "", onChange: fieldChange, required: required, disabled: off, readOnly: readOnly, form: props.form, "aria-hidden": true, tabIndex: -1, autoComplete: "off", onFocus: () => input.current?.focus(), className: "sr-only" })] }));
238
250
  } }));
239
251
  }
252
+ /**
253
+ * The chips, drawn again only when the tags (or how they look) change — not at each key typed in
254
+ * the input, which with hundreds of tags would redraw them all.
255
+ */
256
+ const TagChips = React.memo(function TagChips({ tags, keys, repeat, size, readOnly, validate, removeLabel, invalidLabel, chips, actions, }) {
257
+ return (_jsx(_Fragment, { children: tags.map((tag, index) => {
258
+ const invalid = validate ? !validate(tag) : false;
259
+ return (_jsxs("div", { ref: (el) => {
260
+ chips.current[index] = el;
261
+ }, "data-slot": "tags-input-chip", "data-invalid": invalid ? "" : undefined, "data-repeat": repeat === tag ? "" : undefined, tabIndex: -1, onKeyDown: (event) => actions.current.keyDown(index, event), className: cn("inline-flex max-w-full cursor-default items-center gap-1 rounded-item border border-transparent leading-none font-medium text-foreground", "bg-control-surface bg-linear-to-b from-control-inner-edge to-transparent shadow-raised", "transition-[box-shadow,border-color,color] duration-(--duration) ease-(--ease)", "group-data-disabled/tags-input:pointer-events-none", chipSizes[size], readOnly && "pe-2.5 group-data-[size=sm]/tags-input:pe-2",
262
+ // Not valid: a 1px red edge, red ink and an alert icon, read out as "invalid".
263
+ "data-invalid:border-destructive/60 data-invalid:text-destructive",
264
+ // The tag that was typed again is marked with a current item's edge for a moment.
265
+ "data-repeat:shadow-raised-current"), children: [invalid && _jsx(CircleAlertIcon, { "aria-hidden": true, className: "size-3.5 shrink-0" }), _jsxs("span", { className: "truncate", children: [tag, invalid && _jsxs("span", { className: "sr-only", children: [" ", invalidLabel] })] }), !readOnly && (_jsx("button", { type: "button", tabIndex: -1, "data-slot": "tags-input-chip-remove", "aria-label": removeLabel(tag),
266
+ // Keep focus where it is: a click removes the tag, then returns to the input.
267
+ onMouseDown: (event) => event.preventDefault(), onClick: () => {
268
+ actions.current.remove(index);
269
+ }, className: cn("inline-flex shrink-0 cursor-pointer items-center justify-center rounded-full text-muted-foreground transition-[background-color,color] duration-(--duration) ease-brand hover:bg-control-hover hover:text-foreground", "group-data-disabled/tags-input:pointer-events-none", removeSizes[size]), children: _jsx(XIcon, {}) }))] }, keys[index]));
270
+ }) }));
271
+ });