@openartifactsai/ui 0.0.0-stage → 0.23.0-rc.0

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 (91) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +5 -2
  4. package/package.json +82 -4
  5. package/src/button.d.ts +30 -0
  6. package/src/button.js +29 -0
  7. package/src/clicks.d.ts +63 -0
  8. package/src/clicks.js +75 -0
  9. package/src/cn.d.ts +3 -0
  10. package/src/cn.js +5 -0
  11. package/src/color-picker.d.ts +98 -0
  12. package/src/color-picker.js +395 -0
  13. package/src/color.d.ts +150 -0
  14. package/src/color.js +543 -0
  15. package/src/crash-boundary.d.ts +29 -0
  16. package/src/crash-boundary.js +32 -0
  17. package/src/data/google-fonts.json +1775 -0
  18. package/src/drag-cover.d.ts +48 -0
  19. package/src/drag-cover.js +41 -0
  20. package/src/effect-editors.d.ts +48 -0
  21. package/src/effect-editors.js +230 -0
  22. package/src/effects.d.ts +118 -0
  23. package/src/effects.js +425 -0
  24. package/src/escape-to-popup.d.ts +53 -0
  25. package/src/escape-to-popup.js +78 -0
  26. package/src/field-announce.d.ts +28 -0
  27. package/src/field-announce.js +43 -0
  28. package/src/fields.d.ts +176 -0
  29. package/src/fields.js +377 -0
  30. package/src/focusables.d.ts +50 -0
  31. package/src/focusables.js +61 -0
  32. package/src/font-picker.d.ts +62 -0
  33. package/src/font-picker.js +242 -0
  34. package/src/fonts.d.ts +121 -0
  35. package/src/fonts.js +273 -0
  36. package/src/history-diff.d.ts +64 -0
  37. package/src/history-diff.js +151 -0
  38. package/src/history.d.ts +95 -0
  39. package/src/history.js +160 -0
  40. package/src/index.d.ts +38 -0
  41. package/src/index.js +38 -0
  42. package/src/key-chord.d.ts +74 -0
  43. package/src/key-chord.js +228 -0
  44. package/src/keys.d.ts +142 -0
  45. package/src/keys.js +172 -0
  46. package/src/layout.d.ts +74 -0
  47. package/src/layout.js +173 -0
  48. package/src/link-card-view.d.ts +97 -0
  49. package/src/link-card-view.js +175 -0
  50. package/src/link-card.d.ts +136 -0
  51. package/src/link-card.js +158 -0
  52. package/src/menu-shortcuts.d.ts +31 -0
  53. package/src/menu-shortcuts.js +91 -0
  54. package/src/menu.d.ts +120 -0
  55. package/src/menu.js +100 -0
  56. package/src/number-entry.d.ts +140 -0
  57. package/src/number-entry.js +202 -0
  58. package/src/number-limits.d.ts +2 -0
  59. package/src/number-limits.js +2 -0
  60. package/src/number-wheel.d.ts +29 -0
  61. package/src/number-wheel.js +27 -0
  62. package/src/numbers.d.ts +28 -0
  63. package/src/numbers.js +124 -0
  64. package/src/overlays.d.ts +71 -0
  65. package/src/overlays.js +131 -0
  66. package/src/pressed.d.ts +5 -0
  67. package/src/pressed.js +10 -0
  68. package/src/scroll.d.ts +24 -0
  69. package/src/scroll.js +111 -0
  70. package/src/scrub-number-field.d.ts +79 -0
  71. package/src/scrub-number-field.js +254 -0
  72. package/src/select-announce.d.ts +50 -0
  73. package/src/select-announce.js +71 -0
  74. package/src/slider-session.d.ts +43 -0
  75. package/src/slider-session.js +39 -0
  76. package/src/styles.css +96 -0
  77. package/src/tab-strip-layout.d.ts +76 -0
  78. package/src/tab-strip-layout.js +111 -0
  79. package/src/tab-strip.d.ts +73 -0
  80. package/src/tab-strip.js +498 -0
  81. package/src/text-area-view.d.ts +35 -0
  82. package/src/text-area-view.js +142 -0
  83. package/src/text-area.d.ts +38 -0
  84. package/src/text-area.js +51 -0
  85. package/src/theme.css +34 -0
  86. package/src/toast-fit.d.ts +21 -0
  87. package/src/toast-fit.js +30 -0
  88. package/src/toast-tone.d.ts +27 -0
  89. package/src/toast-tone.js +22 -0
  90. package/src/tooltip.d.ts +23 -0
  91. package/src/tooltip.js +18 -0
