@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 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
@@ -117,7 +117,8 @@ Visit [drawer.abhivarde.in](https://drawer.abhivarde.in) to see live examples.
117
117
 
118
118
  ## License
119
119
 
120
- MIT
120
+ This project is licensed under the MIT License.
121
+ See the [LICENSE](./LICENSE) file for details.
121
122
 
122
123
  ## Credits
123
124
 
@@ -0,0 +1,4 @@
1
+ declare module "*.svg" {
2
+ const content: string;
3
+ export default content;
4
+ }
@@ -1,42 +1,48 @@
1
- <script>
2
- import { Spring } from 'svelte/motion';
3
- import { setContext } from 'svelte';
4
-
5
- let {
6
- open = $bindable(false),
7
- onOpenChange = undefined,
8
- direction = 'bottom',
9
- children
10
- } = $props();
11
-
12
- let overlayOpacity = new Spring(0, { stiffness: 0.32, damping: 0.72 });
13
- let drawerPosition = new Spring(100, { stiffness: 0.32, damping: 0.72 });
14
-
15
- $effect(() => {
16
- if (open) {
17
- overlayOpacity.target = 1;
18
- drawerPosition.target = 0;
19
- } else {
20
- overlayOpacity.target = 0;
21
- drawerPosition.target = 100;
22
- }
23
- });
24
-
25
- function closeDrawer() {
26
- open = false;
27
- if (onOpenChange) {
28
- onOpenChange(false);
29
- }
30
- }
31
-
32
- // Provide context to child components
33
- setContext('drawer', {
34
- get open() { return open; },
35
- get overlayOpacity() { return overlayOpacity; },
36
- get drawerPosition() { return drawerPosition; },
37
- get direction() { return direction; },
38
- closeDrawer
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 $$ComponentProps = {
13
- open?: boolean;
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
- import { getContext } from 'svelte';
3
-
4
- let {
5
- class: className = '',
6
- children,
7
- ...restProps
8
- } = $props();
9
-
10
- const drawer = getContext('drawer');
11
-
12
- function getTransform() {
13
- const pos = drawer.drawerPosition.current;
14
- switch (drawer.direction) {
15
- case 'bottom':
16
- return `translateY(${pos}%)`;
17
- case 'top':
18
- return `translateY(-${pos}%)`;
19
- case 'left':
20
- return `translateX(-${pos}%)`;
21
- case 'right':
22
- return `translateX(${pos}%)`;
23
- default:
24
- return `translateY(${pos}%)`;
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
- <div
31
- class={className}
32
- style="transform: {getTransform()}; z-index: 50;"
33
- {...restProps}
34
- >
35
- {@render children()}
36
- </div>
37
- {/if}
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 $$ComponentProps = {
11
- class?: string;
12
- children: any;
13
- } & Record<string, any>;
5
+ type DrawerContent = ReturnType<typeof DrawerContent>;
6
+ export default DrawerContent;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abhivarde/svelte-drawer",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "A drawer component for Svelte 5, inspired by Vaul",
5
5
  "author": "Abhi Varde",
6
6
  "license": "MIT",