@atmo-dev/kit 0.19.1 → 0.20.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/components/composer/PostComposer.svelte +41 -24
- package/dist/components/composer/PostComposer.svelte.d.ts +5 -0
- package/dist/drawer.svelte.d.ts +17 -0
- package/dist/drawer.svelte.js +208 -0
- package/dist/gestures.d.ts +41 -0
- package/dist/gestures.js +171 -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">;
|
|
@@ -70,6 +70,11 @@
|
|
|
70
70
|
notice?: string;
|
|
71
71
|
/** More toolbar buttons, after the built-in ones. */
|
|
72
72
|
actions?: Snippet<[{ post: Post; insert: (text: string) => void; disabled: boolean }]>;
|
|
73
|
+
/**
|
|
74
|
+
* `inline` sits in the page, e.g. a reply field under a post: no header or dividers,
|
|
75
|
+
* with Cancel and the submit button at the end of the toolbar.
|
|
76
|
+
*/
|
|
77
|
+
layout?: 'default' | 'inline';
|
|
73
78
|
class?: string;
|
|
74
79
|
};
|
|
75
80
|
</script>
|
|
@@ -134,8 +139,10 @@
|
|
|
134
139
|
notice,
|
|
135
140
|
drafts: draftStore,
|
|
136
141
|
actions,
|
|
142
|
+
layout = 'default',
|
|
137
143
|
class: className
|
|
138
144
|
}: PostComposerProps = $props();
|
|
145
|
+
const inline = untrack(() => layout === 'inline');
|
|
139
146
|
|
|
140
147
|
let limits = $derived({ ...defaultComposerLimits, ...limitOverrides });
|
|
141
148
|
const on = (feature: keyof ComposerFeatures) => features[feature] !== false;
|
|
@@ -395,6 +402,26 @@
|
|
|
395
402
|
}
|
|
396
403
|
</script>
|
|
397
404
|
|
|
405
|
+
{#snippet controls()}
|
|
406
|
+
{#if oncancel}
|
|
407
|
+
<Button variant="ghost" size="sm" onclick={cancel} disabled={busy}>
|
|
408
|
+
{labels.cancel ?? 'Cancel'}
|
|
409
|
+
</Button>
|
|
410
|
+
{/if}
|
|
411
|
+
<span class="min-w-0 flex-1 truncate text-right text-xs {muted}" aria-live="polite">{status}</span
|
|
412
|
+
>
|
|
413
|
+
{#if canDraft}
|
|
414
|
+
<Button variant="ghost" size="sm" onclick={() => (draftsOpen = true)} disabled={busy}>
|
|
415
|
+
Drafts
|
|
416
|
+
</Button>
|
|
417
|
+
{/if}
|
|
418
|
+
<Button size="sm" class="min-w-16" onclick={submit} disabled={!canPost} loading={busy}>
|
|
419
|
+
{posts.length > 1
|
|
420
|
+
? (labels.submitAll ?? 'Post all')
|
|
421
|
+
: (labels.submit ?? (reply ? 'Reply' : 'Post'))}
|
|
422
|
+
</Button>
|
|
423
|
+
{/snippet}
|
|
424
|
+
|
|
398
425
|
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
399
426
|
<div
|
|
400
427
|
class={cn('relative flex min-h-0 flex-1 flex-col', className)}
|
|
@@ -406,28 +433,13 @@
|
|
|
406
433
|
ondragover={(e) => hasFiles(e) && e.preventDefault()}
|
|
407
434
|
ondrop={drop}
|
|
408
435
|
>
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
{/if}
|
|
415
|
-
<span class="min-w-0 flex-1 truncate text-right text-xs {muted}" aria-live="polite"
|
|
416
|
-
>{status}</span
|
|
417
|
-
>
|
|
418
|
-
{#if canDraft}
|
|
419
|
-
<Button variant="ghost" size="sm" onclick={() => (draftsOpen = true)} disabled={busy}>
|
|
420
|
-
Drafts
|
|
421
|
-
</Button>
|
|
422
|
-
{/if}
|
|
423
|
-
<Button size="sm" class="min-w-16" onclick={submit} disabled={!canPost} loading={busy}>
|
|
424
|
-
{posts.length > 1
|
|
425
|
-
? (labels.submitAll ?? 'Post all')
|
|
426
|
-
: (labels.submit ?? (reply ? 'Reply' : 'Post'))}
|
|
427
|
-
</Button>
|
|
428
|
-
</div>
|
|
436
|
+
{#if !inline}
|
|
437
|
+
<div class="flex shrink-0 items-center gap-2 px-3 py-2.5">
|
|
438
|
+
{@render controls()}
|
|
439
|
+
</div>
|
|
440
|
+
{/if}
|
|
429
441
|
|
|
430
|
-
<div class="min-h-0 flex-1 overflow-y-auto overscroll-contain px-4 pt-2">
|
|
442
|
+
<div class="min-h-0 flex-1 overflow-y-auto overscroll-contain {inline ? 'pt-1' : 'px-4 pt-2'}">
|
|
431
443
|
{#if replyTo}
|
|
432
444
|
<div class="mb-3 flex gap-3 border-b pb-3 {line}">
|
|
433
445
|
{#if author}<span class="w-10 shrink-0"></span>{/if}
|
|
@@ -466,10 +478,10 @@
|
|
|
466
478
|
{/if}
|
|
467
479
|
</div>
|
|
468
480
|
|
|
469
|
-
{#if notice}<p class="shrink-0
|
|
481
|
+
{#if notice}<p class="shrink-0 pb-2 text-xs {inline ? '' : 'px-4'} {muted}">{notice}</p>{/if}
|
|
470
482
|
|
|
471
483
|
{#if on('interactions') || (on('contentWarnings') && contentWarnings.length && hasMedia(current))}
|
|
472
|
-
<div class="flex shrink-0 flex-wrap items-center gap-2
|
|
484
|
+
<div class="flex shrink-0 flex-wrap items-center gap-2 pb-1.5 {inline ? '' : 'px-4'}">
|
|
473
485
|
{#if on('interactions')}
|
|
474
486
|
<Button
|
|
475
487
|
variant="secondary"
|
|
@@ -498,7 +510,11 @@
|
|
|
498
510
|
</div>
|
|
499
511
|
{/if}
|
|
500
512
|
|
|
501
|
-
<div
|
|
513
|
+
<div
|
|
514
|
+
class="flex shrink-0 items-center gap-0.5 {inline
|
|
515
|
+
? '-ml-2 pb-1'
|
|
516
|
+
: `border-t px-2 py-1.5 ${line}`}"
|
|
517
|
+
>
|
|
502
518
|
{#if on('images')}
|
|
503
519
|
<input
|
|
504
520
|
bind:this={imageInput}
|
|
@@ -592,6 +608,7 @@
|
|
|
592
608
|
onclick={addPost}><Icon img={Plus} /></IconButton
|
|
593
609
|
>
|
|
594
610
|
{/if}
|
|
611
|
+
{#if inline}<span class="ml-1.5 flex items-center gap-2">{@render controls()}</span>{/if}
|
|
595
612
|
</div>
|
|
596
613
|
|
|
597
614
|
{#if dragging}
|
|
@@ -72,6 +72,11 @@ export type PostComposerProps = {
|
|
|
72
72
|
insert: (text: string) => void;
|
|
73
73
|
disabled: boolean;
|
|
74
74
|
}]>;
|
|
75
|
+
/**
|
|
76
|
+
* `inline` sits in the page, e.g. a reply field under a post: no header or dividers,
|
|
77
|
+
* with Cancel and the submit button at the end of the toolbar.
|
|
78
|
+
*/
|
|
79
|
+
layout?: 'default' | 'inline';
|
|
75
80
|
class?: string;
|
|
76
81
|
};
|
|
77
82
|
declare const PostComposer: import("svelte").Component<PostComposerProps, {
|
|
@@ -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,41 @@
|
|
|
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
|
+
* Swipes are ignored where a drawer's are: at the left edge, in sideways scrollers, fields,
|
|
39
|
+
* dialogs and `data-no-swipe`. A page that uses this should not also open a drawer by swipe.
|
|
40
|
+
*/
|
|
41
|
+
export declare function swipeBack(target: () => HTMLElement | null | undefined, onback: () => unknown): () => void;
|
package/dist/gestures.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
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
|
+
* Swipes are ignored where a drawer's are: at the left edge, in sideways scrollers, fields,
|
|
70
|
+
* dialogs and `data-no-swipe`. A page that uses this should not also open a drawer by swipe.
|
|
71
|
+
*/
|
|
72
|
+
export function swipeBack(target, onback) {
|
|
73
|
+
let start;
|
|
74
|
+
let node;
|
|
75
|
+
let moves = [];
|
|
76
|
+
let leaving = false;
|
|
77
|
+
const clear = (from = node) => {
|
|
78
|
+
from?.getAnimations().forEach((animation) => animation.cancel());
|
|
79
|
+
from?.style.removeProperty('transform');
|
|
80
|
+
if (from === node)
|
|
81
|
+
node = undefined;
|
|
82
|
+
};
|
|
83
|
+
const onStart = (event) => {
|
|
84
|
+
if (node || leaving)
|
|
85
|
+
return;
|
|
86
|
+
start = undefined;
|
|
87
|
+
if (event.touches.length !== 1 || !target())
|
|
88
|
+
return;
|
|
89
|
+
const touch = event.touches[0];
|
|
90
|
+
if (touch.clientX < EDGE || ownsTouch(event.target, 'x'))
|
|
91
|
+
return;
|
|
92
|
+
start = { id: touch.identifier, x: touch.clientX, y: touch.clientY };
|
|
93
|
+
moves = [];
|
|
94
|
+
track(moves, touch, event);
|
|
95
|
+
};
|
|
96
|
+
const onMove = (event) => {
|
|
97
|
+
if (!start || leaving)
|
|
98
|
+
return;
|
|
99
|
+
const touch = touchOf(event, start.id);
|
|
100
|
+
if (!touch)
|
|
101
|
+
return;
|
|
102
|
+
const dx = touch.clientX - start.x;
|
|
103
|
+
if (!node) {
|
|
104
|
+
const dy = touch.clientY - start.y;
|
|
105
|
+
const right = dx > Math.abs(dy) * LEAN;
|
|
106
|
+
if (undecided(event, dx, dy, right))
|
|
107
|
+
return;
|
|
108
|
+
const element = right ? target() : undefined;
|
|
109
|
+
if (!element)
|
|
110
|
+
return void (start = undefined);
|
|
111
|
+
node = element;
|
|
112
|
+
// It moves from where the finger is now, without a jump.
|
|
113
|
+
start.x = touch.clientX;
|
|
114
|
+
}
|
|
115
|
+
if (event.cancelable)
|
|
116
|
+
event.preventDefault();
|
|
117
|
+
track(moves, touch, event);
|
|
118
|
+
if (!reducedMotion())
|
|
119
|
+
node.style.transform = `translateX(${Math.max(0, touch.clientX - start.x)}px)`;
|
|
120
|
+
};
|
|
121
|
+
const onEnd = (event) => {
|
|
122
|
+
if (!start || touchOf(event, start.id))
|
|
123
|
+
return;
|
|
124
|
+
const element = node;
|
|
125
|
+
const dx = moves[moves.length - 1].x - start.x;
|
|
126
|
+
start = undefined;
|
|
127
|
+
if (!element)
|
|
128
|
+
return;
|
|
129
|
+
const fast = speed(moves, 'x');
|
|
130
|
+
const back = event.type === 'touchend' &&
|
|
131
|
+
(fast > 0.4 || (fast > -0.3 && dx > Math.min(140, innerWidth * 0.35)));
|
|
132
|
+
if (reducedMotion()) {
|
|
133
|
+
node = undefined;
|
|
134
|
+
return void (back && onback());
|
|
135
|
+
}
|
|
136
|
+
const from = element.style.transform || 'none';
|
|
137
|
+
if (!back) {
|
|
138
|
+
element.style.removeProperty('transform');
|
|
139
|
+
element.animate([{ transform: from }, { transform: 'none' }], {
|
|
140
|
+
duration: 200,
|
|
141
|
+
easing: EASE
|
|
142
|
+
}).onfinish = () => clear(element);
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
leaving = true;
|
|
146
|
+
element.animate([{ transform: from }, { transform: `translateX(${innerWidth}px)` }], {
|
|
147
|
+
duration: 160,
|
|
148
|
+
easing: 'ease-in',
|
|
149
|
+
fill: 'forwards'
|
|
150
|
+
}).onfinish = () => void Promise.resolve()
|
|
151
|
+
.then(onback)
|
|
152
|
+
.catch(() => { })
|
|
153
|
+
.then(() => {
|
|
154
|
+
leaving = false;
|
|
155
|
+
clear(element);
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
document.addEventListener('touchstart', onStart, { passive: true });
|
|
159
|
+
document.addEventListener('touchmove', onMove, { passive: false });
|
|
160
|
+
document.addEventListener('touchend', onEnd);
|
|
161
|
+
document.addEventListener('touchcancel', onEnd);
|
|
162
|
+
return () => {
|
|
163
|
+
document.removeEventListener('touchstart', onStart);
|
|
164
|
+
document.removeEventListener('touchmove', onMove);
|
|
165
|
+
document.removeEventListener('touchend', onEnd);
|
|
166
|
+
document.removeEventListener('touchcancel', onEnd);
|
|
167
|
+
start = undefined;
|
|
168
|
+
if (!leaving)
|
|
169
|
+
clear();
|
|
170
|
+
};
|
|
171
|
+
}
|
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';
|