@human-kit/ui 1.0.0-beta.0 → 1.0.0-beta.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 (157) hide show
  1. package/README.md +2 -0
  2. package/dist/FOCUS_STATE_CONTRACT.md +1 -0
  3. package/dist/accordion/root/accordion-root.svelte +15 -3
  4. package/dist/accordion/root/context.svelte.js +4 -4
  5. package/dist/accordion/types.d.ts +9 -2
  6. package/dist/autocomplete/list/autocomplete-list.svelte.d.ts +6 -0
  7. package/dist/calendar/root/context.svelte.js +8 -2
  8. package/dist/checkbox/root/checkbox-root.svelte +28 -11
  9. package/dist/checkbox/root/checkbox-root.svelte.d.ts +13 -0
  10. package/dist/clock/root/create-time-selection-state.svelte.d.ts +5 -0
  11. package/dist/clock/root/create-time-selection-state.svelte.js +113 -10
  12. package/dist/clock/root/wheel-options.d.ts +7 -3
  13. package/dist/clock/root/wheel-options.js +7 -15
  14. package/dist/clock/wheel-column/clock-wheel-column.svelte +10 -2
  15. package/dist/clock/wheel-column/clock-wheel-column.svelte.d.ts +2 -1
  16. package/dist/collapsible/root/collapsible-root.svelte +9 -4
  17. package/dist/collapsible/types.d.ts +9 -0
  18. package/dist/combobox/input/combobox-input.svelte +12 -1
  19. package/dist/combobox/list/combobox-listbox.svelte +93 -1
  20. package/dist/combobox/list/combobox-listbox.svelte.d.ts +20 -0
  21. package/dist/combobox/root/combobox.svelte +118 -45
  22. package/dist/combobox/root/combobox.svelte.d.ts +11 -0
  23. package/dist/combobox/root/context.d.ts +12 -0
  24. package/dist/combobox/status/README.md +2 -2
  25. package/dist/combobox/tag-remove/combobox-tag-remove.svelte +14 -2
  26. package/dist/combobox/tag-remove/combobox-tag-remove.svelte.d.ts +7 -0
  27. package/dist/combobox/tags/combobox-tags.svelte +5 -2
  28. package/dist/combobox/tags/combobox-tags.svelte.d.ts +2 -0
  29. package/dist/combobox/trigger/combobox-trigger.svelte +4 -1
  30. package/dist/datepicker/README.md +3 -2
  31. package/dist/datepicker/root/date-picker-root.svelte +38 -2
  32. package/dist/datepicker/root/date-picker-root.svelte.d.ts +2 -0
  33. package/dist/datepicker/root/draft-evaluation.d.ts +7 -0
  34. package/dist/datepicker/root/draft-evaluation.js +2 -2
  35. package/dist/daterangepicker/root/date-range-picker-root.svelte +19 -1
  36. package/dist/daterangepicker/root/date-range-picker-root.svelte.d.ts +2 -0
  37. package/dist/dialog/README.md +9 -1
  38. package/dist/dialog/close/dialog-close.svelte +57 -0
  39. package/dist/dialog/close/dialog-close.svelte.d.ts +19 -0
  40. package/dist/dialog/content/dialog-content.svelte +2 -0
  41. package/dist/dialog/description/dialog-description.svelte +46 -0
  42. package/dist/dialog/description/dialog-description.svelte.d.ts +20 -0
  43. package/dist/dialog/index.d.ts +9 -0
  44. package/dist/dialog/index.js +3 -0
  45. package/dist/dialog/index.parts.d.ts +3 -0
  46. package/dist/dialog/index.parts.js +3 -0
  47. package/dist/dialog/root/context.d.ts +8 -0
  48. package/dist/dialog/root/dialog-root.svelte +58 -16
  49. package/dist/dialog/root/dialog-root.svelte.d.ts +10 -3
  50. package/dist/dialog/root/dialog-stack.d.ts +6 -22
  51. package/dist/dialog/root/dialog-stack.js +4 -33
  52. package/dist/dialog/title/dialog-title.svelte +43 -0
  53. package/dist/dialog/title/dialog-title.svelte.d.ts +21 -0
  54. package/dist/drawer/README.md +111 -0
  55. package/dist/drawer/body/drawer-body.svelte +47 -0
  56. package/dist/drawer/body/drawer-body.svelte.d.ts +32 -0
  57. package/dist/drawer/close/drawer-close.svelte +52 -0
  58. package/dist/drawer/close/drawer-close.svelte.d.ts +20 -0
  59. package/dist/drawer/content/drawer-content.svelte +597 -0
  60. package/dist/drawer/content/drawer-content.svelte.d.ts +28 -0
  61. package/dist/drawer/description/drawer-description.svelte +40 -0
  62. package/dist/drawer/description/drawer-description.svelte.d.ts +20 -0
  63. package/dist/drawer/indent/drawer-indent-background.svelte +36 -0
  64. package/dist/drawer/indent/drawer-indent-background.svelte.d.ts +17 -0
  65. package/dist/drawer/indent/drawer-indent.svelte +59 -0
  66. package/dist/drawer/indent/drawer-indent.svelte.d.ts +32 -0
  67. package/dist/drawer/index.d.ts +49 -0
  68. package/dist/drawer/index.js +25 -0
  69. package/dist/drawer/index.parts.d.ts +14 -0
  70. package/dist/drawer/index.parts.js +15 -0
  71. package/dist/drawer/overlay/drawer-overlay.svelte +67 -0
  72. package/dist/drawer/overlay/drawer-overlay.svelte.d.ts +12 -0
  73. package/dist/drawer/portal/drawer-portal.svelte +57 -0
  74. package/dist/drawer/portal/drawer-portal.svelte.d.ts +13 -0
  75. package/dist/drawer/root/context.d.ts +81 -0
  76. package/dist/drawer/root/context.js +20 -0
  77. package/dist/drawer/root/drawer-root.svelte +333 -0
  78. package/dist/drawer/root/drawer-root.svelte.d.ts +77 -0
  79. package/dist/drawer/root/drawer-stack.svelte.d.ts +73 -0
  80. package/dist/drawer/root/drawer-stack.svelte.js +113 -0
  81. package/dist/drawer/root/handle.svelte.d.ts +39 -0
  82. package/dist/drawer/root/handle.svelte.js +64 -0
  83. package/dist/drawer/root/keyboard-context.d.ts +13 -0
  84. package/dist/drawer/root/keyboard-context.js +8 -0
  85. package/dist/drawer/root/presence-context.d.ts +22 -0
  86. package/dist/drawer/root/presence-context.js +8 -0
  87. package/dist/drawer/root/snap-points.d.ts +68 -0
  88. package/dist/drawer/root/snap-points.js +120 -0
  89. package/dist/drawer/root/starting-style.svelte.d.ts +18 -0
  90. package/dist/drawer/root/starting-style.svelte.js +38 -0
  91. package/dist/drawer/root/types.d.ts +36 -0
  92. package/dist/drawer/root/types.js +1 -0
  93. package/dist/drawer/root/viewport-context.d.ts +2 -0
  94. package/dist/drawer/root/viewport-context.js +16 -0
  95. package/dist/drawer/swipe-area/drawer-swipe-area.svelte +143 -0
  96. package/dist/drawer/swipe-area/drawer-swipe-area.svelte.d.ts +22 -0
  97. package/dist/drawer/title/drawer-title.svelte +37 -0
  98. package/dist/drawer/title/drawer-title.svelte.d.ts +21 -0
  99. package/dist/drawer/trigger/drawer-trigger.svelte +90 -0
  100. package/dist/drawer/trigger/drawer-trigger.svelte.d.ts +41 -0
  101. package/dist/drawer/viewport/drawer-viewport.svelte +69 -0
  102. package/dist/drawer/viewport/drawer-viewport.svelte.d.ts +15 -0
  103. package/dist/drawer/virtual-keyboard/drawer-virtual-keyboard-provider.svelte +88 -0
  104. package/dist/drawer/virtual-keyboard/drawer-virtual-keyboard-provider.svelte.d.ts +29 -0
  105. package/dist/hooks/use-virtual-focus.svelte.d.ts +9 -0
  106. package/dist/hooks/use-virtual-focus.svelte.js +7 -1
  107. package/dist/index.d.ts +2 -0
  108. package/dist/index.js +2 -0
  109. package/dist/listbox/item/listbox-item.svelte +8 -1
  110. package/dist/listbox/root/context.d.ts +30 -0
  111. package/dist/listbox/root/context.js +10 -0
  112. package/dist/listbox/root/listbox.svelte +291 -8
  113. package/dist/listbox/root/listbox.svelte.d.ts +35 -2
  114. package/dist/menu/content/menu-content.svelte +3 -1
  115. package/dist/menu/overlay/menu-overlay.svelte +1 -1
  116. package/dist/numberfield/root/number-field-root.svelte +2 -1
  117. package/dist/popover/content/popover-content.svelte +1 -1
  118. package/dist/popover/overlay/popover-overlay.svelte +1 -1
  119. package/dist/popover/root/popover-root.svelte +23 -11
  120. package/dist/popover/root/popover-root.svelte.d.ts +10 -2
  121. package/dist/primitives/floating.d.ts +7 -0
  122. package/dist/primitives/floating.js +6 -2
  123. package/dist/primitives/index.d.ts +1 -0
  124. package/dist/primitives/index.js +1 -0
  125. package/dist/primitives/keyboard-navigation.js +3 -1
  126. package/dist/primitives/layer-stack.d.ts +29 -0
  127. package/dist/primitives/layer-stack.js +35 -0
  128. package/dist/primitives/list-virtualizer.d.ts +75 -0
  129. package/dist/primitives/list-virtualizer.js +59 -0
  130. package/dist/primitives/scroll-lock.d.ts +23 -3
  131. package/dist/primitives/scroll-lock.js +122 -9
  132. package/dist/primitives/swipe-gesture.d.ts +126 -0
  133. package/dist/primitives/swipe-gesture.js +351 -0
  134. package/dist/primitives/text.d.ts +8 -0
  135. package/dist/primitives/text.js +13 -0
  136. package/dist/switch/root/switch-root.svelte +23 -9
  137. package/dist/switch/root/switch-root.svelte.d.ts +9 -2
  138. package/dist/table/body/table-body.svelte +84 -9
  139. package/dist/table/cell/table-cell.svelte +1 -3
  140. package/dist/table/column/table-column.svelte +5 -1
  141. package/dist/table/column-resizer/table-column-resizer.svelte +1 -5
  142. package/dist/table/interactive-cell/table-interactive-cell.svelte +1 -3
  143. package/dist/table/root/context.svelte.js +99 -49
  144. package/dist/table/row/table-row.svelte +7 -7
  145. package/dist/table/sort-trigger/table-sort-trigger.svelte +5 -1
  146. package/dist/table/types.d.ts +6 -0
  147. package/dist/tabs/root/tabs-root.svelte +20 -6
  148. package/dist/tabs/types.d.ts +9 -0
  149. package/dist/timepicker/root/time-picker-root.svelte +11 -0
  150. package/dist/timepicker/root/time-picker-root.svelte.d.ts +2 -0
  151. package/dist/toggle-group/root/context.svelte.js +11 -4
  152. package/dist/toggle-group/root/toggle-group-root.svelte +19 -4
  153. package/dist/toggle-group/types.d.ts +9 -0
  154. package/dist/tree/root/context.svelte.js +6 -0
  155. package/dist/tree/root/tree-root.svelte +22 -8
  156. package/dist/tree/types.d.ts +13 -0
  157. package/package.json +227 -224
