@abhivarde/svelte-drawer 0.0.1 → 0.0.2
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/LICENSE +21 -0
- package/README.md +2 -1
- package/dist/assets.d.ts +4 -0
- package/dist/components/Drawer.svelte +46 -40
- package/dist/components/Drawer.svelte.d.ts +2 -11
- package/dist/components/DrawerContent.svelte +97 -34
- package/dist/components/DrawerContent.svelte.d.ts +2 -9
- package/package.json +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Abhi Varde
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
package/dist/assets.d.ts
ADDED
|
@@ -1,42 +1,48 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { Tween } from "svelte/motion";
|
|
3
|
+
import { cubicOut } from "svelte/easing";
|
|
4
|
+
import { setContext } from "svelte";
|
|
5
|
+
|
|
6
|
+
let {
|
|
7
|
+
open = $bindable(false),
|
|
8
|
+
onOpenChange = undefined,
|
|
9
|
+
direction = "bottom",
|
|
10
|
+
children,
|
|
11
|
+
} = $props();
|
|
12
|
+
|
|
13
|
+
let overlayOpacity = new Tween(0, { duration: 300, easing: cubicOut });
|
|
14
|
+
let drawerPosition = new Tween(100, { duration: 300, easing: cubicOut });
|
|
15
|
+
|
|
16
|
+
$effect(() => {
|
|
17
|
+
if (open) {
|
|
18
|
+
overlayOpacity.set(1);
|
|
19
|
+
drawerPosition.set(0);
|
|
20
|
+
} else {
|
|
21
|
+
overlayOpacity.set(0);
|
|
22
|
+
drawerPosition.set(100);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
function closeDrawer() {
|
|
27
|
+
open = false;
|
|
28
|
+
if (onOpenChange) onOpenChange(false);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
setContext("drawer", {
|
|
32
|
+
get open() {
|
|
33
|
+
return open;
|
|
34
|
+
},
|
|
35
|
+
get overlayOpacity() {
|
|
36
|
+
return overlayOpacity;
|
|
37
|
+
},
|
|
38
|
+
get drawerPosition() {
|
|
39
|
+
return drawerPosition;
|
|
40
|
+
},
|
|
41
|
+
get direction() {
|
|
42
|
+
return direction;
|
|
43
|
+
},
|
|
44
|
+
closeDrawer,
|
|
45
|
+
});
|
|
40
46
|
</script>
|
|
41
47
|
|
|
42
|
-
{@render children()}
|
|
48
|
+
{@render children()}
|
|
@@ -1,17 +1,8 @@
|
|
|
1
|
-
export default Drawer;
|
|
2
|
-
type Drawer = {
|
|
3
|
-
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
-
$set?(props: Partial<$$ComponentProps>): void;
|
|
5
|
-
};
|
|
6
1
|
declare const Drawer: import("svelte").Component<{
|
|
7
2
|
open?: boolean;
|
|
8
3
|
onOpenChange?: any;
|
|
9
4
|
direction?: string;
|
|
10
5
|
children: any;
|
|
11
6
|
}, {}, "open">;
|
|
12
|
-
type
|
|
13
|
-
|
|
14
|
-
onOpenChange?: any;
|
|
15
|
-
direction?: string;
|
|
16
|
-
children: any;
|
|
17
|
-
};
|
|
7
|
+
type Drawer = ReturnType<typeof Drawer>;
|
|
8
|
+
export default Drawer;
|
|
@@ -1,37 +1,100 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext } from "svelte";
|
|
3
|
+
|
|
4
|
+
// Minimal inline type just to fix TypeScript
|
|
5
|
+
type DrawerContext = {
|
|
6
|
+
open: boolean;
|
|
7
|
+
overlayOpacity: { current: number; set: (v: number) => void };
|
|
8
|
+
drawerPosition: { current: number; set: (v: number) => void };
|
|
9
|
+
direction: "bottom" | "top" | "left" | "right";
|
|
10
|
+
closeDrawer: () => void;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
let { class: className = "", children, ...restProps } = $props();
|
|
14
|
+
|
|
15
|
+
// Tell TypeScript that context has this shape
|
|
16
|
+
const drawer = getContext<DrawerContext>("drawer");
|
|
17
|
+
|
|
18
|
+
let startPos = 0;
|
|
19
|
+
let dragging = false;
|
|
20
|
+
|
|
21
|
+
function getTransform(): string {
|
|
22
|
+
const pos = drawer.drawerPosition.current;
|
|
23
|
+
switch (drawer.direction) {
|
|
24
|
+
case "bottom":
|
|
25
|
+
return `translateY(${pos}%)`;
|
|
26
|
+
case "top":
|
|
27
|
+
return `translateY(-${pos}%)`;
|
|
28
|
+
case "left":
|
|
29
|
+
return `translateX(-${pos}%)`;
|
|
30
|
+
case "right":
|
|
31
|
+
return `translateX(${pos}%)`;
|
|
32
|
+
default:
|
|
33
|
+
return `translateY(${pos}%)`;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function onPointerDown(e: PointerEvent | TouchEvent) {
|
|
38
|
+
dragging = true;
|
|
39
|
+
startPos =
|
|
40
|
+
drawer.direction === "bottom" || drawer.direction === "top"
|
|
41
|
+
? "clientY" in e
|
|
42
|
+
? e.clientY
|
|
43
|
+
: (e.touches[0]?.clientY ?? 0)
|
|
44
|
+
: "clientX" in e
|
|
45
|
+
? e.clientX
|
|
46
|
+
: (e.touches[0]?.clientX ?? 0);
|
|
47
|
+
|
|
48
|
+
window.addEventListener("pointermove", onPointerMove);
|
|
49
|
+
window.addEventListener("pointerup", onPointerUp);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function onPointerMove(e: PointerEvent | TouchEvent) {
|
|
53
|
+
if (!dragging) return;
|
|
54
|
+
const current =
|
|
55
|
+
drawer.direction === "bottom" || drawer.direction === "top"
|
|
56
|
+
? "clientY" in e
|
|
57
|
+
? e.clientY
|
|
58
|
+
: (e.touches[0]?.clientY ?? 0)
|
|
59
|
+
: "clientX" in e
|
|
60
|
+
? e.clientX
|
|
61
|
+
: (e.touches[0]?.clientX ?? 0);
|
|
62
|
+
|
|
63
|
+
const delta = current - startPos;
|
|
64
|
+
|
|
65
|
+
if (drawer.direction === "bottom")
|
|
66
|
+
drawer.drawerPosition.set(
|
|
67
|
+
Math.max(0, (delta / window.innerHeight) * 100)
|
|
68
|
+
);
|
|
69
|
+
else if (drawer.direction === "top")
|
|
70
|
+
drawer.drawerPosition.set(
|
|
71
|
+
Math.max(0, (-delta / window.innerHeight) * 100)
|
|
72
|
+
);
|
|
73
|
+
else if (drawer.direction === "left")
|
|
74
|
+
drawer.drawerPosition.set(
|
|
75
|
+
Math.max(0, (-delta / window.innerWidth) * 100)
|
|
76
|
+
);
|
|
77
|
+
else if (drawer.direction === "right")
|
|
78
|
+
drawer.drawerPosition.set(Math.max(0, (delta / window.innerWidth) * 100));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function onPointerUp() {
|
|
82
|
+
dragging = false;
|
|
83
|
+
if (drawer.drawerPosition.current > 30) drawer.closeDrawer();
|
|
84
|
+
else drawer.drawerPosition.set(0);
|
|
85
|
+
|
|
86
|
+
window.removeEventListener("pointermove", onPointerMove);
|
|
87
|
+
window.removeEventListener("pointerup", onPointerUp);
|
|
88
|
+
}
|
|
27
89
|
</script>
|
|
28
90
|
|
|
29
91
|
{#if drawer.open}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
92
|
+
<div
|
|
93
|
+
class={className}
|
|
94
|
+
style="transform: {getTransform()}; z-index: 50;"
|
|
95
|
+
onpointerdown={onPointerDown}
|
|
96
|
+
{...restProps}
|
|
97
|
+
>
|
|
98
|
+
{@render children()}
|
|
99
|
+
</div>
|
|
100
|
+
{/if}
|
|
@@ -1,13 +1,6 @@
|
|
|
1
|
-
export default DrawerContent;
|
|
2
|
-
type DrawerContent = {
|
|
3
|
-
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
-
$set?(props: Partial<$$ComponentProps>): void;
|
|
5
|
-
};
|
|
6
1
|
declare const DrawerContent: import("svelte").Component<{
|
|
7
2
|
class?: string;
|
|
8
3
|
children: any;
|
|
9
4
|
} & Record<string, any>, {}, "">;
|
|
10
|
-
type
|
|
11
|
-
|
|
12
|
-
children: any;
|
|
13
|
-
} & Record<string, any>;
|
|
5
|
+
type DrawerContent = ReturnType<typeof DrawerContent>;
|
|
6
|
+
export default DrawerContent;
|