@atmo-dev/kit 0.19.2 → 0.21.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/dist/components/IconRail.svelte +16 -20
- package/dist/components/IconRail.svelte.d.ts +2 -0
- package/dist/components/Modal.svelte +10 -5
- package/dist/components/Modal.svelte.d.ts +6 -2
- package/dist/components/PhotoLightbox.svelte +92 -1
- package/dist/components/PullToRefresh.svelte +114 -0
- package/dist/components/PullToRefresh.svelte.d.ts +12 -0
- package/dist/components/WorkspaceSidebar.svelte +16 -1
- package/dist/components/WorkspaceSidebar.svelte.d.ts +2 -0
- package/dist/drawer.svelte.d.ts +17 -0
- package/dist/drawer.svelte.js +208 -0
- package/dist/gestures.d.ts +45 -0
- package/dist/gestures.js +174 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +1 -1
|
@@ -21,12 +21,14 @@
|
|
|
21
21
|
import Icon from './Icon.svelte';
|
|
22
22
|
import Modal from './Modal.svelte';
|
|
23
23
|
import { cn } from '../utils.js';
|
|
24
|
+
import { drawerMotion } from '../drawer.svelte.js';
|
|
24
25
|
let {
|
|
25
26
|
label,
|
|
26
27
|
items,
|
|
27
28
|
backed = false,
|
|
28
29
|
mobile = 'bar',
|
|
29
30
|
open = $bindable(false),
|
|
31
|
+
swipe = true,
|
|
30
32
|
footer,
|
|
31
33
|
class: className,
|
|
32
34
|
ref = $bindable(null),
|
|
@@ -37,11 +39,22 @@
|
|
|
37
39
|
backed?: boolean;
|
|
38
40
|
mobile?: 'bar' | 'drawer';
|
|
39
41
|
open?: boolean;
|
|
42
|
+
/** On touch screens, swipe right on the page to open the drawer and left to close it. */
|
|
43
|
+
swipe?: boolean;
|
|
40
44
|
footer?: Snippet<[{ expanded: boolean; close: () => void }]>;
|
|
41
45
|
ref?: HTMLElement | null;
|
|
42
46
|
} = $props();
|
|
43
47
|
const small = new MediaQuery('(width < 40rem)', false);
|
|
44
48
|
const close = () => (open = false);
|
|
49
|
+
let panel = $state<HTMLElement | null>(null);
|
|
50
|
+
let overlay = $state<HTMLElement | null>(null);
|
|
51
|
+
const drawer = drawerMotion({
|
|
52
|
+
swipe: () => swipe && mobile === 'drawer' && small.current,
|
|
53
|
+
open: () => open,
|
|
54
|
+
setOpen: (value) => (open = value),
|
|
55
|
+
panel: () => panel,
|
|
56
|
+
overlay: () => overlay
|
|
57
|
+
});
|
|
45
58
|
let drawerWasOpen = false;
|
|
46
59
|
$effect(() => {
|
|
47
60
|
if (mobile !== 'drawer') return;
|
|
@@ -59,7 +72,9 @@
|
|
|
59
72
|
|
|
60
73
|
{#if mobile === 'drawer' && small.current}
|
|
61
74
|
<Modal
|
|
62
|
-
bind:open
|
|
75
|
+
bind:open={() => open, (value) => (value ? (open = true) : drawer.dismiss())}
|
|
76
|
+
bind:ref={panel}
|
|
77
|
+
bind:overlayRef={overlay}
|
|
63
78
|
title={label}
|
|
64
79
|
onCloseAutoFocus={(event) => {
|
|
65
80
|
if (!small.current) event.preventDefault();
|
|
@@ -177,22 +192,3 @@
|
|
|
177
192
|
</nav>
|
|
178
193
|
</Tooltip.Provider>
|
|
179
194
|
{/if}
|
|
180
|
-
|
|
181
|
-
<style>
|
|
182
|
-
@keyframes drawer-enter {
|
|
183
|
-
from {
|
|
184
|
-
transform: translateX(-100%);
|
|
185
|
-
}
|
|
186
|
-
to {
|
|
187
|
-
transform: translateX(0);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
:global([data-slot='icon-rail-drawer'][data-state='open']) {
|
|
191
|
-
animation: drawer-enter 130ms cubic-bezier(0.2, 0.8, 0.2, 1);
|
|
192
|
-
}
|
|
193
|
-
@media (prefers-reduced-motion: reduce) {
|
|
194
|
-
:global([data-slot='icon-rail-drawer']) {
|
|
195
|
-
animation: none;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
</style>
|
|
@@ -17,6 +17,8 @@ type $$ComponentProps = Omit<HTMLAttributes<HTMLElement>, 'children'> & {
|
|
|
17
17
|
backed?: boolean;
|
|
18
18
|
mobile?: 'bar' | 'drawer';
|
|
19
19
|
open?: boolean;
|
|
20
|
+
/** On touch screens, swipe right on the page to open the drawer and left to close it. */
|
|
21
|
+
swipe?: boolean;
|
|
20
22
|
footer?: Snippet<[{
|
|
21
23
|
expanded: boolean;
|
|
22
24
|
close: () => void;
|
|
@@ -11,20 +11,25 @@
|
|
|
11
11
|
title,
|
|
12
12
|
description,
|
|
13
13
|
dismissible = true,
|
|
14
|
+
ref = $bindable(null),
|
|
15
|
+
overlayRef = $bindable(null),
|
|
14
16
|
class: className,
|
|
15
17
|
style,
|
|
16
18
|
children,
|
|
17
19
|
onOpenAutoFocus,
|
|
18
20
|
...rest
|
|
19
|
-
}: Omit<Primitive.ContentProps, 'children' | 'child' | 'title'> & {
|
|
21
|
+
}: Omit<Primitive.ContentProps, 'children' | 'child' | 'title' | 'ref'> & {
|
|
20
22
|
open?: boolean;
|
|
21
23
|
title: string;
|
|
22
24
|
description?: string;
|
|
23
25
|
dismissible?: boolean;
|
|
26
|
+
/** The dialog element, while open. */
|
|
27
|
+
ref?: HTMLElement | null;
|
|
28
|
+
/** The backdrop element, while open. */
|
|
29
|
+
overlayRef?: HTMLElement | null;
|
|
24
30
|
children?: Snippet;
|
|
25
31
|
} = $props();
|
|
26
32
|
let origin = $state<HTMLSpanElement>();
|
|
27
|
-
let content = $state<HTMLElement | null>(null);
|
|
28
33
|
let inherited = $state('');
|
|
29
34
|
$effect(() => {
|
|
30
35
|
if (!open || !origin) {
|
|
@@ -41,10 +46,10 @@
|
|
|
41
46
|
<span hidden bind:this={origin} class={cn('atmo-theme', className)} {style}></span>
|
|
42
47
|
<Primitive.Root bind:open>
|
|
43
48
|
{#if inherited}<Primitive.Portal>
|
|
44
|
-
<Primitive.Overlay class="fixed inset-0 z-50 bg-black/55" />
|
|
49
|
+
<Primitive.Overlay bind:ref={overlayRef} class="fixed inset-0 z-50 bg-black/55" />
|
|
45
50
|
<Primitive.Content
|
|
46
51
|
{...rest}
|
|
47
|
-
bind:ref
|
|
52
|
+
bind:ref
|
|
48
53
|
interactOutsideBehavior={dismissible ? 'close' : 'ignore'}
|
|
49
54
|
escapeKeydownBehavior={dismissible ? 'close' : 'ignore'}
|
|
50
55
|
style={[inherited, style].filter(Boolean).join(';')}
|
|
@@ -55,7 +60,7 @@
|
|
|
55
60
|
onOpenAutoFocus={(event) => {
|
|
56
61
|
onOpenAutoFocus?.(event);
|
|
57
62
|
if (!event.defaultPrevented) {
|
|
58
|
-
const target =
|
|
63
|
+
const target = ref?.querySelector<HTMLElement>('[data-autofocus]');
|
|
59
64
|
if (target) {
|
|
60
65
|
event.preventDefault();
|
|
61
66
|
target.focus();
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import type { Snippet } from 'svelte';
|
|
2
2
|
import { Dialog as Primitive } from 'bits-ui';
|
|
3
|
-
type $$ComponentProps = Omit<Primitive.ContentProps, 'children' | 'child' | 'title'> & {
|
|
3
|
+
type $$ComponentProps = Omit<Primitive.ContentProps, 'children' | 'child' | 'title' | 'ref'> & {
|
|
4
4
|
open?: boolean;
|
|
5
5
|
title: string;
|
|
6
6
|
description?: string;
|
|
7
7
|
dismissible?: boolean;
|
|
8
|
+
/** The dialog element, while open. */
|
|
9
|
+
ref?: HTMLElement | null;
|
|
10
|
+
/** The backdrop element, while open. */
|
|
11
|
+
overlayRef?: HTMLElement | null;
|
|
8
12
|
children?: Snippet;
|
|
9
13
|
};
|
|
10
|
-
declare const Modal: import("svelte").Component<$$ComponentProps, {}, "open">;
|
|
14
|
+
declare const Modal: import("svelte").Component<$$ComponentProps, {}, "ref" | "open" | "overlayRef">;
|
|
11
15
|
type Modal = ReturnType<typeof Modal>;
|
|
12
16
|
export default Modal;
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { ChevronLeft, ChevronRight, X } from '@sketchyicons/svelte';
|
|
5
5
|
import Icon from './Icon.svelte';
|
|
6
6
|
import { cn } from '../utils.js';
|
|
7
|
+
import { EASE, reducedMotion, speed, touchOf, track, undecided, type Move } from '../gestures.js';
|
|
7
8
|
import type { Photo } from '../media.js';
|
|
8
9
|
export type PhotoLightboxProps = {
|
|
9
10
|
photos: Photo[];
|
|
@@ -157,6 +158,96 @@
|
|
|
157
158
|
settleSoon();
|
|
158
159
|
}
|
|
159
160
|
|
|
161
|
+
// On touch screens a photo pulled down follows the finger, shrinking as the backdrop
|
|
162
|
+
// thins; let go far or fast enough and the viewer closes.
|
|
163
|
+
let overlay = $state<HTMLElement | null>(null);
|
|
164
|
+
$effect(() => {
|
|
165
|
+
const node = viewport;
|
|
166
|
+
if (!node) return;
|
|
167
|
+
let start: { id: number; x: number; y: number } | undefined;
|
|
168
|
+
let pulled: HTMLElement | undefined;
|
|
169
|
+
let moves: Move[] = [];
|
|
170
|
+
const restore = (animate: boolean) => {
|
|
171
|
+
const image = pulled;
|
|
172
|
+
pulled = undefined;
|
|
173
|
+
start = undefined;
|
|
174
|
+
if (!image) return;
|
|
175
|
+
const from = image.style.transform,
|
|
176
|
+
dark = overlay?.style.opacity;
|
|
177
|
+
image.style.removeProperty('transform');
|
|
178
|
+
overlay?.style.removeProperty('opacity');
|
|
179
|
+
if (!animate || reducedMotion()) return;
|
|
180
|
+
const timing = { duration: 200, easing: EASE };
|
|
181
|
+
image.animate([{ transform: from }, { transform: 'none' }], timing);
|
|
182
|
+
if (dark) overlay?.animate([{ opacity: dark }, { opacity: 1 }], timing);
|
|
183
|
+
};
|
|
184
|
+
const onStart = (event: TouchEvent) => {
|
|
185
|
+
if (pulled) return;
|
|
186
|
+
start = undefined;
|
|
187
|
+
// Two fingers zoom; a zoomed photo is panned, not pulled.
|
|
188
|
+
if (event.touches.length !== 1 || (visualViewport?.scale ?? 1) > 1.01) return;
|
|
189
|
+
const touch = event.touches[0];
|
|
190
|
+
start = { id: touch.identifier, x: touch.clientX, y: touch.clientY };
|
|
191
|
+
moves = [];
|
|
192
|
+
track(moves, touch, event);
|
|
193
|
+
};
|
|
194
|
+
const onMove = (event: TouchEvent) => {
|
|
195
|
+
if (!start) return;
|
|
196
|
+
if (event.touches.length !== 1) return restore(true);
|
|
197
|
+
const touch = touchOf(event, start.id);
|
|
198
|
+
if (!touch) return;
|
|
199
|
+
const dx = touch.clientX - start.x,
|
|
200
|
+
dy = touch.clientY - start.y;
|
|
201
|
+
if (!pulled) {
|
|
202
|
+
const down = dy > Math.abs(dx);
|
|
203
|
+
if (undecided(event, dx, dy, down)) return;
|
|
204
|
+
const slide = node.children[Math.round(node.scrollLeft / (node.clientWidth || 1))];
|
|
205
|
+
const image = slide?.querySelector('img');
|
|
206
|
+
// Sideways is the track's own scrolling.
|
|
207
|
+
if (!down || !image) return void (start = undefined);
|
|
208
|
+
pulled = image;
|
|
209
|
+
}
|
|
210
|
+
if (event.cancelable) event.preventDefault();
|
|
211
|
+
track(moves, touch, event);
|
|
212
|
+
const far = Math.min(1, Math.max(0, dy) / innerHeight);
|
|
213
|
+
pulled.style.transform = `translate(${dx}px, ${Math.max(0, dy)}px) scale(${1 - far * 0.3})`;
|
|
214
|
+
if (overlay) overlay.style.opacity = String(1 - Math.min(1, far * 2.5) * 0.85);
|
|
215
|
+
};
|
|
216
|
+
const onEnd = (event: TouchEvent) => {
|
|
217
|
+
if (!start || touchOf(event, start.id)) return;
|
|
218
|
+
const image = pulled,
|
|
219
|
+
last = moves[moves.length - 1];
|
|
220
|
+
if (!image) return void (start = undefined);
|
|
221
|
+
// No click from the drag, which would close the viewer on its own.
|
|
222
|
+
if (event.cancelable) event.preventDefault();
|
|
223
|
+
const away = event.type === 'touchend' && (last.y - start.y > 100 || speed(moves, 'y') > 0.5);
|
|
224
|
+
if (!away) return restore(true);
|
|
225
|
+
if (reducedMotion()) return void (open = false);
|
|
226
|
+
const across = last.x - start.x;
|
|
227
|
+
start = undefined;
|
|
228
|
+
const timing = { duration: 160, easing: 'ease-in', fill: 'forwards' as const };
|
|
229
|
+
overlay?.animate([{ opacity: overlay.style.opacity }, { opacity: 0 }], timing);
|
|
230
|
+
image.animate(
|
|
231
|
+
[
|
|
232
|
+
{ transform: image.style.transform, opacity: 1 },
|
|
233
|
+
{ transform: `translate(${across}px, ${innerHeight * 0.6}px) scale(0.7)`, opacity: 0 }
|
|
234
|
+
],
|
|
235
|
+
timing
|
|
236
|
+
).onfinish = () => (open = false);
|
|
237
|
+
};
|
|
238
|
+
node.addEventListener('touchstart', onStart, { passive: true });
|
|
239
|
+
node.addEventListener('touchmove', onMove, { passive: false });
|
|
240
|
+
node.addEventListener('touchend', onEnd);
|
|
241
|
+
node.addEventListener('touchcancel', onEnd);
|
|
242
|
+
return () => {
|
|
243
|
+
node.removeEventListener('touchstart', onStart);
|
|
244
|
+
node.removeEventListener('touchmove', onMove);
|
|
245
|
+
node.removeEventListener('touchend', onEnd);
|
|
246
|
+
node.removeEventListener('touchcancel', onEnd);
|
|
247
|
+
restore(false);
|
|
248
|
+
};
|
|
249
|
+
});
|
|
250
|
+
|
|
160
251
|
$effect(() => {
|
|
161
252
|
if (!photos.length) open = false;
|
|
162
253
|
if (index < 0 || index >= photos.length) index = 0;
|
|
@@ -221,7 +312,7 @@
|
|
|
221
312
|
<Dialog.Root bind:open>
|
|
222
313
|
{#if open && selected}
|
|
223
314
|
<Dialog.Portal>
|
|
224
|
-
<Dialog.Overlay class="fixed inset-0 z-50 bg-black/85" />
|
|
315
|
+
<Dialog.Overlay bind:ref={overlay} class="fixed inset-0 z-50 bg-black/85" />
|
|
225
316
|
<Dialog.Content
|
|
226
317
|
aria-describedby={undefined}
|
|
227
318
|
onclick={(event) => {
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { RefreshCw } from '@sketchyicons/svelte';
|
|
3
|
+
import Icon from './Icon.svelte';
|
|
4
|
+
import { cn } from '../utils.js';
|
|
5
|
+
import { LEAN, SLOP, ownsTouch, touchOf, undecided } from '../gestures.js';
|
|
6
|
+
|
|
7
|
+
/** How far the indicator comes down at most, where it refreshes, and where it waits. */
|
|
8
|
+
const MAX = 96;
|
|
9
|
+
const AT = 64;
|
|
10
|
+
const REST = 52;
|
|
11
|
+
/** Long enough to be seen, should the refresh return at once. */
|
|
12
|
+
const LEAST = 500;
|
|
13
|
+
|
|
14
|
+
let {
|
|
15
|
+
onrefresh,
|
|
16
|
+
disabled = false,
|
|
17
|
+
label = 'Refreshing',
|
|
18
|
+
class: className
|
|
19
|
+
}: {
|
|
20
|
+
/** Called when the page is pulled far enough; the indicator waits for its promise. */
|
|
21
|
+
onrefresh: () => unknown;
|
|
22
|
+
disabled?: boolean;
|
|
23
|
+
/** Announced while refreshing. */
|
|
24
|
+
label?: string;
|
|
25
|
+
/** For the indicator's fixed track, e.g. a `top-*` below a sticky header. */
|
|
26
|
+
class?: string;
|
|
27
|
+
} = $props();
|
|
28
|
+
|
|
29
|
+
let pull = $state(0);
|
|
30
|
+
let pulling = $state(false);
|
|
31
|
+
let refreshing = $state(false);
|
|
32
|
+
|
|
33
|
+
$effect(() => {
|
|
34
|
+
if (disabled) return;
|
|
35
|
+
let start: { id: number; x: number; y: number } | undefined;
|
|
36
|
+
const onStart = (event: TouchEvent) => {
|
|
37
|
+
if (pulling) return;
|
|
38
|
+
start = undefined;
|
|
39
|
+
if (event.touches.length !== 1 || refreshing || scrollY > 0) return;
|
|
40
|
+
if (ownsTouch(event.target, 'y')) return;
|
|
41
|
+
const touch = event.touches[0];
|
|
42
|
+
start = { id: touch.identifier, x: touch.clientX, y: touch.clientY };
|
|
43
|
+
};
|
|
44
|
+
const onMove = (event: TouchEvent) => {
|
|
45
|
+
if (!start) return;
|
|
46
|
+
const touch = touchOf(event, start.id);
|
|
47
|
+
if (!touch) return;
|
|
48
|
+
const dx = touch.clientX - start.x,
|
|
49
|
+
dy = touch.clientY - start.y;
|
|
50
|
+
if (!pulling) {
|
|
51
|
+
const down = dy > Math.abs(dx) * LEAN && scrollY <= 0;
|
|
52
|
+
if (undecided(event, dx, dy, down)) return;
|
|
53
|
+
if (!down) return void (start = undefined);
|
|
54
|
+
pulling = true;
|
|
55
|
+
}
|
|
56
|
+
if (event.cancelable) event.preventDefault();
|
|
57
|
+
// Heavier the further it goes, as a page stretched past its end is.
|
|
58
|
+
pull = MAX * (1 - Math.exp(-Math.max(0, dy - SLOP) / MAX));
|
|
59
|
+
};
|
|
60
|
+
const onEnd = (event: TouchEvent) => {
|
|
61
|
+
if (!start || touchOf(event, start.id)) return;
|
|
62
|
+
start = undefined;
|
|
63
|
+
if (!pulling) return;
|
|
64
|
+
pulling = false;
|
|
65
|
+
if (pull < AT || event.type === 'touchcancel') return void (pull = 0);
|
|
66
|
+
pull = REST;
|
|
67
|
+
refreshing = true;
|
|
68
|
+
const least = new Promise((resolve) => setTimeout(resolve, LEAST));
|
|
69
|
+
void Promise.allSettled([Promise.resolve().then(onrefresh), least]).then(() => {
|
|
70
|
+
refreshing = false;
|
|
71
|
+
pull = 0;
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
document.addEventListener('touchstart', onStart, { passive: true });
|
|
75
|
+
document.addEventListener('touchmove', onMove, { passive: false });
|
|
76
|
+
document.addEventListener('touchend', onEnd);
|
|
77
|
+
document.addEventListener('touchcancel', onEnd);
|
|
78
|
+
return () => {
|
|
79
|
+
document.removeEventListener('touchstart', onStart);
|
|
80
|
+
document.removeEventListener('touchmove', onMove);
|
|
81
|
+
document.removeEventListener('touchend', onEnd);
|
|
82
|
+
document.removeEventListener('touchcancel', onEnd);
|
|
83
|
+
pulling = false;
|
|
84
|
+
if (!refreshing) pull = 0;
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
</script>
|
|
88
|
+
|
|
89
|
+
<!-- Clipped at its top edge, so the indicator comes out from under whatever is above it. -->
|
|
90
|
+
<div
|
|
91
|
+
data-slot="pull-to-refresh"
|
|
92
|
+
data-state={refreshing ? 'refreshing' : pulling ? 'pulling' : 'idle'}
|
|
93
|
+
class={cn(
|
|
94
|
+
'pointer-events-none fixed inset-x-0 top-[env(safe-area-inset-top)] z-40 flex h-28 justify-center overflow-hidden',
|
|
95
|
+
className
|
|
96
|
+
)}
|
|
97
|
+
>
|
|
98
|
+
<div
|
|
99
|
+
aria-hidden="true"
|
|
100
|
+
style:transform="translateY({pull - 44}px)"
|
|
101
|
+
class={cn(
|
|
102
|
+
'grid size-9 place-items-center rounded-full border border-base-300 bg-base-50 text-base-600 dark:border-[color-mix(in_srgb,var(--base-800)_80%,var(--base-950))] dark:bg-base-925 dark:text-base-400',
|
|
103
|
+
!pulling && 'transition-transform duration-200 ease-out motion-reduce:transition-none',
|
|
104
|
+
pull >= AT && 'text-accent-700 dark:text-accent-300'
|
|
105
|
+
)}
|
|
106
|
+
>
|
|
107
|
+
<span
|
|
108
|
+
class={refreshing ? 'animate-spin motion-reduce:animate-none' : ''}
|
|
109
|
+
style:transform={refreshing ? undefined : `rotate(${(pull / AT) * 270}deg)`}
|
|
110
|
+
><Icon img={RefreshCw} size={18} /></span
|
|
111
|
+
>
|
|
112
|
+
</div>
|
|
113
|
+
<span role="status" class="sr-only">{refreshing ? label : ''}</span>
|
|
114
|
+
</div>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type $$ComponentProps = {
|
|
2
|
+
/** Called when the page is pulled far enough; the indicator waits for its promise. */
|
|
3
|
+
onrefresh: () => unknown;
|
|
4
|
+
disabled?: boolean;
|
|
5
|
+
/** Announced while refreshing. */
|
|
6
|
+
label?: string;
|
|
7
|
+
/** For the indicator's fixed track, e.g. a `top-*` below a sticky header. */
|
|
8
|
+
class?: string;
|
|
9
|
+
};
|
|
10
|
+
declare const PullToRefresh: import("svelte").Component<$$ComponentProps, {}, "">;
|
|
11
|
+
type PullToRefresh = ReturnType<typeof PullToRefresh>;
|
|
12
|
+
export default PullToRefresh;
|
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
import Surface from './Surface.svelte';
|
|
6
6
|
import Modal from './Modal.svelte';
|
|
7
7
|
import { cn } from '../utils.js';
|
|
8
|
+
import { drawerMotion } from '../drawer.svelte.js';
|
|
8
9
|
let {
|
|
9
10
|
label,
|
|
10
11
|
open = $bindable(false),
|
|
12
|
+
swipe = true,
|
|
11
13
|
children,
|
|
12
14
|
class: className,
|
|
13
15
|
style,
|
|
@@ -15,6 +17,8 @@
|
|
|
15
17
|
}: Omit<HTMLAttributes<HTMLElement>, 'children'> & {
|
|
16
18
|
label: string;
|
|
17
19
|
open?: boolean;
|
|
20
|
+
/** On touch screens, swipe right on the page to open the drawer and left to close it. */
|
|
21
|
+
swipe?: boolean;
|
|
18
22
|
children: Snippet;
|
|
19
23
|
} = $props();
|
|
20
24
|
// Native null means “remove the attribute”; Bits UI accepts undefined instead.
|
|
@@ -23,6 +27,15 @@
|
|
|
23
27
|
);
|
|
24
28
|
let desktop = $state<HTMLElement | null>(null);
|
|
25
29
|
const mobile = new MediaQuery('(width < 48rem)', false);
|
|
30
|
+
let panel = $state<HTMLElement | null>(null);
|
|
31
|
+
let overlay = $state<HTMLElement | null>(null);
|
|
32
|
+
const drawer = drawerMotion({
|
|
33
|
+
swipe: () => swipe && mobile.current,
|
|
34
|
+
open: () => open,
|
|
35
|
+
setOpen: (value) => (open = value),
|
|
36
|
+
panel: () => panel,
|
|
37
|
+
overlay: () => overlay
|
|
38
|
+
});
|
|
26
39
|
let drawerWasOpen = false;
|
|
27
40
|
$effect(() => {
|
|
28
41
|
const leavingDrawer = !mobile.current && drawerWasOpen;
|
|
@@ -61,7 +74,9 @@
|
|
|
61
74
|
{:else}
|
|
62
75
|
<Modal
|
|
63
76
|
{...modalAttributes}
|
|
64
|
-
bind:open
|
|
77
|
+
bind:open={() => open, (value) => (value ? (open = true) : drawer.dismiss())}
|
|
78
|
+
bind:ref={panel}
|
|
79
|
+
bind:overlayRef={overlay}
|
|
65
80
|
title={label}
|
|
66
81
|
onCloseAutoFocus={(event) => {
|
|
67
82
|
if (!mobile.current) {
|
|
@@ -3,6 +3,8 @@ import type { HTMLAttributes } from 'svelte/elements';
|
|
|
3
3
|
type $$ComponentProps = Omit<HTMLAttributes<HTMLElement>, 'children'> & {
|
|
4
4
|
label: string;
|
|
5
5
|
open?: boolean;
|
|
6
|
+
/** On touch screens, swipe right on the page to open the drawer and left to close it. */
|
|
7
|
+
swipe?: boolean;
|
|
6
8
|
children: Snippet;
|
|
7
9
|
};
|
|
8
10
|
declare const WorkspaceSidebar: import("svelte").Component<$$ComponentProps, {}, "open">;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How a left-hand mobile drawer moves. It slides in when opened and out when dismissed, and
|
|
3
|
+
* on touch screens it follows a finger: a swipe to the right anywhere on the page draws it
|
|
4
|
+
* out, one to the left pushes it back, and letting go settles it by distance and speed.
|
|
5
|
+
* Internal to the navigation components.
|
|
6
|
+
*/
|
|
7
|
+
export declare function drawerMotion(options: {
|
|
8
|
+
/** Whether swipes are listened for at all. */
|
|
9
|
+
swipe: () => boolean;
|
|
10
|
+
open: () => boolean;
|
|
11
|
+
setOpen: (open: boolean) => void;
|
|
12
|
+
panel: () => HTMLElement | null;
|
|
13
|
+
overlay: () => HTMLElement | null;
|
|
14
|
+
}): {
|
|
15
|
+
/** Slide out, then close. For dismissals; a navigation closes at once. */
|
|
16
|
+
dismiss(): void;
|
|
17
|
+
};
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { untrack } from 'svelte';
|
|
2
|
+
import { EASE, EDGE, LEAN, undecided, ownsTouch, reducedMotion, speed, touchOf, track } from './gestures.js';
|
|
3
|
+
/** A flick this fast settles the drawer the way it is going, in pixels per millisecond. */
|
|
4
|
+
const FLING = 0.3;
|
|
5
|
+
/**
|
|
6
|
+
* How a left-hand mobile drawer moves. It slides in when opened and out when dismissed, and
|
|
7
|
+
* on touch screens it follows a finger: a swipe to the right anywhere on the page draws it
|
|
8
|
+
* out, one to the left pushes it back, and letting go settles it by distance and speed.
|
|
9
|
+
* Internal to the navigation components.
|
|
10
|
+
*/
|
|
11
|
+
export function drawerMotion(options) {
|
|
12
|
+
let start;
|
|
13
|
+
let dragging = false;
|
|
14
|
+
// How much of the drawer is out, in pixels.
|
|
15
|
+
let shown = 0;
|
|
16
|
+
let moves = [];
|
|
17
|
+
let settling = [];
|
|
18
|
+
// Where it is settling to.
|
|
19
|
+
let goal = false;
|
|
20
|
+
const connected = (node) => (node?.isConnected ? node : null);
|
|
21
|
+
const panel = () => connected(options.panel());
|
|
22
|
+
const overlay = () => connected(options.overlay());
|
|
23
|
+
// The drawer's right edge at rest: its width, and any gap it keeps from the screen's edge.
|
|
24
|
+
const extent = (node) => node.offsetLeft + node.offsetWidth;
|
|
25
|
+
// How much of it is out right now, wherever an animation has it.
|
|
26
|
+
const out = (node) => extent(node) + new DOMMatrix(getComputedStyle(node).transform).m41;
|
|
27
|
+
function clear() {
|
|
28
|
+
for (const animation of settling)
|
|
29
|
+
animation.cancel();
|
|
30
|
+
settling = [];
|
|
31
|
+
panel()?.style.removeProperty('transform');
|
|
32
|
+
overlay()?.style.removeProperty('opacity');
|
|
33
|
+
}
|
|
34
|
+
function place() {
|
|
35
|
+
const node = panel();
|
|
36
|
+
if (!node)
|
|
37
|
+
return;
|
|
38
|
+
const size = extent(node);
|
|
39
|
+
const at = Math.min(shown, size);
|
|
40
|
+
node.style.transform = `translateX(${at - size}px)`;
|
|
41
|
+
const shade = overlay();
|
|
42
|
+
if (shade)
|
|
43
|
+
shade.style.opacity = String(at / size);
|
|
44
|
+
}
|
|
45
|
+
/** Carry the drawer from where it is to open or closed. */
|
|
46
|
+
function settle(open) {
|
|
47
|
+
const node = panel();
|
|
48
|
+
if (!node)
|
|
49
|
+
return void (open || options.setOpen(false));
|
|
50
|
+
const size = extent(node);
|
|
51
|
+
const at = Math.min(shown, size);
|
|
52
|
+
const left = (open ? size - at : at) / size;
|
|
53
|
+
const done = () => {
|
|
54
|
+
if (open)
|
|
55
|
+
return clear();
|
|
56
|
+
options.setOpen(false);
|
|
57
|
+
// It stays out of sight until it is removed; kept open by the app, it comes back.
|
|
58
|
+
requestAnimationFrame(() => options.open() && clear());
|
|
59
|
+
};
|
|
60
|
+
for (const animation of settling)
|
|
61
|
+
animation.cancel();
|
|
62
|
+
settling = [];
|
|
63
|
+
goal = open;
|
|
64
|
+
if (reducedMotion() || left === 0)
|
|
65
|
+
return done();
|
|
66
|
+
const timing = { duration: 110 + 90 * left, easing: EASE, fill: 'forwards' };
|
|
67
|
+
const slide = node.animate([
|
|
68
|
+
{ transform: `translateX(${at - size}px)` },
|
|
69
|
+
{ transform: `translateX(${open ? 0 : -size}px)` }
|
|
70
|
+
], timing);
|
|
71
|
+
const shade = overlay();
|
|
72
|
+
settling = [slide];
|
|
73
|
+
if (shade)
|
|
74
|
+
settling.push(shade.animate([{ opacity: at / size }, { opacity: open ? 1 : 0 }], timing));
|
|
75
|
+
slide.onfinish = () => settling.includes(slide) && done();
|
|
76
|
+
}
|
|
77
|
+
// A drawer that appears slides in, unless a finger already has it.
|
|
78
|
+
$effect(() => {
|
|
79
|
+
if (!options.panel())
|
|
80
|
+
return;
|
|
81
|
+
untrack(() => {
|
|
82
|
+
if (dragging)
|
|
83
|
+
return place();
|
|
84
|
+
shown = 0;
|
|
85
|
+
settle(true);
|
|
86
|
+
});
|
|
87
|
+
return () => {
|
|
88
|
+
settling = [];
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
function onStart(event) {
|
|
92
|
+
if (dragging)
|
|
93
|
+
return;
|
|
94
|
+
start = undefined;
|
|
95
|
+
if (event.touches.length !== 1)
|
|
96
|
+
return;
|
|
97
|
+
const touch = event.touches[0];
|
|
98
|
+
// Still sliding in, it can be caught and pushed back.
|
|
99
|
+
const open = settling.length ? goal : options.open();
|
|
100
|
+
const node = panel();
|
|
101
|
+
const target = event.target instanceof Node ? event.target : null;
|
|
102
|
+
if (open) {
|
|
103
|
+
// Open, it is pushed back from itself or from the page dimmed behind it.
|
|
104
|
+
if (!node || !(node.contains(target) || overlay()?.contains(target)))
|
|
105
|
+
return;
|
|
106
|
+
if (target instanceof Element && target.closest('[data-no-swipe], input[type="range"]'))
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
else if (touch.clientX < EDGE || ownsTouch(target, 'x'))
|
|
110
|
+
return;
|
|
111
|
+
start = {
|
|
112
|
+
id: touch.identifier,
|
|
113
|
+
x: touch.clientX,
|
|
114
|
+
y: touch.clientY,
|
|
115
|
+
open,
|
|
116
|
+
base: 0
|
|
117
|
+
};
|
|
118
|
+
moves = [];
|
|
119
|
+
track(moves, touch, event);
|
|
120
|
+
}
|
|
121
|
+
function onMove(event) {
|
|
122
|
+
if (!start)
|
|
123
|
+
return;
|
|
124
|
+
const touch = touchOf(event, start.id);
|
|
125
|
+
if (!touch)
|
|
126
|
+
return;
|
|
127
|
+
const dx = touch.clientX - start.x;
|
|
128
|
+
if (!dragging) {
|
|
129
|
+
const dy = touch.clientY - start.y;
|
|
130
|
+
const sideways = Math.abs(dx) > Math.abs(dy) * LEAN && (start.open ? dx < 0 : dx > 0);
|
|
131
|
+
if (undecided(event, dx, dy, sideways))
|
|
132
|
+
return;
|
|
133
|
+
// Up or down, or the wrong way for where it is.
|
|
134
|
+
if (!sideways) {
|
|
135
|
+
start = undefined;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
dragging = true;
|
|
139
|
+
// It moves from where the finger is now, without a jump.
|
|
140
|
+
start.x = touch.clientX;
|
|
141
|
+
const node = panel();
|
|
142
|
+
start.base = start.open && node ? out(node) : 0;
|
|
143
|
+
for (const animation of settling)
|
|
144
|
+
animation.cancel();
|
|
145
|
+
settling = [];
|
|
146
|
+
if (!start.open)
|
|
147
|
+
options.setOpen(true);
|
|
148
|
+
}
|
|
149
|
+
if (event.cancelable)
|
|
150
|
+
event.preventDefault();
|
|
151
|
+
shown = Math.max(0, start.base + touch.clientX - start.x);
|
|
152
|
+
track(moves, touch, event);
|
|
153
|
+
place();
|
|
154
|
+
}
|
|
155
|
+
function onEnd(event) {
|
|
156
|
+
if (!start || touchOf(event, start.id))
|
|
157
|
+
return;
|
|
158
|
+
const { open: was } = start;
|
|
159
|
+
start = undefined;
|
|
160
|
+
if (!dragging)
|
|
161
|
+
return;
|
|
162
|
+
dragging = false;
|
|
163
|
+
const node = panel();
|
|
164
|
+
const size = node ? extent(node) : 300;
|
|
165
|
+
const at = Math.min(shown, size);
|
|
166
|
+
const fast = speed(moves, 'x');
|
|
167
|
+
const open = event.type === 'touchcancel'
|
|
168
|
+
? was
|
|
169
|
+
: fast > FLING || (fast > -FLING && at > size * (was ? 0.6 : 0.4));
|
|
170
|
+
// Let go before it was even drawn: it opens as if asked to, or not at all.
|
|
171
|
+
if (!node)
|
|
172
|
+
return void (open || options.setOpen(false));
|
|
173
|
+
shown = at;
|
|
174
|
+
settle(open);
|
|
175
|
+
}
|
|
176
|
+
$effect(() => {
|
|
177
|
+
if (!options.swipe())
|
|
178
|
+
return;
|
|
179
|
+
document.addEventListener('touchstart', onStart, { passive: true });
|
|
180
|
+
document.addEventListener('touchmove', onMove, { passive: false });
|
|
181
|
+
document.addEventListener('touchend', onEnd);
|
|
182
|
+
document.addEventListener('touchcancel', onEnd);
|
|
183
|
+
return () => {
|
|
184
|
+
document.removeEventListener('touchstart', onStart);
|
|
185
|
+
document.removeEventListener('touchmove', onMove);
|
|
186
|
+
document.removeEventListener('touchend', onEnd);
|
|
187
|
+
document.removeEventListener('touchcancel', onEnd);
|
|
188
|
+
start = undefined;
|
|
189
|
+
if (dragging) {
|
|
190
|
+
dragging = false;
|
|
191
|
+
clear();
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
});
|
|
195
|
+
return {
|
|
196
|
+
/** Slide out, then close. For dismissals; a navigation closes at once. */
|
|
197
|
+
dismiss() {
|
|
198
|
+
const node = panel();
|
|
199
|
+
if (!node)
|
|
200
|
+
return options.setOpen(false);
|
|
201
|
+
if (dragging)
|
|
202
|
+
return;
|
|
203
|
+
// From wherever it is, should it still be on its way in.
|
|
204
|
+
shown = out(node);
|
|
205
|
+
settle(false);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** How far a finger moves before a gesture is decided. */
|
|
2
|
+
export declare const SLOP = 10;
|
|
3
|
+
/** How much further it must go one way than the other for that way to be meant. */
|
|
4
|
+
export declare const LEAN = 1.5;
|
|
5
|
+
export declare const EASE = "cubic-bezier(0.2, 0.8, 0.2, 1)";
|
|
6
|
+
/** Touches this close to the left edge belong to the system's back gesture. */
|
|
7
|
+
export declare const EDGE = 20;
|
|
8
|
+
export declare const reducedMotion: () => boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Whether a touch began in something that handles movement along this axis itself: a layer
|
|
11
|
+
* above the page, a control, something that scrolls that way, or anything marked
|
|
12
|
+
* `data-no-swipe` or given a `touch-action`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function ownsTouch(target: EventTarget | null, axis: 'x' | 'y'): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Whether a gesture can be decided yet. Browsers start scrolling within the first few
|
|
17
|
+
* pixels and then stop asking, so a touch that already leans our way (`ours`) is held back
|
|
18
|
+
* from scrolling until it has gone far enough to tell. A touch that stops a page still
|
|
19
|
+
* coasting cannot be held back at all; its direction alone decides.
|
|
20
|
+
*/
|
|
21
|
+
export declare function undecided(event: TouchEvent, dx: number, dy: number, ours: boolean): boolean;
|
|
22
|
+
export type Move = {
|
|
23
|
+
x: number;
|
|
24
|
+
y: number;
|
|
25
|
+
t: number;
|
|
26
|
+
};
|
|
27
|
+
/** Remember a move; only the last few matter for how fast the finger was going. */
|
|
28
|
+
export declare function track(moves: Move[], touch: Touch, event: TouchEvent): void;
|
|
29
|
+
/** Speed over the last few moves, in pixels per millisecond. */
|
|
30
|
+
export declare function speed(moves: Move[], axis: 'x' | 'y'): number;
|
|
31
|
+
export declare const touchOf: (event: TouchEvent, id: number) => Touch | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Go back by swiping right, on touch screens. The element follows the finger; let go far or
|
|
34
|
+
* fast enough and it slides away and `onback` is called, otherwise it returns. It stays
|
|
35
|
+
* out of sight until what `onback` returns has settled, so return a promise for a navigation
|
|
36
|
+
* that takes a moment. Returns a function that stops listening.
|
|
37
|
+
*
|
|
38
|
+
* With `follow: false` nothing moves: `onback` is called on release, as a back button would.
|
|
39
|
+
*
|
|
40
|
+
* Swipes are ignored where a drawer's are: at the left edge, in sideways scrollers, fields,
|
|
41
|
+
* dialogs and `data-no-swipe`. A page that uses this should not also open a drawer by swipe.
|
|
42
|
+
*/
|
|
43
|
+
export declare function swipeBack(target: () => HTMLElement | null | undefined, onback: () => unknown, { follow }?: {
|
|
44
|
+
follow?: boolean;
|
|
45
|
+
}): () => void;
|
package/dist/gestures.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// Shared rules for touch gestures. Internal apart from `swipeBack`.
|
|
2
|
+
/** How far a finger moves before a gesture is decided. */
|
|
3
|
+
export const SLOP = 10;
|
|
4
|
+
/** How much further it must go one way than the other for that way to be meant. */
|
|
5
|
+
export const LEAN = 1.5;
|
|
6
|
+
export const EASE = 'cubic-bezier(0.2, 0.8, 0.2, 1)';
|
|
7
|
+
/** Touches this close to the left edge belong to the system's back gesture. */
|
|
8
|
+
export const EDGE = 20;
|
|
9
|
+
const LAYERS = '[role="dialog"], [role="alertdialog"], dialog, [role="menu"], [role="listbox"], [data-dialog-overlay]';
|
|
10
|
+
const FIELDS = 'input, textarea, select, [contenteditable]:not([contenteditable="false"]), [role="slider"]';
|
|
11
|
+
export const reducedMotion = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
12
|
+
/**
|
|
13
|
+
* Whether a touch began in something that handles movement along this axis itself: a layer
|
|
14
|
+
* above the page, a control, something that scrolls that way, or anything marked
|
|
15
|
+
* `data-no-swipe` or given a `touch-action`.
|
|
16
|
+
*/
|
|
17
|
+
export function ownsTouch(target, axis) {
|
|
18
|
+
if (!(target instanceof Element))
|
|
19
|
+
return false;
|
|
20
|
+
if (target.closest(`[data-no-swipe], input[type="range"], ${LAYERS}`))
|
|
21
|
+
return true;
|
|
22
|
+
if (axis === 'x' && target.closest(FIELDS))
|
|
23
|
+
return true;
|
|
24
|
+
for (let node = target; node; node = node.parentElement) {
|
|
25
|
+
const style = getComputedStyle(node);
|
|
26
|
+
if (style.touchAction !== 'auto' && style.touchAction !== 'manipulation')
|
|
27
|
+
return true;
|
|
28
|
+
if (axis === 'y') {
|
|
29
|
+
if (node.scrollTop > 0 && node !== document.scrollingElement)
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
else if (node.scrollWidth > node.clientWidth + 1 &&
|
|
33
|
+
(style.overflowX === 'auto' || style.overflowX === 'scroll'))
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Whether a gesture can be decided yet. Browsers start scrolling within the first few
|
|
40
|
+
* pixels and then stop asking, so a touch that already leans our way (`ours`) is held back
|
|
41
|
+
* from scrolling until it has gone far enough to tell. A touch that stops a page still
|
|
42
|
+
* coasting cannot be held back at all; its direction alone decides.
|
|
43
|
+
*/
|
|
44
|
+
export function undecided(event, dx, dy, ours) {
|
|
45
|
+
if (Math.abs(dx) >= SLOP || Math.abs(dy) >= SLOP)
|
|
46
|
+
return false;
|
|
47
|
+
if (ours && Math.max(Math.abs(dx), Math.abs(dy)) >= 4 && event.cancelable)
|
|
48
|
+
event.preventDefault();
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
/** Remember a move; only the last few matter for how fast the finger was going. */
|
|
52
|
+
export function track(moves, touch, event) {
|
|
53
|
+
moves.push({ x: touch.clientX, y: touch.clientY, t: event.timeStamp });
|
|
54
|
+
if (moves.length > 5)
|
|
55
|
+
moves.shift();
|
|
56
|
+
}
|
|
57
|
+
/** Speed over the last few moves, in pixels per millisecond. */
|
|
58
|
+
export function speed(moves, axis) {
|
|
59
|
+
const first = moves[0], last = moves[moves.length - 1];
|
|
60
|
+
return first && last.t > first.t ? (last[axis] - first[axis]) / (last.t - first.t) : 0;
|
|
61
|
+
}
|
|
62
|
+
export const touchOf = (event, id) => Array.from(event.touches).find((touch) => touch.identifier === id);
|
|
63
|
+
/**
|
|
64
|
+
* Go back by swiping right, on touch screens. The element follows the finger; let go far or
|
|
65
|
+
* fast enough and it slides away and `onback` is called, otherwise it returns. It stays
|
|
66
|
+
* out of sight until what `onback` returns has settled, so return a promise for a navigation
|
|
67
|
+
* that takes a moment. Returns a function that stops listening.
|
|
68
|
+
*
|
|
69
|
+
* With `follow: false` nothing moves: `onback` is called on release, as a back button would.
|
|
70
|
+
*
|
|
71
|
+
* Swipes are ignored where a drawer's are: at the left edge, in sideways scrollers, fields,
|
|
72
|
+
* dialogs and `data-no-swipe`. A page that uses this should not also open a drawer by swipe.
|
|
73
|
+
*/
|
|
74
|
+
export function swipeBack(target, onback, { follow = true } = {}) {
|
|
75
|
+
const still = () => !follow || reducedMotion();
|
|
76
|
+
let start;
|
|
77
|
+
let node;
|
|
78
|
+
let moves = [];
|
|
79
|
+
let leaving = false;
|
|
80
|
+
const clear = (from = node) => {
|
|
81
|
+
from?.getAnimations().forEach((animation) => animation.cancel());
|
|
82
|
+
from?.style.removeProperty('transform');
|
|
83
|
+
if (from === node)
|
|
84
|
+
node = undefined;
|
|
85
|
+
};
|
|
86
|
+
const onStart = (event) => {
|
|
87
|
+
if (node || leaving)
|
|
88
|
+
return;
|
|
89
|
+
start = undefined;
|
|
90
|
+
if (event.touches.length !== 1 || !target())
|
|
91
|
+
return;
|
|
92
|
+
const touch = event.touches[0];
|
|
93
|
+
if (touch.clientX < EDGE || ownsTouch(event.target, 'x'))
|
|
94
|
+
return;
|
|
95
|
+
start = { id: touch.identifier, x: touch.clientX, y: touch.clientY };
|
|
96
|
+
moves = [];
|
|
97
|
+
track(moves, touch, event);
|
|
98
|
+
};
|
|
99
|
+
const onMove = (event) => {
|
|
100
|
+
if (!start || leaving)
|
|
101
|
+
return;
|
|
102
|
+
const touch = touchOf(event, start.id);
|
|
103
|
+
if (!touch)
|
|
104
|
+
return;
|
|
105
|
+
const dx = touch.clientX - start.x;
|
|
106
|
+
if (!node) {
|
|
107
|
+
const dy = touch.clientY - start.y;
|
|
108
|
+
const right = dx > Math.abs(dy) * LEAN;
|
|
109
|
+
if (undecided(event, dx, dy, right))
|
|
110
|
+
return;
|
|
111
|
+
const element = right ? target() : undefined;
|
|
112
|
+
if (!element)
|
|
113
|
+
return void (start = undefined);
|
|
114
|
+
node = element;
|
|
115
|
+
// It moves from where the finger is now, without a jump.
|
|
116
|
+
start.x = touch.clientX;
|
|
117
|
+
}
|
|
118
|
+
if (event.cancelable)
|
|
119
|
+
event.preventDefault();
|
|
120
|
+
track(moves, touch, event);
|
|
121
|
+
if (!still())
|
|
122
|
+
node.style.transform = `translateX(${Math.max(0, touch.clientX - start.x)}px)`;
|
|
123
|
+
};
|
|
124
|
+
const onEnd = (event) => {
|
|
125
|
+
if (!start || touchOf(event, start.id))
|
|
126
|
+
return;
|
|
127
|
+
const element = node;
|
|
128
|
+
const dx = moves[moves.length - 1].x - start.x;
|
|
129
|
+
start = undefined;
|
|
130
|
+
if (!element)
|
|
131
|
+
return;
|
|
132
|
+
const fast = speed(moves, 'x');
|
|
133
|
+
const back = event.type === 'touchend' &&
|
|
134
|
+
(fast > 0.4 || (fast > -0.3 && dx > Math.min(140, innerWidth * 0.35)));
|
|
135
|
+
if (still()) {
|
|
136
|
+
node = undefined;
|
|
137
|
+
return void (back && onback());
|
|
138
|
+
}
|
|
139
|
+
const from = element.style.transform || 'none';
|
|
140
|
+
if (!back) {
|
|
141
|
+
element.style.removeProperty('transform');
|
|
142
|
+
element.animate([{ transform: from }, { transform: 'none' }], {
|
|
143
|
+
duration: 200,
|
|
144
|
+
easing: EASE
|
|
145
|
+
}).onfinish = () => clear(element);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
leaving = true;
|
|
149
|
+
element.animate([{ transform: from }, { transform: `translateX(${innerWidth}px)` }], {
|
|
150
|
+
duration: 160,
|
|
151
|
+
easing: 'ease-in',
|
|
152
|
+
fill: 'forwards'
|
|
153
|
+
}).onfinish = () => void Promise.resolve()
|
|
154
|
+
.then(onback)
|
|
155
|
+
.catch(() => { })
|
|
156
|
+
.then(() => {
|
|
157
|
+
leaving = false;
|
|
158
|
+
clear(element);
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
document.addEventListener('touchstart', onStart, { passive: true });
|
|
162
|
+
document.addEventListener('touchmove', onMove, { passive: false });
|
|
163
|
+
document.addEventListener('touchend', onEnd);
|
|
164
|
+
document.addEventListener('touchcancel', onEnd);
|
|
165
|
+
return () => {
|
|
166
|
+
document.removeEventListener('touchstart', onStart);
|
|
167
|
+
document.removeEventListener('touchmove', onMove);
|
|
168
|
+
document.removeEventListener('touchend', onEnd);
|
|
169
|
+
document.removeEventListener('touchcancel', onEnd);
|
|
170
|
+
start = undefined;
|
|
171
|
+
if (!leaving)
|
|
172
|
+
clear();
|
|
173
|
+
};
|
|
174
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -66,6 +66,8 @@ export type { IconNode, SketchyIconProps } from '@sketchyicons/svelte';
|
|
|
66
66
|
export { default as IconRail } from './components/IconRail.svelte';
|
|
67
67
|
export type { IconRailItem } from './components/IconRail.svelte';
|
|
68
68
|
export { default as WorkspaceSidebar } from './components/WorkspaceSidebar.svelte';
|
|
69
|
+
export { swipeBack } from './gestures.js';
|
|
70
|
+
export { default as PullToRefresh } from './components/PullToRefresh.svelte';
|
|
69
71
|
export { default as AccountMenu } from './components/AccountMenu.svelte';
|
|
70
72
|
export type { AccountMenuAccount } from './components/AccountMenu.svelte';
|
|
71
73
|
export { default as LoginForm } from './components/login/LoginForm.svelte';
|
package/dist/index.js
CHANGED
|
@@ -44,6 +44,8 @@ export { default as Tabs } from './components/Tabs.svelte';
|
|
|
44
44
|
export { default as Icon } from './components/Icon.svelte';
|
|
45
45
|
export { default as IconRail } from './components/IconRail.svelte';
|
|
46
46
|
export { default as WorkspaceSidebar } from './components/WorkspaceSidebar.svelte';
|
|
47
|
+
export { swipeBack } from './gestures.js';
|
|
48
|
+
export { default as PullToRefresh } from './components/PullToRefresh.svelte';
|
|
47
49
|
export { default as AccountMenu } from './components/AccountMenu.svelte';
|
|
48
50
|
export { default as LoginForm } from './components/login/LoginForm.svelte';
|
|
49
51
|
export { default as LoginDialog } from './components/login/LoginDialog.svelte';
|