@@ -0,0 +1,351 @@
1
+ /**
2
+ * Single-axis pointer drag, for panels that follow the finger and settle on
3
+ * release (drawers, sheets, swipe-to-open edges).
4
+ *
5
+ * The action deliberately owns as little policy as possible: it reports a signed
6
+ * displacement and a release velocity, and leaves clamping, snapping and
7
+ * dismissal thresholds to the caller. Everything that can be decided without a
8
+ * DOM is exported as a pure function so it can be unit-tested directly.
9
+ *
10
+ * Two behaviours are worth calling out, because they are what separates a usable
11
+ * sheet from an infuriating one:
12
+ *
13
+ * 1. The gesture is CLAIMED, not assumed. Nothing happens until the finger has
14
+ * moved past a small threshold, mostly along the axis, and no scrollable
15
+ * region between the touch and the panel could have consumed that movement.
16
+ * Without the last check, a bottom sheet with a scrolling body drags itself
17
+ * off screen the moment you try to scroll it.
18
+ * 2. `touch-action` is left alone by default. Setting `none` on the panel would
19
+ * kill native scrolling for every descendant — `touch-action` intersects down
20
+ * the ancestor chain, so a child cannot opt back in. Horizontal drawers can
21
+ * safely pass `pan-y` (children keep scrolling vertically, we take the
22
+ * horizontal axis); vertical ones rely on `preventDefault` once claimed.
23
+ */
24
+ /** Axis a panel anchored to `side` travels along. */
25
+ export function getSwipeAxis(side) {
26
+ return side === 'left' || side === 'right' ? 'x' : 'y';
27
+ }
28
+ /**
29
+ * Sign of the OUTWARD direction along the axis — the way the panel moves to leave
30
+ * the screen. A bottom drawer exits downward (+y), a left drawer leftward (-x).
31
+ * Displacements reported by the action are already multiplied by this, so
32
+ * "positive means closer to dismissed" holds for every side.
33
+ */
34
+ export function getOutwardSign(side) {
35
+ return side === 'bottom' || side === 'right' ? 1 : -1;
36
+ }
37
+ /** Window (ms) over which the release velocity is measured. */
38
+ export const RELEASE_VELOCITY_WINDOW_MS = 100;
39
+ /**
40
+ * Release velocity in px/ms (signed, positive = outward) over the trailing
41
+ * `windowMs` before `now`.
42
+ *
43
+ * Returns `0` when there is no recent movement — no samples, or the newest one is
44
+ * already older than the window, which is what a finger held still before lifting
45
+ * looks like. Samples sharing a timestamp with a non-zero displacement yield
46
+ * `±Infinity`; callers treat that as a flick.
47
+ *
48
+ * (The Clock wheel has a sibling of this function measuring `scrollTop` instead;
49
+ * they are kept separate so gesture work cannot regress the picker.)
50
+ */
51
+ export function getReleaseVelocity(samples, now, windowMs = RELEASE_VELOCITY_WINDOW_MS) {
52
+ if (samples.length === 0)
53
+ return 0;
54
+ const newest = samples[samples.length - 1];
55
+ if (now - newest.time > windowMs)
56
+ return 0;
57
+ // Oldest sample still inside the window.
58
+ let referenceIndex = samples.length - 1;
59
+ while (referenceIndex > 0 && now - samples[referenceIndex - 1].time <= windowMs) {
60
+ referenceIndex -= 1;
61
+ }
62
+ // Only reach OUTSIDE the window when the newest sample is alone in it and
63
+ // there is nothing to measure against. Stepping back unconditionally (as the
64
+ // Clock's sibling does) lets one stale buffered sample stretch the elapsed
65
+ // time and flatten a genuine flick to a crawl.
66
+ if (referenceIndex === samples.length - 1 && referenceIndex > 0) {
67
+ referenceIndex -= 1;
68
+ }
69
+ const reference = samples[referenceIndex];
70
+ const distance = newest.position - reference.position;
71
+ const elapsed = newest.time - reference.time;
72
+ if (elapsed <= 0) {
73
+ if (distance === 0)
74
+ return 0;
75
+ return distance > 0 ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY;
76
+ }
77
+ return distance / elapsed;
78
+ }
79
+ /** How far (ms of travel) a flick is projected past the release point. */
80
+ export const RELEASE_MOMENTUM_MS = 180;
81
+ /**
82
+ * Where the panel would come to rest if the flick kept its speed for
83
+ * `momentumMs`. Used to pick a snap point that respects intent: a short but fast
84
+ * flick should skip ahead, a long slow drag should not.
85
+ *
86
+ * Infinite velocities (samples with identical timestamps) project to the
87
+ * corresponding infinity, which callers clamp against their own bounds.
88
+ */
89
+ export function projectRelease(position, velocity, momentumMs = RELEASE_MOMENTUM_MS) {
90
+ if (!Number.isFinite(velocity))
91
+ return velocity > 0 ? Infinity : -Infinity;
92
+ return position + velocity * momentumMs;
93
+ }
94
+ /**
95
+ * Diminishing-returns resistance for dragging past a bound, so the panel keeps
96
+ * moving (the gesture stays alive) without following the finger 1:1.
97
+ *
98
+ * Asymptotic: no matter how far the finger travels, the result stays below
99
+ * `dimension`. `factor` is the initial stiffness — lower is stiffer.
100
+ */
101
+ export function applyRubberBand(overshoot, dimension, factor = 0.55) {
102
+ if (overshoot <= 0)
103
+ return 0;
104
+ if (dimension <= 0)
105
+ return 0;
106
+ return (1 - 1 / ((overshoot * factor) / dimension + 1)) * dimension;
107
+ }
108
+ /**
109
+ * Marks a subtree as not starting a swipe (sliders, carousels, drag handles).
110
+ *
111
+ * Valueless — or any value other than `"mouse"` — opts the subtree out for every
112
+ * input type. `"mouse"` opts it out for a mouse only, which is how a region can
113
+ * stay swipeable under a finger while a mouse drag inside it selects text: on
114
+ * touch there is no drag-to-select to protect, so the two never compete.
115
+ */
116
+ export const SWIPE_IGNORE_ATTRIBUTE = 'data-hk-swipe-ignore';
117
+ /** Whether `target` sits in a subtree that opts out of swipes for `pointerType`. */
118
+ function isSwipeIgnored(target, pointerType) {
119
+ const ignored = target.closest(`[${SWIPE_IGNORE_ATTRIBUTE}]`);
120
+ if (!ignored)
121
+ return false;
122
+ if (ignored.getAttribute(SWIPE_IGNORE_ATTRIBUTE) === 'mouse')
123
+ return pointerType === 'mouse';
124
+ return true;
125
+ }
126
+ function isScrollableOn(element, axis) {
127
+ const style = getComputedStyle(element);
128
+ const overflow = axis === 'y' ? style.overflowY : style.overflowX;
129
+ return /(auto|scroll|overlay)/.test(overflow);
130
+ }
131
+ /**
132
+ * Whether a scrollable region between `target` and `node` (inclusive) could
133
+ * absorb a drag of `delta` px along `axis`, and so should keep the gesture.
134
+ *
135
+ * `delta` is raw finger movement, not outward displacement: a finger moving down
136
+ * scrolls the content up, which only a scroller with room above can do.
137
+ */
138
+ function scrollerCanConsume(target, node, axis, delta) {
139
+ let element = target;
140
+ while (element) {
141
+ if (element instanceof HTMLElement && isScrollableOn(element, axis)) {
142
+ if (axis === 'y') {
143
+ const max = element.scrollHeight - element.clientHeight;
144
+ if (max > 0) {
145
+ // Sub-pixel scroll offsets are common on zoomed/HiDPI viewports;
146
+ // the 1px slack keeps "at the bound" from flickering.
147
+ if (delta > 0 && element.scrollTop > 0)
148
+ return true;
149
+ if (delta < 0 && element.scrollTop < max - 1)
150
+ return true;
151
+ }
152
+ }
153
+ else {
154
+ const max = element.scrollWidth - element.clientWidth;
155
+ if (max > 0) {
156
+ if (delta > 0 && element.scrollLeft > 0)
157
+ return true;
158
+ if (delta < 0 && element.scrollLeft < max - 1)
159
+ return true;
160
+ }
161
+ }
162
+ }
163
+ if (element === node)
164
+ break;
165
+ element = element.parentElement;
166
+ }
167
+ return false;
168
+ }
169
+ /**
170
+ * Svelte action wiring a single-axis drag on `node`.
171
+ *
172
+ * @example
173
+ * ```svelte
174
+ * <div use:swipeGesture={{ side: 'bottom', onMove: ({ displacement }) => (offset = displacement) }}>
175
+ * ```
176
+ */
177
+ export function swipeGesture(node, options) {
178
+ let current = options;
179
+ let pointerId = null;
180
+ let startX = 0;
181
+ let startY = 0;
182
+ let startTarget = null;
183
+ let claimed = false;
184
+ let samples = [];
185
+ let suppressClickTimer = null;
186
+ let suppressClickListener = null;
187
+ function axis() {
188
+ return getSwipeAxis(current.side);
189
+ }
190
+ function applyTouchAction() {
191
+ const value = current.touchAction ?? null;
192
+ if (value === null) {
193
+ node.style.removeProperty('touch-action');
194
+ return;
195
+ }
196
+ node.style.touchAction = value;
197
+ }
198
+ /**
199
+ * Removes the pending click suppression, if any.
200
+ *
201
+ * Must also run on `destroy`: the timeout below normally lifts the listener a
202
+ * macrotask later, but a gesture that ends by unmounting the node never gets
203
+ * there — and a capture-phase listener left on `window` goes on to swallow the
204
+ * next click anywhere on the page.
205
+ */
206
+ function clearClickSuppression() {
207
+ if (suppressClickTimer !== null) {
208
+ clearTimeout(suppressClickTimer);
209
+ suppressClickTimer = null;
210
+ }
211
+ if (suppressClickListener) {
212
+ window.removeEventListener('click', suppressClickListener, true);
213
+ suppressClickListener = null;
214
+ }
215
+ }
216
+ /**
217
+ * A claimed drag that started on a button must not also activate it on release.
218
+ * One capture-phase listener, lifted on the next macrotask in case no click ever
219
+ * follows (a drag that ended outside the element it started on).
220
+ */
221
+ function suppressNextClick() {
222
+ clearClickSuppression();
223
+ suppressClickListener = (event) => {
224
+ event.preventDefault();
225
+ event.stopPropagation();
226
+ clearClickSuppression();
227
+ };
228
+ window.addEventListener('click', suppressClickListener, true);
229
+ suppressClickTimer = setTimeout(clearClickSuppression, 0);
230
+ }
231
+ function detachWindowListeners() {
232
+ window.removeEventListener('pointermove', handleMove);
233
+ window.removeEventListener('pointerup', handleUp);
234
+ window.removeEventListener('pointercancel', handleCancel);
235
+ }
236
+ function reset() {
237
+ if (claimed && pointerId !== null && node.hasPointerCapture?.(pointerId)) {
238
+ node.releasePointerCapture(pointerId);
239
+ }
240
+ detachWindowListeners();
241
+ pointerId = null;
242
+ startTarget = null;
243
+ claimed = false;
244
+ samples = [];
245
+ }
246
+ function handleDown(event) {
247
+ if (current.enabled === false)
248
+ return;
249
+ // A gesture is already in flight (second finger, or a stale capture).
250
+ if (pointerId !== null)
251
+ return;
252
+ if (event.pointerType === 'mouse' && event.button !== 0)
253
+ return;
254
+ if (!(event.target instanceof Element))
255
+ return;
256
+ if (isSwipeIgnored(event.target, event.pointerType))
257
+ return;
258
+ pointerId = event.pointerId;
259
+ startX = event.clientX;
260
+ startY = event.clientY;
261
+ startTarget = event.target;
262
+ claimed = false;
263
+ samples = [{ time: performance.now(), position: 0 }];
264
+ window.addEventListener('pointermove', handleMove, { passive: false });
265
+ window.addEventListener('pointerup', handleUp);
266
+ window.addEventListener('pointercancel', handleCancel);
267
+ }
268
+ function handleMove(event) {
269
+ if (pointerId === null || event.pointerId !== pointerId)
270
+ return;
271
+ const currentAxis = axis();
272
+ const deltaX = event.clientX - startX;
273
+ const deltaY = event.clientY - startY;
274
+ const main = currentAxis === 'x' ? deltaX : deltaY;
275
+ const cross = currentAxis === 'x' ? deltaY : deltaX;
276
+ if (!claimed) {
277
+ const threshold = current.threshold ?? 4;
278
+ if (Math.abs(main) < threshold && Math.abs(cross) < threshold)
279
+ return;
280
+ // Mostly a cross-axis movement: this is someone else's gesture.
281
+ if (Math.abs(cross) > Math.abs(main)) {
282
+ reset();
283
+ return;
284
+ }
285
+ if (startTarget && scrollerCanConsume(startTarget, node, currentAxis, main)) {
286
+ reset();
287
+ return;
288
+ }
289
+ claimed = true;
290
+ try {
291
+ node.setPointerCapture(pointerId);
292
+ }
293
+ catch {
294
+ // Capture is an optimisation (it keeps events flowing when the finger
295
+ // leaves the panel); the window listeners work without it.
296
+ }
297
+ current.onStart?.(event);
298
+ }
299
+ const displacement = main * getOutwardSign(current.side);
300
+ samples.push({ time: performance.now(), position: displacement });
301
+ // Keep only what the release-velocity window can still use as a baseline.
302
+ while (samples.length > 2 && performance.now() - samples[1].time > RELEASE_VELOCITY_WINDOW_MS) {
303
+ samples.shift();
304
+ }
305
+ // Stops the browser from turning the claimed drag into a scroll or a
306
+ // text selection. Non-cancelable once a native scroll has begun, which is
307
+ // exactly when we should not be dragging anyway.
308
+ if (event.cancelable)
309
+ event.preventDefault();
310
+ current.onMove?.({ displacement, event });
311
+ }
312
+ function handleUp(event) {
313
+ if (pointerId === null || event.pointerId !== pointerId)
314
+ return;
315
+ const wasClaimed = claimed;
316
+ const displacement = (axis() === 'x' ? event.clientX - startX : event.clientY - startY) *
317
+ getOutwardSign(current.side);
318
+ const velocity = getReleaseVelocity(samples, performance.now());
319
+ reset();
320
+ if (!wasClaimed)
321
+ return;
322
+ suppressNextClick();
323
+ current.onEnd?.({ displacement, velocity, event });
324
+ }
325
+ function handleCancel(event) {
326
+ if (pointerId === null || event.pointerId !== pointerId)
327
+ return;
328
+ const wasClaimed = claimed;
329
+ reset();
330
+ if (wasClaimed)
331
+ current.onCancel?.();
332
+ }
333
+ node.addEventListener('pointerdown', handleDown);
334
+ applyTouchAction();
335
+ return {
336
+ update(newOptions) {
337
+ const touchActionChanged = (newOptions.touchAction ?? null) !== (current.touchAction ?? null);
338
+ current = newOptions;
339
+ if (touchActionChanged)
340
+ applyTouchAction();
341
+ // Disabling mid-gesture must not leave the pointer captured.
342
+ if (current.enabled === false && pointerId !== null)
343
+ reset();
344
+ },
345
+ destroy() {
346
+ node.removeEventListener('pointerdown', handleDown);
347
+ clearClickSuppression();
348
+ reset();
349
+ }
350
+ };
351
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Normalizes a string for searching: lower case, and without the diacritics.
3
+ *
4
+ * Decomposing first (`NFD`) splits an accented letter into the letter plus its combining
5
+ * mark, so stripping the marks leaves the plain letter — `Á` becomes `a`. Used by the
6
+ * combobox's default filter, so a list can be searched the way people type.
7
+ */
8
+ export declare function foldForSearch(value: string): string;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Normalizes a string for searching: lower case, and without the diacritics.
3
+ *
4
+ * Decomposing first (`NFD`) splits an accented letter into the letter plus its combining
5
+ * mark, so stripping the marks leaves the plain letter — `Á` becomes `a`. Used by the
6
+ * combobox's default filter, so a list can be searched the way people type.
7
+ */
8
+ export function foldForSearch(value) {
9
+ return value
10
+ .normalize('NFD')
11
+ .replace(/\p{Diacritic}/gu, '')
12
+ .toLowerCase();
13
+ }
@@ -31,10 +31,17 @@
31
31
  value?: string;
32
32
  /** Associates the hidden input with a form by id. */
33
33
  form?: string;
34
- /** Controlled checked state. Bindable. */
34
+ /** Checked state. Two-way by default — use `bind:checked`. */
35
35
  checked?: boolean;
36
- /** Initial checked state when uncontrolled. */
36
+ /** Initial checked state, for when `checked` is not supplied. */
37
37
  defaultChecked?: boolean;
38
+ /**
39
+ * Opt into fully controlled state: the component stops writing back to `checked`
40
+ * and only reports through `onCheckedChange`, so the parent can reject a change by
41
+ * not flowing the new value back down. Off by default, because `bind:checked` —
42
+ * the common case — needs the write-back to work at all.
43
+ */
44
+ controlledChecked?: boolean;
38
45
  /** Called when the user toggles the switch. */
39
46
  onCheckedChange?: (checked: boolean) => void;
40
47
  /** Removes the switch from interaction and focus order. */
@@ -87,6 +94,7 @@
87
94
  form,
88
95
  checked = $bindable(),
89
96
  defaultChecked = false,
97
+ controlledChecked = false,
90
98
  onCheckedChange,
91
99
  disabled = false,
92
100
  readonly = false,
@@ -141,14 +149,20 @@
141
149
  }
