glasslikeui 1.4.0 → 1.6.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 (38) hide show
  1. package/README.md +53 -2
  2. package/dist/actions/deviceMotion.js +2 -3
  3. package/dist/actions/dragSnap.d.ts +31 -1
  4. package/dist/actions/dragSnap.js +68 -50
  5. package/dist/actions/index.d.ts +2 -0
  6. package/dist/actions/index.js +2 -0
  7. package/dist/actions/refreshable.d.ts +36 -0
  8. package/dist/actions/refreshable.js +157 -0
  9. package/dist/actions/swipeActions.d.ts +61 -0
  10. package/dist/actions/swipeActions.js +201 -0
  11. package/dist/components/glass/Glass.svelte +28 -6
  12. package/dist/components/glass/Glass.svelte.d.ts +3 -0
  13. package/dist/components/glass/Sheet.svelte +3 -0
  14. package/dist/components/interactive/Button.svelte +81 -1
  15. package/dist/components/interactive/Button.svelte.d.ts +2 -1
  16. package/dist/components/interactive/Toggle.svelte +110 -0
  17. package/dist/components/interactive/Toggle.svelte.d.ts +11 -0
  18. package/dist/components/interactive/index.d.ts +1 -0
  19. package/dist/components/interactive/index.js +1 -0
  20. package/dist/components/layout/ScrollView.svelte +83 -15
  21. package/dist/components/layout/ScrollView.svelte.d.ts +3 -0
  22. package/dist/components/layout/ZStack.svelte +3 -1
  23. package/dist/components/list/ListRow.svelte +135 -77
  24. package/dist/components/list/ListRow.svelte.d.ts +6 -0
  25. package/dist/components/typography/Label.svelte +82 -0
  26. package/dist/components/typography/Label.svelte.d.ts +11 -0
  27. package/dist/components/typography/index.d.ts +1 -0
  28. package/dist/components/typography/index.js +1 -0
  29. package/dist/constants/index.d.ts +2 -0
  30. package/dist/constants/index.js +2 -0
  31. package/dist/constants/label.d.ts +15 -0
  32. package/dist/constants/label.js +14 -0
  33. package/dist/constants/shapes.d.ts +18 -0
  34. package/dist/constants/shapes.js +26 -0
  35. package/dist/constants/variants.d.ts +28 -0
  36. package/dist/constants/variants.js +26 -0
  37. package/dist/icons/index.js +32 -1
  38. package/package.json +6 -2
package/README.md CHANGED
@@ -83,19 +83,70 @@ attributes on non-Safari browsers:
83
83
  | `NavigationBar` | Top chrome; supports `largeTitle` collapse. |
84
84
  | `NavigationLink` | Pill-style link. |
85
85
  | `TabView` | Floating tab bar. |
86
- | `Button` / `IconButton` | `filled` / `outlined` / `plain` / `tinted` / `destructive`. |
86
+ | `Button` | `filled` / `outlined` / `plain` / `tinted` / `destructive` / `glass` / `glassProminent`. |
87
+ | `IconButton` | `plain` / `filled`. |
88
+ | `Toggle` | iOS-style switch over a native checkbox. |
87
89
  | `Badge` | `default` / `accent`. |
88
90
  | `Text` | Dynamic-Type-aware text. |
89
- | `List` / `ListRow` | Grouped list with insets. |
91
+ | `Label` | Icon + title pair with SwiftUI label styles. |
92
+ | `List` / `ListRow` | Grouped list with insets and swipe actions. |
90
93
  | `HStack` / `VStack` / `ZStack` / `Grid` / `Spacer` / `Divider` / `ScrollView` | Layout primitives. |
91
94
  | `SymbolImage` | SVG icon renderer. |
92
95
 