@@ -0,0 +1,498 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * A row of tabs like a browser's: an icon on the left, the title, a close button on the right. Tabs are dragged to
4
+ * change their order (the others slide out of the way), closed with ×, a middle click, Delete or the right-click menu,
5
+ * and the ones that do not fit go into a list behind a ▾ button at the end of the row. Picking a tab from that list
6
+ * brings it into the row next to the last tab shown. + after the row either does what its owner says or, given
7
+ * `newMenu`, opens a menu under it (the owner's lines: items with icons, headings and rules).
8
+ *
9
+ * A `fixed` tab (a home page) comes first, is never dragged, closed or put in the list.
10
+ *
11
+ * The row knows nothing of what its tabs show: the owner passes the tabs in order, which one is in front, and is told
12
+ * what the person did (`onSelect`, `onClose`, `onReorder`). A reorder is shown at once and kept until the owner's
13
+ * order says the same (or a second goes by).
14
+ *
15
+ * Keyboard: ←/→, Home and End move between tabs, Enter or Space brings one to the front, Delete or Backspace closes it,
16
+ * Ctrl+Shift+←/→ (⌘⇧ on a Mac) moves it, Shift+F10 or the menu key opens its menu. A drag is cancelled with Escape.
17
+ *
18
+ * Its parts carry `data-slot` (`tab`, `tab-title`, `tab-close`) for an app's own rules and tests. The tab in front is
19
+ * drawn on `surface`, the others on whatever the owner puts behind the row.
20
+ */
21
+ import { Menu as BaseMenu } from '@base-ui/react/menu';
22
+ import { ChevronDown, Plus, X } from 'lucide-react';
23
+ import { useCallback, useEffect, useLayoutEffect, useRef, useState, } from 'react';
24
+ import { cn } from "./cn.js";
25
+ import { MENU_ITEM_CLASS, MenuLabel, MenuSeparator, POPUP_CLASS } from "./menu.js";
26
+ import { closeGroups, dragLeft, dragOrder, fitTabs, moveTo, pickedPlace, sameFit } from "./tab-strip-layout.js";
27
+ /**
28
+ * A tab's width without the room it was given. Not its width less that room: the room is whole pixels and the title
29
+ * may take a little less of it, and a width off by that little flips the fit at the edge (the room goes, the tab fits,
30
+ * the room comes back, it does not), so the title's own box is put back to its cap.
31
+ */
32
+ function unstretched(el, stretch) {
33
+ const width = el.getBoundingClientRect().width;
34
+ const title = stretch ? el.querySelector('[data-slot=tab-title]') : null;
35
+ if (!title)
36
+ return width;
37
+ const shown = title.getBoundingClientRect().width;
38
+ const cap = parseFloat(getComputedStyle(title).maxWidth) - stretch;
39
+ return Number.isFinite(cap) ? width - shown + Math.min(shown, cap) : width - stretch;
40
+ }
41
+ /** How far the pointer moves before a press becomes a drag. */
42
+ const DRAG_START_PX = 4;
43
+ /** How long a reorder is shown while the owner has not said it (it may have refused). */
44
+ const REORDER_WAIT_MS = 1000;
45
+ const FRONT = '\0front';
46
+ const EMPTY_FIT = { hidden: new Set(), stretch: new Map(), clipped: new Set(), start: 0 };
47
+ const sameSet = (a, b) => a.length === b.length && a.every((id) => b.includes(id));
48
+ const reducedMotion = () => window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ?? false;
49
+ const closableOf = (tab) => tab.closable ?? !tab.fixed;
50
+ const isMac = () => /Mac/i.test(navigator.platform);
51
+ const ROW_BUTTON = 'grid size-7 flex-none cursor-default place-items-center rounded-[7px] border-0 bg-transparent p-0 text-fg-muted hover:bg-hover aria-expanded:bg-hover';
52
+ const SCROLLING_POPUP = 'overflow-y-auto overscroll-contain [scrollbar-width:thin] [scrollbar-color:color-mix(in_srgb,var(--color-fg)_22%,transparent)_transparent]';
53
+ const LIFTED = 'shadow-[0_0_0_0.5px_var(--color-line-strong),0_1px_2px_rgb(0_0_0/0.08)]';
54
+ export function TabStrip(props) {
55
+ const { tabs, activeId, words, onSelect, onClose, onReorder, onNew, newMenu, newTestId, menuItems, onPopupChange, className } = props;
56
+ const rowRef = useRef(null);
57
+ const stripRef = useRef(null);
58
+ const moreRef = useRef(null);
59
+ const newRef = useRef(null);
60
+ const tabEls = useRef(new Map());
61
+ // ---- the order shown: the owner's, or a reorder it has not said yet ----
62
+ const ownIds = tabs.map((tab) => tab.id);
63
+ const ownKey = ownIds.join('\0');
64
+ const [override, setOverride] = useState(null);
65
+ const shownIds = override && sameSet(override, ownIds) ? override : ownIds;
66
+ const byId = new Map(tabs.map((tab) => [tab.id, tab]));
67
+ const shown = shownIds.map((id) => byId.get(id));
68
+ const shownKey = shownIds.join('\0');
69
+ useEffect(() => {
70
+ if (!override)
71
+ return;
72
+ if (override.join('\0') === ownKey || !sameSet(override, ownIds))
73
+ return setOverride(null);
74
+ const timer = setTimeout(() => setOverride(null), REORDER_WAIT_MS);
75
+ return () => clearTimeout(timer);
76
+ // eslint-disable-next-line react-hooks/exhaustive-deps
77
+ }, [override, ownKey]);
78
+ const [layout, setLayout] = useState(EMPTY_FIT);
79
+ const layoutRef = useRef(layout);
80
+ layoutRef.current = layout;
81
+ const [popup, setPopup] = useState(null);
82
+ const [dragId, setDragId] = useState(null);
83
+ const drag = useRef(null);
84
+ const moreWidth = useRef(28);
85
+ /** The tab that takes the keyboard after the next render: after a move by key, or (FRONT) the one then in front after
86
+ * a close by key or a pick from the list. */
87
+ const focusNext = useRef(null);
88
+ const latest = useRef({ shown, activeId });
89
+ latest.current = { shown, activeId };
90
+ // The owner hears of a menu only when one opens or the last one closes (and once more if the row goes away).
91
+ const popupChange = useRef(onPopupChange);
92
+ popupChange.current = onPopupChange;
93
+ const popupOpen = !!popup;
94
+ const reported = useRef(false);
95
+ useEffect(() => {
96
+ if (reported.current === popupOpen)
97
+ return;
98
+ reported.current = popupOpen;
99
+ popupChange.current?.(popupOpen);
100
+ }, [popupOpen]);
101
+ useEffect(() => () => {
102
+ if (reported.current)
103
+ popupChange.current?.(false);
104
+ }, []);
105
+ // ---- which tabs fit ----
106
+ const refits = useRef(0);
107
+ const measure = useCallback(() => {
108
+ const strip = stripRef.current;
109
+ const row = rowRef.current;
110
+ if (!strip || !row || drag.current?.started)
111
+ return;
112
+ const { shown, activeId } = latest.current;
113
+ const els = shown.map((tab) => tabEls.current.get(tab.id));
114
+ if (els.some((el) => !el))
115
+ return;
116
+ const current = layoutRef.current;
117
+ const gap = parseFloat(getComputedStyle(strip).columnGap) || 0;
118
+ const rowStyle = getComputedStyle(row);
119
+ const rowGap = parseFloat(rowStyle.columnGap) || 0;
120
+ const more = moreRef.current?.getBoundingClientRect().width;
121
+ if (more)
122
+ moreWidth.current = more;
123
+ const next = fitTabs({
124
+ ids: shown.map((tab) => tab.id),
125
+ fixed: shown.map((tab) => !!tab.fixed),
126
+ // What each tab takes on its own (the room it was given taken off), and what its title would need to be whole.
127
+ widths: shown.map((tab, i) => unstretched(els[i], current.stretch.get(tab.id) ?? 0)),
128
+ needs: shown.map((tab, i) => {
129
+ const title = els[i].querySelector('[data-slot=tab-title]');
130
+ return title ? (current.stretch.get(tab.id) ?? 0) + Math.max(0, title.scrollWidth - title.clientWidth) : 0;
131
+ }),
132
+ gap,
133
+ avail: row.clientWidth -
134
+ (parseFloat(rowStyle.paddingLeft) || 0) -
135
+ (parseFloat(rowStyle.paddingRight) || 0) -
136
+ (newRef.current ? newRef.current.getBoundingClientRect().width + rowGap : 0),
137
+ more: moreWidth.current + rowGap,
138
+ activeId,
139
+ start: current.start,
140
+ });
141
+ if (sameFit(next, current) && next.start === current.start)
142
+ return;
143
+ // A fit that keeps changing within one frame would render forever: it waits for the next frame instead.
144
+ if (++refits.current > 8)
145
+ return;
146
+ if (refits.current === 1)
147
+ requestAnimationFrame(() => (refits.current = 0));
148
+ setLayout(next);
149
+ }, []);
150
+ useLayoutEffect(measure);
151
+ useEffect(() => {
152
+ const row = rowRef.current;
153
+ if (!row)
154
+ return;
155
+ const observer = new ResizeObserver(() => measure());
156
+ observer.observe(row);
157
+ void document.fonts?.ready.then(() => measure());
158
+ return () => observer.disconnect();
159
+ }, [measure]);
160
+ // ---- the others slide when the order changes ----
161
+ // Where each tab was drawn before this render, read while the page still shows the old order.
162
+ const committedKey = useRef(shownKey);
163
+ const before = useRef(null);
164
+ if (committedKey.current !== shownKey && !before.current) {
165
+ before.current = new Map();
166
+ for (const [id, el] of tabEls.current)
167
+ if (el.isConnected)
168
+ before.current.set(id, el.getBoundingClientRect().left);
169
+ }
170
+ useLayoutEffect(() => {
171
+ const was = before.current;
172
+ before.current = null;
173
+ if (committedKey.current === shownKey)
174
+ return;
175
+ committedKey.current = shownKey;
176
+ const dragging = drag.current?.started ? drag.current : null;
177
+ if (dragging)
178
+ dragging.el.style.transform = `translateX(${dragging.left - dragging.el.offsetLeft}px)`;
179
+ if (!was || reducedMotion())
180
+ return;
181
+ for (const [id, el] of tabEls.current) {
182
+ if (el === dragging?.el || layoutRef.current.hidden.has(id))
183
+ continue;
184
+ const from = was.get(id);
185
+ if (from === undefined)
186
+ continue;
187
+ el.style.transition = 'none';
188
+ el.style.transform = '';
189
+ const delta = from - el.getBoundingClientRect().left;
190
+ if (Math.abs(delta) >= 0.5) {
191
+ el.style.transform = `translateX(${delta}px)`;
192
+ void el.offsetWidth;
193
+ }
194
+ el.style.transition = '';
195
+ el.style.transform = '';
196
+ }
197
+ }, [shownKey]);
198
+ // ---- the keyboard follows a tab moved or brought to the front by key ----
199
+ useEffect(() => {
200
+ const id = focusNext.current;
201
+ if (id === null)
202
+ return;
203
+ focusNext.current = null;
204
+ tabEls.current.get(id === FRONT ? activeId : id)?.focus();
205
+ });
206
+ // ---- dragging ----
207
+ const endDrag = useCallback((commit) => {
208
+ const current = drag.current;
209
+ if (!current)
210
+ return;
211
+ drag.current = null;
212
+ window.removeEventListener('pointermove', onMove, true);
213
+ window.removeEventListener('pointerup', onUp, true);
214
+ window.removeEventListener('pointercancel', onCancel, true);
215
+ window.removeEventListener('keydown', onKey, true);
216
+ window.removeEventListener('auxclick', onAside, true);
217
+ window.removeEventListener('contextmenu', onAside, true);
218
+ window.removeEventListener('blur', onCancel);
219
+ if (!current.started)
220
+ return;
221
+ const { el, id } = current;
222
+ if (el.hasPointerCapture(current.pointer))
223
+ el.releasePointerCapture(current.pointer);
224
+ // The click that ends a drag does not bring the tab to the front.
225
+ const swallow = (event) => {
226
+ event.stopPropagation();
227
+ event.preventDefault();
228
+ };
229
+ window.addEventListener('click', swallow, { capture: true, once: true });
230
+ setTimeout(() => window.removeEventListener('click', swallow, true), 0);
231
+ // The tab settles into its place.
232
+ el.style.transition = '';
233
+ el.style.transform = '';
234
+ setTimeout(() => setDragId((was) => (was === id ? null : was)), reducedMotion() ? 0 : 200);
235
+ const order = latest.current.shown.map((tab) => tab.id);
236
+ if (!commit) {
237
+ setOverride(sameSet(current.before, order) ? current.before : null);
238
+ return;
239
+ }
240
+ const to = order.indexOf(id);
241
+ if (to !== current.before.indexOf(id))
242
+ onReorder(id, to);
243
+ else
244
+ setOverride(null);
245
+ },
246
+ // The listeners are the stable ones below.
247
+ // eslint-disable-next-line react-hooks/exhaustive-deps
248
+ [onReorder]);
249
+ const endRef = useRef(endDrag);
250
+ endRef.current = endDrag;
251
+ const onMove = useRef((event) => {
252
+ const current = drag.current;
253
+ const strip = stripRef.current;
254
+ if (!current || !strip || event.pointerId !== current.pointer)
255
+ return;
256
+ // The button came up where the page did not hear it: the drag ends there.
257
+ if (!(event.buttons & 1)) {
258
+ endRef.current(true);
259
+ return;
260
+ }
261
+ if (!current.started) {
262
+ if (Math.abs(event.clientX - current.x0) < DRAG_START_PX)
263
+ return;
264
+ current.started = true;
265
+ current.el.setPointerCapture(current.pointer);
266
+ if (strip.contains(document.activeElement))
267
+ document.getSelection()?.removeAllRanges();
268
+ current.el.style.transition = 'none';
269
+ setPopup(null);
270
+ setDragId(current.id);
271
+ }
272
+ event.preventDefault();
273
+ const { shown } = latest.current;
274
+ const { el } = current;
275
+ // The row's own coordinates: what it has scrolled by counts in.
276
+ const rect = strip.getBoundingClientRect();
277
+ const origin = rect.left - strip.scrollLeft;
278
+ // The fixed tabs keep their place: the dragged one goes no further left than their end.
279
+ const fixedEnd = shown.reduce((end, tab) => {
280
+ const fixed = tab.fixed ? tabEls.current.get(tab.id) : undefined;
281
+ return fixed ? Math.max(end, fixed.offsetLeft + fixed.offsetWidth) : end;
282
+ }, -Infinity);
283
+ const box = {
284
+ left: event.clientX - current.grab - origin,
285
+ width: el.offsetWidth,
286
+ slot: el.offsetLeft,
287
+ rowLeft: rect.left - origin,
288
+ rowRight: rect.right - origin,
289
+ fixedEnd,
290
+ };
291
+ const left = dragLeft(box);
292
+ current.left = left;
293
+ el.style.transform = `translateX(${left - el.offsetLeft}px)`;
294
+ const hidden = layoutRef.current.hidden;
295
+ const order = shown.map((tab) => tab.id);
296
+ const row = shown.filter((tab) => !hidden.has(tab.id) && !tab.fixed).map((tab) => tab.id);
297
+ const next = dragOrder(order, row, current.id, box, (id) => {
298
+ const other = tabEls.current.get(id);
299
+ return other ? { left: other.offsetLeft, width: other.offsetWidth } : undefined;
300
+ });
301
+ if (next.join('\0') !== order.join('\0'))
302
+ setOverride(next);
303
+ }).current;
304
+ const onUp = useRef((event) => {
305
+ if (drag.current && event.pointerId === drag.current.pointer)
306
+ endRef.current(true);
307
+ }).current;
308
+ const onCancel = useRef(() => endRef.current(false)).current;
309
+ // While a tab is dragged, the other buttons open no menu.
310
+ const onAside = useRef((event) => {
311
+ if (!drag.current?.started)
312
+ return;
313
+ event.preventDefault();
314
+ event.stopPropagation();
315
+ }).current;
316
+ const onKey = useRef((event) => {
317
+ if (event.key !== 'Escape' || !drag.current?.started)
318
+ return;
319
+ event.preventDefault();
320
+ event.stopPropagation();
321
+ endRef.current(false);
322
+ }).current;
323
+ useEffect(() => () => endRef.current(false), []);
324
+ const onPointerDown = (event, tab) => {
325
+ if (event.button !== 0 || tab.fixed || drag.current)
326
+ return;
327
+ if (event.target instanceof Element && event.target.closest('button'))
328
+ return;
329
+ const strip = stripRef.current;
330
+ const el = tabEls.current.get(tab.id);
331
+ if (!strip || !el)
332
+ return;
333
+ drag.current = {
334
+ id: tab.id,
335
+ pointer: event.pointerId,
336
+ x0: event.clientX,
337
+ grab: event.clientX - el.getBoundingClientRect().left,
338
+ started: false,
339
+ before: latest.current.shown.map((x) => x.id),
340
+ left: el.offsetLeft,
341
+ el,
342
+ };
343
+ window.addEventListener('pointermove', onMove, true);
344
+ window.addEventListener('pointerup', onUp, true);
345
+ window.addEventListener('pointercancel', onCancel, true);
346
+ window.addEventListener('keydown', onKey, true);
347
+ window.addEventListener('auxclick', onAside, true);
348
+ window.addEventListener('contextmenu', onAside, true);
349
+ window.addEventListener('blur', onCancel);
350
+ };
351
+ // ---- what the tabs do ----
352
+ const close = (ids, byKey = false) => {
353
+ if (!ids.length)
354
+ return;
355
+ if (byKey)
356
+ focusNext.current = FRONT;
357
+ onClose(ids);
358
+ };
359
+ const move = (tab, step) => {
360
+ const order = shown.map((x) => x.id);
361
+ const from = order.indexOf(tab.id);
362
+ const firstMoving = shown.findIndex((x) => !x.fixed);
363
+ const to = from + step;
364
+ if (tab.fixed || to < firstMoving || to >= order.length)
365
+ return;
366
+ setOverride(moveTo(order, tab.id, to));
367
+ focusNext.current = tab.id;
368
+ onReorder(tab.id, to);
369
+ };
370
+ const openMenu = (tab) => {
371
+ if (tab.fixed || !tabEls.current.get(tab.id))
372
+ return;
373
+ setPopup({ kind: 'tab', id: tab.id });
374
+ };
375
+ const visible = shown.filter((tab) => !layout.hidden.has(tab.id));
376
+ const focusTab = (tab) => tab && tabEls.current.get(tab.id)?.focus();
377
+ const onTabKey = (event, tab) => {
378
+ if (event.target !== event.currentTarget)
379
+ return;
380
+ const at = visible.indexOf(tab);
381
+ const modifier = isMac() ? event.metaKey : event.ctrlKey;
382
+ if ((event.key === 'ArrowLeft' || event.key === 'ArrowRight') && modifier && event.shiftKey) {
383
+ event.preventDefault();
384
+ move(tab, event.key === 'ArrowLeft' ? -1 : 1);
385
+ }
386
+ else if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
387
+ event.preventDefault();
388
+ const step = event.key === 'ArrowLeft' ? -1 : 1;
389
+ focusTab(visible[(at + step + visible.length) % visible.length]);
390
+ }
391
+ else if (event.key === 'Home' || event.key === 'End') {
392
+ event.preventDefault();
393
+ focusTab(event.key === 'Home' ? visible[0] : visible[visible.length - 1]);
394
+ }
395
+ else if (event.key === 'Enter' || event.key === ' ') {
396
+ event.preventDefault();
397
+ onSelect(tab.id);
398
+ }
399
+ else if ((event.key === 'Delete' || event.key === 'Backspace') && closableOf(tab)) {
400
+ event.preventDefault();
401
+ close([tab.id], true);
402
+ }
403
+ else if (event.key === 'ContextMenu' || (event.key === 'F10' && event.shiftKey)) {
404
+ event.preventDefault();
405
+ openMenu(tab);
406
+ }
407
+ };
408
+ // ---- the list of tabs that do not fit ----
409
+ const listed = shown.filter((tab) => layout.hidden.has(tab.id));
410
+ const pick = (tab) => {
411
+ const order = shown.map((x) => x.id);
412
+ const to = pickedPlace(order, tab.id, visible[visible.length - 1]?.id);
413
+ setPopup(null);
414
+ if (to !== null) {
415
+ setOverride(moveTo(order, tab.id, to));
416
+ onReorder(tab.id, to);
417
+ }
418
+ onSelect(tab.id);
419
+ focusNext.current = FRONT;
420
+ };
421
+ useEffect(() => {
422
+ if (popup?.kind === 'list' && listed.length === 0)
423
+ setPopup(null);
424
+ if (popup?.kind === 'new' && !newMenu)
425
+ setPopup(null);
426
+ if (popup?.kind === 'tab' && !shown.some((tab) => tab.id === popup.id))
427
+ setPopup(null);
428
+ });
429
+ const menuTab = popup?.kind === 'tab' ? byId.get(popup.id) : undefined;
430
+ const menuEntries = (tab) => {
431
+ const own = menuItems?.(tab) ?? [];
432
+ const groups = closeGroups(shown.map((x) => x.id), (id) => closableOf(byId.get(id)), tab.id);
433
+ return [
434
+ ...own,
435
+ ...(own.length ? ['separator'] : []),
436
+ { key: 'close', label: words.closeTab, testId: 'tab-close-this', disabled: !closableOf(tab), onSelect: () => close([tab.id]) },
437
+ { key: 'close-others', label: words.closeOthers, testId: 'tab-close-others', disabled: !groups.others.length, onSelect: () => close(groups.others) },
438
+ { key: 'close-right', label: words.closeRight, testId: 'tab-close-right', disabled: !groups.right.length, onSelect: () => close(groups.right) },
439
+ { key: 'close-all', label: words.closeAll, testId: 'tab-close-all', disabled: !groups.all.length, onSelect: () => close(groups.all) },
440
+ ];
441
+ };
442
+ const openChange = (kind) => (open) => setPopup((was) => (open ? { kind } : was?.kind === kind ? null : was));
443
+ return (_jsxs("div", { ref: rowRef, className: cn('flex min-w-0 flex-auto items-center gap-1 [--tab-floor:7rem] [--tab-title-max:10rem]', className), children: [_jsx("div", { ref: stripRef, className: "relative flex h-full min-w-0 flex-initial items-center gap-[3px] overflow-hidden select-none", role: "tablist", "aria-label": words.label, "aria-orientation": "horizontal", "data-dragging": dragId ? 'true' : undefined, children: shown.map((tab) => {
444
+ const selected = tab.id === activeId;
445
+ const hidden = layout.hidden.has(tab.id);
446
+ const stretch = layout.stretch.get(tab.id);
447
+ const canClose = closableOf(tab);
448
+ const menuOpen = menuTab?.id === tab.id;
449
+ const dragging = dragId === tab.id;
450
+ return (_jsxs("div", { ref: (el) => {
451
+ if (el)
452
+ tabEls.current.set(tab.id, el);
453
+ else
454
+ tabEls.current.delete(tab.id);
455
+ }, ...tab.attrs, "data-slot": "tab", className: cn('group/tab flex h-7 flex-none cursor-default items-center gap-1.5 rounded-[7px] text-[13px] whitespace-nowrap text-fg-muted', 'transition-[transform,background-color] duration-200 ease-[cubic-bezier(0.165,0.84,0.44,1)] focus-visible:-outline-offset-2 motion-reduce:transition-none', tab.fixed ? 'px-3' : 'min-w-[var(--tab-floor)] pr-1 pl-[9px]',
456
+ // Out of the row, still measured: one of the two positions only (the later rule would win).
457
+ hidden ? 'pointer-events-none invisible absolute top-0 left-0 w-max' : 'relative', selected ? cn('bg-surface font-medium text-fg', LIFTED) : !dragId && 'hover:bg-hover', menuOpen && !selected && 'bg-hover', dragging && cn('z-[2] shadow-[0_0_0_0.5px_var(--color-line-strong),0_2px_6px_rgb(0_0_0/0.12)]', selected ? 'bg-surface' : 'bg-canvas')), role: "tab", "aria-selected": selected, "aria-hidden": hidden || undefined, tabIndex: selected && !hidden ? 0 : -1, title: tab.tooltip ?? tab.title, "data-hidden": hidden ? 'true' : undefined, "data-clipped": layout.clipped.has(tab.id) ? 'true' : undefined, "data-marked": tab.marked ? 'true' : undefined, "data-dragging": dragging ? 'true' : undefined, "data-menu-open": menuOpen ? 'true' : undefined, style: stretch ? { '--tab-stretch': `${stretch}px` } : undefined, onPointerDown: (event) => onPointerDown(event, tab), onClick: () => onSelect(tab.id), onMouseDown: (event) => event.button === 1 && event.preventDefault(), onAuxClick: (event) => {
458
+ if (event.button !== 1 || !canClose)
459
+ return;
460
+ event.preventDefault();
461
+ close([tab.id]);
462
+ }, onContextMenu: (event) => {
463
+ event.preventDefault();
464
+ if (!drag.current?.started)
465
+ openMenu(tab);
466
+ }, onKeyDown: (event) => onTabKey(event, tab), children: [tab.icon && (_jsx("span", { className: "flex flex-none", "aria-hidden": "true", children: tab.icon })), _jsx("span", { "data-slot": "tab-title", className: cn('max-w-[calc(var(--tab-title-max)+var(--tab-stretch,0px))] min-w-0 flex-initial overflow-hidden', layout.clipped.has(tab.id) && '[mask-image:linear-gradient(to_right,#000_calc(100%-0.875rem),transparent)]'), children: tab.title }), tab.extra, canClose && (_jsxs("span", { className: "ml-auto grid size-5 flex-none place-items-center *:[grid-area:1/1]", children: [tab.marked && (_jsx("span", { "data-testid": "unsaved", className: cn('size-1.5 rounded-full bg-fg-subtle group-hover/tab:opacity-0 group-focus-visible/tab:opacity-0', menuOpen && 'opacity-0'), title: tab.markLabel, "aria-label": tab.markLabel })), _jsx("button", { type: "button", "data-slot": "tab-close", className: cn('grid size-5 place-items-center rounded-[5px] border-0 bg-transparent p-0 text-fg-muted hover:bg-hover', menuOpen || (selected && !tab.marked) ? 'opacity-100' : 'opacity-0 group-hover/tab:opacity-100 group-focus-visible/tab:opacity-100', dragId && 'pointer-events-none'), tabIndex: -1, "aria-label": words.close(tab.title), title: words.close(tab.title), onClick: (event) => {
467
+ event.stopPropagation();
468
+ close([tab.id]);
469
+ }, children: _jsx(X, { className: "size-3.5", strokeWidth: 2 }) })] }))] }, tab.id));
470
+ }) }), _jsxs(BaseMenu.Root, { open: popup?.kind === 'list', onOpenChange: openChange('list'), children: [_jsx(BaseMenu.Trigger, { ref: moreRef, className: cn(ROW_BUTTON, listed.length === 0 && 'hidden'), "data-testid": "tab-more", "aria-label": words.moreTabs, title: words.moreTabs, children: _jsx(ChevronDown, { className: "size-3.5" }) }), _jsx(BaseMenu.Portal, { children: _jsx(BaseMenu.Positioner, { side: "bottom", align: "end", sideOffset: 4, className: "z-[1000]", children: _jsx(BaseMenu.Popup, { className: cn(POPUP_CLASS, SCROLLING_POPUP, 'max-h-[min(420px,var(--available-height))] w-[300px] select-none'), "data-testid": "tab-list", "aria-label": words.moreTabs, children: listed.map((tab) => (_jsxs(BaseMenu.Item, { closeOnClick: false, className: "group/row flex min-h-10 cursor-default items-center gap-2.5 rounded-md py-1 pr-1 pl-2.5 outline-none data-[highlighted]:bg-hover", "data-tab-row": tab.id, "data-marked": tab.marked ? 'true' : undefined, title: tab.tooltip ?? tab.title, onClick: (event) => {
471
+ if (event.target instanceof Element && event.target.closest('[data-slot=tab-close]'))
472
+ return;
473
+ pick(tab);
474
+ }, onMouseDown: (event) => event.button === 1 && event.preventDefault(), onAuxClick: (event) => {
475
+ if (event.button !== 1 || !closableOf(tab))
476
+ return;
477
+ event.preventDefault();
478
+ close([tab.id]);
479
+ }, onKeyDown: (event) => {
480
+ if ((event.key === 'Delete' || event.key === 'Backspace') && closableOf(tab)) {
481
+ event.preventDefault();
482
+ close([tab.id]);
483
+ }
484
+ }, children: [tab.icon && (_jsx("span", { className: "flex flex-none", "aria-hidden": "true", children: tab.icon })), _jsxs("span", { className: "flex min-w-0 flex-1 flex-col", children: [_jsx("span", { className: "truncate", children: tab.title }), tab.detail && _jsx("span", { className: "truncate text-xs text-fg-subtle", children: tab.detail })] }), tab.marked && _jsx("span", { className: "size-1.5 flex-none rounded-full bg-fg-subtle", title: tab.markLabel, "aria-label": tab.markLabel }), closableOf(tab) && (_jsx("button", { type: "button", "data-slot": "tab-close", className: "grid size-6 flex-none place-items-center rounded-[5px] border-0 bg-transparent p-0 text-fg-muted opacity-0 group-hover/row:opacity-100 group-data-[highlighted]/row:opacity-100 hover:bg-hover", tabIndex: -1, "aria-label": words.close(tab.title), title: words.close(tab.title), onClick: (event) => {
485
+ event.stopPropagation();
486
+ close([tab.id]);
487
+ }, children: _jsx(X, { className: "size-3.5", strokeWidth: 2 }) }))] }, tab.id))) }) }) })] }), newMenu ? (_jsxs(BaseMenu.Root, { open: popup?.kind === 'new', onOpenChange: openChange('new'), children: [_jsx(BaseMenu.Trigger, { ref: newRef, className: ROW_BUTTON, "data-testid": newTestId, "aria-label": words.newTab, title: words.newTab, children: _jsx(Plus, { className: "size-4" }) }), _jsx(BaseMenu.Portal, { children: _jsx(BaseMenu.Positioner, { side: "bottom", align: "start", sideOffset: 4, className: "z-[1000]", children: _jsx(BaseMenu.Popup, { className: cn(POPUP_CLASS, SCROLLING_POPUP, 'max-h-[min(520px,var(--available-height))] w-60'), "data-testid": "tab-new-menu", "aria-label": words.newTab, children: _jsx(MenuLines, { entries: newMenu }) }) }) })] })) : (onNew && (_jsx("button", { type: "button", ref: newRef, className: ROW_BUTTON, "data-testid": newTestId, "aria-label": words.newTab, title: words.newTab, onClick: onNew, children: _jsx(Plus, { className: "size-4" }) }))), _jsx(BaseMenu.Root, { open: !!menuTab, onOpenChange: (open) => !open && setPopup((was) => (was?.kind === 'tab' ? null : was)), children: _jsx(BaseMenu.Portal, { children: _jsx(BaseMenu.Positioner, { anchor: () => (menuTab ? (tabEls.current.get(menuTab.id) ?? null) : null), side: "bottom", align: "start", sideOffset: 4, className: "z-[1000]", children: _jsx(BaseMenu.Popup, { className: cn(POPUP_CLASS, 'w-56'), "data-testid": "tab-menu", "data-tab-menu": menuTab?.id, "aria-label": menuTab ? words.menu(menuTab.title) : undefined, finalFocus: () => (menuTab ? (tabEls.current.get(menuTab.id) ?? null) : null), children: menuTab && _jsx(MenuLines, { entries: menuEntries(menuTab) }) }) }) }) })] }));
488
+ }
489
+ /** A menu's lines: its items, rules and group names. */
490
+ function MenuLines({ entries }) {
491
+ return entries.map((item, i) => {
492
+ if (item === 'separator')
493
+ return _jsx(MenuSeparator, {}, `rule-${i}`);
494
+ if ('heading' in item)
495
+ return _jsx(MenuLabel, { children: item.heading }, `heading-${i}`);
496
+ return (_jsxs(BaseMenu.Item, { className: MENU_ITEM_CLASS, "data-testid": item.testId, ...item.attrs, disabled: item.disabled, title: item.detail ? `${item.label} — ${item.detail}` : undefined, onClick: () => item.onSelect(), children: [item.icon && (_jsx("span", { className: "flex size-4 flex-none items-center justify-center", "aria-hidden": "true", children: item.icon })), _jsx("span", { className: "min-w-0 flex-1 truncate", children: item.label })] }, item.key));
497
+ });
498
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Multi-line text: `TextArea`, a box that can grow with its text, and
3
+ * `FieldTextArea`, the same with a label, a hint and an error under it.
4
+ */
5
+ import { type ReactNode, type TextareaHTMLAttributes } from 'react';
6
+ /** Props of {@link TextArea}. */
7
+ export interface TextAreaProps extends TextareaHTMLAttributes<HTMLTextAreaElement> {
8
+ /** Lines it shows; with `autosize`, the fewest it shows. */
9
+ rows?: number;
10
+ /** Whether the person can drag its height (default: `y`, `none` when it grows by itself). */
11
+ resize?: 'none' | 'y';
12
+ /** Grows with its text, from `rows` lines up to its `max-height`, then scrolls. */
13
+ autosize?: boolean;
14
+ size?: 'xs' | 'sm' | 'md';
15
+ /** Takes the width it is given (default); `false`: as wide as its `cols`. */
16
+ fullWidth?: boolean;
17
+ /** No box: no border, fill or padding. */
18
+ bare?: boolean;
19
+ }
20
+ /** A multi-line text box. */
21
+ export declare const TextArea: import("react").ForwardRefExoticComponent<TextAreaProps & import("react").RefAttributes<HTMLTextAreaElement>>;
22
+ /** Props of {@link FieldTextArea}. */
23
+ export interface FieldTextAreaProps extends TextAreaProps {
24
+ label: ReactNode;
25
+ /** A line under the label that says more. */
26
+ hint?: ReactNode;
27
+ /** What is wrong with the text; marks the field invalid. */
28
+ error?: ReactNode;
29
+ /** Keeps room for this many lines of error even when there is none, so nothing moves when one shows. */
30
+ reserveErrorLines?: number;
31
+ /** Something is running on the text: the field waits, and takes the focus back after if it had it. */
32
+ busy?: boolean;
33
+ }
34
+ /** A labelled multi-line text box with an optional hint and error. */
35
+ export declare const FieldTextArea: import("react").ForwardRefExoticComponent<FieldTextAreaProps & import("react").RefAttributes<HTMLTextAreaElement>>;