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
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ Svelte component library with frosted glass materials. Pseudo-element
|
|
|
4
4
|
lensing, two-axis variant API, drag-snap sheets, View Transitions morph,
|
|
5
5
|
accessibility-aware out of the box, cross-platform typography.
|
|
6
6
|
|
|
7
|
-
Svelte 5 only.
|
|
7
|
+
Svelte 5 only. MPL-2.0 licensed.
|
|
8
8
|
|
|
9
9
|
## Install
|
|
10
10
|
|
|
@@ -107,6 +107,12 @@ attributes on non-Safari browsers:
|
|
|
107
107
|
- `capsule` -- fully rounded ends.
|
|
108
108
|
- `circle` -- 50% radius, locked to a 1:1 box.
|
|
109
109
|
|
|
110
|
+
`capsule` and `circle` always centre their content on both axes. A `rect`
|
|
111
|
+
centres its content the same way only when it holds a single text-level
|
|
112
|
+
child (`span`, `label`, `strong`, `b`, `em`, `small`, `svg` or `img`), so
|
|
113
|
+
panels and cards built from stacks, paragraphs and grids keep their layout.
|
|
114
|
+
Bare text directly inside a `rect` is not centred, wrap it in a `span`.
|
|
115
|
+
|
|
110
116
|
Buttons get the same treatment through the `glass` and
|
|
111
117
|
`glassProminent` variants, which read their blur and saturation from
|
|
112
118
|
the shared intensity tokens rather than duplicating them.
|
|
@@ -135,16 +141,60 @@ settles -- including when it rejects:
|
|
|
135
141
|
</ScrollView>
|
|
136
142
|
```
|
|
137
143
|
|
|
144
|
+
Dragging past the width of the action strip is damped by a rubber band
|
|
145
|
+
and never travels further than the row is wide. The strip grows to fill
|
|
146
|
+
exactly the space the row has uncovered, so it stays attached to the row
|
|
147
|
+
and the edge-most action (the last for trailing, the first for leading)
|
|
148
|
+
keeps its label centred as it stretches. Both rules use `:where()`, so
|
|
149
|
+
your own styles on the buttons still win.
|
|
150
|
+
|
|
138
151
|
Both are also available as standalone actions for your own markup.
|
|
139
152
|
They drive `--swipe-x` / `--refresh-pull` and `data-swipe-state` /
|
|
140
|
-
`data-refresh-phase
|
|
153
|
+
`data-refresh-phase`. Style off those. `swipeActions` also writes the
|
|
154
|
+
unsigned distance travelled to `--swipe-reveal` and sets
|
|
155
|
+
`data-swipe-dragging` while a drag is in progress. Pass `stateElement`
|
|
156
|
+
to mirror all of these onto an ancestor that your strip can read.
|
|
157
|
+
|
|
158
|
+
## Sheet
|
|
159
|
+
|
|
160
|
+
```svelte
|
|
161
|
+
<Sheet bind:isOpen title="Options" onClose={() => (selected = null)}>
|
|
162
|
+
{#snippet children()}...{/snippet}
|
|
163
|
+
</Sheet>
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
A sheet with a `title` shows a header with the title centred and an icon-only
|
|
167
|
+
close button (`aria-label="Close"`) at the trailing side. The sheet is a modal
|
|
168
|
+
`dialog`, labelled by its title (or by an `aria-label` when there is none).
|
|
169
|
+
|
|
170
|
+
- Dragging the handle or the header moves the sheet between its detents.
|
|
171
|
+
Presses on the close button or on interactive content never start a drag.
|
|
172
|
+
- The body drags too. A press in the content starts a drag once it pulls down
|
|
173
|
+
with the content scrolled to the top. Taps, scrolling and controls in the
|
|
174
|
+
content keep working, and a press on a button or field never drags. Under
|
|
175
|
+
reduced motion the snap back and the exit are instant. On engines without
|
|
176
|
+
directional `touch-action` (for example Safari on iOS), a touch pull on
|
|
177
|
+
content that scrolls is a scroll, so touch users dismiss from the handle or
|
|
178
|
+
the header there. Mouse and pen are not affected.
|
|
179
|
+
- Dragging down below the smallest detent follows the finger. Releasing past
|
|
180
|
+
30% of that detent's height below it, or flicking down quickly, dismisses
|
|
181
|
+
the sheet. Anything shorter snaps back. Set `interactiveDismissDisabled`
|
|
182
|
+
to turn this off, mirroring SwiftUI. The close button, Escape and the
|
|
183
|
+
backdrop still close the sheet.
|
|
184
|
+
- Every close path (button, Escape, backdrop, drag, or the parent setting
|
|
185
|
+
`isOpen` to `false`) slides the sheet down and fades the backdrop before
|
|
186
|
+
it unmounts. `isOpen` turns false and `onClose` fires when the close
|
|
187
|
+
starts, and reopening during the slide cancels it. With
|
|
188
|
+
`prefers-reduced-motion` or `data-reduced-motion="true"` the sheet
|
|
189
|
+
unmounts immediately.
|
|
141
190
|
|
|
142
191
|
## Actions
|
|
143
192
|
|
|
144
193
|
| Export | Purpose |
|
|
145
194
|
| -------------------------------- | ------------------------------------------------------ |
|
|
146
195
|
| `scrollEdge` | Fade a scroll container at edges meeting glass chrome. |
|
|
147
|
-
| `dragSnap` | Detent-based drag gesture (used by `Sheet`).
|
|
196
|
+
| `dragSnap` | Detent-based drag gesture (used by `Sheet`). Pass `onDismiss` to let a long pull or flick below the smallest detent dismiss. |
|
|
197
|
+
| `startSheetExit` | Slide a sheet down and out, then report completion. |
|
|
148
198
|
| `swipeActions` | Swipe a row aside to reveal actions. |
|
|
149
199
|
| `refreshable` | Pull-to-refresh gesture. |
|
|
150
200
|
| `deviceMotion` | Ties the glass highlight angle to device tilt. |
|
|
@@ -224,6 +274,20 @@ Cross-browser fallback attributes (flip at `:root`):
|
|
|
224
274
|
|
|
225
275
|
Call `syncAccessibilityPreferences()` to manage these automatically.
|
|
226
276
|
|
|
277
|
+
`Sheet` is a modal dialog. While it is open or sliding out, the siblings of the
|
|
278
|
+
sheet and of each ancestor up to `body` get the `inert` attribute, and are
|
|
279
|
+
restored exactly when it closes. Ancestors are never made inert, and anything
|
|
280
|
+
you had already made inert stays inert. Tab and Shift+Tab wrap inside the
|
|
281
|
+
sheet, and focus returns to the element that opened it.
|
|
282
|
+
|
|
283
|
+
Under reduced transparency, `Sheet` turns its blur off, including the backdrop
|
|
284
|
+
blur (`--glass-backdrop-blur`), and its background becomes near opaque
|
|
285
|
+
(`--glass-sheet-bg` at 0.98 alpha in both themes). `Menu` goes opaque with it.
|
|
286
|
+
|
|
287
|
+
The `Sheet` close button focus ring is two tones, so it holds 3:1 on both the
|
|
288
|
+
dark and the light sheet. Override `--sheet-focus-ring-inner` (the ring against
|
|
289
|
+
the button) and `--sheet-focus-ring-outer` (the outline) to rebrand it.
|
|
290
|
+
|
|
227
291
|
## Theming
|
|
228
292
|
|
|
229
293
|
All design tokens are CSS custom properties. Toggle dark / light with
|
|
@@ -263,4 +327,4 @@ npm run test:coverage # with v8 coverage
|
|
|
263
327
|
|
|
264
328
|
## License
|
|
265
329
|
|
|
266
|
-
|
|
330
|
+
Mozilla Public License 2.0 (MPL-2.0). Copyright (c) 2026 Beau Ayres. See `LICENSE`.
|
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Attaches pointer handlers to a handle element and drags a target surface
|
|
5
5
|
* between a set of named detent positions. Velocity-based snap on release,
|
|
6
|
-
* rubber-band feel when dragged past bounds.
|
|
6
|
+
* rubber-band feel when dragged past bounds. When `onDismiss` is given, pulling
|
|
7
|
+
* the surface below its smallest detent tracks the finger one to one, and a long
|
|
8
|
+
* enough pull (or a downward flick) dismisses it instead of snapping back.
|
|
7
9
|
*
|
|
8
10
|
* Usage:
|
|
9
11
|
* <div use:dragSnap={{
|
|
@@ -32,46 +34,21 @@ export interface DragSnapOptions {
|
|
|
32
34
|
respectScroll?: boolean;
|
|
33
35
|
/** Disable the gesture. */
|
|
34
36
|
disabled?: boolean;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Called when a release dismisses the surface (see `shouldDismiss`). The
|
|
39
|
+
* surface has already been sent down and out by then. When absent, dragging
|
|
40
|
+
* below the smallest detent rubber-bands and snaps back, exactly as before.
|
|
41
|
+
*/
|
|
42
|
+
onDismiss?: () => void;
|
|
43
|
+
/**
|
|
44
|
+
* Decides whether a pointer press may start a drag. Receives the pointerdown
|
|
45
|
+
* event. Omit to allow every press on the node.
|
|
46
|
+
*/
|
|
47
|
+
canStart?: (event: PointerEvent) => boolean;
|
|
39
48
|
}
|
|
40
49
|
export declare function dragSnap(node: HTMLElement, initialOptions: DragSnapOptions): {
|
|
41
50
|
update(newOptions: DragSnapOptions): void;
|
|
42
51
|
destroy(): void;
|
|
43
52
|
};
|
|
44
|
-
|
|
45
|
-
export
|
|
46
|
-
/** The fraction at an index, falling back to the first detent then 0. */
|
|
47
|
-
export declare function detentAt(detents: number[], index: number): number;
|
|
48
|
-
/**
|
|
49
|
-
* Rubber-band a fraction dragged beyond the detent range, so overshoot moves
|
|
50
|
-
* at a reduced rate instead of tracking the pointer one-to-one.
|
|
51
|
-
*/
|
|
52
|
-
export declare function applyRubberBand(fraction: number, minDetent: number, maxDetent: number, rubber: number): number;
|
|
53
|
-
/**
|
|
54
|
-
* Pick the detent to settle on: a flick faster than the threshold moves one
|
|
55
|
-
* step in its direction, otherwise snap to whichever detent is nearest.
|
|
56
|
-
*/
|
|
57
|
-
export declare function chooseNextIndex(currentIndex: number, velocity: number, threshold: number, finalFraction: number, detents: number[]): number;
|
|
58
|
-
/** Index of the detent closest to `fraction`; ties resolve to the lower index. */
|
|
59
|
-
export declare function nearestDetentIndex(fraction: number, detents: number[]): number;
|
|
60
|
-
/** How many pointer samples the velocity window keeps. */
|
|
61
|
-
export declare const SAMPLE_WINDOW = 5;
|
|
62
|
-
/**
|
|
63
|
-
* Append a sample, keeping at most SAMPLE_WINDOW of them. Velocity is measured
|
|
64
|
-
* across this window so a release reflects the recent flick rather than the
|
|
65
|
-
* whole gesture. Returns a new array; the input is not mutated.
|
|
66
|
-
*/
|
|
67
|
-
export declare function pushSample(samples: PointerSample[], sample: PointerSample, max?: number): PointerSample[];
|
|
68
|
-
/** Average pointer velocity in px/s across the sample window; 0 if unknowable. */
|
|
69
|
-
export declare function computeVelocity(samples: PointerSample[]): number;
|
|
70
|
-
export declare const DEFAULT_DETENT_FRACTIONS: {
|
|
71
|
-
readonly small: 0.25;
|
|
72
|
-
readonly medium: 0.5;
|
|
73
|
-
readonly large: 0.9;
|
|
74
|
-
readonly fullscreen: 1;
|
|
75
|
-
};
|
|
76
|
-
export type SheetDetentName = keyof typeof DEFAULT_DETENT_FRACTIONS;
|
|
77
|
-
export declare function detentFractions(names: readonly SheetDetentName[]): number[];
|
|
53
|
+
export { DEFAULT_DETENT_FRACTIONS, DRAG_BLOCKER_SELECTOR, SAMPLE_WINDOW, SHEET_DISMISS_FRACTION, SHEET_TRANSITION, applyRubberBand, chooseNextIndex, clampIndex, computeVelocity, detentAt, detentFractions, isDragStartAllowed, nearestDetentIndex, pushSample, resolveDragFraction, shouldDismiss, sheetYForFraction } from './dragSnapMath';
|
|
54
|
+
export type { PointerSample, SheetDetentName } from './dragSnapMath';
|
package/dist/actions/dragSnap.js
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Attaches pointer handlers to a handle element and drags a target surface
|
|
5
5
|
* between a set of named detent positions. Velocity-based snap on release,
|
|
6
|
-
* rubber-band feel when dragged past bounds.
|
|
6
|
+
* rubber-band feel when dragged past bounds. When `onDismiss` is given, pulling
|
|
7
|
+
* the surface below its smallest detent tracks the finger one to one, and a long
|
|
8
|
+
* enough pull (or a downward flick) dismisses it instead of snapping back.
|
|
7
9
|
*
|
|
8
10
|
* Usage:
|
|
9
11
|
* <div use:dragSnap={{
|
|
@@ -13,228 +15,8 @@
|
|
|
13
15
|
* onSnap: (i) => currentDetent = i
|
|
14
16
|
* }}>...</div>
|
|
15
17
|
*/
|
|
16
|
-
|
|
17
|
-
__assign = Object.assign || function(t) {
|
|
18
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
19
|
-
s = arguments[i];
|
|
20
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
21
|
-
t[p] = s[p];
|
|
22
|
-
}
|
|
23
|
-
return t;
|
|
24
|
-
};
|
|
25
|
-
return __assign.apply(this, arguments);
|
|
26
|
-
};
|
|
27
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
28
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
29
|
-
if (ar || !(i in from)) {
|
|
30
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
31
|
-
ar[i] = from[i];
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
35
|
-
};
|
|
18
|
+
import { dragSurface } from './dragSurface';
|
|
36
19
|
export function dragSnap(node, initialOptions) {
|
|
37
|
-
|
|
38
|
-
var options = __assign({}, initialOptions);
|
|
39
|
-
var currentIndex = clampIndex((_a = options.initial) !== null && _a !== void 0 ? _a : 0, options.detents.length);
|
|
40
|
-
var dragging = false;
|
|
41
|
-
var pointerId = null;
|
|
42
|
-
var startY = 0;
|
|
43
|
-
var startFraction = detentAt(options.detents, currentIndex);
|
|
44
|
-
var samples = [];
|
|
45
|
-
var target = function () { var _a; return (_a = options.target) !== null && _a !== void 0 ? _a : node; };
|
|
46
|
-
function applyFraction(fraction, animate) {
|
|
47
|
-
// target() falls back to `node`, so there is always an element.
|
|
48
|
-
var el = target();
|
|
49
|
-
el.style.transition = animate
|
|
50
|
-
? 'transform 0.4s cubic-bezier(0.32, 0.72, 0, 1)'
|
|
51
|
-
: 'none';
|
|
52
|
-
var offset = (1 - fraction) * 100;
|
|
53
|
-
el.style.setProperty('--sheet-y', "".concat(offset, "vh"));
|
|
54
|
-
}
|
|
55
|
-
function onPointerDown(event) {
|
|
56
|
-
if (options.disabled)
|
|
57
|
-
return;
|
|
58
|
-
if (event.button !== undefined && event.button !== 0)
|
|
59
|
-
return;
|
|
60
|
-
if (options.respectScroll !== false) {
|
|
61
|
-
var scrollable = findScrollableAncestor(event.target, node);
|
|
62
|
-
if (scrollable && scrollable.scrollTop > 0)
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
dragging = true;
|
|
66
|
-
pointerId = event.pointerId;
|
|
67
|
-
startY = event.clientY;
|
|
68
|
-
startFraction = detentAt(options.detents, currentIndex);
|
|
69
|
-
samples = [{ y: event.clientY, t: performance.now() }];
|
|
70
|
-
try {
|
|
71
|
-
node.setPointerCapture(event.pointerId);
|
|
72
|
-
}
|
|
73
|
-
catch (_a) {
|
|
74
|
-
/* jsdom / test env may not support */
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
function onPointerMove(event) {
|
|
78
|
-
var _a, _b;
|
|
79
|
-
if (!dragging || event.pointerId !== pointerId)
|
|
80
|
-
return;
|
|
81
|
-
var viewportH = window.innerHeight || 1;
|
|
82
|
-
var deltaY = event.clientY - startY;
|
|
83
|
-
var fractionDelta = -deltaY / viewportH;
|
|
84
|
-
var newFraction = applyRubberBand(startFraction + fractionDelta, Math.min.apply(Math, options.detents), Math.max.apply(Math, options.detents), (_a = options.rubberBand) !== null && _a !== void 0 ? _a : 0.3);
|
|
85
|
-
applyFraction(newFraction, false);
|
|
86
|
-
samples = pushSample(samples, { y: event.clientY, t: performance.now() });
|
|
87
|
-
(_b = options.onDrag) === null || _b === void 0 ? void 0 : _b.call(options, deltaY, newFraction);
|
|
88
|
-
}
|
|
89
|
-
function onPointerUp(event) {
|
|
90
|
-
var _a, _b;
|
|
91
|
-
if (!dragging || event.pointerId !== pointerId)
|
|
92
|
-
return;
|
|
93
|
-
dragging = false;
|
|
94
|
-
pointerId = null;
|
|
95
|
-
var viewportH = window.innerHeight || 1;
|
|
96
|
-
var finalFraction = startFraction + -(event.clientY - startY) / viewportH;
|
|
97
|
-
currentIndex = chooseNextIndex(currentIndex, computeVelocity(samples), (_a = options.velocityThreshold) !== null && _a !== void 0 ? _a : 500, finalFraction, options.detents);
|
|
98
|
-
var snappedFraction = detentAt(options.detents, currentIndex);
|
|
99
|
-
applyFraction(snappedFraction, true);
|
|
100
|
-
(_b = options.onSnap) === null || _b === void 0 ? void 0 : _b.call(options, currentIndex, snappedFraction);
|
|
101
|
-
try {
|
|
102
|
-
node.releasePointerCapture(event.pointerId);
|
|
103
|
-
}
|
|
104
|
-
catch (_c) {
|
|
105
|
-
/* noop */
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
function onPointerCancel(event) {
|
|
109
|
-
if (!dragging || event.pointerId !== pointerId)
|
|
110
|
-
return;
|
|
111
|
-
dragging = false;
|
|
112
|
-
pointerId = null;
|
|
113
|
-
applyFraction(detentAt(options.detents, currentIndex), true);
|
|
114
|
-
}
|
|
115
|
-
node.addEventListener('pointerdown', onPointerDown);
|
|
116
|
-
node.addEventListener('pointermove', onPointerMove);
|
|
117
|
-
node.addEventListener('pointerup', onPointerUp);
|
|
118
|
-
node.addEventListener('pointercancel', onPointerCancel);
|
|
119
|
-
// Start at closed (100vh offset) then animate into the initial detent
|
|
120
|
-
// on the next frame so the sheet's entry reads as a spring.
|
|
121
|
-
var el = target();
|
|
122
|
-
el.style.transition = 'none';
|
|
123
|
-
el.style.setProperty('--sheet-y', '100vh');
|
|
124
|
-
var raf = typeof requestAnimationFrame === 'function' ? requestAnimationFrame : function (cb) { return cb(); };
|
|
125
|
-
raf(function () { return applyFraction(detentAt(options.detents, currentIndex), true); });
|
|
126
|
-
return {
|
|
127
|
-
update: function (newOptions) {
|
|
128
|
-
var _a;
|
|
129
|
-
options = __assign({}, newOptions);
|
|
130
|
-
currentIndex = clampIndex((_a = options.initial) !== null && _a !== void 0 ? _a : currentIndex, options.detents.length);
|
|
131
|
-
applyFraction(detentAt(options.detents, currentIndex), true);
|
|
132
|
-
},
|
|
133
|
-
destroy: function () {
|
|
134
|
-
node.removeEventListener('pointerdown', onPointerDown);
|
|
135
|
-
node.removeEventListener('pointermove', onPointerMove);
|
|
136
|
-
node.removeEventListener('pointerup', onPointerUp);
|
|
137
|
-
node.removeEventListener('pointercancel', onPointerCancel);
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
/** Clamp a detent index into range; 0 when there are no detents. */
|
|
142
|
-
export function clampIndex(i, len) {
|
|
143
|
-
// An empty list yields 0: Math.min(i, -1) then Math.max(0, ...) collapses there.
|
|
144
|
-
return Math.max(0, Math.min(i, len - 1));
|
|
145
|
-
}
|
|
146
|
-
/** The fraction at an index, falling back to the first detent then 0. */
|
|
147
|
-
export function detentAt(detents, index) {
|
|
148
|
-
var _a, _b;
|
|
149
|
-
return (_b = (_a = detents[index]) !== null && _a !== void 0 ? _a : detents[0]) !== null && _b !== void 0 ? _b : 0;
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Rubber-band a fraction dragged beyond the detent range, so overshoot moves
|
|
153
|
-
* at a reduced rate instead of tracking the pointer one-to-one.
|
|
154
|
-
*/
|
|
155
|
-
export function applyRubberBand(fraction, minDetent, maxDetent, rubber) {
|
|
156
|
-
if (fraction > maxDetent)
|
|
157
|
-
return maxDetent + (fraction - maxDetent) * rubber;
|
|
158
|
-
if (fraction < minDetent)
|
|
159
|
-
return minDetent - (minDetent - fraction) * rubber;
|
|
160
|
-
return fraction;
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Pick the detent to settle on: a flick faster than the threshold moves one
|
|
164
|
-
* step in its direction, otherwise snap to whichever detent is nearest.
|
|
165
|
-
*/
|
|
166
|
-
export function chooseNextIndex(currentIndex, velocity, threshold, finalFraction, detents) {
|
|
167
|
-
if (Math.abs(velocity) > threshold) {
|
|
168
|
-
// Clamp the step: a currentIndex outside the list would otherwise step to
|
|
169
|
-
// an unindexable position (e.g. -1) rather than a real detent.
|
|
170
|
-
var stepped = velocity < 0 ? currentIndex + 1 : currentIndex - 1;
|
|
171
|
-
return clampIndex(stepped, detents.length);
|
|
172
|
-
}
|
|
173
|
-
return nearestDetentIndex(finalFraction, detents);
|
|
174
|
-
}
|
|
175
|
-
/** Index of the detent closest to `fraction`; ties resolve to the lower index. */
|
|
176
|
-
export function nearestDetentIndex(fraction, detents) {
|
|
177
|
-
var bestIndex = 0;
|
|
178
|
-
var bestDistance = Infinity;
|
|
179
|
-
for (var i = 0; i < detents.length; i++) {
|
|
180
|
-
var value = detents[i];
|
|
181
|
-
if (value === undefined)
|
|
182
|
-
continue;
|
|
183
|
-
var d = Math.abs(value - fraction);
|
|
184
|
-
if (d < bestDistance) {
|
|
185
|
-
bestDistance = d;
|
|
186
|
-
bestIndex = i;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
return bestIndex;
|
|
190
|
-
}
|
|
191
|
-
/** How many pointer samples the velocity window keeps. */
|
|
192
|
-
export var SAMPLE_WINDOW = 5;
|
|
193
|
-
/**
|
|
194
|
-
* Append a sample, keeping at most SAMPLE_WINDOW of them. Velocity is measured
|
|
195
|
-
* across this window so a release reflects the recent flick rather than the
|
|
196
|
-
* whole gesture. Returns a new array; the input is not mutated.
|
|
197
|
-
*/
|
|
198
|
-
export function pushSample(samples, sample, max) {
|
|
199
|
-
if (max === void 0) { max = SAMPLE_WINDOW; }
|
|
200
|
-
var next = __spreadArray(__spreadArray([], samples, true), [sample], false);
|
|
201
|
-
return next.length > max ? next.slice(next.length - max) : next;
|
|
202
|
-
}
|
|
203
|
-
/** Average pointer velocity in px/s across the sample window; 0 if unknowable. */
|
|
204
|
-
export function computeVelocity(samples) {
|
|
205
|
-
if (samples.length < 2)
|
|
206
|
-
return 0;
|
|
207
|
-
var first = samples[0];
|
|
208
|
-
var last = samples[samples.length - 1];
|
|
209
|
-
if (!first || !last)
|
|
210
|
-
return 0;
|
|
211
|
-
var dt = (last.t - first.t) / 1000;
|
|
212
|
-
if (dt <= 0)
|
|
213
|
-
return 0;
|
|
214
|
-
var velocity = (last.y - first.y) / dt;
|
|
215
|
-
// A vanishingly small dt overflows the division to Infinity, which would
|
|
216
|
-
// then clear every velocity threshold and force a flick snap.
|
|
217
|
-
return Number.isFinite(velocity) ? velocity : 0;
|
|
218
|
-
}
|
|
219
|
-
function findScrollableAncestor(el, stopAt) {
|
|
220
|
-
var node = el;
|
|
221
|
-
while (node && node !== stopAt) {
|
|
222
|
-
if (node instanceof HTMLElement) {
|
|
223
|
-
var overflowY = getComputedStyle(node).overflowY;
|
|
224
|
-
if ((overflowY === 'auto' || overflowY === 'scroll') && node.scrollHeight > node.clientHeight) {
|
|
225
|
-
return node;
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
node = node.parentElement;
|
|
229
|
-
}
|
|
230
|
-
return null;
|
|
231
|
-
}
|
|
232
|
-
export var DEFAULT_DETENT_FRACTIONS = {
|
|
233
|
-
small: 0.25,
|
|
234
|
-
medium: 0.5,
|
|
235
|
-
large: 0.9,
|
|
236
|
-
fullscreen: 1
|
|
237
|
-
};
|
|
238
|
-
export function detentFractions(names) {
|
|
239
|
-
return names.map(function (n) { return DEFAULT_DETENT_FRACTIONS[n]; });
|
|
20
|
+
return dragSurface(node, initialOptions);
|
|
240
21
|
}
|
|
22
|
+
export { DEFAULT_DETENT_FRACTIONS, DRAG_BLOCKER_SELECTOR, SAMPLE_WINDOW, SHEET_DISMISS_FRACTION, SHEET_TRANSITION, applyRubberBand, chooseNextIndex, clampIndex, computeVelocity, detentAt, detentFractions, isDragStartAllowed, nearestDetentIndex, pushSample, resolveDragFraction, shouldDismiss, sheetYForFraction } from './dragSnapMath';
|
|
@@ -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,165 @@
|
|
|
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
|
+
if (node instanceof HTMLElement) {
|
|
141
|
+
var overflowY = getComputedStyle(node).overflowY;
|
|
142
|
+
if ((overflowY === 'auto' || overflowY === 'scroll') && node.scrollHeight > node.clientHeight) {
|
|
143
|
+
return node;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
/** 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. */
|
|
150
|
+
export function highestScrollTop(el, stopAt) {
|
|
151
|
+
var highest = 0;
|
|
152
|
+
for (var scroller = findScrollableAncestor(el, stopAt); scroller; scroller = findScrollableAncestor(scroller.parentElement, stopAt)) {
|
|
153
|
+
highest = Math.max(highest, scroller.scrollTop);
|
|
154
|
+
}
|
|
155
|
+
return highest;
|
|
156
|
+
}
|
|
157
|
+
export var DEFAULT_DETENT_FRACTIONS = {
|
|
158
|
+
small: 0.25,
|
|
159
|
+
medium: 0.5,
|
|
160
|
+
large: 0.9,
|
|
161
|
+
fullscreen: 1
|
|
162
|
+
};
|
|
163
|
+
export function detentFractions(names) {
|
|
164
|
+
return names.map(function (n) { return DEFAULT_DETENT_FRACTIONS[n]; });
|
|
165
|
+
}
|