glasslikeui 1.6.1 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/LICENSE +373 -674
  2. package/README.md +299 -4
  3. package/dist/actions/dragSnap.d.ts +16 -39
  4. package/dist/actions/dragSnap.js +6 -224
  5. package/dist/actions/dragSnapMath.d.ts +68 -0
  6. package/dist/actions/dragSnapMath.js +166 -0
  7. package/dist/actions/dragSurface.d.ts +26 -0
  8. package/dist/actions/dragSurface.js +187 -0
  9. package/dist/actions/focusRingFit.d.ts +22 -0
  10. package/dist/actions/focusRingFit.js +115 -0
  11. package/dist/actions/focusTrap.d.ts +22 -0
  12. package/dist/actions/focusTrap.js +103 -0
  13. package/dist/actions/index.d.ts +3 -0
  14. package/dist/actions/index.js +3 -0
  15. package/dist/actions/inertSiblings.d.ts +10 -0
  16. package/dist/actions/inertSiblings.js +35 -0
  17. package/dist/actions/reducedMotion.d.ts +25 -0
  18. package/dist/actions/reducedMotion.js +26 -0
  19. package/dist/actions/sheetBodyDrag.d.ts +14 -0
  20. package/dist/actions/sheetBodyDrag.js +26 -0
  21. package/dist/actions/sheetExit.d.ts +36 -0
  22. package/dist/actions/sheetExit.js +65 -0
  23. package/dist/actions/swipeActions.d.ts +14 -5
  24. package/dist/actions/swipeActions.js +89 -16
  25. package/dist/actions/viewTransition.d.ts +2 -1
  26. package/dist/actions/viewTransition.js +4 -2
  27. package/dist/components/glass/Glass.svelte +97 -6
  28. package/dist/components/glass/GlassMorph.svelte +21 -0
  29. package/dist/components/glass/Sheet.svelte +243 -61
  30. package/dist/components/glass/Sheet.svelte.d.ts +7 -1
  31. package/dist/components/interactive/Badge.svelte +2 -1
  32. package/dist/components/interactive/Button.svelte +164 -18
  33. package/dist/components/interactive/IconButton.svelte +23 -1
  34. package/dist/components/interactive/TabView.svelte +24 -1
  35. package/dist/components/interactive/Toggle.svelte +22 -4
  36. package/dist/components/layout/ScrollView.svelte +6 -0
  37. package/dist/components/list/ListRow.svelte +43 -9
  38. package/dist/components/menu/Menu.svelte +23 -0
  39. package/dist/components/menu/MenuItem.svelte +15 -2
  40. package/dist/components/navigation/NavigationLink.svelte +38 -2
  41. package/dist/components/typography/Text.svelte +2 -2
  42. package/dist/constants/shapes.d.ts +7 -0
  43. package/dist/constants/shapes.js +16 -0
  44. package/dist/styles/glasslikeui.css +143 -15
  45. package/dist/styles/lens.css +77 -0
  46. package/package.json +2 -2
