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,103 @@
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 var FOCUSABLE_SELECTOR = [
8
+ 'a[href]',
9
+ 'area[href]',
10
+ 'button',
11
+ 'input',
12
+ 'select',
13
+ 'textarea',
14
+ 'summary',
15
+ 'iframe',
16
+ 'audio[controls]',
17
+ 'video[controls]',
18
+ '[contenteditable]:not([contenteditable="false"])',
19
+ '[tabindex]'
20
+ ].join(',');
21
+ /** True when the element can take focus by Tab: not disabled, hidden or inert, and not tabindex -1. */
22
+ export function isTabbable(element) {
23
+ return tabbableWith(element, new Map());
24
+ }
25
+ /**
26
+ * isTabbable, remembering in `hidden` whether each node is `display: none`, so
27
+ * that elements checked together read each shared ancestor's style only once.
28
+ */
29
+ function tabbableWith(element, hidden) {
30
+ if (element.tabIndex < 0 || element.matches(':disabled, input[type="hidden"]'))
31
+ return false;
32
+ if (element.closest('[inert], [hidden]'))
33
+ return false;
34
+ var view = element.ownerDocument.defaultView;
35
+ if (!view)
36
+ return true;
37
+ if (view.getComputedStyle(element).visibility === 'hidden')
38
+ return false;
39
+ for (var node = element; node; node = node.parentElement) {
40
+ var none = hidden.get(node);
41
+ if (none === undefined) {
42
+ none = view.getComputedStyle(node).display === 'none';
43
+ hidden.set(node, none);
44
+ }
45
+ if (none)
46
+ return false;
47
+ }
48
+ return true;
49
+ }
50
+ function tabbableAmong(elements) {
51
+ var hidden = new Map();
52
+ return elements.filter(function (element) { return tabbableWith(element, hidden); });
53
+ }
54
+ /**
55
+ * Where Tab should send focus, or `null` to let the browser move it as usual.
56
+ * Wraps from the last tabbable element to the first (and back for Shift+Tab),
57
+ * and pulls focus in when it is on the container itself or outside the list.
58
+ */
59
+ export function nextFocusTarget(focusables, active, direction) {
60
+ return edgeTarget(tabbableAmong(focusables), active, direction);
61
+ }
62
+ /** nextFocusTarget over elements already known to be tabbable. */
63
+ function edgeTarget(tabbable, active, direction) {
64
+ var first = tabbable[0];
65
+ var last = tabbable[tabbable.length - 1];
66
+ if (!first || !last)
67
+ return null;
68
+ var forward = direction === 'forward';
69
+ var edge = forward ? first : last;
70
+ if (!active)
71
+ return edge;
72
+ if (active === (forward ? last : first))
73
+ return edge;
74
+ if (tabbable.includes(active))
75
+ return null;
76
+ // Not in the list. Strictly between the first and the last, the browser's own
77
+ // step lands on a listed element. Anywhere else, including the container
78
+ // itself, it would leave the list, so send it to the edge.
79
+ var afterFirst = (first.compareDocumentPosition(active) & Node.DOCUMENT_POSITION_FOLLOWING) !== 0;
80
+ var beforeLast = (last.compareDocumentPosition(active) & Node.DOCUMENT_POSITION_PRECEDING) !== 0;
81
+ return afterFirst && beforeLast ? null : edge;
82
+ }
83
+ /**
84
+ * Handles a Tab keydown for a modal container: wraps focus at either end, and
85
+ * keeps it on the container when nothing inside can take it.
86
+ */
87
+ export function trapTab(event, container) {
88
+ if (event.key !== 'Tab' || event.defaultPrevented)
89
+ return;
90
+ var focusables = Array.from(container.querySelectorAll(FOCUSABLE_SELECTOR));
91
+ var direction = event.shiftKey ? 'backward' : 'forward';
92
+ var tabbable = tabbableAmong(focusables);
93
+ if (tabbable.length === 0) {
94
+ event.preventDefault();
95
+ container.focus({ preventScroll: true });
96
+ return;
97
+ }
98
+ var target = edgeTarget(tabbable, container.ownerDocument.activeElement, direction);
99
+ if (!target)
100
+ return;
101
+ event.preventDefault();
102
+ target.focus({ preventScroll: true });
103
+ }
@@ -1,7 +1,10 @@
1
1
  export * from './viewTransition';
