glasslikeui 1.6.0 → 1.7.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.
- package/LICENSE +373 -674
- package/README.md +68 -4
- package/dist/actions/dragSnap.d.ts +16 -39
- package/dist/actions/dragSnap.js +6 -224
- package/dist/actions/dragSnapMath.d.ts +68 -0
- package/dist/actions/dragSnapMath.js +165 -0
- package/dist/actions/dragSurface.d.ts +26 -0
- package/dist/actions/dragSurface.js +187 -0
- package/dist/actions/focusTrap.d.ts +22 -0
- package/dist/actions/focusTrap.js +83 -0
- package/dist/actions/index.d.ts +3 -0
- package/dist/actions/index.js +3 -0
- package/dist/actions/inertSiblings.d.ts +10 -0
- package/dist/actions/inertSiblings.js +35 -0
- package/dist/actions/reducedMotion.d.ts +25 -0
- package/dist/actions/reducedMotion.js +26 -0
- package/dist/actions/sheetBodyDrag.d.ts +14 -0
- package/dist/actions/sheetBodyDrag.js +26 -0
- package/dist/actions/sheetExit.d.ts +36 -0
- package/dist/actions/sheetExit.js +65 -0
- package/dist/actions/swipeActions.d.ts +14 -5
- package/dist/actions/swipeActions.js +89 -16
- package/dist/components/glass/Glass.svelte +41 -6
- package/dist/components/glass/Sheet.svelte +227 -60
- package/dist/components/glass/Sheet.svelte.d.ts +7 -1
- package/dist/components/interactive/Button.svelte +4 -2
- package/dist/components/list/ListRow.svelte +33 -10
- package/dist/constants/shapes.d.ts +7 -0
- package/dist/constants/shapes.js +16 -0
- package/dist/styles/glasslikeui.css +31 -0
- package/dist/styles/lens.css +41 -0
- package/package.json +2 -2
|
@@ -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
|
+
* 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;
|
|
@@ -0,0 +1,83 @@
|
|
|
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
|
+
if (element.tabIndex < 0 || element.matches(':disabled, input[type="hidden"]'))
|
|
24
|
+
return false;
|
|
25
|
+
if (element.closest('[inert], [hidden]'))
|
|
26
|
+
return false;
|
|
27
|
+
var view = element.ownerDocument.defaultView;
|
|
28
|
+
if (!view)
|
|
29
|
+
return true;
|
|
30
|
+
if (view.getComputedStyle(element).visibility === 'hidden')
|
|
31
|
+
return false;
|
|
32
|
+
for (var node = element; node; node = node.parentElement) {
|
|
33
|
+
if (view.getComputedStyle(node).display === 'none')
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Where Tab should send focus, or `null` to let the browser move it as usual.
|
|
40
|
+
* Wraps from the last tabbable element to the first (and back for Shift+Tab),
|
|
41
|
+
* and pulls focus in when it is on the container itself or outside the list.
|
|
42
|
+
*/
|
|
43
|
+
export function nextFocusTarget(focusables, active, direction) {
|
|
44
|
+
var tabbable = focusables.filter(isTabbable);
|
|
45
|
+
var first = tabbable[0];
|
|
46
|
+
var last = tabbable[tabbable.length - 1];
|
|
47
|
+
if (!first || !last)
|
|
48
|
+
return null;
|
|
49
|
+
var forward = direction === 'forward';
|
|
50
|
+
var edge = forward ? first : last;
|
|
51
|
+
if (!active)
|
|
52
|
+
return edge;
|
|
53
|
+
if (active === (forward ? last : first))
|
|
54
|
+
return edge;
|
|
55
|
+
if (tabbable.includes(active))
|
|
56
|
+
return null;
|
|
57
|
+
// Not in the list. Strictly between the first and the last, the browser's own
|
|
58
|
+
// step lands on a listed element. Anywhere else, including the container
|
|
59
|
+
// itself, it would leave the list, so send it to the edge.
|
|
60
|
+
var afterFirst = (first.compareDocumentPosition(active) & Node.DOCUMENT_POSITION_FOLLOWING) !== 0;
|
|
61
|
+
var beforeLast = (last.compareDocumentPosition(active) & Node.DOCUMENT_POSITION_PRECEDING) !== 0;
|
|
62
|
+
return afterFirst && beforeLast ? null : edge;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Handles a Tab keydown for a modal container: wraps focus at either end, and
|
|
66
|
+
* keeps it on the container when nothing inside can take it.
|
|
67
|
+
*/
|
|
68
|
+
export function trapTab(event, container) {
|
|
69
|
+
if (event.key !== 'Tab' || event.defaultPrevented)
|
|
70
|
+
return;
|
|
71
|
+
var focusables = Array.from(container.querySelectorAll(FOCUSABLE_SELECTOR));
|
|
72
|
+
var direction = event.shiftKey ? 'backward' : 'forward';
|
|
73
|
+
if (!focusables.some(isTabbable)) {
|
|
74
|
+
event.preventDefault();
|
|
75
|
+
container.focus({ preventScroll: true });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
var target = nextFocusTarget(focusables, container.ownerDocument.activeElement, direction);
|
|
79
|
+
if (!target)
|
|
80
|
+
return;
|
|
81
|
+
event.preventDefault();
|
|
82
|
+
target.focus({ preventScroll: true });
|
|
83
|
+
}
|
package/dist/actions/index.d.ts
CHANGED
|
@@ -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';
|
package/dist/actions/index.js
CHANGED
|
@@ -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. */
|