@@ -0,0 +1,68 @@
1
+ /**
2
+ * The pure helpers behind dragSnap and dragSurface: constants, the maths that
3
+ * turns a drag into a fraction, and the release decisions. Internal; dragSnap
4
+ * re-exports the public names.
5
+ */
6
+ /**
7
+ * The inline transition written for every animated move. It reads the
8
+ * reduced-motion aware token, so `prefers-reduced-motion` and
9
+ * `data-reduced-motion` (which set it to 0s) switch the animation off.
10
+ */
11
+ export declare const SHEET_TRANSITION = "transform var(--transition-spring, 0.4s cubic-bezier(0.32, 0.72, 0, 1))";
12
+ /**
13
+ * Share of the smallest detent's height that the sheet must be pulled below it,
14
+ * on release, to count as a dismissal. 0.3 means a sheet at a 50% detent
15
+ * dismisses once dragged below 35% of the viewport.
16
+ */
17
+ export declare const SHEET_DISMISS_FRACTION = 0.3;
18
+ /** The `--sheet-y` value for a visible fraction of the viewport (1 = fully open). */
19
+ export declare function sheetYForFraction(fraction: number): string;
20
+ export interface PointerSample {
21
+ y: number;
22
+ t: number;
23
+ }
24
+ /** Clamp a detent index into range; 0 when there are no detents. */
25
+ export declare function clampIndex(i: number, len: number): number;
26
+ /** The fraction at an index, falling back to the first detent then 0. */
27
+ export declare function detentAt(detents: number[], index: number): number;
28
+ /**
29
+ * Rubber-band a fraction dragged beyond the detent range, so overshoot moves
30
+ * at a reduced rate instead of tracking the pointer one-to-one.
31
+ */
32
+ export declare function applyRubberBand(fraction: number, minDetent: number, maxDetent: number, rubber: number): number;
33
+ /** The fraction to render for a dragged position: rubber-banded past the detents, except that a dismissable sheet follows the finger one to one down to closed. */
34
+ export declare function resolveDragFraction(fraction: number, minDetent: number, maxDetent: number, rubber: number, canDismiss: boolean): number;
35
+ /** Whether a release dismisses the sheet: pulled more than SHEET_DISMISS_FRACTION of the smallest detent below it, or flicked down faster than the velocity threshold at or below it (`velocity` is px/s along y, positive downward). */
36
+ export declare function shouldDismiss(finalFraction: number, minDetent: number, velocity: number, velocityThreshold: number): boolean;
37
+ /** Whether a press at `target` may start a sheet drag: it sits in an element matching `regionSelector` inside `boundary`, with no button, link or field in between. */
38
+ export declare function isDragStartAllowed(target: Element | null, boundary: Element, regionSelector: string): boolean;
39
+ /** Elements a press on which is meant for the element, never for a drag. */
40
+ export declare const DRAG_BLOCKER_SELECTOR = "button, a[href], input, select, textarea, summary, [role=\"button\"], [role=\"link\"], [role=\"menuitem\"], [contenteditable]:not([contenteditable=\"false\"])";
41
+ /**
42
+ * Pick the detent to settle on: a flick faster than the threshold moves one
43
+ * step in its direction, otherwise snap to whichever detent is nearest.
44
+ */
45
+ export declare function chooseNextIndex(currentIndex: number, velocity: number, threshold: number, finalFraction: number, detents: number[]): number;
46
+ /** Index of the detent closest to `fraction`; ties resolve to the lower index. */
47
+ export declare function nearestDetentIndex(fraction: number, detents: number[]): number;
48
+ /** How many pointer samples the velocity window keeps. */
49
+ export declare const SAMPLE_WINDOW = 5;
50
+ /**
51
+ * Append a sample, keeping at most SAMPLE_WINDOW of them. Velocity is measured
52
+ * across this window so a release reflects the recent flick rather than the
53
+ * whole gesture. Returns a new array; the input is not mutated.
54
+ */
55
+ export declare function pushSample(samples: PointerSample[], sample: PointerSample, max?: number): PointerSample[];
56
+ /** Average pointer velocity in px/s across the sample window; 0 if unknowable. */
57
+ export declare function computeVelocity(samples: PointerSample[]): number;
58
+ export declare function findScrollableAncestor(el: Element | null, stopAt: Element): HTMLElement | null;
59
+ /** The largest scroll offset among the vertical scrollers from `el` up to `stopAt`, so a nested scroller at its top inside a scrolled one still counts as scrolled. */
60
+ export declare function highestScrollTop(el: Element | null, stopAt: Element): number;
61
+ export declare const DEFAULT_DETENT_FRACTIONS: {
62
+ readonly small: 0.25;
63
+ readonly medium: 0.5;
64
+ readonly large: 0.9;
65
+ readonly fullscreen: 1;
66
+ };
67
+ export type SheetDetentName = keyof typeof DEFAULT_DETENT_FRACTIONS;
68
+ export declare function detentFractions(names: readonly SheetDetentName[]): number[];
@@ -0,0 +1,166 @@
1
+ /**
2
+ * The pure helpers behind dragSnap and dragSurface: constants, the maths that
3
+ * turns a drag into a fraction, and the release decisions. Internal; dragSnap
4
+ * re-exports the public names.
5
+ */
6
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
7
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
8
+ if (ar || !(i in from)) {
9
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
10
+ ar[i] = from[i];
11
+ }
12
+ }
13
+ return to.concat(ar || Array.prototype.slice.call(from));
14
+ };
15
+ /**
16
+ * The inline transition written for every animated move. It reads the
17
+ * reduced-motion aware token, so `prefers-reduced-motion` and
18
+ * `data-reduced-motion` (which set it to 0s) switch the animation off.
19
+ */
20
+ export var SHEET_TRANSITION = 'transform var(--transition-spring, 0.4s cubic-bezier(0.32, 0.72, 0, 1))';
21
+ /**
22
+ * Share of the smallest detent's height that the sheet must be pulled below it,
23
+ * on release, to count as a dismissal. 0.3 means a sheet at a 50% detent
24
+ * dismisses once dragged below 35% of the viewport.
25
+ */
26
+ export var SHEET_DISMISS_FRACTION = 0.3;
27
+ /** The `--sheet-y` value for a visible fraction of the viewport (1 = fully open). */
28
+ export function sheetYForFraction(fraction) {
29
+ return "".concat((1 - fraction) * 100, "vh");
30
+ }
31
+ /** Clamp a detent index into range; 0 when there are no detents. */
32
+ export function clampIndex(i, len) {
33
+ // An empty list yields 0: Math.min(i, -1) then Math.max(0, ...) collapses there.
34
+ return Math.max(0, Math.min(i, len - 1));
35
+ }
36
+ /** The fraction at an index, falling back to the first detent then 0. */
37
+ export function detentAt(detents, index) {
38
+ var _a, _b;
39
+ return (_b = (_a = detents[index]) !== null && _a !== void 0 ? _a : detents[0]) !== null && _b !== void 0 ? _b : 0;
40
+ }
41
+ /**
42
+ * Rubber-band a fraction dragged beyond the detent range, so overshoot moves
43
+ * at a reduced rate instead of tracking the pointer one-to-one.
44
+ */
45
+ export function applyRubberBand(fraction, minDetent, maxDetent, rubber) {
46
+ if (fraction > maxDetent)
47
+ return maxDetent + (fraction - maxDetent) * rubber;
48
+ if (fraction < minDetent)
49
+ return minDetent - (minDetent - fraction) * rubber;
50
+ return fraction;
51
+ }
52
+ /** The fraction to render for a dragged position: rubber-banded past the detents, except that a dismissable sheet follows the finger one to one down to closed. */
53
+ export function resolveDragFraction(fraction, minDetent, maxDetent, rubber, canDismiss) {
54
+ if (canDismiss && fraction < minDetent)
55
+ return Math.max(fraction, 0);
56
+ return applyRubberBand(fraction, minDetent, maxDetent, rubber);
57
+ }
58
+ /** Whether a release dismisses the sheet: pulled more than SHEET_DISMISS_FRACTION of the smallest detent below it, or flicked down faster than the velocity threshold at or below it (`velocity` is px/s along y, positive downward). */
59
+ export function shouldDismiss(finalFraction, minDetent, velocity, velocityThreshold) {
60
+ if (!Number.isFinite(finalFraction) || !Number.isFinite(minDetent) || minDetent <= 0) {
61
+ return false;
62
+ }
63
+ var pulledPast = minDetent - finalFraction > minDetent * SHEET_DISMISS_FRACTION;
64
+ var flickedDown = Number.isFinite(velocity) && velocity > velocityThreshold && finalFraction <= minDetent;
65
+ return pulledPast || flickedDown;
66
+ }
67
+ /** Whether a press at `target` may start a sheet drag: it sits in an element matching `regionSelector` inside `boundary`, with no button, link or field in between. */
68
+ export function isDragStartAllowed(target, boundary, regionSelector) {
69
+ if (!target || !boundary.contains(target))
70
+ return false;
71
+ for (var node = target; node && node !== boundary; node = node.parentElement) {
72
+ if (node.matches(DRAG_BLOCKER_SELECTOR))
73
+ return false;
74
+ if (node.matches(regionSelector))
75
+ return true;
76
+ }
77
+ return false;
78
+ }
79
+ /** Elements a press on which is meant for the element, never for a drag. */
80
+ export var DRAG_BLOCKER_SELECTOR = 'button, a[href], input, select, textarea, summary, [role="button"], [role="link"], [role="menuitem"], [contenteditable]:not([contenteditable="false"])';
81
+ /**
82
+ * Pick the detent to settle on: a flick faster than the threshold moves one
83
+ * step in its direction, otherwise snap to whichever detent is nearest.
84
+ */
85
+ export function chooseNextIndex(currentIndex, velocity, threshold, finalFraction, detents) {
86
+ if (Math.abs(velocity) > threshold) {
87
+ // Clamp the step: a currentIndex outside the list would otherwise step to
88
+ // an unindexable position (e.g. -1) rather than a real detent.
89
+ var stepped = velocity < 0 ? currentIndex + 1 : currentIndex - 1;
90
+ return clampIndex(stepped, detents.length);
91
+ }
92
+ return nearestDetentIndex(finalFraction, detents);
93
+ }
94
+ /** Index of the detent closest to `fraction`; ties resolve to the lower index. */
95
+ export function nearestDetentIndex(fraction, detents) {
96
+ var bestIndex = 0;
97
+ var bestDistance = Infinity;
98
+ for (var _i = 0, _a = detents.entries(); _i < _a.length; _i++) {
99
+ var _b = _a[_i], i = _b[0], value = _b[1];
100
+ if (value === undefined)
101
+ continue;
102
+ var d = Math.abs(value - fraction);
103
+ if (d < bestDistance) {
104
+ bestDistance = d;
105
+ bestIndex = i;
106
+ }
107
+ }
108
+ return bestIndex;
109
+ }
110
+ /** How many pointer samples the velocity window keeps. */
111
+ export var SAMPLE_WINDOW = 5;
112
+ /**
113
+ * Append a sample, keeping at most SAMPLE_WINDOW of them. Velocity is measured
114
+ * across this window so a release reflects the recent flick rather than the
115
+ * whole gesture. Returns a new array; the input is not mutated.
116
+ */
117
+ export function pushSample(samples, sample, max) {
118
+ if (max === void 0) { max = SAMPLE_WINDOW; }
119
+ var next = __spreadArray(__spreadArray([], samples, true), [sample], false);
120
+ return next.length > max ? next.slice(next.length - max) : next;
121
+ }
122
+ /** Average pointer velocity in px/s across the sample window; 0 if unknowable. */
123
+ export function computeVelocity(samples) {
124
+ if (samples.length < 2)
125
+ return 0;
126
+ var first = samples[0];
127
+ var last = samples[samples.length - 1];
128
+ if (!first || !last)
129
+ return 0;
130
+ var dt = (last.t - first.t) / 1000;
131
+ if (dt <= 0)
132
+ return 0;
133
+ var velocity = (last.y - first.y) / dt;
134
+ // A vanishingly small dt overflows the division to Infinity, which would
135
+ // then clear every velocity threshold and force a flick snap.
136
+ return Number.isFinite(velocity) ? velocity : 0;
137
+ }
138
+ export function findScrollableAncestor(el, stopAt) {
139
+ for (var node = el; node && node !== stopAt; node = node.parentElement) {
140
+ // Overflow first: reading the computed style costs far more, and most
141
+ // elements have nothing to scroll.
142
+ if (node instanceof HTMLElement && node.scrollHeight > node.clientHeight) {
143
+ var overflowY = getComputedStyle(node).overflowY;
144
+ if (overflowY === 'auto' || overflowY === 'scroll')
145
+ return node;
146
+ }
147
+ }
148
+ return null;
149
+ }
150
+ /** The largest scroll offset among the vertical scrollers from `el` up to `stopAt`, so a nested scroller at its top inside a scrolled one still counts as scrolled. */
151
+ export function highestScrollTop(el, stopAt) {
152
+ var highest = 0;
153
+ for (var scroller = findScrollableAncestor(el, stopAt); scroller; scroller = findScrollableAncestor(scroller.parentElement, stopAt)) {
154
+ highest = Math.max(highest, scroller.scrollTop);
155
+ }
156
+ return highest;
157
+ }
158
+ export var DEFAULT_DETENT_FRACTIONS = {
159
+ small: 0.25,
160
+ medium: 0.5,
161
+ large: 0.9,
162
+ fullscreen: 1
163
+ };
164
+ export function detentFractions(names) {
165
+ return names.map(function (n) { return DEFAULT_DETENT_FRACTIONS[n]; });
166
+ }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * The drag-to-snap engine behind dragSnap and the Sheet. It takes the public
3
+ * dragSnap options plus the body drag options, which stay internal: presses
4
+ * that `canStartAfterSlop` accepts only arm a candidate, which becomes a drag
5
+ * once it moves down past the slop with its scroller at the top (see
6
+ * sheetBodyDrag), so taps and native scrolling there keep working.
7
+ */
8
+ import type { DragSnapOptions } from './dragSnap';
9
+ import { type MotionEnvironment } from './reducedMotion';
10
+ export interface DragSurfaceOptions extends DragSnapOptions {
11
+ /**
12
+ * Decides whether a press that `canStart` refused may still become a drag
13
+ * once it has moved `dragSlop` px, mostly downward, with every scroller
14
+ * between it and the node at the top. Until then the pointer is not captured
15
+ * and the surface does not move. Omit to start drags only from `canStart`.
16
+ */
17
+ canStartAfterSlop?: ((event: PointerEvent) => boolean) | undefined;
18
+ /** Travel in px before a `canStartAfterSlop` press is judged. Default BODY_DRAG_SLOP. */
19
+ dragSlop?: number | undefined;
20
+ /** Motion preferences: under reduced motion every move is written with no transition. Defaults to the running page. */
21
+ env?: MotionEnvironment | undefined;
22
+ }
23
+ export declare function dragSurface(node: HTMLElement, initialOptions: DragSurfaceOptions): {
24
+ update(newOptions: DragSurfaceOptions): void;
25
+ destroy(): void;
26
+ };
@@ -0,0 +1,187 @@
1
+ /**
2
+ * The drag-to-snap engine behind dragSnap and the Sheet. It takes the public
3
+ * dragSnap options plus the body drag options, which stay internal: presses
4
+ * that `canStartAfterSlop` accepts only arm a candidate, which becomes a drag
5
+ * once it moves down past the slop with its scroller at the top (see
6
+ * sheetBodyDrag), so taps and native scrolling there keep working.
7
+ */
8
+ var __assign = (this && this.__assign) || function () {
9
+ __assign = Object.assign || function(t) {
10
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
11
+ s = arguments[i];
12
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
13
+ t[p] = s[p];
14
+ }
15
+ return t;
16
+ };
17
+ return __assign.apply(this, arguments);
18
+ };
19
+ import { BODY_DRAG_SLOP, bodyDragIntent } from './sheetBodyDrag';
20
+ import { browserMotionEnvironment, prefersReducedMotion } from './reducedMotion';
21
+ import { SHEET_TRANSITION, chooseNextIndex, clampIndex, computeVelocity, detentAt, findScrollableAncestor, highestScrollTop, pushSample, resolveDragFraction, shouldDismiss, sheetYForFraction } from './dragSnapMath';
22
+ export function dragSurface(node, initialOptions) {
23
+ var _a;
24
+ var options = __assign({}, initialOptions);
25
+ var currentIndex = clampIndex((_a = options.initial) !== null && _a !== void 0 ? _a : 0, options.detents.length);
26
+ var dragging = false;
27
+ var pointerId = null;
28
+ var startY = 0;
29
+ var startFraction = detentAt(options.detents, currentIndex);
30
+ var samples = [];
31
+ // A press armed by canStartAfterSlop that has not yet moved far enough.
32
+ var candidate = null;
33
+ var target = function () { var _a; return (_a = options.target) !== null && _a !== void 0 ? _a : node; };
34
+ function applyFraction(fraction, animate) {
35
+ var _a;
36
+ // target() falls back to `node`, so there is always an element.
37
+ var el = target();
38
+ // Decided per move rather than trusting the token alone: a page without the
39
+ // token stylesheet would otherwise still animate through the fallback.
40
+ var reduced = animate && prefersReducedMotion((_a = options.env) !== null && _a !== void 0 ? _a : browserMotionEnvironment());
41
+ el.style.transition = animate && !reduced ? SHEET_TRANSITION : 'none';
42
+ el.style.setProperty('--sheet-y', sheetYForFraction(fraction));
43
+ }
44
+ function onPointerDown(event) {
45
+ if (options.disabled)
46
+ return;
47
+ if (event.button !== undefined && event.button !== 0)
48
+ return;
49
+ if (options.canStart && !options.canStart(event)) {
50
+ armCandidate(event);
51
+ return;
52
+ }
53
+ if (options.respectScroll !== false) {
54
+ var scrollable = findScrollableAncestor(event.target, node);
55
+ if (scrollable && scrollable.scrollTop > 0)
56
+ return;
57
+ }
58
+ beginDrag(event);
59
+ }
60
+ function armCandidate(event) {
61
+ var _a, _b;
62
+ if (dragging || !((_a = options.canStartAfterSlop) === null || _a === void 0 ? void 0 : _a.call(options, event)))
63
+ return;
64
+ candidate = {
65
+ pointerId: event.pointerId,
66
+ x: (_b = event.clientX) !== null && _b !== void 0 ? _b : 0,
67
+ y: event.clientY,
68
+ target: event.target
69
+ };
70
+ }
71
+ function beginDrag(event) {
72
+ candidate = null;
73
+ dragging = true;
74
+ pointerId = event.pointerId;
75
+ startY = event.clientY;
76
+ startFraction = detentAt(options.detents, currentIndex);
77
+ samples = [{ y: event.clientY, t: performance.now() }];
78
+ try {
79
+ node.setPointerCapture(event.pointerId);
80
+ }
81
+ catch (_a) {
82
+ /* jsdom / test env may not support */
83
+ }
84
+ }
85
+ /** Settle a candidate on a move; true when it has just become a drag. */
86
+ function resolveCandidate(event) {
87
+ var _a, _b;
88
+ if (!candidate || event.pointerId !== candidate.pointerId)
89
+ return false;
90
+ var intent = bodyDragIntent(((_a = event.clientX) !== null && _a !== void 0 ? _a : 0) - candidate.x, event.clientY - candidate.y, highestScrollTop(candidate.target, node), (_b = options.dragSlop) !== null && _b !== void 0 ? _b : BODY_DRAG_SLOP);
91
+ if (intent === 'pending')
92
+ return false;
93
+ if (intent === 'reject') {
94
+ candidate = null;
95
+ return false;
96
+ }
97
+ // Measured from here, not from the press, so the surface does not jump by
98
+ // the slop it has already travelled.
99
+ beginDrag(event);
100
+ return true;
101
+ }
102
+ function onPointerMove(event) {
103
+ var _a, _b;
104
+ if (resolveCandidate(event))
105
+ return;
106
+ if (!dragging || event.pointerId !== pointerId)
107
+ return;
108
+ var viewportH = window.innerHeight || 1;
109
+ var deltaY = event.clientY - startY;
110
+ var fractionDelta = -deltaY / viewportH;
111
+ var newFraction = resolveDragFraction(startFraction + fractionDelta, Math.min.apply(Math, options.detents), Math.max.apply(Math, options.detents), (_a = options.rubberBand) !== null && _a !== void 0 ? _a : 0.3, options.onDismiss !== undefined);
112
+ applyFraction(newFraction, false);
113
+ samples = pushSample(samples, { y: event.clientY, t: performance.now() });
114
+ (_b = options.onDrag) === null || _b === void 0 ? void 0 : _b.call(options, deltaY, newFraction);
115
+ }
116
+ function onPointerUp(event) {
117
+ var _a, _b;
118
+ if ((candidate === null || candidate === void 0 ? void 0 : candidate.pointerId) === event.pointerId)
119
+ candidate = null;
120
+ if (!dragging || event.pointerId !== pointerId)
121
+ return;
122
+ dragging = false;
123
+ pointerId = null;
124
+ var viewportH = window.innerHeight || 1;
125
+ var finalFraction = startFraction + -(event.clientY - startY) / viewportH;
126
+ var velocity = computeVelocity(samples);
127
+ var velocityThreshold = (_a = options.velocityThreshold) !== null && _a !== void 0 ? _a : 500;
128
+ if (options.onDismiss &&
129
+ shouldDismiss(finalFraction, Math.min.apply(Math, options.detents), velocity, velocityThreshold)) {
130
+ // Down and out, then hand over: the caller's close path takes it from here.
131
+ applyFraction(0, true);
132
+ try {
133
+ node.releasePointerCapture(event.pointerId);
134
+ }
135
+ catch (_c) {
136
+ /* noop */
137
+ }
138
+ options.onDismiss();
139
+ return;
140
+ }
141
+ currentIndex = chooseNextIndex(currentIndex, velocity, velocityThreshold, finalFraction, options.detents);
142
+ var snappedFraction = detentAt(options.detents, currentIndex);
143
+ applyFraction(snappedFraction, true);
144
+ (_b = options.onSnap) === null || _b === void 0 ? void 0 : _b.call(options, currentIndex, snappedFraction);
145
+ try {
146
+ node.releasePointerCapture(event.pointerId);
147
+ }
148
+ catch (_d) {
149
+ /* noop */
150
+ }
151
+ }
152
+ function onPointerCancel(event) {
153
+ // A cancelled candidate never moved the surface, so there is nothing to undo.
154
+ if ((candidate === null || candidate === void 0 ? void 0 : candidate.pointerId) === event.pointerId)
155
+ candidate = null;
156
+ if (!dragging || event.pointerId !== pointerId)
157
+ return;
158
+ dragging = false;
159
+ pointerId = null;
160
+ applyFraction(detentAt(options.detents, currentIndex), true);
161
+ }
162
+ node.addEventListener('pointerdown', onPointerDown);
163
+ node.addEventListener('pointermove', onPointerMove);
164
+ node.addEventListener('pointerup', onPointerUp);
165
+ node.addEventListener('pointercancel', onPointerCancel);
166
+ // Start at closed (100vh offset) then animate into the initial detent
167
+ // on the next frame so the sheet's entry reads as a spring.
168
+ var el = target();
169
+ el.style.transition = 'none';
170
+ el.style.setProperty('--sheet-y', '100vh');
171
+ var raf = typeof requestAnimationFrame === 'function' ? requestAnimationFrame : function (cb) { return cb(); };
172
+ raf(function () { return applyFraction(detentAt(options.detents, currentIndex), true); });
173
+ return {
174
+ update: function (newOptions) {
175
+ var _a;
176
+ options = __assign({}, newOptions);
177
+ currentIndex = clampIndex((_a = options.initial) !== null && _a !== void 0 ? _a : currentIndex, options.detents.length);
178
+ applyFraction(detentAt(options.detents, currentIndex), true);
179
+ },
180
+ destroy: function () {
181
+ node.removeEventListener('pointerdown', onPointerDown);
182
+ node.removeEventListener('pointermove', onPointerMove);
183
+ node.removeEventListener('pointerup', onPointerUp);
184
+ node.removeEventListener('pointercancel', onPointerCancel);
185
+ }
186
+ };
187
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Moves a keyboard focus ring inside its part when a container would cut it
3
+ * off, such as a flush full width button in a surface with no padding. CSS
4
+ * cannot ask whether an outline is clipped, so it is measured when the part
5
+ * takes focus, and again on a key press while it is unmarked, which can bring
6
+ * the ring out after a click. A clipped part is marked
7
+ * `data-focus-ring="inset"`. Internal.
8
+ */
9
+ /** The four edges of a box, in viewport pixels. */
10
+ export interface Edges {
11
+ top: number;
12
+ right: number;
13
+ bottom: number;
14
+ left: number;
15
+ }
16
+ /** Whether a ring reaching `reach` px outside `box` is cut by any of the clipping boxes. */
17
+ export declare function isRingClipped(box: Edges, clips: readonly Edges[], reach: number): boolean;
18
+ /**
19
+ * Watches `node` for focus and marks the element that draws its ring, which is
20
+ * `node` itself unless `ringOf` names another. Returns a cleanup function.
21
+ */
22
+ export declare function fitFocusRing(node: HTMLElement, ringOf?: (node: HTMLElement) => Element | null): () => void;
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Moves a keyboard focus ring inside its part when a container would cut it
3
+ * off, such as a flush full width button in a surface with no padding. CSS
4
+ * cannot ask whether an outline is clipped, so it is measured when the part
5
+ * takes focus, and again on a key press while it is unmarked, which can bring
6
+ * the ring out after a click. A clipped part is marked
7
+ * `data-focus-ring="inset"`. Internal.
8
+ */
9
+ var ATTRIBUTE = 'data-focus-ring';
10
+ /** Layout rounding that does not show as a clipped ring, in px. */
11
+ var SLACK = 0.5;
12
+ /** Whether a ring reaching `reach` px outside `box` is cut by any of the clipping boxes. */
13
+ export function isRingClipped(box, clips, reach) {
14
+ var needed = reach - SLACK;
15
+ return clips.some(function (clip) {
16
+ return box.top - clip.top < needed ||
17
+ clip.right - box.right < needed ||
18
+ box.bottom - clip.bottom > -needed ||
19
+ box.left - clip.left < needed;
20
+ });
21
+ }
22
+ /**
23
+ * The inner boxes of the ancestors of `element` that clip what sticks out. An
24
+ * axis that does not clip is left unbounded. The page itself is left out.
25
+ */
26
+ function clippingBoxes(element) {
27
+ var boxes = [];
28
+ var body = element.ownerDocument.body;
29
+ // A fixed box slips past the overflow of its ancestors, up to one that holds it.
30
+ var fixed = getComputedStyle(element).position === 'fixed';
31
+ for (var node = element.parentElement; node && node !== body; node = node.parentElement) {
32
+ var style = getComputedStyle(node);
33
+ if (fixed && !holdsFixed(style))
34
+ continue;
35
+ fixed = style.position === 'fixed';
36
+ // An element with no box of its own, such as an inline one, clips nothing.
37
+ if (node.clientWidth === 0 && node.clientHeight === 0)
38
+ continue;
39
+ var across = clips(style.overflowX);
40
+ var down = clips(style.overflowY);
41
+ var rect = node.getBoundingClientRect();
42
+ var left = rect.left + node.clientLeft;
43
+ var top = rect.top + node.clientTop;
44
+ boxes.push({
45
+ top: down ? top : -Infinity,
46
+ right: across ? left + node.clientWidth : Infinity,
47
+ bottom: down ? top + node.clientHeight : Infinity,
48
+ left: across ? left : -Infinity
49
+ });
50
+ }
51
+ return boxes;
52
+ }
53
+ /** Whether an element with this style is the containing block of fixed descendants. */
54
+ function holdsFixed(style) {
55
+ return ([style.transform, style.perspective, style.filter, style.backdropFilter].some(function (value) { return !!value && value !== 'none'; }) ||
56
+ /paint|layout|strict|content/.test(style.contain) ||
57
+ /transform|perspective|filter/.test(style.willChange));
58
+ }
59
+ function clips(overflow) {
60
+ return !!overflow && overflow !== 'visible';
61
+ }
62
+ /** How far the outline drawn on `element` sticks out of it, in px; 0 or less if it does not. */
63
+ function ringReach(element) {
64
+ var style = getComputedStyle(element);
65
+ if (style.outlineStyle === 'none')
66
+ return 0;
67
+ return parseFloat(style.outlineWidth) + parseFloat(style.outlineOffset);
68
+ }
69
+ /**
70
+ * Watches `node` for focus and marks the element that draws its ring, which is
71
+ * `node` itself unless `ringOf` names another. Returns a cleanup function.
72
+ */
73
+ export function fitFocusRing(node, ringOf) {
74
+ if (ringOf === void 0) { ringOf = function (self) { return self; }; }
75
+ var frame;
76
+ function measure() {
77
+ var ring = ringOf(node);
78
+ if (!ring)
79
+ return;
80
+ var reach = ringReach(ring);
81
+ if (reach > 0 && isRingClipped(ring.getBoundingClientRect(), clippingBoxes(ring), reach)) {
82
+ ring.setAttribute(ATTRIBUTE, 'inset');
83
+ }
84
+ }
85
+ function reset() {
86
+ var _a;
87
+ if (frame !== undefined)
88
+ cancelAnimationFrame(frame);
89
+ frame = undefined;
90
+ (_a = ringOf(node)) === null || _a === void 0 ? void 0 : _a.removeAttribute(ATTRIBUTE);
91
+ }
92
+ function schedule() {
93
+ reset();
94
+ // A frame later, once the browser has scrolled the part into view.
95
+ if (typeof requestAnimationFrame === 'function')
96
+ frame = requestAnimationFrame(measure);
97
+ else
98
+ measure();
99
+ }
100
+ // :focus-visible can start matching on a key press, with no new focus event.
101
+ function onKeyDown() {
102
+ var _a;
103
+ if (!((_a = ringOf(node)) === null || _a === void 0 ? void 0 : _a.hasAttribute(ATTRIBUTE)))
104
+ schedule();
105
+ }
106
+ node.addEventListener('focus', schedule);
107
+ node.addEventListener('keydown', onKeyDown);
108
+ node.addEventListener('blur', reset);
109
+ return function () {
110
+ reset();
111
+ node.removeEventListener('focus', schedule);
112
+ node.removeEventListener('keydown', onKeyDown);
113
+ node.removeEventListener('blur', reset);
114
+ };
115
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Keeps Tab inside a modal surface. The decision of where focus goes next is a
3
+ * pure function of the focusable elements, the active element and the
4
+ * direction, so it can be tested without a browser moving focus.
5
+ */
6
+ /** Matches the elements that can take part in the Tab order. */
7
+ export declare const FOCUSABLE_SELECTOR: any;
8
+ /** Which way Tab is moving. */
9
+ export type TabDirection = 'forward' | 'backward';
10
+ /** True when the element can take focus by Tab: not disabled, hidden or inert, and not tabindex -1. */
11
+ export declare function isTabbable(element: HTMLElement): boolean;
12
+ /**
13
+ * Where Tab should send focus, or `null` to let the browser move it as usual.
14
+ * Wraps from the last tabbable element to the first (and back for Shift+Tab),
15
+ * and pulls focus in when it is on the container itself or outside the list.
16
+ */
17
+ export declare function nextFocusTarget(focusables: readonly HTMLElement[], active: Element | null, direction: TabDirection): HTMLElement | null;
18
+ /**
19
+ * Handles a Tab keydown for a modal container: wraps focus at either end, and
20
+ * keeps it on the container when nothing inside can take it.
21
+ */
22
+ export declare function trapTab(event: KeyboardEvent, container: HTMLElement): void;