2
2
  export * from './dragSnap';
3
+ export * from './sheetExit';
3
4
  export * from './scrollEdge';
4
5
  export * from './a11ySync';
5
6
  export * from './deviceMotion';
6
7
  export * from './swipeActions';
7
8
  export * from './refreshable';
9
+ export * from './inertSiblings';
10
+ export * from './focusTrap';
@@ -1,7 +1,10 @@
1
1
  export * from './viewTransition';
2
2
  export * from './dragSnap';
3
+ export * from './sheetExit';
3
4
  export * from './scrollEdge';
4
5
  export * from './a11ySync';
5
6
  export * from './deviceMotion';
6
7
  export * from './swipeActions';
7
8
  export * from './refreshable';
9
+ export * from './inertSiblings';
10
+ export * from './focusTrap';
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Makes the rest of the page inert while a modal surface is on screen, so
3
+ * assistive technology and the keyboard cannot reach what sits behind it.
4
+ *
5
+ * Only the siblings of the surface, and of each ancestor on the way up to
6
+ * `body`, are made inert. Ancestors are never touched: a surface mounted inside
7
+ * page content keeps that content, and so itself, interactive.
8
+ */
9
+ /** Sets `inert` on the siblings of the surface and returns a function that undoes exactly that. */
10
+ export declare function inertSiblings(surface: Element): () => void;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Makes the rest of the page inert while a modal surface is on screen, so
3
+ * assistive technology and the keyboard cannot reach what sits behind it.
4
+ *
5
+ * Only the siblings of the surface, and of each ancestor on the way up to
6
+ * `body`, are made inert. Ancestors are never touched: a surface mounted inside
7
+ * page content keeps that content, and so itself, interactive.
8
+ */
9
+ /** Sets `inert` on the siblings of the surface and returns a function that undoes exactly that. */
10
+ export function inertSiblings(surface) {
11
+ var body = surface.ownerDocument.body;
12
+ var added = [];
13
+ // Walk up, inerting the siblings at each level. Stop once the node is the body.
14
+ for (var node = surface; node !== body && node.parentElement; node = node.parentElement) {
15
+ var parent = node.parentElement;
16
+ for (var _i = 0, _a = parent.children; _i < _a.length; _i++) {
17
+ var sibling = _a[_i];
18
+ // A host-set inert is the host's: leave it, and leave it in place on restore.
19
+ if (sibling === node || sibling.hasAttribute('inert'))
20
+ continue;
21
+ sibling.setAttribute('inert', '');
22
+ added.push(sibling);
23
+ }
24
+ }
25
+ var restored = false;
26
+ return function () {
27
+ if (restored)
28
+ return;
29
+ restored = true;
30
+ for (var _i = 0, added_1 = added; _i < added_1.length; _i++) {
31
+ var element = added_1[_i];
32
+ element.removeAttribute('inert');
33
+ }
34
+ };
35
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * The reduced-motion decision shared by the sheet's exit animation and its drag
3
+ * action. Kept free of either so both can import it without a cycle.
4
+ */
5
+ /** The media query that signals a reduced-motion preference. */
6
+ export declare const REDUCED_MOTION_QUERY = "(prefers-reduced-motion: reduce)";
7
+ /**
8
+ * The slice of the browser that the reduced-motion decision reads. Passed in so
9
+ * tests can supply doubles instead of a real `matchMedia` and document.
10
+ */
11
+ export interface MotionEnvironment {
12
+ matchMedia?: ((query: string) => {
13
+ matches: boolean;
14
+ }) | undefined;
15
+ root?: {
16
+ getAttribute(name: string): string | null;
17
+ } | null | undefined;
18
+ }
19
+ /** The environment of the running page: `window.matchMedia` and the root element. */
20
+ export declare function browserMotionEnvironment(): MotionEnvironment;
21
+ /**
22
+ * Whether motion should be skipped: the OS asks for reduced motion, or the page
23
+ * set `data-reduced-motion="true"` on the root (see a11ySync).
24
+ */
25
+ export declare function prefersReducedMotion(env?: MotionEnvironment): boolean;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * The reduced-motion decision shared by the sheet's exit animation and its drag
3
+ * action. Kept free of either so both can import it without a cycle.
4
+ */
5
+ /** The media query that signals a reduced-motion preference. */
6
+ export var REDUCED_MOTION_QUERY = '(prefers-reduced-motion: reduce)';
7
+ /** The environment of the running page: `window.matchMedia` and the root element. */
8
+ export function browserMotionEnvironment() {
9
+ return {
10
+ matchMedia: typeof window !== 'undefined' && typeof window.matchMedia === 'function'
11
+ ? function (query) { return window.matchMedia(query); }
12
+ : undefined,
13
+ root: typeof document !== 'undefined' ? document.documentElement : undefined
14
+ };
15
+ }
16
+ /**
17
+ * Whether motion should be skipped: the OS asks for reduced motion, or the page
18
+ * set `data-reduced-motion="true"` on the root (see a11ySync).
19
+ */
20
+ export function prefersReducedMotion(env) {
21
+ var _a, _b;
22
+ if (env === void 0) { env = browserMotionEnvironment(); }
23
+ if (((_a = env.root) === null || _a === void 0 ? void 0 : _a.getAttribute('data-reduced-motion')) === 'true')
24
+ return true;
25
+ return ((_b = env.matchMedia) === null || _b === void 0 ? void 0 : _b.call(env, REDUCED_MOTION_QUERY).matches) === true;
26
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The decision behind dragging a sheet by its body. A press in the body only
3
+ * arms a candidate: starting a drag (and taking pointer capture) on the press
4
+ * itself would swallow taps and fight native scrolling. The candidate becomes a
5
+ * drag once the pointer has clearly moved down while the body is scrolled to
6
+ * the top, the same moment an iOS sheet hands a pull from its scroll view to the
7
+ * sheet. Anything else gives the gesture back to the page for good.
8
+ */
9
+ /** How far, in CSS px, the pointer must travel before a body press is judged a drag or not. */
10
+ export declare const BODY_DRAG_SLOP = 8;
11
+ /** What a body press has become so far: still undecided, a drag, or not a drag for the rest of the gesture. */
12
+ export type BodyDragIntent = 'pending' | 'start' | 'reject';
13
+ /** Judges a body press from its movement since pointerdown (`dx`, `dy` in px, positive right and down) and the scroll offset of the body above it: a drag only when it has travelled `slop`, mostly downward, with the body at the top. */
14
+ export declare function bodyDragIntent(dx: number, dy: number, scrollTop: number, slop?: number): BodyDragIntent;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * The decision behind dragging a sheet by its body. A press in the body only
3
+ * arms a candidate: starting a drag (and taking pointer capture) on the press
4
+ * itself would swallow taps and fight native scrolling. The candidate becomes a
5
+ * drag once the pointer has clearly moved down while the body is scrolled to
6
+ * the top, the same moment an iOS sheet hands a pull from its scroll view to the
7
+ * sheet. Anything else gives the gesture back to the page for good.
8
+ */
9
+ /** How far, in CSS px, the pointer must travel before a body press is judged a drag or not. */
10
+ export var BODY_DRAG_SLOP = 8;
11
+ /** Judges a body press from its movement since pointerdown (`dx`, `dy` in px, positive right and down) and the scroll offset of the body above it: a drag only when it has travelled `slop`, mostly downward, with the body at the top. */
12
+ export function bodyDragIntent(dx, dy, scrollTop, slop) {
13
+ if (slop === void 0) { slop = BODY_DRAG_SLOP; }
14
+ if (![dx, dy, scrollTop, slop].every(Number.isFinite))
15
+ return 'reject';
16
+ // Scrolled content owns a downward pull: it scrolls back up first.
17
+ if (scrollTop > 0)
18
+ return 'reject';
19
+ if (Math.hypot(dx, dy) < slop)
20
+ return 'pending';
21
+ // Strictly more down than sideways, which also rules out any upward move. A
22
+ // diagonal or sideways swipe stays with carousels and horizontal widgets.
23
+ if (dy > Math.abs(dx))
24
+ return 'start';
25
+ return 'reject';
26
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Exit animation for a bottom sheet: slide it down and out, then report that it
3
+ * can be unmounted. Shared by every close path (close button, Escape, backdrop,
4
+ * drag dismiss, the parent clearing `isOpen`), so none of them pops the sheet
5
+ * away.
6
+ *
7
+ * The slide is the `transform` transition on `--sheet-y`. Completion is the
8
+ * `transitionend` for `transform` on the sheet itself, with a bounded fallback
9
+ * timer in case that event never fires (an interrupted or zero-length
10
+ * transition). Under reduced motion there is no animation at all.
11
+ */
12
+ import { type MotionEnvironment } from './reducedMotion';
13
+ export { REDUCED_MOTION_QUERY, browserMotionEnvironment, prefersReducedMotion, type MotionEnvironment } from './reducedMotion';
14
+ /** The `--sheet-y` value of a sheet that is fully off the bottom of the screen. */
15
+ export declare const SHEET_CLOSED_Y = "100vh";
16
+ /**
17
+ * How long to wait for `transitionend` before unmounting anyway, in ms. A little
18
+ * longer than the 0.4s spring so a normal exit always finishes on the event.
19
+ */
20
+ export declare const SHEET_EXIT_FALLBACK_MS = 520;
21
+ export interface SheetExitOptions {
22
+ /** The sheet container: the element carrying `--sheet-y` and the transform transition. */
23
+ element: HTMLElement;
24
+ /** Called once, when the exit has finished and the sheet may be unmounted. */
25
+ onDone: () => void;
26
+ /** `--sheet-y` to return to if the exit is cancelled. Defaults to the value at the start. */
27
+ restoreY?: string | undefined;
28
+ /** Motion preferences. Defaults to the running page. */
29
+ env?: MotionEnvironment | undefined;
30
+ }
31
+ export interface SheetExit {
32
+ /** Abandon the exit and slide back to where the sheet was. No-op once finished. */
33
+ cancel(): void;
34
+ }
35
+ /** Slide the sheet down and out and call `onDone` when the transform transition ends, the fallback timer fires, or at once under reduced motion. */
36
+ export declare function startSheetExit(options: SheetExitOptions): SheetExit;
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Exit animation for a bottom sheet: slide it down and out, then report that it
3
+ * can be unmounted. Shared by every close path (close button, Escape, backdrop,
4
+ * drag dismiss, the parent clearing `isOpen`), so none of them pops the sheet
5
+ * away.
6
+ *
7
+ * The slide is the `transform` transition on `--sheet-y`. Completion is the
8
+ * `transitionend` for `transform` on the sheet itself, with a bounded fallback
9
+ * timer in case that event never fires (an interrupted or zero-length
10
+ * transition). Under reduced motion there is no animation at all.
11
+ */
12
+ import { SHEET_TRANSITION } from './dragSnapMath';
13
+ import { browserMotionEnvironment, prefersReducedMotion } from './reducedMotion';
14
+ // Re-exported so the reduced-motion helpers stay importable from here.
15
+ export { REDUCED_MOTION_QUERY, browserMotionEnvironment, prefersReducedMotion } from './reducedMotion';
16
+ /** The `--sheet-y` value of a sheet that is fully off the bottom of the screen. */
17
+ export var SHEET_CLOSED_Y = '100vh';
18
+ /**
19
+ * How long to wait for `transitionend` before unmounting anyway, in ms. A little
20
+ * longer than the 0.4s spring so a normal exit always finishes on the event.
21
+ */
22
+ export var SHEET_EXIT_FALLBACK_MS = 520;
23
+ /** Slide the sheet down and out and call `onDone` when the transform transition ends, the fallback timer fires, or at once under reduced motion. */
24
+ export function startSheetExit(options) {
25
+ var _a;
26
+ var element = options.element, onDone = options.onDone;
27
+ if (prefersReducedMotion((_a = options.env) !== null && _a !== void 0 ? _a : browserMotionEnvironment())) {
28
+ onDone();
29
+ return { cancel: function () { } };
30
+ }
31
+ var startY = element.style.getPropertyValue('--sheet-y');
32
+ var settled = false;
33
+ var onEnd = function (event) {
34
+ if (event.target !== element)
35
+ return;
36
+ if (event.propertyName !== 'transform')
37
+ return;
38
+ finish();
39
+ };
40
+ var timer = setTimeout(function () { return finish(); }, SHEET_EXIT_FALLBACK_MS);
41
+ function release() {
42
+ settled = true;
43
+ element.removeEventListener('transitionend', onEnd);
44
+ clearTimeout(timer);
45
+ }
46
+ function finish() {
47
+ if (settled)
48
+ return;
49
+ release();
50
+ onDone();
51
+ }
52
+ element.addEventListener('transitionend', onEnd);
53
+ element.style.transition = SHEET_TRANSITION;
54
+ element.style.setProperty('--sheet-y', SHEET_CLOSED_Y);
55
+ return {
56
+ cancel: function () {
57
+ var _a;
58
+ if (settled)
59
+ return;
60
+ release();
61
+ element.style.transition = SHEET_TRANSITION;
62
+ element.style.setProperty('--sheet-y', (_a = options.restoreY) !== null && _a !== void 0 ? _a : startY);
63
+ }
64
+ };
65
+ }
@@ -3,7 +3,12 @@
3
3
  * `.swipeActions(edge:allowsFullSwipe:)`.
4
4
  *
5
5
  * The node is translated by the `--swipe-x` custom property and tagged with
6
- * `data-swipe-state`; the action strip itself is the caller's markup.
6
+ * `data-swipe-state`; the action strip itself is the caller's markup. When
7
+ * `stateElement` is given (an ancestor both the content and the strip can see),
8
+ * the same properties are mirrored onto it, together with `--swipe-reveal`, the
9
+ * unsigned distance the content has travelled. A strip with
10
+ * `min-width: var(--swipe-reveal)` then grows to fill exactly the revealed
11
+ * space, so no gap opens when the row is dragged past the action width.
7
12
  *
8
13
  * Usage:
9
14
  * <div use:swipeActions={{
@@ -29,6 +34,11 @@ export interface SwipeActionsOptions {
29
34
  actionsWidth?: number | undefined;
30
35
  /** The action strip. Measured lazily, so it needs no layout on first paint. */
31
36
  actionsElement?: HTMLElement | null | undefined;
37
+ /**
38
+ * Optional ancestor that also receives `--swipe-x`, `--swipe-reveal`,
39
+ * `data-swipe-state` and, while a drag is in progress, `data-swipe-dragging`.
40
+ */
41
+ stateElement?: HTMLElement | null | undefined;
32
42
  /** Overrides the measured row width; needed where layout is unavailable. */
33
43
  rowWidth?: number | undefined;
34
44
  allowsFullSwipe?: boolean | undefined;
@@ -46,11 +56,10 @@ export declare const DEFAULT_SWIPE_VELOCITY = 500;
46
56
  export declare const SWIPE_SLOP = 4;
47
57
  /** Direction the content travels to reveal this edge's actions. */
48
58
  export declare function edgeSign(edge: SwipeEdge): number;
49
- /**
50
- * Constrain a raw pointer delta to the row's travel: one-to-one up to the
51
- * limit, damped beyond it, and zero against the configured edge.
52
- */
59
+ /** Constrain a raw pointer delta to the row's travel: one-to-one to the limit, damped beyond it, never past the row width, and zero against the configured edge. */
53
60
  export declare function clampSwipeOffset(rawDx: number, geo: SwipeGeometry): number;
61
+ /** The unsigned distance a given offset has slid the content, in px. */
62
+ export declare function revealedWidth(offset: number): number;
54
63
  /** Where the row settles once the pointer is released. */
55
64
  export declare function resolveSwipeState(offset: number, velocity: number, geo: SwipeGeometry): SwipeState;
56
65
  /** The resting offset for a settled state. */
@@ -3,7 +3,12 @@
3
3
  * `.swipeActions(edge:allowsFullSwipe:)`.
4
4
  *
5
5
  * The node is translated by the `--swipe-x` custom property and tagged with
6
- * `data-swipe-state`; the action strip itself is the caller's markup.
6
+ * `data-swipe-state`; the action strip itself is the caller's markup. When
7
+ * `stateElement` is given (an ancestor both the content and the strip can see),
8
+ * the same properties are mirrored onto it, together with `--swipe-reveal`, the
9
+ * unsigned distance the content has travelled. A strip with
10
+ * `min-width: var(--swipe-reveal)` then grows to fill exactly the revealed
11
+ * space, so no gap opens when the row is dragged past the action width.
7
12
  *
8
13
  * Usage:
9
14
  * <div use:swipeActions={{
@@ -26,7 +31,7 @@ var __assign = (this && this.__assign) || function () {
26
31
  };
27
32
  // The velocity helpers are axis-agnostic -- `y` is simply "position along the
28
33
  // drag axis", so the x-axis gesture reuses them rather than cloning them.
29
- import { computeVelocity, pushSample } from './dragSnap';
34
+ import { computeVelocity, pushSample } from './dragSnapMath';
30
35
  /** Fraction of the row that must be crossed to arm a full swipe. */
31
36
  export var DEFAULT_FULL_SWIPE_THRESHOLD = 0.5;
32
37
  /** How much of an overshoot past the limit is tracked. */
@@ -40,10 +45,7 @@ export function edgeSign(edge) {
40
45
  // Unknown edges fall back to trailing, matching SwiftUI's default.
41
46
  return edge === 'leading' ? 1 : -1;
42
47
  }
43
- /**
44
- * Constrain a raw pointer delta to the row's travel: one-to-one up to the
45
- * limit, damped beyond it, and zero against the configured edge.
46
- */
48
+ /** Constrain a raw pointer delta to the row's travel: one-to-one to the limit, damped beyond it, never past the row width, and zero against the configured edge. */
47
49
  export function clampSwipeOffset(rawDx, geo) {
48
50
  if (!Number.isFinite(rawDx))
49
51
  return 0;
@@ -60,7 +62,14 @@ export function clampSwipeOffset(rawDx, geo) {
60
62
  ? Math.max(geo.actionsWidth, geo.rowWidth)
61
63
  : geo.actionsWidth;
62
64
  var travel = magnitude <= limit ? magnitude : limit + (magnitude - limit) * DEFAULT_SWIPE_RUBBER_BAND;
63
- return travel * sign;
65
+ // The content cannot leave the row, however far the pointer goes. An
66
+ // unmeasured row (0) puts no cap on it.
67
+ var capped = geo.rowWidth > 0 ? Math.min(travel, geo.rowWidth) : travel;
68
+ return capped * sign;
69
+ }
70
+ /** The unsigned distance a given offset has slid the content, in px. */
71
+ export function revealedWidth(offset) {
72
+ return Number.isFinite(offset) ? Math.abs(offset) : 0;
64
73
  }
65
74
  /** Where the row settles once the pointer is released. */
66
75
  export function resolveSwipeState(offset, velocity, geo) {
@@ -93,8 +102,25 @@ export function offsetForState(state, geo) {
93
102
  return geo.rowWidth * sign;
94
103
  return 0;
95
104
  }
105
+ /**
106
+ * The strip's own width. Its `min-width` (which tracks the revealed distance)
107
+ * is lifted for the read, otherwise a strip stretched by an earlier drag would
108
+ * report its stretched width and raise its own limit.
109
+ */
96
110
  function measureWidth(el) {
97
- return el ? el.getBoundingClientRect().width : 0;
111
+ if (!el)
112
+ return 0;
113
+ var previous = el.style.minWidth;
114
+ el.style.minWidth = '0px';
115
+ var width = el.getBoundingClientRect().width;
116
+ el.style.minWidth = previous;
117
+ return width;
118
+ }
119
+ function clearState(el) {
120
+ el.style.removeProperty('--swipe-x');
121
+ el.style.removeProperty('--swipe-reveal');
122
+ el.removeAttribute('data-swipe-state');
123
+ el.removeAttribute('data-swipe-dragging');
98
124
  }
99
125
  export function swipeActions(node, initialOptions) {
100
126
  var options = __assign({}, initialOptions);
@@ -105,21 +131,57 @@ export function swipeActions(node, initialOptions) {
105
131
  var claimed = false;
106
132
  var abandoned = false;
107
133
  var samples = [];
134
+ // The strip's natural width, read once per gesture. Re-reading it on every
135
+ // move would let a strip that grows with the drag raise its own limit.
136
+ var naturalWidth = null;
137
+ function actionsWidth() {
138
+ if (options.actionsWidth !== undefined)
139
+ return options.actionsWidth;
140
+ if (naturalWidth === null) {
141
+ var measured = measureWidth(options.actionsElement);
142
+ // 0 means "not laid out yet": do not remember it.
143
+ if (measured > 0)
144
+ naturalWidth = measured;
145
+ return measured;
146
+ }
147
+ return naturalWidth;
148
+ }
108
149
  function geometry() {
109
- var _a, _b, _c, _d;
150
+ var _a, _b, _c;
110
151
  return {
111
152
  edge: (_a = options.edge) !== null && _a !== void 0 ? _a : 'trailing',
112
- actionsWidth: (_b = options.actionsWidth) !== null && _b !== void 0 ? _b : measureWidth(options.actionsElement),
113
- rowWidth: (_c = options.rowWidth) !== null && _c !== void 0 ? _c : node.getBoundingClientRect().width,
114
- allowsFullSwipe: (_d = options.allowsFullSwipe) !== null && _d !== void 0 ? _d : false
153
+ actionsWidth: actionsWidth(),
154
+ rowWidth: (_b = options.rowWidth) !== null && _b !== void 0 ? _b : node.getBoundingClientRect().width,
155
+ allowsFullSwipe: (_c = options.allowsFullSwipe) !== null && _c !== void 0 ? _c : false
115
156
  };
116
157
  }
158
+ /** Every element the gesture reports to: the node, plus the optional ancestor. */
159
+ function targets() {
160
+ return options.stateElement ? [node, options.stateElement] : [node];
161
+ }
117
162
  function applyOffset(offset) {
118
- node.style.setProperty('--swipe-x', "".concat(offset, "px"));
163
+ for (var _i = 0, _a = targets(); _i < _a.length; _i++) {
164
+ var el = _a[_i];
165
+ el.style.setProperty('--swipe-x', "".concat(offset, "px"));
166
+ el.style.setProperty('--swipe-reveal', "".concat(revealedWidth(offset), "px"));
167
+ }
168
+ }
169
+ function setDragging(on) {
170
+ for (var _i = 0, _a = targets(); _i < _a.length; _i++) {
171
+ var el = _a[_i];
172
+ if (on)
173
+ el.setAttribute('data-swipe-dragging', 'true');
174
+ else
175
+ el.removeAttribute('data-swipe-dragging');
176
+ }
119
177
  }
120
178
  function settle(next) {
121
179
  state = next;
122
- node.setAttribute('data-swipe-state', next);
180
+ setDragging(false);
181
+ for (var _i = 0, _a = targets(); _i < _a.length; _i++) {
182
+ var el = _a[_i];
183
+ el.setAttribute('data-swipe-state', next);
184
+ }
123
185
  applyOffset(offsetForState(next, geometry()));
124
186
  }
125
187
  function onPointerDown(event) {
@@ -132,6 +194,9 @@ export function swipeActions(node, initialOptions) {
132
194
  startY = event.clientY;
133
195
  claimed = false;
134
196
  abandoned = false;
197
+ // Measure the strip once, now, before the drag can stretch it.
198
+ naturalWidth = null;
199
+ actionsWidth();
135
200
  samples = [{ y: event.clientX, t: performance.now() }];
136
201
  }
137
202
  function onPointerMove(event) {
@@ -148,6 +213,7 @@ export function swipeActions(node, initialOptions) {
148
213
  if (Math.abs(dx) < SWIPE_SLOP)
149
214
  return;
150
215
  claimed = true;
216
+ setDragging(true);
151
217
  }
152
218
  var resting = offsetForState(state, geometry());
153
219
  applyOffset(clampSwipeOffset(resting + dx, geometry()));
@@ -186,7 +252,12 @@ export function swipeActions(node, initialOptions) {
186
252
  settle('closed');
187
253
  return {
188
254
  update: function (newOptions) {
255
+ var previous = options.stateElement;
189
256
  options = __assign({}, newOptions);
257
+ naturalWidth = null;
258
+ // A swapped-out ancestor must not keep stale state.
259
+ if (previous && previous !== options.stateElement)
260
+ clearState(previous);
190
261
  settle(state);
191
262
  },
192
263
  destroy: function () {
@@ -194,8 +265,10 @@ export function swipeActions(node, initialOptions) {
194
265
  node.removeEventListener('pointermove', onPointerMove);
195
266
  node.removeEventListener('pointerup', onPointerUp);
196
267
  node.removeEventListener('pointercancel', onPointerCancel);
197
- node.style.removeProperty('--swipe-x');
198
- node.removeAttribute('data-swipe-state');
268
+ for (var _i = 0, _a = targets(); _i < _a.length; _i++) {
269
+ var el = _a[_i];
270
+ clearState(el);
271
+ }
199
272
  }
200
273
  };
201
274
  }
@@ -6,7 +6,8 @@
6
6
  * `view-transition-name` animate between their old and new positions.
7
7
  *
8
8
  * Falls through to invoking the callback directly when the API is
9
- * unavailable (SSR, older browsers, or Firefox behind a flag).
9
+ * unavailable (SSR, older browsers, or Firefox behind a flag), and under
10
+ * reduced motion.
10
11
  */
11
12
  type ViewTransitionLike = {
12
13
  finished: Promise<void>;
@@ -6,8 +6,10 @@
6
6
  * `view-transition-name` animate between their old and new positions.
7
7
  *
8
8
  * Falls through to invoking the callback directly when the API is
9
- * unavailable (SSR, older browsers, or Firefox behind a flag).
9
+ * unavailable (SSR, older browsers, or Firefox behind a flag), and under
10
+ * reduced motion.
10
11
  */
12
+ import { prefersReducedMotion } from './reducedMotion';
11
13
  export function withGlassTransition(callback) {
12
14
  if (typeof document === 'undefined') {
13
15
  var result = callback();
@@ -17,7 +19,7 @@ export function withGlassTransition(callback) {
17
19
  return { supported: false };
18
20
  }
19
21
  var doc = document;
20
- if (typeof doc.startViewTransition !== 'function') {
22
+ if (typeof doc.startViewTransition !== 'function' || prefersReducedMotion()) {
21
23
  var result = callback();
22
24
  if (result instanceof Promise) {
23
25
  return result.then(function () { return ({ supported: false }); });