96
+ ## Glass shape
97
+
98
+ `Glass` takes a `shape` prop mirroring SwiftUI's
99
+ `glassEffect(_:in:)`. It overrides `cornerRadius` entirely.
100
+
101
+ ```svelte
102
+ <Glass shape="capsule">...</Glass>
103
+ <Glass shape="circle">...</Glass>
104
+ ```
105
+
106
+ - `rect` (default) -- uses the `cornerRadius` token.
107
+ - `capsule` -- fully rounded ends.
108
+ - `circle` -- 50% radius, locked to a 1:1 box.
109
+
110
+ Buttons get the same treatment through the `glass` and
111
+ `glassProminent` variants, which read their blur and saturation from
112
+ the shared intensity tokens rather than duplicating them.
113
+
114
+ ## Swipe actions and pull to refresh
115
+
116
+ `ListRow` accepts a `swipeActions` snippet, mirroring SwiftUI's
117
+ `.swipeActions(edge:allowsFullSwipe:)`:
118
+
119
+ ```svelte
120
+ <ListRow allowsFullSwipe onFullSwipe={() => remove(item)}>
121
+ {#snippet children()}{item.title}{/snippet}
122
+ {#snippet swipeActions()}
123
+ <button onclick={() => remove(item)}>Delete</button>
124
+ {/snippet}
125
+ </ListRow>
126
+ ```
127
+
128
+ `ScrollView` accepts an `onRefresh` handler, mirroring
129
+ `.refreshable`. The indicator stays up until the returned promise
130
+ settles -- including when it rejects:
131
+
132
+ ```svelte
133
+ <ScrollView onRefresh={async () => await reload()}>
134
+ ...
135
+ </ScrollView>
136
+ ```
137
+
138
+ Both are also available as standalone actions for your own markup.
139
+ They drive `--swipe-x` / `--refresh-pull` and `data-swipe-state` /
140
+ `data-refresh-phase`; style off those.
141
+
93
142
  ## Actions
94
143
 
95
144
  | Export | Purpose |
96
145
  | -------------------------------- | ------------------------------------------------------ |
97
146
  | `scrollEdge` | Fade a scroll container at edges meeting glass chrome. |
98
147
  | `dragSnap` | Detent-based drag gesture (used by `Sheet`). |
148
+ | `swipeActions` | Swipe a row aside to reveal actions. |
149
+ | `refreshable` | Pull-to-refresh gesture. |
99
150
  | `deviceMotion` | Ties the glass highlight angle to device tilt. |
100
151
  | `withGlassTransition` | Wraps a DOM update in a view transition. |
101
152
  | `syncAccessibilityPreferences` | Bridges `prefers-*` media to data-attrs at `:root`. |
