@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,597 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from 'svelte';
3
+ import type { HTMLAttributes } from 'svelte/elements';
4
+ import { onMount, onDestroy } from 'svelte';
5
+ import { browser } from '../../internal/environment';
6
+ import { focusTrap } from '../../primitives/focus-trap';
7
+ import { scrollLock } from '../../primitives/scroll-lock';
8
+ import { clickOutside } from '../../primitives/click-outside';
9
+ import { ariaHideOutside } from '../../primitives/aria-hide-outside';
10
+ import {
11
+ swipeGesture,
12
+ getSwipeAxis,
13
+ getOutwardSign,
14
+ projectRelease,
15
+ applyRubberBand,
16
+ type SwipeGestureOptions
17
+ } from '../../primitives/swipe-gesture';
18
+ import {
19
+ resolveSnapOffsets,
20
+ resolveSnapPointSize,
21
+ findNearestSnapIndex,
22
+ resolveSnapOnRelease
23
+ } from '../root/snap-points';
24
+ import {
25
+ pushLayer,
26
+ removeLayer,
27
+ isTopmostLayer,
28
+ getLayerKindIndex,
29
+ getContentZIndex,
30
+ subscribeLayerStack
31
+ } from '../../primitives/layer-stack';
32
+ import {
33
+ registerDrawer,
34
+ unregisterDrawer,
35
+ updateDrawer,
36
+ hasDrawerAbove,
37
+ hasSwipingDrawerAbove,
38
+ getDepthFromFront,
39
+ getDrawerIndex,
40
+ drawerStack
41
+ } from '../root/drawer-stack.svelte';
42
+ import { requireDrawerContext } from '../root/context';
43
+ import { getDrawerPresenceContext } from '../root/presence-context';
44
+ import { hasDrawerViewport } from '../root/viewport-context';
45
+ import { getDrawerKeyboardContext } from '../root/keyboard-context';
46
+ import { createStartingStyle } from '../root/starting-style.svelte';
47
+ import type { DrawerSide, DrawerSnapPoint } from '../root/types';
48
+
49
+ /**
50
+ * Drawer.Content — the panel itself.
51
+ * Belongs inside a Drawer.Portal, optionally wrapped in a Drawer.Viewport.
52
+ */
53
+ type DrawerContentProps = {
54
+ /** Content of the panel. */
55
+ children?: Snippet;
56
+ /** CSS class for the panel. */
57
+ class?: string;
58
+ /**
59
+ * Where focus lands when the drawer opens. Defaults to the first focusable
60
+ * element inside the panel.
61
+ */
62
+ initialFocus?: HTMLElement | string | (() => HTMLElement | null | undefined);
63
+ /**
64
+ * `touch-action` for the panel. Defaults to `pan-y` for left/right drawers —
65
+ * children keep scrolling vertically while the horizontal axis is reserved for
66
+ * the drag — and to nothing for top/bottom ones, where reserving the vertical
67
+ * axis would break every scrollable region inside (`touch-action` intersects
68
+ * down the ancestor chain, so a child cannot opt back in).
69
+ */
70
+ touchAction?: string | null;
71
+ } & Omit<HTMLAttributes<HTMLDivElement>, 'class' | 'children'>;
72
+
73
+ let {
74
+ children,
75
+ class: className = '',
76
+ initialFocus,
77
+ touchAction,
78
+ style: styleProp,
79
+ ...restProps
80
+ }: DrawerContentProps = $props();
81
+
82
+ const ctx = requireDrawerContext('Drawer.Content');
83
+ const insideViewport = hasDrawerViewport();
84
+ // Present only under a Drawer.VirtualKeyboardProvider.
85
+ const keyboard = getDrawerKeyboardContext();
86
+ // The panel only mounts as it opens, so its first frame IS the starting frame.
87
+ const startingStyle = createStartingStyle();
88
+
89
+ // Present only when rendered inside Drawer.Portal. Drives the enter/exit animation
90
+ // and lets the Portal time the exit from this element. Falls back to "always open".
91
+ const presence = getDrawerPresenceContext();
92
+ const isOpen = $derived(presence ? presence.state === 'open' : true);
93
+
94
+ // The scroll lock must survive the exit animation: releasing it the moment `isOpen`
95
+ // flips false brings the page scrollbar back mid-exit and shifts the layout behind
96
+ // the still-visible panel. It releases only once the exit has finished (unmount).
97
+ const isModal = $derived(ctx.modal === true);
98
+ const shouldLockScroll = $derived(isModal && (presence ? isOpen || presence.isExiting : true));
99
+ const shouldTrapFocus = $derived(ctx.modal !== false && isOpen);
100
+
101
+ let panelRef: HTMLElement | undefined = $state();
102
+ let layerId: symbol | null = null;
103
+ let layerLevel = $state(0);
104
+ /** Identity in the drawer-only stack, for nesting. Assigned on mount. */
105
+ let stackId = $state<symbol | null>(null);
106
+
107
+ /* ── swipe ─────────────────────────────────────────────────────────── */
108
+
109
+ /** px/ms past which a release counts as a flick regardless of distance. */
110
+ const FLICK_VELOCITY = 0.4;
111
+ /** Fraction of the panel that must be projected off-screen to dismiss. */
112
+ const DISMISS_FRACTION = 0.5;
113
+ /**
114
+ * Ceiling, in px, for the resistance reported when the panel is pulled further
115
+ * open than it can go. Fixed rather than proportional to the panel: scaling it
116
+ * with the panel's own size let a tall sheet be hauled most of its own height,
117
+ * which felt less like resistance and more like the drawer coming loose.
118
+ */
119
+ const OVERDRAG_LIMIT = 40;
120
+
121
+ const axis = $derived(getSwipeAxis(ctx.side));
122
+ const outwardSign = $derived(getOutwardSign(ctx.side));
123
+
124
+ /** Outward displacement in px, after resistance. Never negative — see `handleSwipeMove`. */
125
+ let offset = $state(0);
126
+ /** Resisted distance the panel has been pulled past fully open, in px. */
127
+ let overdrag = $state(0);
128
+ let isSwiping = $state(false);
129
+ let dismissedBySwipe = $state(false);
130
+ let releaseStrength = $state(1);
131
+
132
+ let panelWidth = $state(0);
133
+ let panelHeight = $state(0);
134
+ let viewportWidth = $state(0);
135
+ let viewportHeight = $state(0);
136
+ let rootFontSize = $state(16);
137
+
138
+ /**
139
+ * Panel extent along its axis. Measured reactively rather than captured at
140
+ * gesture start: `swipeProgress` derives from it, and a plain `let` left the
141
+ * progress frozen at its first read (zero), so the backdrop never faded.
142
+ */
143
+ const panelExtent = $derived(axis === 'x' ? panelWidth : panelHeight);
144
+ const viewportExtent = $derived(axis === 'x' ? viewportWidth : viewportHeight);
145
+
146
+ /* ── snap points ───────────────────────────────────────────────────── */
147
+
148
+ const snapOffsets = $derived(
149
+ ctx.snapPoints && ctx.snapPoints.length > 0 && panelExtent > 0
150
+ ? resolveSnapOffsets(ctx.snapPoints, viewportExtent, panelExtent, rootFontSize)
151
+ : []
152
+ );
153
+ const hasSnapPoints = $derived(snapOffsets.length > 0);
154
+
155
+ /** Translation the panel rests at, before any drag. */
156
+ const restingOffset = $derived.by(() => {
157
+ if (!hasSnapPoints) return 0;
158
+ if (ctx.snapPoint === null || ctx.snapPoint === undefined) return snapOffsets[0];
159
+ const size = resolveSnapPointSize(ctx.snapPoint, viewportExtent, rootFontSize);
160
+ if (size === null) return snapOffsets[0];
161
+ const wanted = Math.max(0, panelExtent - size);
162
+ return snapOffsets[findNearestSnapIndex(snapOffsets, wanted)];
163
+ });
164
+ const activeSnapIndex = $derived(
165
+ hasSnapPoints ? findNearestSnapIndex(snapOffsets, restingOffset) : -1
166
+ );
167
+ /** True at the most open snap point — the "expanded" state consumers style. */
168
+ const isExpanded = $derived(hasSnapPoints && activeSnapIndex === 0);
169
+
170
+ /** Snap point value at `index`, for handing back to the root. */
171
+ function snapPointAt(index: number): DrawerSnapPoint | null {
172
+ const points = ctx.snapPoints;
173
+ if (!points || index < 0) return null;
174
+ const target = snapOffsets[index];
175
+ // Map back through the same resolution the offsets came from, so the value
176
+ // reported is one the consumer actually wrote.
177
+ for (const point of points) {
178
+ const size = resolveSnapPointSize(point, viewportExtent, rootFontSize);
179
+ if (size === null) continue;
180
+ if (Math.max(0, panelExtent - size) === target) return point;
181
+ }
182
+ return null;
183
+ }
184
+
185
+ /* ── gesture ───────────────────────────────────────────────────────── */
186
+
187
+ /**
188
+ * Displacement from the resting position, in px outward.
189
+ *
190
+ * An opening drag from `Drawer.SwipeArea` takes precedence: the panel starts
191
+ * fully out and is pulled in by the distance travelled, so it arrives with the
192
+ * finger instead of playing its enter animation underneath it.
193
+ */
194
+ const movement = $derived.by(() => {
195
+ if (ctx.swipeOpenDistance !== null) {
196
+ return Math.max(0, panelExtent - ctx.swipeOpenDistance) - restingOffset;
197
+ }
198
+ return offset;
199
+ });
200
+
201
+ const isDragging = $derived(isSwiping || ctx.swipeOpenDistance !== null);
202
+
203
+ /**
204
+ * Offset of the most closed snap point — where "on the way out" begins.
205
+ * Zero without snap points, since fully open is the only resting place.
206
+ */
207
+ const dismissFloor = $derived(hasSnapPoints ? snapOffsets[snapOffsets.length - 1] : 0);
208
+
209
+ /**
210
+ * Progress toward DISMISSAL, 0–1 — not progress through the gesture.
211
+ *
212
+ * Travelling between snap points reports 0 the whole way: it is movement within
213
+ * the open drawer, and fading the backdrop for it made the page flash brighter
214
+ * on the way down and snap dark again on release, for a drawer that never went
215
+ * anywhere. The backdrop only reacts once the panel is past the last snap point
216
+ * and genuinely leaving.
217
+ */
218
+ const swipeProgress = $derived.by(() => {
219
+ const travel = panelExtent - dismissFloor;
220
+ if (travel <= 0) return 0;
221
+ const total = restingOffset + movement;
222
+ return Math.min(Math.max((total - dismissFloor) / travel, 0), 1);
223
+ });
224
+
225
+ // Republish to the context so Drawer.Overlay can fade with a gesture it doesn't own,
226
+ // and so Drawer.SwipeArea knows how far a full open is.
227
+ $effect(() => {
228
+ ctx.setSwipeState(swipeProgress, isDragging);
229
+ });
230
+ $effect(() => {
231
+ ctx.setPanelExtent(panelExtent);
232
+ });
233
+
234
+ /* ── nesting ───────────────────────────────────────────────────────── */
235
+
236
+ // Published to the drawer-only stack so the drawers behind this one — and the
237
+ // app behind all of them — can react. `presence` falls as the panel is dragged
238
+ // away, which is what lets the indent unwind with the gesture instead of
239
+ // snapping back the instant the drawer closes.
240
+ $effect(() => {
241
+ const id = stackId;
242
+ if (!id) return;
243
+ updateDrawer(id, {
244
+ extent: panelExtent,
245
+ presence: 1 - swipeProgress,
246
+ swiping: isDragging,
247
+ exiting: presence?.isExiting ?? false
248
+ });
249
+ });
250
+
251
+ // Published so Drawer.Overlay can tell whether a backdrop is already on screen.
252
+ $effect(() => {
253
+ ctx.setStackIndex(stackId ? Math.max(0, getDrawerIndex(stackId)) : 0);
254
+ });
255
+
256
+ const nestedDrawerOpen = $derived(stackId ? hasDrawerAbove(stackId) : false);
257
+ const nestedDrawerSwiping = $derived(stackId ? hasSwipingDrawerAbove(stackId) : false);
258
+ const depthFromFront = $derived(stackId ? getDepthFromFront(stackId) : 0);
259
+
260
+ function handleSwipeStart() {
261
+ isSwiping = true;
262
+ releaseStrength = 1;
263
+ }
264
+
265
+ /** Displacement range the drag may cover freely, before resistance kicks in. */
266
+ function dragBounds(): { lower: number; upper: number } {
267
+ const lower = (hasSnapPoints ? snapOffsets[0] : 0) - restingOffset;
268
+ // A dismissible drawer can be pulled all the way off; otherwise it stops at
269
+ // the most closed snap point (or at fully open, with no snap points).
270
+ const upper = ctx.dismissible
271
+ ? Number.POSITIVE_INFINITY
272
+ : (hasSnapPoints ? snapOffsets[snapOffsets.length - 1] : 0) - restingOffset;
273
+ return { lower, upper };
274
+ }
275
+
276
+ function handleSwipeMove(displacement: number) {
277
+ const { lower, upper } = dragBounds();
278
+
279
+ if (displacement < lower) {
280
+ // Pulled further open than there is anywhere to go. The panel follows, with
281
+ // heavy resistance, and the strip of page it uncovers behind its anchored
282
+ // edge is covered by the bleed element below — moving the panel without that
283
+ // is what made the drawer look like it was coming apart.
284
+ overdrag = applyRubberBand(lower - displacement, OVERDRAG_LIMIT);
285
+ offset = lower - overdrag;
286
+ return;
287
+ }
288
+
289
+ overdrag = 0;
290
+
291
+ if (displacement > upper) {
292
+ // A drawer that cannot go further still moves, just reluctantly — a dead
293
+ // gesture reads as a broken one.
294
+ offset = upper + applyRubberBand(displacement - upper, OVERDRAG_LIMIT);
295
+ return;
296
+ }
297
+ offset = displacement;
298
+ }
299
+
300
+ function handleSwipeEnd(displacement: number, velocity: number, event: PointerEvent) {
301
+ isSwiping = false;
302
+ overdrag = 0;
303
+
304
+ // 1 for a still release (the panel eases back over its full transition), down to
305
+ // 0.1 for a hard flick, which should land almost immediately.
306
+ const normalized = Math.min(Math.abs(velocity) / FLICK_VELOCITY, 1);
307
+ releaseStrength = Number.isFinite(normalized) ? 1 - normalized * 0.9 : 0.1;
308
+
309
+ if (hasSnapPoints) {
310
+ const release = resolveSnapOnRelease({
311
+ offset: restingOffset + displacement,
312
+ velocity,
313
+ offsets: snapOffsets,
314
+ currentIndex: activeSnapIndex,
315
+ panelSize: panelExtent,
316
+ sequential: ctx.snapToSequentialPoints,
317
+ dismissible: ctx.dismissible
318
+ });
319
+
320
+ if (release.dismiss) {
321
+ dismissedBySwipe = true;
322
+ ctx.close('swipe', event);
323
+ return;
324
+ }
325
+
326
+ ctx.setSnapPoint(snapPointAt(release.index), event);
327
+ offset = 0;
328
+ return;
329
+ }
330
+
331
+ const projected = projectRelease(displacement, velocity);
332
+ const travelledFarEnough = projected >= panelExtent * DISMISS_FRACTION;
333
+ const flicked = velocity >= FLICK_VELOCITY;
334
+
335
+ if (ctx.dismissible && (flicked || travelledFarEnough)) {
336
+ // The offset is deliberately NOT reset: the exit animation continues from
337
+ // where the finger left the panel instead of snapping back to open first.
338
+ dismissedBySwipe = true;
339
+ ctx.close('swipe', event);
340
+ return;
341
+ }
342
+
343
+ offset = 0;
344
+ }
345
+
346
+ function handleSwipeCancel() {
347
+ isSwiping = false;
348
+ offset = 0;
349
+ overdrag = 0;
350
+ }
351
+
352
+ const resolvedTouchAction = $derived(
353
+ touchAction !== undefined ? touchAction : axis === 'x' ? 'pan-y' : null
354
+ );
355
+
356
+ const swipeOptions = $derived<SwipeGestureOptions>({
357
+ enabled: isOpen,
358
+ side: ctx.side,
359
+ touchAction: resolvedTouchAction,
360
+ onStart: handleSwipeStart,
361
+ onMove: ({ displacement }) => handleSwipeMove(displacement),
362
+ onEnd: ({ displacement, velocity, event }) => handleSwipeEnd(displacement, velocity, event),
363
+ onCancel: handleSwipeCancel
364
+ });
365
+
366
+ /**
367
+ * Box that sits just outside the panel's anchored edge, painted in the panel's own
368
+ * background, covering the strip of page a stretch would otherwise uncover.
369
+ *
370
+ * It exists because there is no way to stretch the panel from here without
371
+ * fighting the consumer's own padding: `background: inherit` borrows their colour
372
+ * and an absolutely positioned box costs them nothing.
373
+ *
374
+ * Always present, at the full overdrag limit, rather than sized to the live pull.
375
+ * At rest it sits entirely outside the viewport, so it is invisible and free — and
376
+ * being permanent it also covers the RELEASE, where a box tied to the live value
377
+ * vanished the instant the finger lifted while the panel was still easing back,
378
+ * flashing the gap open again at the very end of the gesture.
379
+ *
380
+ * The one assumption: a panel with `overflow: hidden` clips it away.
381
+ */
382
+ function bleedPosition(side: DrawerSide): string {
383
+ // Overlaps the panel by a pixel. Butting the two edges together leaves a
384
+ // hairline of backdrop showing through wherever the panel lands on a
385
+ // fractional position, which is most of the time during a drag.
386
+ const span = `${OVERDRAG_LIMIT + 1}px`;
387
+ if (side === 'bottom') return `top: calc(100% - 1px); left: 0; right: 0; height: ${span};`;
388
+ if (side === 'top') return `bottom: calc(100% - 1px); left: 0; right: 0; height: ${span};`;
389
+ if (side === 'left') return `right: calc(100% - 1px); top: 0; bottom: 0; width: ${span};`;
390
+ return `left: calc(100% - 1px); top: 0; bottom: 0; width: ${span};`;
391
+ }
392
+
393
+ /** Outward direction as a name, for `data-swipe-direction`. */
394
+ function directionFor(side: DrawerSide): 'up' | 'down' | 'left' | 'right' {
395
+ if (side === 'bottom') return 'down';
396
+ if (side === 'top') return 'up';
397
+ return side;
398
+ }
399
+
400
+ /* ── layout ────────────────────────────────────────────────────────── */
401
+
402
+ /**
403
+ * Without a Drawer.Viewport the panel pins itself to its edge, so the plain
404
+ * `Portal → Overlay + Content` composition works. Inside one it stays in flow and
405
+ * the viewport's flexbox places it.
406
+ */
407
+ function standalonePosition(side: DrawerSide): string {
408
+ const zIndex = getContentZIndex(layerLevel);
409
+ const edges =
410
+ side === 'top'
411
+ ? 'top: 0; left: 0; right: 0;'
412
+ : side === 'bottom'
413
+ ? 'bottom: 0; left: 0; right: 0;'
414
+ : side === 'left'
415
+ ? 'left: 0; top: 0; bottom: 0;'
416
+ : 'right: 0; top: 0; bottom: 0;';
417
+ return `position: fixed; ${edges} z-index: ${zIndex};`;
418
+ }
419
+
420
+ // A consumer `style` is APPENDED, not spread through `restProps`: spreading would
421
+ // place it after this attribute and silently drop every custom property the
422
+ // gesture publishes, taking the panel's own animation with it.
423
+ const style = $derived(
424
+ [
425
+ // The bleed box is positioned against the panel, so the panel has to be a
426
+ // containing block. Fixed positioning already is one; in a viewport it is not.
427
+ insideViewport ? 'position: relative;' : standalonePosition(ctx.side),
428
+ // The viewport is transparent to pointers so the page stays usable around a
429
+ // non-modal drawer; the panel has to opt back in.
430
+ 'pointer-events: auto;',
431
+ `--drawer-swipe-movement-x: ${axis === 'x' ? movement * outwardSign : 0}px;`,
432
+ `--drawer-swipe-movement-y: ${axis === 'y' ? movement * outwardSign : 0}px;`,
433
+ `--drawer-swipe-progress: ${swipeProgress};`,
434
+ `--drawer-swipe-strength: ${releaseStrength};`,
435
+ `--drawer-overdrag: ${overdrag}px;`,
436
+ `--drawer-snap-point-offset: ${restingOffset * outwardSign}px;`,
437
+ `--drawer-width: ${panelWidth}px;`,
438
+ `--drawer-height: ${panelHeight}px;`,
439
+ `--drawer-frontmost-height: ${drawerStack.frontmostExtent}px;`,
440
+ `--nested-drawers: ${depthFromFront};`,
441
+ // Deliberately absent while the keyboard is down, so the consumer's
442
+ // `var(--drawer-keyboard-inset, 0px)` fallback is what applies at rest.
443
+ keyboard && keyboard.inset > 0 ? `--drawer-keyboard-inset: ${keyboard.inset}px;` : '',
444
+ styleProp ?? ''
445
+ ]
446
+ .filter(Boolean)
447
+ .join(' ')
448
+ );
449
+
450
+ /* ── lifecycle ─────────────────────────────────────────────────────── */
451
+
452
+ // Register this element so Drawer.Portal can measure its exit animation.
453
+ $effect(() => {
454
+ presence?.setMotionTarget(panelRef ?? null);
455
+ return () => presence?.setMotionTarget(null);
456
+ });
457
+
458
+ // Keeps --drawer-width/--drawer-height honest as content grows, which nested
459
+ // stacking and snap points both read.
460
+ $effect(() => {
461
+ const element = panelRef;
462
+ if (!browser || !element || typeof ResizeObserver === 'undefined') return;
463
+
464
+ const measure = () => {
465
+ const rect = element.getBoundingClientRect();
466
+ panelWidth = rect.width;
467
+ panelHeight = rect.height;
468
+ };
469
+ measure();
470
+
471
+ const observer = new ResizeObserver(measure);
472
+ observer.observe(element);
473
+ return () => observer.disconnect();
474
+ });
475
+
476
+ // Snap points expressed as fractions or percentages are resolved against the
477
+ // viewport, and `rem` against the root font size, so both have to be tracked
478
+ // rather than read once — a phone rotating mid-session changes both.
479
+ $effect(() => {
480
+ if (!browser) return;
481
+
482
+ const measure = () => {
483
+ viewportWidth = window.innerWidth;
484
+ viewportHeight = window.innerHeight;
485
+ const parsed = Number.parseFloat(getComputedStyle(document.documentElement).fontSize);
486
+ if (Number.isFinite(parsed) && parsed > 0) rootFontSize = parsed;
487
+ };
488
+ measure();
489
+
490
+ window.addEventListener('resize', measure);
491
+ return () => window.removeEventListener('resize', measure);
492
+ });
493
+
494
+ function close(reason: 'escape-key' | 'outside-press' | 'imperative-action', event?: Event) {
495
+ ctx.close(reason, event);
496
+ }
497
+
498
+ /** Outside presses only dismiss the topmost layer of the whole stack. */
499
+ function closeIfTopmost(event: MouseEvent) {
500
+ if (layerId && isTopmostLayer(layerId)) {
501
+ close('outside-press', event);
502
+ }
503
+ }
504
+
505
+ function handleKeydown(event: KeyboardEvent) {
506
+ // Another layer (or a widget inside this one) already consumed this Escape —
507
+ // a single keypress must dismiss at most one thing.
508
+ if (event.defaultPrevented) return;
509
+ if (event.key !== 'Escape' || !isOpen || !ctx.shouldCloseOnEscape) return;
510
+ // A popover or menu opened inside the drawer sits above it in the unified stack.
511
+ if (layerId && isTopmostLayer(layerId)) {
512
+ event.preventDefault();
513
+ close('escape-key', event);
514
+ }
515
+ }
516
+
517
+ let unsubscribeStack: (() => void) | null = null;
518
+
519
+ onMount(() => {
520
+ if (!browser) return;
521
+ // Registered as a modal layer, the same kind dialogs use, so Escape ordering
522
+ // and z-index interleave correctly between the two.
523
+ layerId = pushLayer('dialog');
524
+ layerLevel = getLayerKindIndex(layerId);
525
+ ctx.setStackLevel(layerLevel);
526
+ stackId = registerDrawer();
527
+ // The level is the CURRENT index, not one frozen at push time: with two sibling
528
+ // layers, closing and reopening the first would otherwise mint it the same level
529
+ // as the still-open second (duplicate z-index).
530
+ unsubscribeStack = subscribeLayerStack(() => {
531
+ if (layerId === null) return;
532
+ const currentLevel = getLayerKindIndex(layerId);
533
+ if (currentLevel === -1 || currentLevel === layerLevel) return;
534
+ layerLevel = currentLevel;
535
+ ctx.setStackLevel(currentLevel);
536
+ });
537
+ document.addEventListener('keydown', handleKeydown);
538
+ });
539
+
540
+ onDestroy(() => {
541
+ if (!browser) return;
542
+ unsubscribeStack?.();
543
+ unsubscribeStack = null;
544
+ if (layerId) removeLayer(layerId);
545
+ if (stackId) unregisterDrawer(stackId);
546
+ stackId = null;
547
+ document.removeEventListener('keydown', handleKeydown);
548
+ });
549
+ </script>
550
+
551
+ <!--
552
+ `inert` already removes the closed panel from the a11y tree AND prevents focus, so no
553
+ `aria-hidden` is needed. Setting both meant that during the exit animation (still
554
+ mounted, `isOpen` already false) a descendant that retained focus sat under
555
+ `aria-hidden`, tripping the browser's "aria-hidden on a focused element" warning.
556
+ -->
557
+ <div
558
+ bind:this={panelRef}
559
+ class={className}
560
+ role="dialog"
561
+ aria-modal={isOpen && isModal ? 'true' : undefined}
562
+ aria-labelledby={ctx.labelledBy}
563
+ aria-describedby={ctx.describedBy}
564
+ inert={!isOpen}
565
+ data-drawer-content
566
+ data-side={ctx.side}
567
+ data-state={isOpen ? 'open' : 'closed'}
568
+ data-entering={presence?.isEntering || undefined}
569
+ data-exiting={presence?.isExiting || undefined}
570
+ data-starting-style={startingStyle.active || undefined}
571
+ data-swiping={isDragging || undefined}
572
+ data-swipe-dismiss={dismissedBySwipe || undefined}
573
+ data-swipe-direction={directionFor(ctx.side)}
574
+ data-expanded={isExpanded || undefined}
575
+ data-nested-drawer-open={nestedDrawerOpen || undefined}
576
+ data-nested-drawer-swiping={nestedDrawerSwiping || undefined}
577
+ {style}
578
+ use:clickOutside={{
579
+ handler: closeIfTopmost,
580
+ enabled: isOpen && ctx.shouldCloseOnInteractOutside,
581
+ ignore: [ctx.triggerRef]
582
+ }}
583
+ use:focusTrap={{ enabled: shouldTrapFocus, initialFocus }}
584
+ use:scrollLock={shouldLockScroll}
585
+ use:ariaHideOutside={isOpen && isModal}
586
+ use:swipeGesture={swipeOptions}
587
+ {...restProps}
588
+ >
589
+ <div
590
+ data-drawer-bleed
591
+ aria-hidden="true"
592
+ style="position: absolute; {bleedPosition(ctx.side)} background: inherit; pointer-events: none;"
593
+ ></div>
594
+ {#if children}
595
+ {@render children()}
596
+ {/if}
597
+ </div>
@@ -0,0 +1,28 @@
1
+ import type { Snippet } from 'svelte';
2
+ import type { HTMLAttributes } from 'svelte/elements';
3
+ /**
4
+ * Drawer.Content — the panel itself.
5
+ * Belongs inside a Drawer.Portal, optionally wrapped in a Drawer.Viewport.
6
+ */
7
+ type DrawerContentProps = {
8
+ /** Content of the panel. */
9
+ children?: Snippet;
10
+ /** CSS class for the panel. */
11
+ class?: string;
12
+ /**
13
+ * Where focus lands when the drawer opens. Defaults to the first focusable
14
+ * element inside the panel.
15
+ */
16
+ initialFocus?: HTMLElement | string | (() => HTMLElement | null | undefined);
17
+ /**
18
+ * `touch-action` for the panel. Defaults to `pan-y` for left/right drawers —
19
+ * children keep scrolling vertically while the horizontal axis is reserved for
20
+ * the drag — and to nothing for top/bottom ones, where reserving the vertical
21
+ * axis would break every scrollable region inside (`touch-action` intersects
22
+ * down the ancestor chain, so a child cannot opt back in).
23
+ */
24
+ touchAction?: string | null;
25
+ } & Omit<HTMLAttributes<HTMLDivElement>, 'class' | 'children'>;
26
+ declare const DrawerContent: import("svelte").Component<DrawerContentProps, {}, "">;
27
+ type DrawerContent = ReturnType<typeof DrawerContent>;
28
+ export default DrawerContent;
@@ -0,0 +1,40 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from 'svelte';
3
+ import type { HTMLAttributes } from 'svelte/elements';
4
+ import { requireDrawerContext } from '../root/context';
5
+
6
+ /**
7
+ * Drawer.Description — supporting text announced after the title.
8
+ *
9
+ * Registers its id with the root, which hands it to `Drawer.Content` as
10
+ * `aria-describedby`, so a screen reader reads it when the drawer opens rather
11
+ * than only when the user happens to arrow onto it.
12
+ */
13
+ type DrawerDescriptionProps = {
14
+ /** Supporting text announced after the title. */
15
+ children?: Snippet;
16
+ /** CSS class for the paragraph. */
17
+ class?: string;
18
+ /** Overrides the generated id. */
19
+ id?: string;
20
+ } & Omit<HTMLAttributes<HTMLParagraphElement>, 'class' | 'children' | 'id'>;
21
+
22
+ let {
23
+ children,
24
+ class: className = '',
25
+ id: idProp,
26
+ ...restProps
27
+ }: DrawerDescriptionProps = $props();
28
+
29
+ const ctx = requireDrawerContext('Drawer.Description');
30
+ const generatedId = $props.id();
31
+ const id = $derived(idProp ?? generatedId);
32
+
33
+ $effect(() => ctx.registerDescription(id));
34
+ </script>
35
+
36
+ <p {id} class={className} data-drawer-description {...restProps}>
37
+ {#if children}
38
+ {@render children()}
39
+ {/if}
40
+ </p>
@@ -0,0 +1,20 @@
1
+ import type { Snippet } from 'svelte';
2
+ import type { HTMLAttributes } from 'svelte/elements';
3
+ /**
4
+ * Drawer.Description — supporting text announced after the title.
5
+ *
6
+ * Registers its id with the root, which hands it to `Drawer.Content` as
7
+ * `aria-describedby`, so a screen reader reads it when the drawer opens rather
8
+ * than only when the user happens to arrow onto it.
9
+ */
10
+ type DrawerDescriptionProps = {
11
+ /** Supporting text announced after the title. */
12
+ children?: Snippet;
13
+ /** CSS class for the paragraph. */
14
+ class?: string;
15
+ /** Overrides the generated id. */
16
+ id?: string;
17
+ } & Omit<HTMLAttributes<HTMLParagraphElement>, 'class' | 'children' | 'id'>;
18
+ declare const DrawerDescription: import("svelte").Component<DrawerDescriptionProps, {}, "">;
19
+ type DrawerDescription = ReturnType<typeof DrawerDescription>;
20
+ export default DrawerDescription;