@human-kit/ui 1.0.0-beta.0 → 1.0.0-beta.2
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.
- package/README.md +2 -0
- package/dist/FOCUS_STATE_CONTRACT.md +1 -0
- package/dist/accordion/root/accordion-root.svelte +15 -3
- package/dist/accordion/root/context.svelte.js +4 -4
- package/dist/accordion/types.d.ts +9 -2
- package/dist/autocomplete/list/autocomplete-list.svelte.d.ts +6 -0
- package/dist/calendar/root/context.svelte.js +8 -2
- package/dist/checkbox/root/checkbox-root.svelte +28 -11
- package/dist/checkbox/root/checkbox-root.svelte.d.ts +13 -0
- package/dist/clock/root/create-time-selection-state.svelte.d.ts +5 -0
- package/dist/clock/root/create-time-selection-state.svelte.js +113 -10
- package/dist/clock/root/wheel-options.d.ts +7 -3
- package/dist/clock/root/wheel-options.js +7 -15
- package/dist/clock/wheel-column/clock-wheel-column.svelte +10 -2
- package/dist/clock/wheel-column/clock-wheel-column.svelte.d.ts +2 -1
- package/dist/collapsible/root/collapsible-root.svelte +9 -4
- package/dist/collapsible/types.d.ts +9 -0
- package/dist/combobox/input/combobox-input.svelte +12 -1
- package/dist/combobox/list/combobox-listbox.svelte +93 -1
- package/dist/combobox/list/combobox-listbox.svelte.d.ts +20 -0
- package/dist/combobox/root/combobox.svelte +118 -45
- package/dist/combobox/root/combobox.svelte.d.ts +11 -0
- package/dist/combobox/root/context.d.ts +12 -0
- package/dist/combobox/status/README.md +2 -2
- package/dist/combobox/tag-remove/combobox-tag-remove.svelte +14 -2
- package/dist/combobox/tag-remove/combobox-tag-remove.svelte.d.ts +7 -0
- package/dist/combobox/tags/combobox-tags.svelte +5 -2
- package/dist/combobox/tags/combobox-tags.svelte.d.ts +2 -0
- package/dist/combobox/trigger/combobox-trigger.svelte +4 -1
- package/dist/datepicker/README.md +3 -2
- package/dist/datepicker/root/date-picker-root.svelte +38 -2
- package/dist/datepicker/root/date-picker-root.svelte.d.ts +2 -0
- package/dist/datepicker/root/draft-evaluation.d.ts +7 -0
- package/dist/datepicker/root/draft-evaluation.js +2 -2
- package/dist/daterangepicker/root/date-range-picker-root.svelte +19 -1
- package/dist/daterangepicker/root/date-range-picker-root.svelte.d.ts +2 -0
- package/dist/dialog/README.md +9 -1
- package/dist/dialog/close/dialog-close.svelte +57 -0
- package/dist/dialog/close/dialog-close.svelte.d.ts +19 -0
- package/dist/dialog/content/dialog-content.svelte +2 -0
- package/dist/dialog/description/dialog-description.svelte +46 -0
- package/dist/dialog/description/dialog-description.svelte.d.ts +20 -0
- package/dist/dialog/index.d.ts +9 -0
- package/dist/dialog/index.js +3 -0
- package/dist/dialog/index.parts.d.ts +3 -0
- package/dist/dialog/index.parts.js +3 -0
- package/dist/dialog/root/context.d.ts +8 -0
- package/dist/dialog/root/dialog-root.svelte +58 -16
- package/dist/dialog/root/dialog-root.svelte.d.ts +10 -3
- package/dist/dialog/root/dialog-stack.d.ts +6 -22
- package/dist/dialog/root/dialog-stack.js +4 -33
- package/dist/dialog/title/dialog-title.svelte +43 -0
- package/dist/dialog/title/dialog-title.svelte.d.ts +21 -0
- package/dist/drawer/README.md +111 -0
- package/dist/drawer/body/drawer-body.svelte +47 -0
- package/dist/drawer/body/drawer-body.svelte.d.ts +32 -0
- package/dist/drawer/close/drawer-close.svelte +52 -0
- package/dist/drawer/close/drawer-close.svelte.d.ts +20 -0
- package/dist/drawer/content/drawer-content.svelte +597 -0
- package/dist/drawer/content/drawer-content.svelte.d.ts +28 -0
- package/dist/drawer/description/drawer-description.svelte +40 -0
- package/dist/drawer/description/drawer-description.svelte.d.ts +20 -0
- package/dist/drawer/indent/drawer-indent-background.svelte +36 -0
- package/dist/drawer/indent/drawer-indent-background.svelte.d.ts +17 -0
- package/dist/drawer/indent/drawer-indent.svelte +59 -0
- package/dist/drawer/indent/drawer-indent.svelte.d.ts +32 -0
- package/dist/drawer/index.d.ts +49 -0
- package/dist/drawer/index.js +25 -0
- package/dist/drawer/index.parts.d.ts +14 -0
- package/dist/drawer/index.parts.js +15 -0
- package/dist/drawer/overlay/drawer-overlay.svelte +67 -0
- package/dist/drawer/overlay/drawer-overlay.svelte.d.ts +12 -0
- package/dist/drawer/portal/drawer-portal.svelte +57 -0
- package/dist/drawer/portal/drawer-portal.svelte.d.ts +13 -0
- package/dist/drawer/root/context.d.ts +81 -0
- package/dist/drawer/root/context.js +20 -0
- package/dist/drawer/root/drawer-root.svelte +333 -0
- package/dist/drawer/root/drawer-root.svelte.d.ts +77 -0
- package/dist/drawer/root/drawer-stack.svelte.d.ts +73 -0
- package/dist/drawer/root/drawer-stack.svelte.js +113 -0
- package/dist/drawer/root/handle.svelte.d.ts +39 -0
- package/dist/drawer/root/handle.svelte.js +64 -0
- package/dist/drawer/root/keyboard-context.d.ts +13 -0
- package/dist/drawer/root/keyboard-context.js +8 -0
- package/dist/drawer/root/presence-context.d.ts +22 -0
- package/dist/drawer/root/presence-context.js +8 -0
- package/dist/drawer/root/snap-points.d.ts +68 -0
- package/dist/drawer/root/snap-points.js +120 -0
- package/dist/drawer/root/starting-style.svelte.d.ts +18 -0
- package/dist/drawer/root/starting-style.svelte.js +38 -0
- package/dist/drawer/root/types.d.ts +36 -0
- package/dist/drawer/root/types.js +1 -0
- package/dist/drawer/root/viewport-context.d.ts +2 -0
- package/dist/drawer/root/viewport-context.js +16 -0
- package/dist/drawer/swipe-area/drawer-swipe-area.svelte +143 -0
- package/dist/drawer/swipe-area/drawer-swipe-area.svelte.d.ts +22 -0
- package/dist/drawer/title/drawer-title.svelte +37 -0
- package/dist/drawer/title/drawer-title.svelte.d.ts +21 -0
- package/dist/drawer/trigger/drawer-trigger.svelte +90 -0
- package/dist/drawer/trigger/drawer-trigger.svelte.d.ts +41 -0
- package/dist/drawer/viewport/drawer-viewport.svelte +69 -0
- package/dist/drawer/viewport/drawer-viewport.svelte.d.ts +15 -0
- package/dist/drawer/virtual-keyboard/drawer-virtual-keyboard-provider.svelte +88 -0
- package/dist/drawer/virtual-keyboard/drawer-virtual-keyboard-provider.svelte.d.ts +29 -0
- package/dist/hooks/use-virtual-focus.svelte.d.ts +9 -0
- package/dist/hooks/use-virtual-focus.svelte.js +7 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/listbox/item/listbox-item.svelte +8 -1
- package/dist/listbox/root/context.d.ts +30 -0
- package/dist/listbox/root/context.js +10 -0
- package/dist/listbox/root/listbox.svelte +291 -8
- package/dist/listbox/root/listbox.svelte.d.ts +35 -2
- package/dist/menu/content/menu-content.svelte +3 -1
- package/dist/menu/overlay/menu-overlay.svelte +1 -1
- package/dist/numberfield/root/number-field-root.svelte +2 -1
- package/dist/popover/content/popover-content.svelte +1 -1
- package/dist/popover/overlay/popover-overlay.svelte +1 -1
- package/dist/popover/root/popover-root.svelte +23 -11
- package/dist/popover/root/popover-root.svelte.d.ts +10 -2
- package/dist/primitives/floating.d.ts +7 -0
- package/dist/primitives/floating.js +6 -2
- package/dist/primitives/index.d.ts +1 -0
- package/dist/primitives/index.js +1 -0
- package/dist/primitives/keyboard-navigation.js +3 -1
- package/dist/primitives/layer-stack.d.ts +29 -0
- package/dist/primitives/layer-stack.js +35 -0
- package/dist/primitives/list-virtualizer.d.ts +75 -0
- package/dist/primitives/list-virtualizer.js +59 -0
- package/dist/primitives/scroll-lock.d.ts +23 -3
- package/dist/primitives/scroll-lock.js +122 -9
- package/dist/primitives/swipe-gesture.d.ts +126 -0
- package/dist/primitives/swipe-gesture.js +351 -0
- package/dist/primitives/text.d.ts +8 -0
- package/dist/primitives/text.js +13 -0
- package/dist/switch/root/switch-root.svelte +23 -9
- package/dist/switch/root/switch-root.svelte.d.ts +9 -2
- package/dist/table/body/table-body.svelte +84 -9
- package/dist/table/cell/table-cell.svelte +1 -3
- package/dist/table/column/table-column.svelte +5 -1
- package/dist/table/column-resizer/table-column-resizer.svelte +1 -5
- package/dist/table/index.d.ts +1 -1
- package/dist/table/index.js +1 -1
- package/dist/table/interactive-cell/table-interactive-cell.svelte +1 -3
- package/dist/table/root/context.svelte.js +99 -49
- package/dist/table/row/table-row.svelte +7 -7
- package/dist/table/sort-trigger/table-sort-trigger.svelte +5 -1
- package/dist/table/types.d.ts +6 -0
- package/dist/tabs/root/tabs-root.svelte +20 -6
- package/dist/tabs/types.d.ts +9 -0
- package/dist/timepicker/root/time-picker-root.svelte +11 -0
- package/dist/timepicker/root/time-picker-root.svelte.d.ts +2 -0
- package/dist/toggle-group/root/context.svelte.js +11 -4
- package/dist/toggle-group/root/toggle-group-root.svelte +19 -4
- package/dist/toggle-group/types.d.ts +9 -0
- package/dist/tree/root/context.svelte.js +6 -0
- package/dist/tree/root/tree-root.svelte +22 -8
- package/dist/tree/types.d.ts +13 -0
- package/package.json +227 -224
|
@@ -9,6 +9,17 @@
|
|
|
9
9
|
* over this shared stack; keeping them separate was the root cause of a single
|
|
10
10
|
* Escape or outside press dismissing a dialog together with the popover/menu
|
|
11
11
|
* opened inside it.
|
|
12
|
+
*
|
|
13
|
+
* It also owns the z-index math for every layer, so modal surfaces (dialogs,
|
|
14
|
+
* drawers) and floating ones (popovers, menus) are laid out against a single
|
|
15
|
+
* scale. The math used to live in `dialog/root/dialog-stack.ts`, which meant
|
|
16
|
+
* Menu and Popover imported from Dialog just to place themselves.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* `dialog` covers every MODAL surface — dialogs and drawers alike. They share the
|
|
20
|
+
* kind on purpose: the level that drives their z-index counts layers per kind, so
|
|
21
|
+
* a separate `drawer` kind would mint a drawer opened over a dialog the very same
|
|
22
|
+
* level, and the two would collide at the same z-index.
|
|
12
23
|
*/
|
|
13
24
|
export type LayerKind = 'dialog' | 'popover' | 'menu';
|
|
14
25
|
/** Subscribe to stack changes (any push/remove). Returns an unsubscribe function. */
|
|
@@ -27,3 +38,21 @@ export declare function getLayerKindIndex(id: symbol): number;
|
|
|
27
38
|
export declare function isTopmostLayer(id: symbol): boolean;
|
|
28
39
|
/** Number of open layers, optionally filtered by kind. */
|
|
29
40
|
export declare function getLayerCount(kind?: LayerKind): number;
|
|
41
|
+
/** Z-index for a modal layer's backdrop at `level` (its index among open modals). */
|
|
42
|
+
export declare function getOverlayZIndex(level: number): number;
|
|
43
|
+
/** Z-index for a modal layer's panel — one above its own backdrop. */
|
|
44
|
+
export declare function getContentZIndex(level: number): number;
|
|
45
|
+
/**
|
|
46
|
+
* Z-index a floating layer (popover/menu/calendar) should use so it renders ABOVE
|
|
47
|
+
* the topmost open modal — or above page content when none is open. Without this,
|
|
48
|
+
* a popover opened inside a (nested) dialog kept its fixed z-index and rendered
|
|
49
|
+
* behind the dialog, because each nested modal's panel sits at a higher z-index
|
|
50
|
+
* than the popover's old constant. Resolves to 9999 with no modals open (the prior
|
|
51
|
+
* fixed value), and climbs past the stack as modals nest.
|
|
52
|
+
*/
|
|
53
|
+
export declare function getFloatingLayerZIndex(): number;
|
|
54
|
+
/**
|
|
55
|
+
* Z-index for a floating layer's backdrop — one below its content (mirrors the
|
|
56
|
+
* modal overlay/content pairing).
|
|
57
|
+
*/
|
|
58
|
+
export declare function getFloatingLayerOverlayZIndex(): number;
|
|
@@ -9,6 +9,11 @@
|
|
|
9
9
|
* over this shared stack; keeping them separate was the root cause of a single
|
|
10
10
|
* Escape or outside press dismissing a dialog together with the popover/menu
|
|
11
11
|
* opened inside it.
|
|
12
|
+
*
|
|
13
|
+
* It also owns the z-index math for every layer, so modal surfaces (dialogs,
|
|
14
|
+
* drawers) and floating ones (popovers, menus) are laid out against a single
|
|
15
|
+
* scale. The math used to live in `dialog/root/dialog-stack.ts`, which meant
|
|
16
|
+
* Menu and Popover imported from Dialog just to place themselves.
|
|
12
17
|
*/
|
|
13
18
|
const layerStack = [];
|
|
14
19
|
/**
|
|
@@ -79,3 +84,33 @@ export function getLayerCount(kind) {
|
|
|
79
84
|
}
|
|
80
85
|
return count;
|
|
81
86
|
}
|
|
87
|
+
/* ── z-index scale ──────────────────────────────────────────────────── */
|
|
88
|
+
/** Base z-index for the first modal layer. Each nested one climbs by the increment. */
|
|
89
|
+
const BASE_Z_INDEX = 9990;
|
|
90
|
+
const Z_INDEX_INCREMENT = 10;
|
|
91
|
+
/** Z-index for a modal layer's backdrop at `level` (its index among open modals). */
|
|
92
|
+
export function getOverlayZIndex(level) {
|
|
93
|
+
return BASE_Z_INDEX + level * Z_INDEX_INCREMENT;
|
|
94
|
+
}
|
|
95
|
+
/** Z-index for a modal layer's panel — one above its own backdrop. */
|
|
96
|
+
export function getContentZIndex(level) {
|
|
97
|
+
return BASE_Z_INDEX + level * Z_INDEX_INCREMENT + 1;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Z-index a floating layer (popover/menu/calendar) should use so it renders ABOVE
|
|
101
|
+
* the topmost open modal — or above page content when none is open. Without this,
|
|
102
|
+
* a popover opened inside a (nested) dialog kept its fixed z-index and rendered
|
|
103
|
+
* behind the dialog, because each nested modal's panel sits at a higher z-index
|
|
104
|
+
* than the popover's old constant. Resolves to 9999 with no modals open (the prior
|
|
105
|
+
* fixed value), and climbs past the stack as modals nest.
|
|
106
|
+
*/
|
|
107
|
+
export function getFloatingLayerZIndex() {
|
|
108
|
+
return BASE_Z_INDEX + getLayerCount('dialog') * Z_INDEX_INCREMENT + 9;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Z-index for a floating layer's backdrop — one below its content (mirrors the
|
|
112
|
+
* modal overlay/content pairing).
|
|
113
|
+
*/
|
|
114
|
+
export function getFloatingLayerOverlayZIndex() {
|
|
115
|
+
return getFloatingLayerZIndex() - 1;
|
|
116
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Window maths for a vertically virtualized list of uniform-height rows.
|
|
3
|
+
*
|
|
4
|
+
* Pure and framework-free so the arithmetic can be tested on its own: everything about
|
|
5
|
+
* *when* to recompute (scroll, resize, measurement) belongs to the component that calls it.
|
|
6
|
+
*
|
|
7
|
+
* Rows are assumed to be the same height. That is the trade this makes to stay cheap —
|
|
8
|
+
* knowing the height of every row up front is what lets the list place a scrollbar of the
|
|
9
|
+
* right size without measuring a thousand elements. A list of mixed-height rows must not be
|
|
10
|
+
* virtualized with this.
|
|
11
|
+
*/
|
|
12
|
+
export type ListWindowOptions = {
|
|
13
|
+
/** How many rows the list holds in total, not how many are rendered. */
|
|
14
|
+
count: number;
|
|
15
|
+
/** Height of one row, in pixels. */
|
|
16
|
+
itemHeight: number;
|
|
17
|
+
/** Vertical space between rows (a flex/grid `gap`), in pixels. */
|
|
18
|
+
gap?: number;
|
|
19
|
+
/** Height of the scrolling viewport, in pixels. */
|
|
20
|
+
viewportHeight: number;
|
|
21
|
+
/** Current scroll offset of that viewport, in pixels. */
|
|
22
|
+
scrollTop: number;
|
|
23
|
+
/**
|
|
24
|
+
* Rows rendered beyond each edge of the viewport, as a buffer against how long a row
|
|
25
|
+
* takes to render: while the main thread builds rows the compositor keeps scrolling, and
|
|
26
|
+
* anything past the last rendered row shows as blank.
|
|
27
|
+
*/
|
|
28
|
+
overscan?: number;
|
|
29
|
+
/**
|
|
30
|
+
* The scroller's top padding, in pixels.
|
|
31
|
+
*
|
|
32
|
+
* `scrollTop` is measured from the top of the padding box but the rows start after it, so
|
|
33
|
+
* ignoring it offsets every row by that much — a few pixels, which is exactly enough to
|
|
34
|
+
* leave a row clipped by the edge it was just scrolled to.
|
|
35
|
+
*/
|
|
36
|
+
padding?: number;
|
|
37
|
+
};
|
|
38
|
+
export type ListWindow = {
|
|
39
|
+
/** First rendered index (inclusive). */
|
|
40
|
+
from: number;
|
|
41
|
+
/** Last rendered index (inclusive). `from - 1` when there is nothing to render. */
|
|
42
|
+
to: number;
|
|
43
|
+
/** Distance from the top of the content to the first rendered row, in pixels. */
|
|
44
|
+
offset: number;
|
|
45
|
+
/** Height the full list would occupy, so the scrollbar matches the real count. */
|
|
46
|
+
contentHeight: number;
|
|
47
|
+
};
|
|
48
|
+
export declare function computeListWindow({ count, itemHeight, gap, viewportHeight, scrollTop, overscan, padding }: ListWindowOptions): ListWindow;
|
|
49
|
+
export type ScrollIntoViewOptions = {
|
|
50
|
+
index: number;
|
|
51
|
+
count: number;
|
|
52
|
+
itemHeight: number;
|
|
53
|
+
gap?: number;
|
|
54
|
+
viewportHeight: number;
|
|
55
|
+
scrollTop: number;
|
|
56
|
+
/** The scroller's top padding — see {@link ListWindowOptions.padding}. */
|
|
57
|
+
padding?: number;
|
|
58
|
+
/**
|
|
59
|
+
* How to place the row.
|
|
60
|
+
*
|
|
61
|
+
* `nearest` moves as little as possible, which is what stepping through a list with the
|
|
62
|
+
* arrows wants. `center` puts the row in the middle of the viewport, for when the list
|
|
63
|
+
* opens on a selection the user has to *find*: flush against an edge reads as "the list
|
|
64
|
+
* happens to end here" and, one pixel off, as a row cut in half.
|
|
65
|
+
*/
|
|
66
|
+
align?: 'nearest' | 'center';
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* The scroll offset that brings a row fully into view, or `null` when it already is.
|
|
70
|
+
*
|
|
71
|
+
* Returning `null` for the no-op case matters: assigning `scrollTop` unconditionally on
|
|
72
|
+
* every focus change fights the user's own scrolling and makes the list jitter while the
|
|
73
|
+
* pointer moves over it.
|
|
74
|
+
*/
|
|
75
|
+
export declare function scrollTopForIndex({ index, count, itemHeight, gap, viewportHeight, scrollTop, padding, align }: ScrollIntoViewOptions): number | null;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Window maths for a vertically virtualized list of uniform-height rows.
|
|
3
|
+
*
|
|
4
|
+
* Pure and framework-free so the arithmetic can be tested on its own: everything about
|
|
5
|
+
* *when* to recompute (scroll, resize, measurement) belongs to the component that calls it.
|
|
6
|
+
*
|
|
7
|
+
* Rows are assumed to be the same height. That is the trade this makes to stay cheap —
|
|
8
|
+
* knowing the height of every row up front is what lets the list place a scrollbar of the
|
|
9
|
+
* right size without measuring a thousand elements. A list of mixed-height rows must not be
|
|
10
|
+
* virtualized with this.
|
|
11
|
+
*/
|
|
12
|
+
/** Distance from one row's top edge to the next one's. */
|
|
13
|
+
function pitchOf(itemHeight, gap) {
|
|
14
|
+
return Math.max(1, itemHeight + gap);
|
|
15
|
+
}
|
|
16
|
+
export function computeListWindow({ count, itemHeight, gap = 0, viewportHeight, scrollTop, overscan = 0, padding = 0 }) {
|
|
17
|
+
if (count <= 0 || itemHeight <= 0) {
|
|
18
|
+
return { from: 0, to: -1, offset: 0, contentHeight: 0 };
|
|
19
|
+
}
|
|
20
|
+
const pitch = pitchOf(itemHeight, gap);
|
|
21
|
+
// No trailing gap: the last row ends the content.
|
|
22
|
+
const contentHeight = count * pitch - gap;
|
|
23
|
+
// One extra row: a viewport that isn't a whole multiple of the pitch shows part of one
|
|
24
|
+
// more, and that partial row still has to exist.
|
|
25
|
+
const visibleCount = Math.max(1, Math.ceil(Math.max(0, viewportHeight) / pitch) + 1);
|
|
26
|
+
const startIndex = Math.min(count - 1, Math.max(0, Math.floor(Math.max(0, scrollTop - padding) / pitch)));
|
|
27
|
+
const from = Math.max(0, startIndex - overscan);
|
|
28
|
+
const to = Math.min(count - 1, startIndex + visibleCount - 1 + overscan);
|
|
29
|
+
return { from, to, offset: from * pitch, contentHeight };
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The scroll offset that brings a row fully into view, or `null` when it already is.
|
|
33
|
+
*
|
|
34
|
+
* Returning `null` for the no-op case matters: assigning `scrollTop` unconditionally on
|
|
35
|
+
* every focus change fights the user's own scrolling and makes the list jitter while the
|
|
36
|
+
* pointer moves over it.
|
|
37
|
+
*/
|
|
38
|
+
export function scrollTopForIndex({ index, count, itemHeight, gap = 0, viewportHeight, scrollTop, padding = 0, align = 'nearest' }) {
|
|
39
|
+
if (index < 0 || index >= count || itemHeight <= 0) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
const pitch = pitchOf(itemHeight, gap);
|
|
43
|
+
const contentHeight = count * pitch - gap;
|
|
44
|
+
const maxScrollTop = Math.max(0, padding * 2 + contentHeight - viewportHeight);
|
|
45
|
+
const clamp = (value) => Math.min(Math.max(0, value), maxScrollTop);
|
|
46
|
+
const top = padding + index * pitch;
|
|
47
|
+
const bottom = top + itemHeight;
|
|
48
|
+
if (align === 'center') {
|
|
49
|
+
const centered = clamp(top - (viewportHeight - itemHeight) / 2);
|
|
50
|
+
return centered === scrollTop ? null : centered;
|
|
51
|
+
}
|
|
52
|
+
if (top < scrollTop) {
|
|
53
|
+
return clamp(top);
|
|
54
|
+
}
|
|
55
|
+
if (bottom > scrollTop + viewportHeight) {
|
|
56
|
+
return clamp(bottom - viewportHeight);
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
@@ -1,10 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Scroll lock primitive.
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
|
+
* Prevents the page from scrolling while an overlay is open. Two layers work
|
|
5
|
+
* together:
|
|
6
|
+
*
|
|
7
|
+
* 1. The document body is pinned (`overflow: hidden` + `position: fixed`), which
|
|
8
|
+
* freezes the *document* scroller and blocks iOS Safari touch scrolling. The
|
|
9
|
+
* scrollbar width is compensated so the page doesn't shift.
|
|
10
|
+
*
|
|
11
|
+
* 2. `wheel`/`touchmove` are intercepted in the capture phase and cancelled
|
|
12
|
+
* unless they target the overlay's own scroll region. Pinning the body is NOT
|
|
13
|
+
* enough when the scrollable element is an INNER container (e.g. an app shell
|
|
14
|
+
* whose content scrolls in a pane rather than on `<body>`): hiding body
|
|
15
|
+
* overflow does nothing to that pane. Cancelling the scroll event itself
|
|
16
|
+
* freezes any scroller, inner or not — matching React Aria's `usePreventScroll`.
|
|
17
|
+
* This is why an earlier "body only" lock silently did nothing inside layouts
|
|
18
|
+
* like the docs shell.
|
|
19
|
+
*
|
|
20
|
+
* Scrolling INSIDE the overlay still works, and scroll chaining from the
|
|
21
|
+
* overlay's inner scroller out to the background is blocked once that scroller
|
|
22
|
+
* reaches its bounds.
|
|
4
23
|
*/
|
|
5
24
|
/**
|
|
6
|
-
* Svelte action that locks scrolling
|
|
7
|
-
*
|
|
25
|
+
* Svelte action that locks scrolling while `enabled`. The `node` it is placed on
|
|
26
|
+
* is treated as the overlay whose inner scroll region stays live; everything
|
|
27
|
+
* else is frozen. Handles multiple nested locks correctly (reference-counted).
|
|
8
28
|
*
|
|
9
29
|
* @example
|
|
10
30
|
* ```svelte
|
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Scroll lock primitive.
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
|
+
* Prevents the page from scrolling while an overlay is open. Two layers work
|
|
5
|
+
* together:
|
|
6
|
+
*
|
|
7
|
+
* 1. The document body is pinned (`overflow: hidden` + `position: fixed`), which
|
|
8
|
+
* freezes the *document* scroller and blocks iOS Safari touch scrolling. The
|
|
9
|
+
* scrollbar width is compensated so the page doesn't shift.
|
|
10
|
+
*
|
|
11
|
+
* 2. `wheel`/`touchmove` are intercepted in the capture phase and cancelled
|
|
12
|
+
* unless they target the overlay's own scroll region. Pinning the body is NOT
|
|
13
|
+
* enough when the scrollable element is an INNER container (e.g. an app shell
|
|
14
|
+
* whose content scrolls in a pane rather than on `<body>`): hiding body
|
|
15
|
+
* overflow does nothing to that pane. Cancelling the scroll event itself
|
|
16
|
+
* freezes any scroller, inner or not — matching React Aria's `usePreventScroll`.
|
|
17
|
+
* This is why an earlier "body only" lock silently did nothing inside layouts
|
|
18
|
+
* like the docs shell.
|
|
19
|
+
*
|
|
20
|
+
* Scrolling INSIDE the overlay still works, and scroll chaining from the
|
|
21
|
+
* overlay's inner scroller out to the background is blocked once that scroller
|
|
22
|
+
* reaches its bounds.
|
|
4
23
|
*/
|
|
5
24
|
let lockCount = 0;
|
|
6
25
|
let originalOverflow = '';
|
|
@@ -11,6 +30,10 @@ let originalLeft = '';
|
|
|
11
30
|
let originalRight = '';
|
|
12
31
|
let savedScrollX = 0;
|
|
13
32
|
let savedScrollY = 0;
|
|
33
|
+
// The overlay nodes whose inner scroll regions stay live while the background is
|
|
34
|
+
// frozen. A Set (rather than just the lock count) so the event handlers can tell
|
|
35
|
+
// an overlay-targeted scroll from a background one.
|
|
36
|
+
const overlayNodes = new Set();
|
|
14
37
|
function getScrollbarWidth() {
|
|
15
38
|
return window.innerWidth - document.documentElement.clientWidth;
|
|
16
39
|
}
|
|
@@ -24,7 +47,93 @@ function reservesStableGutter() {
|
|
|
24
47
|
.getPropertyValue('scrollbar-gutter')
|
|
25
48
|
.includes('stable');
|
|
26
49
|
}
|
|
27
|
-
|
|
50
|
+
/** The registered overlay node that contains `target`, if any. */
|
|
51
|
+
function overlayContaining(target) {
|
|
52
|
+
if (!(target instanceof Node))
|
|
53
|
+
return null;
|
|
54
|
+
for (const node of overlayNodes) {
|
|
55
|
+
if (node.contains(target))
|
|
56
|
+
return node;
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
function isScrollable(el) {
|
|
61
|
+
const { overflowX, overflowY } = getComputedStyle(el);
|
|
62
|
+
return /(auto|scroll|overlay)/.test(overflowY) || /(auto|scroll|overlay)/.test(overflowX);
|
|
63
|
+
}
|
|
64
|
+
/** Nearest actually-scrollable element between `target` and `root` (inclusive), or null. */
|
|
65
|
+
function scrollableWithin(target, root) {
|
|
66
|
+
let el = target;
|
|
67
|
+
while (el) {
|
|
68
|
+
if (el instanceof HTMLElement &&
|
|
69
|
+
(el.scrollHeight > el.clientHeight || el.scrollWidth > el.clientWidth) &&
|
|
70
|
+
isScrollable(el)) {
|
|
71
|
+
return el;
|
|
72
|
+
}
|
|
73
|
+
if (el === root)
|
|
74
|
+
break;
|
|
75
|
+
el = el.parentElement;
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
function onWheel(event) {
|
|
80
|
+
if (!event.cancelable)
|
|
81
|
+
return;
|
|
82
|
+
const overlay = overlayContaining(event.target);
|
|
83
|
+
if (!overlay) {
|
|
84
|
+
// Background scroll — freeze it.
|
|
85
|
+
event.preventDefault();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const scroller = scrollableWithin(event.target, overlay);
|
|
89
|
+
if (!scroller) {
|
|
90
|
+
// The overlay has no scroll region of its own; the wheel would chain to the
|
|
91
|
+
// background, so cancel it.
|
|
92
|
+
event.preventDefault();
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
// Otherwise let the inner scroller consume the delta, but cancel once it hits a
|
|
96
|
+
// bound so the scroll doesn't chain out to the frozen background.
|
|
97
|
+
const { deltaX, deltaY } = event;
|
|
98
|
+
if (Math.abs(deltaY) >= Math.abs(deltaX)) {
|
|
99
|
+
const atTop = scroller.scrollTop <= 0;
|
|
100
|
+
const atBottom = scroller.scrollTop + scroller.clientHeight >= scroller.scrollHeight - 1;
|
|
101
|
+
if ((deltaY < 0 && atTop) || (deltaY > 0 && atBottom))
|
|
102
|
+
event.preventDefault();
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
const atLeft = scroller.scrollLeft <= 0;
|
|
106
|
+
const atRight = scroller.scrollLeft + scroller.clientWidth >= scroller.scrollWidth - 1;
|
|
107
|
+
if ((deltaX < 0 && atLeft) || (deltaX > 0 && atRight))
|
|
108
|
+
event.preventDefault();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function onTouchMove(event) {
|
|
112
|
+
if (!event.cancelable)
|
|
113
|
+
return;
|
|
114
|
+
// Leave multi-touch gestures (pinch-zoom) alone.
|
|
115
|
+
if (event.touches.length > 1)
|
|
116
|
+
return;
|
|
117
|
+
const overlay = overlayContaining(event.target);
|
|
118
|
+
if (!overlay) {
|
|
119
|
+
event.preventDefault();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
// An inner scroll region present → let the browser scroll it natively; none →
|
|
123
|
+
// block so the touch doesn't scroll the background.
|
|
124
|
+
if (!scrollableWithin(event.target, overlay))
|
|
125
|
+
event.preventDefault();
|
|
126
|
+
}
|
|
127
|
+
function addEventBlockers() {
|
|
128
|
+
document.addEventListener('wheel', onWheel, { passive: false, capture: true });
|
|
129
|
+
document.addEventListener('touchmove', onTouchMove, { passive: false, capture: true });
|
|
130
|
+
}
|
|
131
|
+
function removeEventBlockers() {
|
|
132
|
+
document.removeEventListener('wheel', onWheel, { capture: true });
|
|
133
|
+
document.removeEventListener('touchmove', onTouchMove, { capture: true });
|
|
134
|
+
}
|
|
135
|
+
function lock(node) {
|
|
136
|
+
overlayNodes.add(node);
|
|
28
137
|
if (lockCount === 0) {
|
|
29
138
|
const body = document.body;
|
|
30
139
|
originalOverflow = body.style.overflow;
|
|
@@ -48,10 +157,12 @@ function lock() {
|
|
|
48
157
|
body.style.top = `${-savedScrollY}px`;
|
|
49
158
|
body.style.left = '0';
|
|
50
159
|
body.style.right = '0';
|
|
160
|
+
addEventBlockers();
|
|
51
161
|
}
|
|
52
162
|
lockCount++;
|
|
53
163
|
}
|
|
54
|
-
function unlock() {
|
|
164
|
+
function unlock(node) {
|
|
165
|
+
overlayNodes.delete(node);
|
|
55
166
|
lockCount--;
|
|
56
167
|
if (lockCount === 0) {
|
|
57
168
|
const body = document.body;
|
|
@@ -61,14 +172,16 @@ function unlock() {
|
|
|
61
172
|
body.style.top = originalTop;
|
|
62
173
|
body.style.left = originalLeft;
|
|
63
174
|
body.style.right = originalRight;
|
|
175
|
+
removeEventBlockers();
|
|
64
176
|
// Pinning the body reset the document scroll offset; put the page back where it was.
|
|
65
177
|
window.scrollTo(savedScrollX, savedScrollY);
|
|
66
178
|
}
|
|
67
179
|
lockCount = Math.max(0, lockCount);
|
|
68
180
|
}
|
|
69
181
|
/**
|
|
70
|
-
* Svelte action that locks scrolling
|
|
71
|
-
*
|
|
182
|
+
* Svelte action that locks scrolling while `enabled`. The `node` it is placed on
|
|
183
|
+
* is treated as the overlay whose inner scroll region stays live; everything
|
|
184
|
+
* else is frozen. Handles multiple nested locks correctly (reference-counted).
|
|
72
185
|
*
|
|
73
186
|
* @example
|
|
74
187
|
* ```svelte
|
|
@@ -79,21 +192,21 @@ function unlock() {
|
|
|
79
192
|
*/
|
|
80
193
|
export function scrollLock(node, enabled = true) {
|
|
81
194
|
if (enabled) {
|
|
82
|
-
lock();
|
|
195
|
+
lock(node);
|
|
83
196
|
}
|
|
84
197
|
return {
|
|
85
198
|
update(newEnabled) {
|
|
86
199
|
if (newEnabled && !enabled) {
|
|
87
|
-
lock();
|
|
200
|
+
lock(node);
|
|
88
201
|
}
|
|
89
202
|
else if (!newEnabled && enabled) {
|
|
90
|
-
unlock();
|
|
203
|
+
unlock(node);
|
|
91
204
|
}
|
|
92
205
|
enabled = newEnabled;
|
|
93
206
|
},
|
|
94
207
|
destroy() {
|
|
95
208
|
if (enabled) {
|
|
96
|
-
unlock();
|
|
209
|
+
unlock(node);
|
|
97
210
|
}
|
|
98
211
|
}
|
|
99
212
|
};
|
|
@@ -0,0 +1,126 @@
|
|
|
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
|
+
export type SwipeSide = 'top' | 'right' | 'bottom' | 'left';
|
|
25
|
+
export type SwipeAxis = 'x' | 'y';
|
|
26
|
+
/** Axis a panel anchored to `side` travels along. */
|
|
27
|
+
export declare function getSwipeAxis(side: SwipeSide): SwipeAxis;
|
|
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 declare function getOutwardSign(side: SwipeSide): 1 | -1;
|
|
35
|
+
export type SwipeSample = {
|
|
36
|
+
/** `performance.now()` timestamp. */
|
|
37
|
+
time: number;
|
|
38
|
+
/** Outward displacement, in px, at that moment. */
|
|
39
|
+
position: number;
|
|
40
|
+
};
|
|
41
|
+
/** Window (ms) over which the release velocity is measured. */
|
|
42
|
+
export declare const RELEASE_VELOCITY_WINDOW_MS = 100;
|
|
43
|
+
/**
|
|
44
|
+
* Release velocity in px/ms (signed, positive = outward) over the trailing
|
|
45
|
+
* `windowMs` before `now`.
|
|
46
|
+
*
|
|
47
|
+
* Returns `0` when there is no recent movement — no samples, or the newest one is
|
|
48
|
+
* already older than the window, which is what a finger held still before lifting
|
|
49
|
+
* looks like. Samples sharing a timestamp with a non-zero displacement yield
|
|
50
|
+
* `±Infinity`; callers treat that as a flick.
|
|
51
|
+
*
|
|
52
|
+
* (The Clock wheel has a sibling of this function measuring `scrollTop` instead;
|
|
53
|
+
* they are kept separate so gesture work cannot regress the picker.)
|
|
54
|
+
*/
|
|
55
|
+
export declare function getReleaseVelocity(samples: readonly SwipeSample[], now: number, windowMs?: number): number;
|
|
56
|
+
/** How far (ms of travel) a flick is projected past the release point. */
|
|
57
|
+
export declare const RELEASE_MOMENTUM_MS = 180;
|
|
58
|
+
/**
|
|
59
|
+
* Where the panel would come to rest if the flick kept its speed for
|
|
60
|
+
* `momentumMs`. Used to pick a snap point that respects intent: a short but fast
|
|
61
|
+
* flick should skip ahead, a long slow drag should not.
|
|
62
|
+
*
|
|
63
|
+
* Infinite velocities (samples with identical timestamps) project to the
|
|
64
|
+
* corresponding infinity, which callers clamp against their own bounds.
|
|
65
|
+
*/
|
|
66
|
+
export declare function projectRelease(position: number, velocity: number, momentumMs?: number): number;
|
|
67
|
+
/**
|
|
68
|
+
* Diminishing-returns resistance for dragging past a bound, so the panel keeps
|
|
69
|
+
* moving (the gesture stays alive) without following the finger 1:1.
|
|
70
|
+
*
|
|
71
|
+
* Asymptotic: no matter how far the finger travels, the result stays below
|
|
72
|
+
* `dimension`. `factor` is the initial stiffness — lower is stiffer.
|
|
73
|
+
*/
|
|
74
|
+
export declare function applyRubberBand(overshoot: number, dimension: number, factor?: number): number;
|
|
75
|
+
/**
|
|
76
|
+
* Marks a subtree as not starting a swipe (sliders, carousels, drag handles).
|
|
77
|
+
*
|
|
78
|
+
* Valueless — or any value other than `"mouse"` — opts the subtree out for every
|
|
79
|
+
* input type. `"mouse"` opts it out for a mouse only, which is how a region can
|
|
80
|
+
* stay swipeable under a finger while a mouse drag inside it selects text: on
|
|
81
|
+
* touch there is no drag-to-select to protect, so the two never compete.
|
|
82
|
+
*/
|
|
83
|
+
export declare const SWIPE_IGNORE_ATTRIBUTE = "data-hk-swipe-ignore";
|
|
84
|
+
export type SwipeMoveState = {
|
|
85
|
+
/** Signed displacement along the axis; positive = outward (toward dismissal). */
|
|
86
|
+
displacement: number;
|
|
87
|
+
event: PointerEvent;
|
|
88
|
+
};
|
|
89
|
+
export type SwipeEndState = {
|
|
90
|
+
displacement: number;
|
|
91
|
+
/** px/ms along the axis; positive = outward. */
|
|
92
|
+
velocity: number;
|
|
93
|
+
event: PointerEvent;
|
|
94
|
+
};
|
|
95
|
+
export type SwipeGestureOptions = {
|
|
96
|
+
/** Whether the gesture listens at all. Defaults to `true`. */
|
|
97
|
+
enabled?: boolean;
|
|
98
|
+
/** Side the panel is anchored to; sets the axis and the outward direction. */
|
|
99
|
+
side: SwipeSide;
|
|
100
|
+
/** Axial movement (px) required before the gesture is claimed. */
|
|
101
|
+
threshold?: number;
|
|
102
|
+
/**
|
|
103
|
+
* `touch-action` written on the node. `null` (the default) leaves it alone —
|
|
104
|
+
* see the note at the top of this file before passing `none`.
|
|
105
|
+
*/
|
|
106
|
+
touchAction?: string | null;
|
|
107
|
+
/** Fires once the gesture is claimed, not on pointerdown. */
|
|
108
|
+
onStart?: (event: PointerEvent) => void;
|
|
109
|
+
onMove?: (state: SwipeMoveState) => void;
|
|
110
|
+
/** Fires on pointerup, only for a claimed gesture. */
|
|
111
|
+
onEnd?: (state: SwipeEndState) => void;
|
|
112
|
+
/** Fires on pointercancel, only for a claimed gesture. */
|
|
113
|
+
onCancel?: () => void;
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Svelte action wiring a single-axis drag on `node`.
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* ```svelte
|
|
120
|
+
* <div use:swipeGesture={{ side: 'bottom', onMove: ({ displacement }) => (offset = displacement) }}>
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
export declare function swipeGesture(node: HTMLElement, options: SwipeGestureOptions): {
|
|
124
|
+
update(newOptions: SwipeGestureOptions): void;
|
|
125
|
+
destroy(): void;
|
|
126
|
+
};
|