@@ -98,10 +98,9 @@ export function deviceMotion(node, initialOptions) {
98
98
  if (initialOptions === void 0) { initialOptions = {}; }
99
99
  var options = __assign({}, initialOptions);
100
100
  if (!isDeviceMotionSupported()) {
101
+ // Nothing listens, so there is no state worth tracking here.
101
102
  return {
102
- update: function (newOptions) {
103
- options = __assign({}, newOptions);
104
- },
103
+ update: function () { },
105
104
  destroy: function () { }
106
105
  };
107
106
  }
@@ -15,7 +15,7 @@
15
15
  */
16
16
  export interface DragSnapOptions {
17
17
  /** Element whose transform is manipulated. If omitted, uses the node itself. */
18
- target?: HTMLElement | null;
18
+ target?: HTMLElement | null | undefined;
19
19
  /** Detent positions as viewport-height fractions (0..1). Higher = taller sheet. */
20
20
  detents: number[];
21
21
  /** Initial detent index. */
@@ -33,10 +33,40 @@ export interface DragSnapOptions {
33
33
  /** Disable the gesture. */
34
34
  disabled?: boolean;
35
35
  }
36
+ export interface PointerSample {
37
+ y: number;
38
+ t: number;
39
+ }
36
40
  export declare function dragSnap(node: HTMLElement, initialOptions: DragSnapOptions): {
37
41
  update(newOptions: DragSnapOptions): void;
38
42
  destroy(): void;
39
43
  };
44
+ /** Clamp a detent index into range; 0 when there are no detents. */
45
+ export declare function clampIndex(i: number, len: number): number;
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;
40
70
  export declare const DEFAULT_DETENT_FRACTIONS: {
41
71
  readonly small: 0.25;
42
72
  readonly medium: 0.5;
@@ -24,6 +24,15 @@ var __assign = (this && this.__assign) || function () {
24
24
  };
25
25
  return __assign.apply(this, arguments);
26
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
+ };
27
36
  export function dragSnap(node, initialOptions) {
28
37
  var _a;
29
38
  var options = __assign({}, initialOptions);
@@ -35,10 +44,8 @@ export function dragSnap(node, initialOptions) {
35
44
  var samples = [];
36
45
  var target = function () { var _a; return (_a = options.target) !== null && _a !== void 0 ? _a : node; };
37
46
  function applyFraction(fraction, animate) {
38
- if (animate === void 0) { animate = false; }
47
+ // target() falls back to `node`, so there is always an element.
39
48
  var el = target();
40
- if (!el)
41
- return;
42
49
  el.style.transition = animate
43
50
  ? 'transform 0.4s cubic-bezier(0.32, 0.72, 0, 1)'
44
51
  : 'none';
@@ -74,22 +81,9 @@ export function dragSnap(node, initialOptions) {
74
81
  var viewportH = window.innerHeight || 1;
75
82
  var deltaY = event.clientY - startY;
76
83
  var fractionDelta = -deltaY / viewportH;
77
- var newFraction = startFraction + fractionDelta;
78
- var maxDetent = Math.max.apply(Math, options.detents);
79
- var minDetent = Math.min.apply(Math, options.detents);
80
- var rubber = (_a = options.rubberBand) !== null && _a !== void 0 ? _a : 0.3;
81
- if (newFraction > maxDetent) {
82
- var excess = newFraction - maxDetent;
83
- newFraction = maxDetent + excess * rubber;
84
- }
85
- else if (newFraction < minDetent) {
86
- var deficit = minDetent - newFraction;
87
- newFraction = minDetent - deficit * rubber;
88
- }
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);
89
85
  applyFraction(newFraction, false);
90
- samples.push({ y: event.clientY, t: performance.now() });
91
- if (samples.length > 5)
92
- samples.shift();
86
+ samples = pushSample(samples, { y: event.clientY, t: performance.now() });
93
87
  (_b = options.onDrag) === null || _b === void 0 ? void 0 : _b.call(options, deltaY, newFraction);
94
88
  }
95
89
  function onPointerUp(event) {
@@ -98,25 +92,9 @@ export function dragSnap(node, initialOptions) {
98
92
  return;
99
93
  dragging = false;
100
94
  pointerId = null;
101
- var velocity = computeVelocity(samples);
102
- var threshold = (_a = options.velocityThreshold) !== null && _a !== void 0 ? _a : 500;
103
- var nextIndex = currentIndex;
104
- if (Math.abs(velocity) > threshold) {
105
- if (velocity < 0) {
106
- nextIndex = Math.min(currentIndex + 1, options.detents.length - 1);
107
- }
108
- else {
109
- nextIndex = Math.max(currentIndex - 1, 0);
110
- }
111
- }
112
- else {
113
- var viewportH = window.innerHeight || 1;
114
- var deltaY = event.clientY - startY;
115
- var fractionDelta = -deltaY / viewportH;
116
- var finalFraction = startFraction + fractionDelta;
117
- nextIndex = nearestDetentIndex(finalFraction, options.detents);
118
- }
119
- currentIndex = nextIndex;
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);
120
98
  var snappedFraction = detentAt(options.detents, currentIndex);
121
99
  applyFraction(snappedFraction, true);
122
100
  (_b = options.onSnap) === null || _b === void 0 ? void 0 : _b.call(options, currentIndex, snappedFraction);
@@ -141,12 +119,10 @@ export function dragSnap(node, initialOptions) {
141
119
  // Start at closed (100vh offset) then animate into the initial detent
142
120
  // on the next frame so the sheet's entry reads as a spring.
143
121
  var el = target();
144
- if (el) {
145
- el.style.transition = 'none';
146
- el.style.setProperty('--sheet-y', '100vh');
147
- var raf = typeof requestAnimationFrame === 'function' ? requestAnimationFrame : function (cb) { return cb(); };
148
- raf(function () { return applyFraction(detentAt(options.detents, currentIndex), true); });
149
- }
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); });
150
126
  return {
151
127
  update: function (newOptions) {
152
128
  var _a;
@@ -162,16 +138,42 @@ export function dragSnap(node, initialOptions) {
162
138
  }
163
139
  };
164
140
  }
165
- function clampIndex(i, len) {
166
- if (len === 0)
167
- return 0;
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.
168
144
  return Math.max(0, Math.min(i, len - 1));
169
145
  }
170
- function detentAt(detents, index) {
146
+ /** The fraction at an index, falling back to the first detent then 0. */
147
+ export function detentAt(detents, index) {
171
148
  var _a, _b;
172
149
  return (_b = (_a = detents[index]) !== null && _a !== void 0 ? _a : detents[0]) !== null && _b !== void 0 ? _b : 0;
173
150
  }
174
- function nearestDetentIndex(fraction, detents) {
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) {
175
177
  var bestIndex = 0;
176
178
  var bestDistance = Infinity;
177
179
  for (var i = 0; i < detents.length; i++) {
@@ -186,7 +188,20 @@ function nearestDetentIndex(fraction, detents) {
186
188
  }
187
189
  return bestIndex;
188
190
  }
189
- function computeVelocity(samples) {
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) {
190
205
  if (samples.length < 2)
191
206
  return 0;
192
207
  var first = samples[0];
@@ -196,7 +211,10 @@ function computeVelocity(samples) {
196
211
  var dt = (last.t - first.t) / 1000;
197
212
  if (dt <= 0)
198
213
  return 0;
199
- return (last.y - first.y) / dt;
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;
200
218
  }
201
219
  function findScrollableAncestor(el, stopAt) {
202
220
  var node = el;
@@ -3,3 +3,5 @@ export * from './dragSnap';
3
3
  export * from './scrollEdge';
4
4
  export * from './a11ySync';
5
5
  export * from './deviceMotion';
6
+ export * from './swipeActions';
7
+ export * from './refreshable';
@@ -3,3 +3,5 @@ export * from './dragSnap';
3
3
  export * from './scrollEdge';
4
4
  export * from './a11ySync';
5
5
  export * from './deviceMotion';
6
+ export * from './swipeActions';
7
+ export * from './refreshable';
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Svelte action: pull-to-refresh, mirroring SwiftUI's `.refreshable`.
3
+ *
4
+ * Drives `--refresh-pull` (px of travel) and `data-refresh-phase` on the node;
5
+ * the indicator itself is the caller's markup.
6
+ *
7
+ * Usage:
8
+ * <div use:refreshable={{ onRefresh: async () => reload() }}>
9
+ * ... scrolling content ...
10
+ * </div>
11
+ */
12
+ export type RefreshPhase = 'idle' | 'pulling' | 'ready' | 'refreshing';
13
+ export interface RefreshableOptions {
14
+ /** Awaited if it returns a promise; the phase stays `refreshing` until it settles. */
15
+ onRefresh: () => void | Promise<void>;
16
+ /** Px of resisted travel needed to arm the refresh. */
17
+ threshold?: number | undefined;
18
+ /** Fraction of pointer travel the content follows. */
19
+ resistance?: number | undefined;
20
+ disabled?: boolean | undefined;
21
+ onPhaseChange?: ((phase: RefreshPhase) => void) | undefined;
22
+ }
23
+ export declare const DEFAULT_REFRESH_THRESHOLD = 64;
24
+ export declare const DEFAULT_REFRESH_RESISTANCE = 0.5;
25
+ /** Furthest the content travels, however hard the user pulls. */
26
+ export declare const MAX_PULL_DISTANCE = 150;
27
+ /** Damped travel for a raw pointer delta. Upward drags do not pull. */
28
+ export declare function resistPull(rawDy: number, resistance?: number): number;
29
+ /** Whether releasing at this distance should start a refresh. */
30
+ export declare function shouldTriggerRefresh(distance: number, threshold: number): boolean;
31
+ /** The phase to render. An in-flight refresh outranks the gesture. */
32
+ export declare function refreshPhase(distance: number, threshold: number, isRefreshing: boolean): RefreshPhase;
33
+ export declare function refreshable(node: HTMLElement, initialOptions: RefreshableOptions): {
34
+ update(newOptions: RefreshableOptions): void;
35
+ destroy(): void;
36
+ };
@@ -0,0 +1,157 @@
1
+ /**
2
+ * Svelte action: pull-to-refresh, mirroring SwiftUI's `.refreshable`.
3
+ *
4
+ * Drives `--refresh-pull` (px of travel) and `data-refresh-phase` on the node;
5
+ * the indicator itself is the caller's markup.
6
+ *
7
+ * Usage:
8
+ * <div use:refreshable={{ onRefresh: async () => reload() }}>
9
+ * ... scrolling content ...
10
+ * </div>
11
+ */
12
+ var __assign = (this && this.__assign) || function () {
13
+ __assign = Object.assign || function(t) {
14
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
15
+ s = arguments[i];
16
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
17
+ t[p] = s[p];
18
+ }
19
+ return t;
20
+ };
21
+ return __assign.apply(this, arguments);
22
+ };
23
+ export var DEFAULT_REFRESH_THRESHOLD = 64;
24
+ export var DEFAULT_REFRESH_RESISTANCE = 0.5;
25
+ /** Furthest the content travels, however hard the user pulls. */
26
+ export var MAX_PULL_DISTANCE = 150;
27
+ /** Damped travel for a raw pointer delta. Upward drags do not pull. */
28
+ export function resistPull(rawDy, resistance) {
29
+ if (resistance === void 0) { resistance = DEFAULT_REFRESH_RESISTANCE; }
30
+ // NaN is meaningless; an infinite drag simply hits the cap below.
31
+ if (Number.isNaN(rawDy) || rawDy <= 0)
32
+ return 0;
33
+ // A zero or negative factor would freeze or invert the indicator.
34
+ var factor = Number.isFinite(resistance) && resistance > 0 ? resistance : DEFAULT_REFRESH_RESISTANCE;
35
+ return Math.min(rawDy * factor, MAX_PULL_DISTANCE);
36
+ }
37
+ /** Whether releasing at this distance should start a refresh. */
38
+ export function shouldTriggerRefresh(distance, threshold) {
39
+ if (!Number.isFinite(distance) || !Number.isFinite(threshold))
40
+ return false;
41
+ if (threshold <= 0)
42
+ return false;
43
+ return distance >= threshold;
44
+ }
45
+ /** The phase to render. An in-flight refresh outranks the gesture. */
46
+ export function refreshPhase(distance, threshold, isRefreshing) {
47
+ if (isRefreshing)
48
+ return 'refreshing';
49
+ if (shouldTriggerRefresh(distance, threshold))
50
+ return 'ready';
51
+ if (Number.isFinite(distance) && distance > 0)
52
+ return 'pulling';
53
+ return 'idle';
54
+ }
55
+ export function refreshable(node, initialOptions) {
56
+ var options = __assign({}, initialOptions);
57
+ var pointerId = null;
58
+ var startY = 0;
59
+ var distance = 0;
60
+ var refreshing = false;
61
+ var phase = 'idle';
62
+ var destroyed = false;
63
+ var threshold = function () { var _a; return (_a = options.threshold) !== null && _a !== void 0 ? _a : DEFAULT_REFRESH_THRESHOLD; };
64
+ function render() {
65
+ var _a;
66
+ if (destroyed)
67
+ return;
68
+ node.style.setProperty('--refresh-pull', "".concat(distance, "px"));
69
+ var next = refreshPhase(distance, threshold(), refreshing);
70
+ node.setAttribute('data-refresh-phase', next);
71
+ // Report transitions only; a pointermove stream would otherwise repeat.
72
+ if (next !== phase) {
73
+ phase = next;
74
+ (_a = options.onPhaseChange) === null || _a === void 0 ? void 0 : _a.call(options, next);
75
+ }
76
+ }
77
+ function reset() {
78
+ distance = 0;
79
+ refreshing = false;
80
+ render();
81
+ }
82
+ function onPointerDown(event) {
83
+ if (options.disabled || refreshing)
84
+ return;
85
+ if (event.button !== undefined && event.button !== 0)
86
+ return;
87
+ // Pull-to-refresh belongs to the top of the scroller.
88
+ if (node.scrollTop > 0)
89
+ return;
90
+ pointerId = event.pointerId;
91
+ startY = event.clientY;
92
+ }
93
+ function onPointerMove(event) {
94
+ if (pointerId === null || event.pointerId !== pointerId || refreshing)
95
+ return;
96
+ distance = resistPull(event.clientY - startY, options.resistance);
97
+ render();
98
+ if (distance > 0 && event.cancelable)
99
+ event.preventDefault();
100
+ }
101
+ function onPointerUp(event) {
102
+ if (pointerId === null || event.pointerId !== pointerId)
103
+ return;
104
+ pointerId = null;
105
+ if (refreshing)
106
+ return;
107
+ if (!shouldTriggerRefresh(distance, threshold())) {
108
+ reset();
109
+ return;
110
+ }
111
+ refreshing = true;
112
+ distance = threshold();
113
+ render();
114
+ // Called synchronously so the request starts on release. Settled either
115
+ // way -- a rejected or throwing refresh must not strand the indicator.
116
+ try {
117
+ var result = options.onRefresh();
118
+ if (result && typeof result.then === 'function') {
119
+ result.then(reset, reset);
120
+ }
121
+ else {
122
+ reset();
123
+ }
124
+ }
125
+ catch (_a) {
126
+ reset();
127
+ }
128
+ }
129
+ function onPointerCancel(event) {
130
+ if (pointerId === null || event.pointerId !== pointerId)
131
+ return;
132
+ pointerId = null;
133
+ if (refreshing)
134
+ return;
135
+ reset();
136
+ }
137
+ node.addEventListener('pointerdown', onPointerDown);
138
+ node.addEventListener('pointermove', onPointerMove);
139
+ node.addEventListener('pointerup', onPointerUp);
140
+ node.addEventListener('pointercancel', onPointerCancel);
141
+ render();
142
+ return {
143
+ update: function (newOptions) {
144
+ options = __assign({}, newOptions);
145
+ render();
146
+ },
147
+ destroy: function () {
148
+ destroyed = true;
149
+ node.removeEventListener('pointerdown', onPointerDown);
150
+ node.removeEventListener('pointermove', onPointerMove);
151
+ node.removeEventListener('pointerup', onPointerUp);
152
+ node.removeEventListener('pointercancel', onPointerCancel);
153
+ node.style.removeProperty('--refresh-pull');
154
+ node.removeAttribute('data-refresh-phase');
155
+ }
156
+ };
157
+ }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Svelte action: swipe a row aside to reveal actions, mirroring SwiftUI's
3
+ * `.swipeActions(edge:allowsFullSwipe:)`.
4
+ *
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.
7
+ *
8
+ * Usage:
9
+ * <div use:swipeActions={{
10
+ * edge: 'trailing',
11
+ * actionsWidth: 160,
12
+ * allowsFullSwipe: true,
13
+ * onFullSwipe: () => remove(item)
14
+ * }}>...</div>
15
+ */
16
+ export type SwipeEdge = 'leading' | 'trailing';
17
+ export type SwipeState = 'closed' | 'open' | 'fullSwipe';
18
+ export interface SwipeGeometry {
19
+ edge: SwipeEdge;
20
+ /** Total width of the revealed action strip, in px. */
21
+ actionsWidth: number;
22
+ /** Width of the row. 0 means "not measured yet". */
23
+ rowWidth: number;
24
+ allowsFullSwipe: boolean;
25
+ }
26
+ export interface SwipeActionsOptions {
27
+ edge?: SwipeEdge | undefined;
28
+ /** Explicit strip width. Omit to measure `actionsElement` at gesture start. */
29
+ actionsWidth?: number | undefined;
30
+ /** The action strip. Measured lazily, so it needs no layout on first paint. */
31
+ actionsElement?: HTMLElement | null | undefined;
32
+ /** Overrides the measured row width; needed where layout is unavailable. */
33
+ rowWidth?: number | undefined;
34
+ allowsFullSwipe?: boolean | undefined;
35
+ disabled?: boolean | undefined;
36
+ onStateChange?: ((state: SwipeState) => void) | undefined;
37
+ onFullSwipe?: (() => void) | undefined;
38
+ }
39
+ /** Fraction of the row that must be crossed to arm a full swipe. */
40
+ export declare const DEFAULT_FULL_SWIPE_THRESHOLD = 0.5;
41
+ /** How much of an overshoot past the limit is tracked. */
42
+ export declare const DEFAULT_SWIPE_RUBBER_BAND = 0.35;
43
+ /** px/s past which a release counts as a flick. */
44
+ export declare const DEFAULT_SWIPE_VELOCITY = 500;
45
+ /** px of travel before the gesture is claimed from the scroller. */
46
+ export declare const SWIPE_SLOP = 4;
47
+ /** Direction the content travels to reveal this edge's actions. */
48
+ 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
+ */
53
+ export declare function clampSwipeOffset(rawDx: number, geo: SwipeGeometry): number;
54
+ /** Where the row settles once the pointer is released. */
55
+ export declare function resolveSwipeState(offset: number, velocity: number, geo: SwipeGeometry): SwipeState;
56
+ /** The resting offset for a settled state. */
57
+ export declare function offsetForState(state: SwipeState, geo: SwipeGeometry): number;
58
+ export declare function swipeActions(node: HTMLElement, initialOptions: SwipeActionsOptions): {
59
+ update(newOptions: SwipeActionsOptions): void;
60
+ destroy(): void;
61
+ };