@abhivarde/svelte-drawer 0.0.8 → 0.0.10
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.
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
|
|
22
22
|
let contentElement = $state<HTMLDivElement | null>(null);
|
|
23
23
|
let startPos = 0;
|
|
24
|
+
let startDragPos = 0;
|
|
24
25
|
let dragging = false;
|
|
25
26
|
|
|
26
27
|
function getTransform(): string {
|
|
@@ -38,8 +39,16 @@
|
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
function onPointerDown(e: PointerEvent | TouchEvent) {
|
|
42
|
+
const target = e.target as HTMLElement;
|
|
43
|
+
|
|
44
|
+
if (
|
|
45
|
+
target.closest('button, a, input, textarea, select') &&
|
|
46
|
+
!target.closest('[data-drawer-drag]')
|
|
47
|
+
) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
41
51
|
dragging = true;
|
|
42
|
-
document.body.style.cursor = "grabbing";
|
|
43
52
|
|
|
44
53
|
startPos =
|
|
45
54
|
drawer.direction === "bottom" || drawer.direction === "top"
|
|
@@ -50,13 +59,21 @@
|
|
|
50
59
|
? e.clientX
|
|
51
60
|
: (e.touches[0]?.clientX ?? 0);
|
|
52
61
|
|
|
53
|
-
|
|
62
|
+
startDragPos = drawer.drawerPosition.current;
|
|
63
|
+
|
|
64
|
+
window.addEventListener("pointermove", onPointerMove, { passive: false });
|
|
54
65
|
window.addEventListener("pointerup", onPointerUp);
|
|
66
|
+
window.addEventListener("touchmove", onPointerMove, { passive: false });
|
|
67
|
+
window.addEventListener("touchend", onPointerUp);
|
|
68
|
+
|
|
69
|
+
e.preventDefault();
|
|
55
70
|
}
|
|
56
71
|
|
|
57
72
|
function onPointerMove(e: PointerEvent | TouchEvent) {
|
|
58
73
|
if (!dragging) return;
|
|
59
74
|
|
|
75
|
+
e.preventDefault();
|
|
76
|
+
|
|
60
77
|
const current =
|
|
61
78
|
drawer.direction === "bottom" || drawer.direction === "top"
|
|
62
79
|
? "clientY" in e
|
|
@@ -67,26 +84,25 @@
|
|
|
67
84
|
: (e.touches[0]?.clientX ?? 0);
|
|
68
85
|
|
|
69
86
|
const delta = current - startPos;
|
|
87
|
+
let newPos = startDragPos;
|
|
88
|
+
|
|
89
|
+
if (drawer.direction === "bottom") {
|
|
90
|
+
newPos = Math.max(0, startDragPos + (delta / window.innerHeight) * 100);
|
|
91
|
+
} else if (drawer.direction === "top") {
|
|
92
|
+
newPos = Math.max(0, startDragPos + (-delta / window.innerHeight) * 100);
|
|
93
|
+
} else if (drawer.direction === "left") {
|
|
94
|
+
newPos = Math.max(0, startDragPos + (-delta / window.innerWidth) * 100);
|
|
95
|
+
} else if (drawer.direction === "right") {
|
|
96
|
+
newPos = Math.max(0, startDragPos + (delta / window.innerWidth) * 100);
|
|
97
|
+
}
|
|
70
98
|
|
|
71
|
-
|
|
72
|
-
drawer.drawerPosition.set(
|
|
73
|
-
Math.max(0, (delta / window.innerHeight) * 100)
|
|
74
|
-
);
|
|
75
|
-
else if (drawer.direction === "top")
|
|
76
|
-
drawer.drawerPosition.set(
|
|
77
|
-
Math.max(0, (-delta / window.innerHeight) * 100)
|
|
78
|
-
);
|
|
79
|
-
else if (drawer.direction === "left")
|
|
80
|
-
drawer.drawerPosition.set(
|
|
81
|
-
Math.max(0, (-delta / window.innerWidth) * 100)
|
|
82
|
-
);
|
|
83
|
-
else if (drawer.direction === "right")
|
|
84
|
-
drawer.drawerPosition.set(Math.max(0, (delta / window.innerWidth) * 100));
|
|
99
|
+
drawer.drawerPosition.set(newPos, { duration: 0 });
|
|
85
100
|
}
|
|
86
101
|
|
|
87
102
|
function onPointerUp() {
|
|
103
|
+
if (!dragging) return;
|
|
104
|
+
|
|
88
105
|
dragging = false;
|
|
89
|
-
document.body.style.cursor = "default";
|
|
90
106
|
|
|
91
107
|
const pos = drawer.drawerPosition.current;
|
|
92
108
|
|
|
@@ -98,6 +114,8 @@
|
|
|
98
114
|
|
|
99
115
|
window.removeEventListener("pointermove", onPointerMove);
|
|
100
116
|
window.removeEventListener("pointerup", onPointerUp);
|
|
117
|
+
window.removeEventListener("touchmove", onPointerMove);
|
|
118
|
+
window.removeEventListener("touchend", onPointerUp);
|
|
101
119
|
}
|
|
102
120
|
|
|
103
121
|
function getFocusableElements(): HTMLElement[] {
|
|
@@ -131,7 +149,11 @@
|
|
|
131
149
|
if (drawer.open && trapFocus && contentElement) {
|
|
132
150
|
tick().then(() => {
|
|
133
151
|
const focusable = getFocusableElements();
|
|
134
|
-
(focusable[0]
|
|
152
|
+
if (focusable[0]) {
|
|
153
|
+
focusable[0].focus({ preventScroll: true });
|
|
154
|
+
} else {
|
|
155
|
+
contentElement?.focus({ preventScroll: true });
|
|
156
|
+
}
|
|
135
157
|
});
|
|
136
158
|
}
|
|
137
159
|
});
|
|
@@ -148,13 +170,14 @@
|
|
|
148
170
|
<div
|
|
149
171
|
bind:this={contentElement}
|
|
150
172
|
class={className}
|
|
151
|
-
style="transform: {getTransform()}; z-index: 50;
|
|
152
|
-
onpointerdown={onPointerDown}
|
|
173
|
+
style="transform: {getTransform()}; z-index: 50; touch-action: none;"
|
|
153
174
|
tabindex="-1"
|
|
154
175
|
role="dialog"
|
|
155
176
|
aria-modal="true"
|
|
177
|
+
onpointerdown={onPointerDown}
|
|
178
|
+
ontouchstart={onPointerDown}
|
|
156
179
|
{...restProps}
|
|
157
180
|
>
|
|
158
181
|
{@render children()}
|
|
159
182
|
</div>
|
|
160
|
-
{/if}
|
|
183
|
+
{/if}
|