@rodrigodagostino/svelte-draggable-drawer 0.4.1 → 0.4.3
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/README.md
CHANGED
|
@@ -16,3 +16,16 @@ A comprehensive package for creating accessible, draggable drawers in Svelte app
|
|
|
16
16
|
|
|
17
17
|
- [Netlify](https://svelte-draggable-drawer.netlify.app)
|
|
18
18
|
- [Cloudflare](https://svelte-draggable-drawer.pages.dev/)
|
|
19
|
+
|
|
20
|
+
## Initial snap point
|
|
21
|
+
|
|
22
|
+
Set `initialSnapPoint` on `Drawer.Root` to a value from `snapPoints`. This example starts the drawer open with half the viewport height visible, limited by the content height and the drawer’s range.
|
|
23
|
+
|
|
24
|
+
```svelte
|
|
25
|
+
<Drawer.Root snapPoints={[160, '50%']} initialSnapPoint="50%">
|
|
26
|
+
<Drawer.Content>Drawer content</Drawer.Content>
|
|
27
|
+
<Drawer.Backdrop />
|
|
28
|
+
</Drawer.Root>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The drawer selects this point once per mount and updates `bind:activeSnapPoint` and `bind:isOpen`. If you omit `initialSnapPoint` or its value has no exact match, `isOpen` determines the starting position.
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
ref = $bindable(null),
|
|
13
13
|
range = { start: RANGE_START_DEFAULT, end: RANGE_END_DEFAULT },
|
|
14
14
|
snapPoints = [],
|
|
15
|
+
initialSnapPoint,
|
|
15
16
|
activeSnapPoint = $bindable(null),
|
|
16
17
|
isOpen = $bindable(false),
|
|
17
18
|
children,
|
|
@@ -19,12 +20,26 @@
|
|
|
19
20
|
}: DrawerRootProps & { class?: string } = $props();
|
|
20
21
|
|
|
21
22
|
const rootState = setDrawerRootState();
|
|
23
|
+
const sortedSnapPoints = $derived(
|
|
24
|
+
[...snapPoints].sort((a, b) => toPixels(a, 0) - toPixels(b, 0))
|
|
25
|
+
);
|
|
26
|
+
let initialized = false;
|
|
27
|
+
|
|
28
|
+
$effect.pre(() => {
|
|
29
|
+
if (!initialized) {
|
|
30
|
+
initialized = true;
|
|
31
|
+
const index = sortedSnapPoints.findIndex((point) => point === initialSnapPoint);
|
|
32
|
+
if (index !== -1) {
|
|
33
|
+
rootState.activeSnapPoint = { index, value: sortedSnapPoints[index] };
|
|
34
|
+
isOpen = true;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
22
37
|
|
|
23
|
-
|
|
38
|
+
if (!isOpen) rootState.activeSnapPoint = null;
|
|
24
39
|
rootState.props = {
|
|
25
40
|
ref,
|
|
26
41
|
range,
|
|
27
|
-
snapPoints:
|
|
42
|
+
snapPoints: sortedSnapPoints,
|
|
28
43
|
activeSnapPoint,
|
|
29
44
|
isOpen,
|
|
30
45
|
};
|
|
@@ -76,7 +91,7 @@
|
|
|
76
91
|
rootState.pointer = { x: e.clientX, y: e.clientY };
|
|
77
92
|
rootState.pointerOrigin = { x: e.clientX, y: e.clientY };
|
|
78
93
|
rootState.contentOrigin = rootState.content.getBoundingClientRect();
|
|
79
|
-
rootState.dragState = 'drag-
|
|
94
|
+
rootState.dragState = 'drag-starting';
|
|
80
95
|
|
|
81
96
|
draggedElem.addEventListener('pointermove', handlePointerMove);
|
|
82
97
|
draggedElem.addEventListener(
|
|
@@ -92,7 +107,7 @@
|
|
|
92
107
|
async function handlePointerMove({ clientX, clientY }: PointerEvent) {
|
|
93
108
|
if (!rootState.content || !rootState.pointerOrigin) return;
|
|
94
109
|
|
|
95
|
-
rootState.dragState = '
|
|
110
|
+
rootState.dragState = 'dragging';
|
|
96
111
|
|
|
97
112
|
rootState.pointer = { x: clientX, y: clientY };
|
|
98
113
|
const draggedTop =
|
|
@@ -127,7 +142,7 @@
|
|
|
127
142
|
function handlePointerUp() {
|
|
128
143
|
if (!rootState.pointerOrigin || !rootState.content) return;
|
|
129
144
|
|
|
130
|
-
rootState.dragState = '
|
|
145
|
+
rootState.dragState = 'snapping';
|
|
131
146
|
|
|
132
147
|
function handleTransitionEnd({ propertyName }: TransitionEvent) {
|
|
133
148
|
if (propertyName === 'transform') {
|
|
@@ -157,11 +172,6 @@
|
|
|
157
172
|
}
|
|
158
173
|
</script>
|
|
159
174
|
|
|
160
|
-
<pre style="position: fixed; top: 2rem; left: 2rem;">
|
|
161
|
-
<code>activeSnapPoint: {JSON.stringify(rootState.activeSnapPoint)}</code>
|
|
162
|
-
<code>isOpen: {JSON.stringify(isOpen)}</code>
|
|
163
|
-
</pre>
|
|
164
|
-
|
|
165
175
|
<div class={classes} data-drag-state={rootState.dragState} onpointerdown={handlePointerDown}>
|
|
166
176
|
{@render children?.()}
|
|
167
177
|
</div>
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import { untrack } from 'svelte';
|
|
3
2
|
import { BROWSER } from 'esm-env';
|
|
4
3
|
import { RANGE_END_DEFAULT, RANGE_START_DEFAULT } from '../constants/index.js';
|
|
5
4
|
import { getDrawerRootState } from '../states/drawer.svelte.js';
|
|
@@ -29,7 +28,7 @@
|
|
|
29
28
|
window.innerHeight - toPixels(rootState.props.range?.end, RANGE_END_DEFAULT)
|
|
30
29
|
);
|
|
31
30
|
|
|
32
|
-
if (rootState.dragState === '
|
|
31
|
+
if (rootState.dragState === 'dragging') {
|
|
33
32
|
if (!rootState.pointer || !rootState.pointerOrigin || !rootState.contentOrigin)
|
|
34
33
|
return 'translate3d(0, 100%, 0)';
|
|
35
34
|
|
|
@@ -69,12 +68,7 @@
|
|
|
69
68
|
: `translate3d(0, calc(100% - ${START_LIMIT}px), 0)`;
|
|
70
69
|
}
|
|
71
70
|
|
|
72
|
-
const styleTransform = $derived
|
|
73
|
-
void rootState.pointer;
|
|
74
|
-
void rootState.dragState;
|
|
75
|
-
void rootState.props.isOpen;
|
|
76
|
-
return untrack(() => getStyleTransform());
|
|
77
|
-
});
|
|
71
|
+
const styleTransform = $derived(getStyleTransform());
|
|
78
72
|
|
|
79
73
|
const styleTransition = $derived(
|
|
80
74
|
!rootState.dragState.startsWith('drag')
|
|
@@ -2,7 +2,7 @@ import type { DrawerRootProps as RootProps } from '../types/index.js';
|
|
|
2
2
|
declare class DrawerRootState {
|
|
3
3
|
ref: HTMLElement | null;
|
|
4
4
|
props: RootProps;
|
|
5
|
-
dragState: 'idle' | 'drag-
|
|
5
|
+
dragState: 'idle' | 'drag-starting' | 'dragging' | 'snapping';
|
|
6
6
|
content: HTMLElement | null;
|
|
7
7
|
handle: HTMLButtonElement | null;
|
|
8
8
|
pointer: {
|
package/dist/types/props.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export interface DrawerRootProps {
|
|
|
6
6
|
end: string | number;
|
|
7
7
|
};
|
|
8
8
|
snapPoints?: (string | number)[];
|
|
9
|
+
/** Value from snapPoints to select when the drawer mounts. */
|
|
10
|
+
initialSnapPoint?: string | number;
|
|
9
11
|
activeSnapPoint?: {
|
|
10
12
|
index: number;
|
|
11
13
|
value: string | number;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rodrigodagostino/svelte-draggable-drawer",
|
|
3
3
|
"description": "Create accessible, draggable drawers in Svelte, with keyboard, mouse, and touch support.",
|
|
4
|
-
"version": "0.4.
|
|
4
|
+
"version": "0.4.3",
|
|
5
5
|
"author": "Rodrigo D’Agostino <rodrigo.dagostino@proton.me>",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"draggable",
|