@rodrigodagostino/svelte-draggable-drawer 0.2.0 → 0.3.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 +4 -0
- package/dist/components/DrawerBackdrop.svelte +33 -14
- package/dist/components/DrawerContent.svelte +38 -23
- package/dist/constants/index.d.ts +2 -0
- package/dist/constants/index.js +2 -0
- package/dist/states/drawer.svelte.d.ts +18 -8
- package/dist/states/drawer.svelte.js +1 -0
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.js +0 -1
- package/dist/types/props.d.ts +5 -1
- package/dist/utils/conversion.d.ts +2 -0
- package/dist/utils/conversion.js +12 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/physics.d.ts +1 -0
- package/dist/utils/physics.js +4 -0
- package/package.json +1 -1
- package/dist/types/states.d.ts +0 -20
- package/dist/types/states.js +0 -1
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
+
import { RANGE_END_DEFAULT, RANGE_START_DEFAULT } from '../constants/index.js';
|
|
2
3
|
import { setDrawerRootState } from '../states/index.js';
|
|
3
4
|
import type { DrawerRootProps } from '../types/props.js';
|
|
5
|
+
|
|
4
6
|
let {
|
|
5
7
|
ref = $bindable(null),
|
|
8
|
+
range = { start: RANGE_START_DEFAULT, end: RANGE_END_DEFAULT },
|
|
6
9
|
isOpen = $bindable(false),
|
|
7
10
|
children,
|
|
8
11
|
...restProps
|
|
@@ -13,6 +16,7 @@
|
|
|
13
16
|
$effect(() => {
|
|
14
17
|
rootState.props = {
|
|
15
18
|
ref,
|
|
19
|
+
range,
|
|
16
20
|
isOpen,
|
|
17
21
|
};
|
|
18
22
|
});
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
+
import { untrack } from 'svelte';
|
|
3
|
+
import { RANGE_END_DEFAULT, RANGE_START_DEFAULT } from '../constants/index.js';
|
|
2
4
|
import { getDrawerRootState } from '../states/drawer.svelte.js';
|
|
3
5
|
import type { DrawerBackdropProps as BackdropProps } from '../types/props.js';
|
|
6
|
+
import { toPixels } from '../utils/index.js';
|
|
4
7
|
|
|
5
8
|
let { ref = $bindable(null), ...restProps }: BackdropProps & { class?: string } = $props();
|
|
6
9
|
|
|
@@ -10,26 +13,42 @@
|
|
|
10
13
|
|
|
11
14
|
const classes = $derived(['sdd-backdrop', restProps.class]);
|
|
12
15
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
function getStyleOpacity() {
|
|
17
|
+
if (rootState.dragState.startsWith('drag') && rootState.pointer) {
|
|
18
|
+
const handleRect = rootState.handle?.getBoundingClientRect();
|
|
19
|
+
if (!handleRect) return 0;
|
|
20
|
+
|
|
21
|
+
const rangeStart = toPixels(rootState.props.range?.start, RANGE_START_DEFAULT);
|
|
22
|
+
const rangeEnd = toPixels(rootState.props.range?.end, RANGE_END_DEFAULT);
|
|
23
|
+
const windowHeightWithoutRange = window.innerHeight - rangeEnd - rangeStart;
|
|
24
|
+
const handleTop = Math.min(
|
|
25
|
+
Math.max((handleRect.top ?? 0) - rangeEnd, 0),
|
|
26
|
+
window.innerHeight - rangeStart
|
|
27
|
+
);
|
|
28
|
+
const opacity = (handleTop / windowHeightWithoutRange / 2 - 0.5) * -1;
|
|
29
|
+
|
|
30
|
+
return Math.max(Math.min(opacity, 0.5), 0);
|
|
17
31
|
}
|
|
18
|
-
|
|
19
|
-
return 0;
|
|
32
|
+
|
|
33
|
+
return rootState.props.isOpen ? 0.5 : 0;
|
|
20
34
|
}
|
|
21
35
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (rootState.dragState !== 'drag-start' && rootState.dragState !== 'drag')
|
|
25
|
-
return 'opacity 240ms';
|
|
36
|
+
function getStyleTransition() {
|
|
37
|
+
if (!rootState.dragState.startsWith('drag')) return 'opacity 240ms';
|
|
26
38
|
return 'none';
|
|
27
39
|
}
|
|
28
40
|
|
|
29
|
-
const styleOpacity = $derived(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
41
|
+
const styleOpacity = $derived.by(() => {
|
|
42
|
+
void rootState.pointer;
|
|
43
|
+
void rootState.dragState;
|
|
44
|
+
void rootState.props.isOpen;
|
|
45
|
+
return untrack(() => getStyleOpacity());
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const styleTransition = $derived.by(() => {
|
|
49
|
+
void rootState.dragState;
|
|
50
|
+
return untrack(() => getStyleTransition());
|
|
51
|
+
});
|
|
33
52
|
|
|
34
53
|
const handleClick = () => {
|
|
35
54
|
rootState.props.isOpen = false;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
+
import { untrack } from 'svelte';
|
|
3
|
+
import { RANGE_END_DEFAULT, RANGE_START_DEFAULT } from '../constants/index.js';
|
|
2
4
|
import { getDrawerRootState } from '../states/drawer.svelte.js';
|
|
3
5
|
import type { DrawerContentProps as ContentProps } from '../types/props.js';
|
|
6
|
+
import { applyRubberBand, toCSSLength, toPixels } from '../utils/index.js';
|
|
4
7
|
|
|
5
8
|
let {
|
|
6
9
|
ref = $bindable(null),
|
|
@@ -16,36 +19,48 @@
|
|
|
16
19
|
|
|
17
20
|
const classes = $derived(['sdd-content', restProps.class]);
|
|
18
21
|
|
|
19
|
-
|
|
20
|
-
function getStyleTransform(...args: unknown[]) {
|
|
22
|
+
function getStyleTransform() {
|
|
21
23
|
if (rootState.dragState === 'drag') {
|
|
22
24
|
if (!rootState.pointer || !rootState.pointerOrigin || !rootState.contentOrigin) return 'none';
|
|
23
|
-
// Prevent the drawer from being dragged above the top of the screen or below the bottom.
|
|
24
|
-
const y =
|
|
25
|
-
rootState.pointerOrigin.y > window.innerHeight / 2
|
|
26
|
-
? rootState.pointer.y > rootState.pointerOrigin.y
|
|
27
|
-
? rootState.pointerOrigin.y
|
|
28
|
-
: rootState.pointer.y
|
|
29
|
-
: rootState.pointer.y < rootState.pointerOrigin.y
|
|
30
|
-
? rootState.pointerOrigin.y
|
|
31
|
-
: rootState.pointer.y;
|
|
32
|
-
// Set the position of the content relative to the pointer.
|
|
33
|
-
const offset = rootState.contentOrigin.y - rootState.pointerOrigin.y;
|
|
34
|
-
|
|
35
|
-
return `translate3d(0, ${y + offset}px, 0)`;
|
|
36
|
-
}
|
|
37
25
|
|
|
38
|
-
|
|
39
|
-
|
|
26
|
+
const startY = rootState.pointerOrigin.y;
|
|
27
|
+
const currentY = rootState.pointer.y;
|
|
28
|
+
const offset = rootState.contentOrigin.y - startY;
|
|
29
|
+
|
|
30
|
+
// Calculate where the drawer would be without any resistance.
|
|
31
|
+
const rawY = currentY + offset;
|
|
32
|
+
|
|
33
|
+
const START_LIMIT =
|
|
34
|
+
window.innerHeight - toPixels(rootState.props.range?.start, RANGE_START_DEFAULT);
|
|
35
|
+
const END_LIMIT = 0 + toPixels(rootState.props.range?.end, RANGE_END_DEFAULT);
|
|
36
|
+
let y = rawY;
|
|
37
|
+
const MAX_OVERDRAG = 120;
|
|
40
38
|
|
|
41
|
-
|
|
39
|
+
if (rawY < END_LIMIT) {
|
|
40
|
+
const overdrag = END_LIMIT - rawY;
|
|
41
|
+
y = END_LIMIT - applyRubberBand(overdrag, MAX_OVERDRAG);
|
|
42
|
+
} else if (rawY > START_LIMIT) {
|
|
43
|
+
const overdrag = rawY - START_LIMIT;
|
|
44
|
+
y = START_LIMIT + applyRubberBand(overdrag, MAX_OVERDRAG);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return `translate3d(0, ${y}px, 0)`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return rootState.props.isOpen
|
|
51
|
+
? `translate3d(0, calc(0% + ${toCSSLength(rootState.props.range?.end ?? RANGE_END_DEFAULT)}), 0)`
|
|
52
|
+
: `translate3d(0, calc(100% - ${toCSSLength(rootState.props.range?.start ?? RANGE_START_DEFAULT)}), 0)`;
|
|
42
53
|
}
|
|
43
54
|
|
|
44
|
-
const styleTransform = $derived(
|
|
45
|
-
|
|
46
|
-
|
|
55
|
+
const styleTransform = $derived.by(() => {
|
|
56
|
+
void rootState.pointer;
|
|
57
|
+
void rootState.dragState;
|
|
58
|
+
void rootState.props.isOpen;
|
|
59
|
+
return untrack(() => getStyleTransform());
|
|
60
|
+
});
|
|
61
|
+
|
|
47
62
|
const styleTransition = $derived(
|
|
48
|
-
rootState.dragState
|
|
63
|
+
!rootState.dragState.startsWith('drag')
|
|
49
64
|
? `transform 400ms cubic-bezier(0.32, 0.72, 0, 1)`
|
|
50
65
|
: 'none'
|
|
51
66
|
);
|
|
@@ -1,12 +1,22 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { DrawerRootProps as RootProps } from '../types/index.js';
|
|
2
2
|
declare class DrawerRootState {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
ref: HTMLElement | null;
|
|
4
|
+
props: RootProps;
|
|
5
|
+
dragState: 'idle' | 'drag-start' | 'drag' | 'drop';
|
|
6
|
+
content: HTMLElement | null;
|
|
7
|
+
handle: HTMLButtonElement | null;
|
|
8
|
+
pointer: {
|
|
9
|
+
x: number;
|
|
10
|
+
y: number;
|
|
11
|
+
} | null;
|
|
12
|
+
pointerOrigin: {
|
|
13
|
+
x: number;
|
|
14
|
+
y: number;
|
|
15
|
+
} | null;
|
|
16
|
+
contentOrigin: {
|
|
17
|
+
x: number;
|
|
18
|
+
y: number;
|
|
19
|
+
} | null;
|
|
10
20
|
}
|
|
11
21
|
export declare function setDrawerRootState(): DrawerRootState;
|
|
12
22
|
export declare function getDrawerRootState(): DrawerRootState;
|
package/dist/types/index.d.ts
CHANGED
package/dist/types/index.js
CHANGED
package/dist/types/props.d.ts
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function toCSSLength(value) {
|
|
2
|
+
return typeof value === 'number' ? `${value}px` : value;
|
|
3
|
+
}
|
|
4
|
+
export function toPixels(value, fallback) {
|
|
5
|
+
if (value === undefined)
|
|
6
|
+
return fallback;
|
|
7
|
+
if (typeof value === 'number')
|
|
8
|
+
return value;
|
|
9
|
+
if (value.endsWith('%'))
|
|
10
|
+
return (parseFloat(value) / 100) * window.innerHeight;
|
|
11
|
+
return parseFloat(value);
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function applyRubberBand(overdrag: number, overdragLimit: number): 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
|
+
"version": "0.3.0",
|
|
5
5
|
"author": "Rodrigo D’Agostino <rodrigo.dagostino@proton.me>",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"draggable",
|
package/dist/types/states.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { DrawerRootProps as RootProps } from './props.js';
|
|
2
|
-
export interface DrawerRootStateContext {
|
|
3
|
-
ref: HTMLUListElement | null;
|
|
4
|
-
props: RootProps;
|
|
5
|
-
dragState: 'idle' | 'drag-start' | 'drag' | 'drop';
|
|
6
|
-
content: HTMLDivElement | null;
|
|
7
|
-
handle: HTMLButtonElement | null;
|
|
8
|
-
pointer: {
|
|
9
|
-
x: number;
|
|
10
|
-
y: number;
|
|
11
|
-
} | null;
|
|
12
|
-
pointerOrigin: {
|
|
13
|
-
x: number;
|
|
14
|
-
y: number;
|
|
15
|
-
} | null;
|
|
16
|
-
contentOrigin: {
|
|
17
|
-
x: number;
|
|
18
|
-
y: number;
|
|
19
|
-
} | null;
|
|
20
|
-
}
|
package/dist/types/states.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|