@rodrigodagostino/svelte-draggable-drawer 0.3.0 → 0.4.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/Drawer.svelte +37 -7
- package/dist/components/Drawer.svelte.d.ts +1 -1
- package/dist/components/DrawerBackdrop.svelte +14 -13
- package/dist/components/DrawerContent.svelte +4 -0
- package/dist/constants/index.d.ts +1 -0
- package/dist/constants/index.js +1 -0
- package/dist/states/drawer.svelte.d.ts +4 -0
- package/dist/states/drawer.svelte.js +1 -0
- package/dist/types/props.d.ts +5 -0
- package/dist/utils/geometry.d.ts +1 -0
- package/dist/utils/geometry.js +3 -0
- package/dist/utils/index.d.ts +2 -1
- package/dist/utils/index.js +2 -1
- package/package.json +1 -1
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
RANGE_END_DEFAULT,
|
|
4
|
+
RANGE_START_DEFAULT,
|
|
5
|
+
SNAP_POINT_OFFSET,
|
|
6
|
+
} from '../constants/index.js';
|
|
3
7
|
import { setDrawerRootState } from '../states/index.js';
|
|
4
8
|
import type { DrawerRootProps } from '../types/props.js';
|
|
9
|
+
import { toPixels } from '../utils/index.js';
|
|
5
10
|
|
|
6
11
|
let {
|
|
7
12
|
ref = $bindable(null),
|
|
8
13
|
range = { start: RANGE_START_DEFAULT, end: RANGE_END_DEFAULT },
|
|
14
|
+
snapPoints = [],
|
|
15
|
+
activeSnapPoint = $bindable(null),
|
|
9
16
|
isOpen = $bindable(false),
|
|
10
17
|
children,
|
|
11
18
|
...restProps
|
|
@@ -17,19 +24,22 @@
|
|
|
17
24
|
rootState.props = {
|
|
18
25
|
ref,
|
|
19
26
|
range,
|
|
27
|
+
snapPoints: snapPoints.sort((a, b) => toPixels(a, 0) - toPixels(b, 0)),
|
|
28
|
+
activeSnapPoint,
|
|
20
29
|
isOpen,
|
|
21
30
|
};
|
|
22
31
|
});
|
|
23
32
|
|
|
24
33
|
$effect(() => {
|
|
34
|
+
activeSnapPoint = rootState.activeSnapPoint ?? null;
|
|
25
35
|
isOpen = rootState.props.isOpen || false;
|
|
26
36
|
});
|
|
27
37
|
|
|
28
38
|
const classes = $derived(['sdd-root', restProps.class]);
|
|
29
39
|
let pointerId: PointerEvent['pointerId'] | null = null;
|
|
30
40
|
|
|
31
|
-
const
|
|
32
|
-
const
|
|
41
|
+
const START_THRESHOLD = 2 / 5;
|
|
42
|
+
const END_THRESHOLD = 2 / 5;
|
|
33
43
|
|
|
34
44
|
let transitionTimeoutId: ReturnType<typeof setTimeout> | null = null;
|
|
35
45
|
|
|
@@ -61,10 +71,30 @@
|
|
|
61
71
|
rootState.dragState = 'drag';
|
|
62
72
|
|
|
63
73
|
rootState.pointer = { x: clientX, y: clientY };
|
|
64
|
-
rootState.
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
74
|
+
const handleTop = rootState.handle?.getBoundingClientRect()?.top ?? clientY;
|
|
75
|
+
const visibleHeight = window.innerHeight - handleTop;
|
|
76
|
+
const snapPoints = rootState.props.snapPoints ?? [];
|
|
77
|
+
const snapPointsInPx = (snapPoints ?? []).map((point) => toPixels(point, 0));
|
|
78
|
+
|
|
79
|
+
if (snapPointsInPx.length) {
|
|
80
|
+
const isBelowFirst = visibleHeight <= snapPointsInPx[0] - SNAP_POINT_OFFSET;
|
|
81
|
+
const isAboveLast = visibleHeight >= snapPointsInPx.at(-1)! + SNAP_POINT_OFFSET;
|
|
82
|
+
|
|
83
|
+
if (isBelowFirst || isAboveLast) {
|
|
84
|
+
rootState.activeSnapPoint = null;
|
|
85
|
+
} else {
|
|
86
|
+
const index = snapPointsInPx.findIndex(
|
|
87
|
+
(point) => Math.abs(visibleHeight - point) <= SNAP_POINT_OFFSET
|
|
88
|
+
);
|
|
89
|
+
if (index !== -1) rootState.activeSnapPoint = { index, value: snapPoints[index] };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
rootState.props.isOpen = !isBelowFirst;
|
|
93
|
+
} else {
|
|
94
|
+
const threshold =
|
|
95
|
+
rootState.pointerOrigin.y > window.innerHeight / 2 ? START_THRESHOLD : END_THRESHOLD;
|
|
96
|
+
rootState.props.isOpen = handleTop < window.innerHeight * threshold;
|
|
97
|
+
}
|
|
68
98
|
}
|
|
69
99
|
|
|
70
100
|
function handlePointerUp() {
|
|
@@ -2,6 +2,6 @@ import type { DrawerRootProps } from '../types/props.js';
|
|
|
2
2
|
type $$ComponentProps = DrawerRootProps & {
|
|
3
3
|
class?: string;
|
|
4
4
|
};
|
|
5
|
-
declare const Drawer: import("svelte").Component<$$ComponentProps, {}, "ref" | "isOpen">;
|
|
5
|
+
declare const Drawer: import("svelte").Component<$$ComponentProps, {}, "ref" | "activeSnapPoint" | "isOpen">;
|
|
6
6
|
type Drawer = ReturnType<typeof Drawer>;
|
|
7
7
|
export default Drawer;
|
|
@@ -3,31 +3,32 @@
|
|
|
3
3
|
import { RANGE_END_DEFAULT, RANGE_START_DEFAULT } from '../constants/index.js';
|
|
4
4
|
import { getDrawerRootState } from '../states/drawer.svelte.js';
|
|
5
5
|
import type { DrawerBackdropProps as BackdropProps } from '../types/props.js';
|
|
6
|
-
import { toPixels } from '../utils/index.js';
|
|
6
|
+
import { clamp, toPixels } from '../utils/index.js';
|
|
7
7
|
|
|
8
8
|
let { ref = $bindable(null), ...restProps }: BackdropProps & { class?: string } = $props();
|
|
9
9
|
|
|
10
|
-
let offsetHeight: number = $state(0);
|
|
11
|
-
|
|
12
10
|
const rootState = getDrawerRootState();
|
|
13
11
|
|
|
14
12
|
const classes = $derived(['sdd-backdrop', restProps.class]);
|
|
15
13
|
|
|
16
14
|
function getStyleOpacity() {
|
|
17
15
|
if (rootState.dragState.startsWith('drag') && rootState.pointer) {
|
|
18
|
-
const
|
|
19
|
-
if (!handleRect) return 0;
|
|
16
|
+
const handleTop = rootState.handle?.getBoundingClientRect().top ?? rootState.pointer.y;
|
|
20
17
|
|
|
21
18
|
const rangeStart = toPixels(rootState.props.range?.start, RANGE_START_DEFAULT);
|
|
22
|
-
const rangeEnd =
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
const rangeEnd = Math.max(
|
|
20
|
+
toPixels(rootState.props.range?.end, RANGE_END_DEFAULT),
|
|
21
|
+
rootState.props.snapPoints?.length && rootState.content
|
|
22
|
+
? rootState.content?.offsetHeight -
|
|
23
|
+
toPixels(rootState.props.snapPoints[0], RANGE_END_DEFAULT)
|
|
24
|
+
: RANGE_END_DEFAULT
|
|
27
25
|
);
|
|
28
|
-
const
|
|
26
|
+
const availableHeight = window.innerHeight - rangeEnd - rangeStart;
|
|
27
|
+
const handleOffset = clamp(handleTop - rangeEnd, 0, window.innerHeight - rangeStart);
|
|
28
|
+
const closedProgress = handleOffset / availableHeight;
|
|
29
|
+
const opacity = 0.5 - closedProgress / 2;
|
|
29
30
|
|
|
30
|
-
return
|
|
31
|
+
return clamp(opacity, 0, 0.5);
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
return rootState.props.isOpen ? 0.5 : 0;
|
|
@@ -51,13 +52,13 @@
|
|
|
51
52
|
});
|
|
52
53
|
|
|
53
54
|
const handleClick = () => {
|
|
55
|
+
rootState.activeSnapPoint = null;
|
|
54
56
|
rootState.props.isOpen = false;
|
|
55
57
|
};
|
|
56
58
|
</script>
|
|
57
59
|
|
|
58
60
|
<div
|
|
59
61
|
bind:this={ref}
|
|
60
|
-
bind:offsetHeight
|
|
61
62
|
class={classes}
|
|
62
63
|
style:opacity={styleOpacity}
|
|
63
64
|
style:transition={styleTransition}
|
|
@@ -47,6 +47,10 @@
|
|
|
47
47
|
return `translate3d(0, ${y}px, 0)`;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
if (rootState.activeSnapPoint !== null) {
|
|
51
|
+
return `translate3d(0, calc(100% - ${toCSSLength(rootState.props.snapPoints![rootState.activeSnapPoint.index])}), 0)`;
|
|
52
|
+
}
|
|
53
|
+
|
|
50
54
|
return rootState.props.isOpen
|
|
51
55
|
? `translate3d(0, calc(0% + ${toCSSLength(rootState.props.range?.end ?? RANGE_END_DEFAULT)}), 0)`
|
|
52
56
|
: `translate3d(0, calc(100% - ${toCSSLength(rootState.props.range?.start ?? RANGE_START_DEFAULT)}), 0)`;
|
package/dist/constants/index.js
CHANGED
|
@@ -17,6 +17,10 @@ declare class DrawerRootState {
|
|
|
17
17
|
x: number;
|
|
18
18
|
y: number;
|
|
19
19
|
} | null;
|
|
20
|
+
activeSnapPoint: {
|
|
21
|
+
index: number;
|
|
22
|
+
value: string | number;
|
|
23
|
+
} | null;
|
|
20
24
|
}
|
|
21
25
|
export declare function setDrawerRootState(): DrawerRootState;
|
|
22
26
|
export declare function getDrawerRootState(): DrawerRootState;
|
package/dist/types/props.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function clamp(value: number, min: number, max: number): number;
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
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
|
+
"version": "0.4.0",
|
|
5
5
|
"author": "Rodrigo D’Agostino <rodrigo.dagostino@proton.me>",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"draggable",
|