142
150
  });
143
151
 
144
- // Controlled-ness is decided once, from whether the prop was provided at init.
145
- const isCheckedControlled = untrack(() => checked) !== undefined;
146
-
147
- // In uncontrolled mode, write the initial state back so `bind:` parents see the default.
148
- if (!isCheckedControlled) {
152
+ // Controlled-ness is NOT inferred from `checked` being defined: `bind:checked={value}`
153
+ // and `checked={value}` are indistinguishable at runtime, so inferring it silently
154
+ // broke every `bind:checked` seeded with `false`. It is opt-in via `controlledChecked`.
155
+ // Write the initial state back so `bind:` parents see the resolved default. Read
156
+ // untracked: this is a deliberate one-time seed at init, not a reactive mirror.
157
+ if (!untrack(() => controlledChecked)) {
149
158
  checked = initialChecked;
150
159
  }
151
- const currentChecked = $derived(isCheckedControlled ? Boolean(checked) : checkedInternal);
160
+
161
+ // `checked` wins whenever it is supplied — that covers both `bind:checked` and a plain
162
+ // `checked={...}` — and the internal state only carries the fully uncontrolled case.
163
+ const currentChecked = $derived(
164
+ controlledChecked ? Boolean(checked) : (checked ?? checkedInternal)
165
+ );
152
166
  const currentUnchecked = $derived(!currentChecked);
153
167
 
154
168
  function clearPressState() {
@@ -159,7 +173,7 @@
159
173
  function publishChecked(nextChecked: boolean, event?: Event) {
160
174
  const previousChecked = currentChecked;
161
175
 
162
- if (!isCheckedControlled) {
176
+ if (!controlledChecked) {
163
177
  checkedInternal = nextChecked;
164
178
  checked = nextChecked;
165
179
  }
@@ -11,10 +11,17 @@ type SwitchRootProps = Omit<HTMLAttributes<HTMLSpanElement>, 'children' | 'class
11
11
  value?: string;
12
12
  /** Associates the hidden input with a form by id. */
13
13
  form?: string;
14
- /** Controlled checked state. Bindable. */
14
+ /** Checked state. Two-way by default — use `bind:checked`. */
15
15
  checked?: boolean;
16
- /** Initial checked state when uncontrolled. */
16
+ /** Initial checked state, for when `checked` is not supplied. */
17
17
  defaultChecked?: boolean;
18
+ /**
19
+ * Opt into fully controlled state: the component stops writing back to `checked`
20
+ * and only reports through `onCheckedChange`, so the parent can reject a change by
21
+ * not flowing the new value back down. Off by default, because `bind:checked` —
22
+ * the common case — needs the write-back to work at all.
23
+ */
24
+ controlledChecked?: boolean;
18
25
  /** Called when the user toggles the switch. */
19
26
  onCheckedChange?: (checked: boolean) => void;
20
27
  /** Removes the switch from interaction and focus order. */
@@ -47,11 +47,41 @@
47
47
  if (!virtualizer || !virtualizerEnabled || itemList.length === 0) return 0;
48
48
  return Math.max(1, Math.ceil(effectiveViewportHeight / virtualizer.rowHeight));
49
49
  });
50
+ /**
51
+ * Rows rendered beyond each edge of the viewport.
52
+ *
53
+ * This is a buffer against how long a row takes to render: while the main
54
+ * thread is busy building rows, the compositor keeps scrolling the layer, and
55
+ * anything past the last rendered row shows as blank.
56
+ *
57
+ * Lowering it makes every frame measurably cheaper (18 -> 8 cut resize p50 by
58
+ * 41% and fast-scroll p50 by 30%) — but that is the buffer being spent, not
59
+ * the cost being fixed, and fast scrolling then shows the blank band the
60
+ * buffer existed to hide. Do NOT lower this to buy frame time; make row
61
+ * rendering cheaper instead.
62
+ */
50
63
  const overscan = $derived.by(() => {
51
64
  if (!virtualizer || !virtualizerEnabled) return 0;
65
+
66
+ // Resizing rewrites a column width every drag frame, and each rewrite
67
+ // relayouts the whole table — a cost proportional to how many rows are
68
+ // mounted. The buffer exists to absorb *scrolling*, and the pointer cannot
69
+ // scroll while dragging a resizer, so during a drag those extra rows are
70
+ // laid out repeatedly for nothing. Measured against a control that keeps
71
+ // the buffer (5000 rows x 8 resizable columns, prod, 4x CPU throttle):
72
+ // resize p50 87.7ms -> 48.5ms, p95 109.5ms -> 70.3ms. The rows come back
73
+ // as soon as the drag ends.
74
+ if (table.resizingColumnId !== null) return 0;
75
+
52
76
  if (virtualizer.overscan !== undefined) return Math.max(0, virtualizer.overscan);
53
77
  return 18;
54
78
  });
79
+
80
+ // Plain (non-reactive) memo of the last window, used only to keep the derived
81
+ // value's identity stable across recomputations that produce the same window.
82
+ let lastVisibleRange:
83
+ { from: number; to: number; topSpacerHeight: number; bottomSpacerHeight: number } | undefined;
84
+
55
85
  const visibleRange = $derived.by(() => {
56
86
  if (!virtualizer || !virtualizerEnabled || itemList.length === 0) {
57
87
  return {
@@ -66,15 +96,60 @@
66
96
  const rowCount = itemList.length;
67
97
  const startIndex = Math.min(rowCount - 1, Math.floor(Math.max(0, scrollTop) / rowHeight));
68
98
  const endIndex = Math.min(rowCount - 1, startIndex + visibleCount - 1);
69
- const from = Math.max(0, startIndex - overscan);
70
- const to = Math.max(from, Math.min(rowCount - 1, endIndex + overscan));
71
-
72
- return {
73
- from,
74
- to,
75
- topSpacerHeight: from * rowHeight,
76
- bottomSpacerHeight: Math.max(0, (rowCount - to - 1) * rowHeight)
77
- };
99
+
100
+ // Snap the window edges to a block boundary so scrolling *within* a block
101
+ // leaves the rendered set identical.
102
+ //
103
+ // Inserting or removing a <tr> forces the browser to lay out the whole
104
+ // table, and that relayout — not the new rows themselves — is the dominant
105
+ // cost of scrolling: ~1.16ms per mounted row, ~62% of a frame that changes
106
+ // the window. Re-slicing on every single row wastes the overscan buffer,
107
+ // which exists precisely so the window need not track the scroll offset
108
+ // exactly. Quantizing spends that buffer on skipping relayouts instead.
109
+ //
110
+ // Safe by construction: `from` only ever rounds *down* and `to` only ever
111
+ // rounds *up*, so the retained margin never drops below `overscan` and this
112
+ // cannot expose a blank band the unquantized window would have covered.
113
+ //
114
+ // Block size swept against a `block = 1` control (which reproduces the
115
+ // unquantized behaviour) within one build, 5000 rows x 8 resizable columns,
116
+ // prod, 4x CPU throttle:
117
+ //
118
+ // block | scroll-fast p50 | scroll-smooth p95 | scroll-wheel p95
119
+ // 1 | 125.0 ms | 128.5 ms | 324.7 ms
120
+ // 5 | 26.5 ms | 61.8 ms | 280.5 ms
121
+ // 9 | 22.8 ms | 46.3 ms | 348.3 ms
122
+ //
123
+ // Bigger blocks skip more relayouts but mount more rows, making each
124
+ // structural change costlier. A wheel flick crosses a block almost every
125
+ // notch, so it pays that cost without collecting the benefit — at
126
+ // overscan/2 its p95 regresses. overscan/4 improves all three.
127
+ const block = Math.max(1, Math.round(overscan / 4));
128
+ const rawFrom = Math.max(0, startIndex - overscan);
129
+ const rawTo = Math.min(rowCount - 1, endIndex + overscan);
130
+ const from = Math.max(0, Math.floor(rawFrom / block) * block);
131
+ const to = Math.max(from, Math.min(rowCount - 1, Math.ceil((rawTo + 1) / block) * block - 1));
132
+
133
+ const topSpacerHeight = from * rowHeight;
134
+ const bottomSpacerHeight = Math.max(0, (rowCount - to - 1) * rowHeight);
135
+
136
+ // Return the *same object* when nothing about the window changed. Scrolling
137
+ // within a block recomputes this on every scroll event, and a fresh object
138
+ // would invalidate `renderedItems`, allocate a new slice and make the keyed
139
+ // each re-diff every mounted row — all to arrive at the identical DOM.
140
+ // Preserving identity makes those frames genuinely free.
141
+ if (
142
+ lastVisibleRange !== undefined &&
143
+ lastVisibleRange.from === from &&
144
+ lastVisibleRange.to === to &&
145
+ lastVisibleRange.topSpacerHeight === topSpacerHeight &&
146
+ lastVisibleRange.bottomSpacerHeight === bottomSpacerHeight
147
+ ) {
148
+ return lastVisibleRange;
149
+ }
150
+
151
+ lastVisibleRange = { from, to, topSpacerHeight, bottomSpacerHeight };
152
+ return lastVisibleRange;
78
153
  });
79
154
  const renderedItems = $derived.by(() => {
80
155
  if (!virtualizerEnabled) return itemList;
@@ -89,9 +89,7 @@
89
89
  // Focus/selection/disabled state is fine-grained `$state` in the context —
90
90
  // the reads below track it directly, no version counters needed.
91
91
  const isFocused = $derived(row.section === 'body' ? table.isCellFocused(key) : false);
92
- const isFocusVisible = $derived(
93
- row.section === 'body' ? isFocused && table.focusVisible : false
94
- );
92
+ const isFocusVisible = $derived(row.section === 'body' ? isFocused && table.focusVisible : false);
95
93
  const isRowSelected = $derived(row.section === 'body' ? row.rowState.isSelected : false);
96
94
  const isRowDisabled = $derived(
97
95
  row.section === 'body'
@@ -1,6 +1,10 @@
1
1
  <script lang="ts">
2
2
  import { onDestroy } from 'svelte';
3
- import { setTableColumnContext, useTableContext, useTableSectionContext } from '../root/context.svelte';
3
+ import {
4
+ setTableColumnContext,
5
+ useTableContext,
6
+ useTableSectionContext
7
+ } from '../root/context.svelte';
4
8
  import type { TableColumnProps } from '../types.js';
5
9
 
6
10
  let {
@@ -39,11 +39,7 @@
39
39
  // The handle sits on the column's inline-end edge and is shifted half of
40
40
  // its width outwards; the physical shift direction depends on the layout
41
41
  // direction, resolved once the element mounts.
42
- let handleRtl = $state(false);
43
-
44
- $effect(() => {
45
- handleRtl = element ? isRightToLeft() : false;
46
- });
42
+ const handleRtl = $derived(element ? isRightToLeft() : false);
47
43
 
48
44
  let isFocused = $state(false);
49
45
  let isFocusVisible = $state(false);
@@ -89,9 +89,7 @@
89
89
  // Focus/selection/disabled state is fine-grained `$state` in the context —
90
90
  // the reads below track it directly, no version counters needed.
91
91
  const isFocused = $derived(row.section === 'body' ? table.isCellFocused(key) : false);
92
- const isFocusVisible = $derived(
93
- row.section === 'body' ? isFocused && table.focusVisible : false
94
- );
92
+ const isFocusVisible = $derived(row.section === 'body' ? isFocused && table.focusVisible : false);
95
93
  const isRowSelected = $derived(row.section === 'body' ? row.rowState.isSelected : false);
96
94
  const isRowDisabled = $derived(
97
95
  row.section === 'body'