@rodrigodagostino/svelte-draggable-drawer 0.4.0 → 0.4.1
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 +43 -17
- package/dist/components/DrawerBackdrop.svelte +19 -13
- package/dist/components/DrawerContent.svelte +45 -25
- package/dist/styles.css +12 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/whereabouts.d.ts +4 -0
- package/dist/utils/whereabouts.js +55 -0
- package/package.json +3 -2
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
} from '../constants/index.js';
|
|
7
7
|
import { setDrawerRootState } from '../states/index.js';
|
|
8
8
|
import type { DrawerRootProps } from '../types/props.js';
|
|
9
|
-
import { toPixels } from '../utils/index.js';
|
|
9
|
+
import { isOrResidesInInteractiveElement, toPixels } from '../utils/index.js';
|
|
10
10
|
|
|
11
11
|
let {
|
|
12
12
|
ref = $bindable(null),
|
|
@@ -44,21 +44,45 @@
|
|
|
44
44
|
let transitionTimeoutId: ReturnType<typeof setTimeout> | null = null;
|
|
45
45
|
|
|
46
46
|
function handlePointerDown(e: PointerEvent) {
|
|
47
|
+
if (!rootState.content) return;
|
|
48
|
+
|
|
47
49
|
const target = e.target as HTMLElement;
|
|
48
|
-
|
|
50
|
+
const isOrResidesInContent = target.closest('.sdd-content');
|
|
51
|
+
if (!isOrResidesInContent) return;
|
|
52
|
+
|
|
53
|
+
const isOrResidesInInteractiveElem = isOrResidesInInteractiveElement(target, rootState.content);
|
|
54
|
+
// Stop non-main buttons from interacting with the drawer.
|
|
55
|
+
if (e.button !== 0) {
|
|
56
|
+
// Let non-main buttons act normally on interactive elements.
|
|
57
|
+
if (!isOrResidesInInteractiveElem) e.preventDefault();
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Prevent dragging if the current drawer contains a content handle, but we’re not dragging from it.
|
|
62
|
+
const hasHandle = !!rootState.handle;
|
|
63
|
+
const isOrResidesInHandle = target.closest('.sdd-content-handle');
|
|
64
|
+
if (hasHandle && !isOrResidesInHandle && !isOrResidesInInteractiveElem) {
|
|
65
|
+
e.preventDefault();
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
49
68
|
|
|
69
|
+
// Prevent dragging if the current drawer contains an interactive element and
|
|
70
|
+
// we’re also not dragging from a handle inside that interactive element.
|
|
71
|
+
if (isOrResidesInInteractiveElem && !isOrResidesInHandle) return;
|
|
72
|
+
|
|
73
|
+
const draggedElem = rootState.handle ?? rootState.content;
|
|
50
74
|
pointerId = e.pointerId;
|
|
51
|
-
|
|
75
|
+
draggedElem.setPointerCapture(pointerId);
|
|
52
76
|
rootState.pointer = { x: e.clientX, y: e.clientY };
|
|
53
77
|
rootState.pointerOrigin = { x: e.clientX, y: e.clientY };
|
|
54
78
|
rootState.contentOrigin = rootState.content.getBoundingClientRect();
|
|
55
79
|
rootState.dragState = 'drag-start';
|
|
56
80
|
|
|
57
|
-
|
|
58
|
-
|
|
81
|
+
draggedElem.addEventListener('pointermove', handlePointerMove);
|
|
82
|
+
draggedElem.addEventListener(
|
|
59
83
|
'pointerup',
|
|
60
84
|
() => {
|
|
61
|
-
|
|
85
|
+
draggedElem?.removeEventListener('pointermove', handlePointerMove);
|
|
62
86
|
handlePointerUp();
|
|
63
87
|
},
|
|
64
88
|
{ once: true }
|
|
@@ -71,8 +95,11 @@
|
|
|
71
95
|
rootState.dragState = 'drag';
|
|
72
96
|
|
|
73
97
|
rootState.pointer = { x: clientX, y: clientY };
|
|
74
|
-
const
|
|
75
|
-
|
|
98
|
+
const draggedTop =
|
|
99
|
+
rootState.handle?.getBoundingClientRect()?.top ??
|
|
100
|
+
rootState.content?.getBoundingClientRect()?.top ??
|
|
101
|
+
clientY;
|
|
102
|
+
const visibleHeight = window.innerHeight - draggedTop;
|
|
76
103
|
const snapPoints = rootState.props.snapPoints ?? [];
|
|
77
104
|
const snapPointsInPx = (snapPoints ?? []).map((point) => toPixels(point, 0));
|
|
78
105
|
|
|
@@ -93,7 +120,7 @@
|
|
|
93
120
|
} else {
|
|
94
121
|
const threshold =
|
|
95
122
|
rootState.pointerOrigin.y > window.innerHeight / 2 ? START_THRESHOLD : END_THRESHOLD;
|
|
96
|
-
rootState.props.isOpen =
|
|
123
|
+
rootState.props.isOpen = draggedTop < window.innerHeight * threshold;
|
|
97
124
|
}
|
|
98
125
|
}
|
|
99
126
|
|
|
@@ -130,6 +157,11 @@
|
|
|
130
157
|
}
|
|
131
158
|
</script>
|
|
132
159
|
|
|
160
|
+
<pre style="position: fixed; top: 2rem; left: 2rem;">
|
|
161
|
+
<code>activeSnapPoint: {JSON.stringify(rootState.activeSnapPoint)}</code>
|
|
162
|
+
<code>isOpen: {JSON.stringify(isOpen)}</code>
|
|
163
|
+
</pre>
|
|
164
|
+
|
|
133
165
|
<div class={classes} data-drag-state={rootState.dragState} onpointerdown={handlePointerDown}>
|
|
134
166
|
{@render children?.()}
|
|
135
167
|
</div>
|
|
@@ -141,15 +173,9 @@
|
|
|
141
173
|
pointer-events: none;
|
|
142
174
|
z-index: 9999;
|
|
143
175
|
|
|
144
|
-
|
|
145
|
-
&
|
|
176
|
+
/* Prevent touch gestures during a pointer drag. */
|
|
177
|
+
&[data-drag-state*='ptr'] {
|
|
146
178
|
touch-action: none;
|
|
147
|
-
cursor: grab;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
&[data-drag-state*='drag'],
|
|
151
|
-
&[data-drag-state*='drag'] :global(.sdd-content-handle) {
|
|
152
|
-
cursor: grabbing;
|
|
153
179
|
}
|
|
154
180
|
}
|
|
155
181
|
</style>
|
|
@@ -13,22 +13,28 @@
|
|
|
13
13
|
|
|
14
14
|
function getStyleOpacity() {
|
|
15
15
|
if (rootState.dragState.startsWith('drag') && rootState.pointer) {
|
|
16
|
-
const
|
|
16
|
+
const draggedTop =
|
|
17
|
+
rootState.handle?.getBoundingClientRect().top ??
|
|
18
|
+
rootState.content?.getBoundingClientRect().top ??
|
|
19
|
+
rootState.pointer.y;
|
|
20
|
+
const currentReveal = window.innerHeight - draggedTop;
|
|
17
21
|
|
|
18
22
|
const rangeStart = toPixels(rootState.props.range?.start, RANGE_START_DEFAULT);
|
|
19
|
-
const rangeEnd =
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const closedProgress = handleOffset / availableHeight;
|
|
29
|
-
const opacity = 0.5 - closedProgress / 2;
|
|
23
|
+
const rangeEnd = toPixels(rootState.props.range?.end, RANGE_END_DEFAULT);
|
|
24
|
+
const contentHeight = rootState.content?.offsetHeight ?? 0;
|
|
25
|
+
// Reveal amount at “fully open”, which is capped to the content’s own height,
|
|
26
|
+
// since it may no longer span the full viewport.
|
|
27
|
+
const openLimit = Math.min(contentHeight, window.innerHeight - rangeEnd);
|
|
28
|
+
// Treat the first (smallest) snap point as the “open” reference for opacity.
|
|
29
|
+
const openReveal = rootState.props.snapPoints?.length
|
|
30
|
+
? Math.min(toPixels(rootState.props.snapPoints[0], RANGE_END_DEFAULT), openLimit)
|
|
31
|
+
: openLimit;
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
const availableHeight = openReveal - rangeStart;
|
|
34
|
+
const handleOffset = clamp(currentReveal - rangeStart, 0, availableHeight);
|
|
35
|
+
const openProgress = handleOffset / availableHeight;
|
|
36
|
+
|
|
37
|
+
return clamp(openProgress / 2, 0, 0.5);
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
return rootState.props.isOpen ? 0.5 : 0;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { untrack } from 'svelte';
|
|
3
|
+
import { BROWSER } from 'esm-env';
|
|
3
4
|
import { RANGE_END_DEFAULT, RANGE_START_DEFAULT } from '../constants/index.js';
|
|
4
5
|
import { getDrawerRootState } from '../states/drawer.svelte.js';
|
|
5
6
|
import type { DrawerContentProps as ContentProps } from '../types/props.js';
|
|
@@ -20,40 +21,52 @@
|
|
|
20
21
|
const classes = $derived(['sdd-content', restProps.class]);
|
|
21
22
|
|
|
22
23
|
function getStyleTransform() {
|
|
24
|
+
if (!BROWSER) return 'translate3d(0, 100%, 0)';
|
|
25
|
+
|
|
26
|
+
const START_LIMIT = toPixels(rootState.props.range?.start, RANGE_START_DEFAULT);
|
|
27
|
+
const END_LIMIT = Math.min(
|
|
28
|
+
rootState.content?.getBoundingClientRect().height ?? 0,
|
|
29
|
+
window.innerHeight - toPixels(rootState.props.range?.end, RANGE_END_DEFAULT)
|
|
30
|
+
);
|
|
31
|
+
|
|
23
32
|
if (rootState.dragState === 'drag') {
|
|
24
|
-
if (!rootState.pointer || !rootState.pointerOrigin || !rootState.contentOrigin)
|
|
33
|
+
if (!rootState.pointer || !rootState.pointerOrigin || !rootState.contentOrigin)
|
|
34
|
+
return 'translate3d(0, 100%, 0)';
|
|
25
35
|
|
|
26
36
|
const startY = rootState.pointerOrigin.y;
|
|
27
37
|
const currentY = rootState.pointer.y;
|
|
28
38
|
const offset = rootState.contentOrigin.y - startY;
|
|
29
39
|
|
|
30
|
-
//
|
|
31
|
-
const
|
|
40
|
+
// Where the content’s top edge would be without any resistance.
|
|
41
|
+
const rawTopY = currentY + offset;
|
|
42
|
+
// How much of the content that leaves revealed above the bottom edge.
|
|
43
|
+
const rawReveal = window.innerHeight - rawTopY;
|
|
32
44
|
|
|
33
|
-
|
|
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;
|
|
45
|
+
let y = rawReveal;
|
|
37
46
|
const MAX_OVERDRAG = 120;
|
|
38
47
|
|
|
39
|
-
if (
|
|
40
|
-
const overdrag =
|
|
41
|
-
y = END_LIMIT
|
|
42
|
-
} else if (
|
|
43
|
-
const overdrag =
|
|
44
|
-
y = START_LIMIT
|
|
48
|
+
if (rawReveal > END_LIMIT) {
|
|
49
|
+
const overdrag = rawReveal - END_LIMIT;
|
|
50
|
+
y = END_LIMIT + applyRubberBand(overdrag, MAX_OVERDRAG);
|
|
51
|
+
} else if (rawReveal < START_LIMIT) {
|
|
52
|
+
const overdrag = START_LIMIT - rawReveal;
|
|
53
|
+
y = START_LIMIT - applyRubberBand(overdrag, MAX_OVERDRAG);
|
|
45
54
|
}
|
|
46
55
|
|
|
47
|
-
return `translate3d(0, ${y}px, 0)`;
|
|
56
|
+
return `translate3d(0, calc(100% - ${y}px), 0)`;
|
|
48
57
|
}
|
|
49
58
|
|
|
50
59
|
if (rootState.activeSnapPoint !== null) {
|
|
51
|
-
|
|
60
|
+
const y = Math.min(
|
|
61
|
+
toPixels(rootState.props.snapPoints![rootState.activeSnapPoint.index], 0),
|
|
62
|
+
END_LIMIT
|
|
63
|
+
);
|
|
64
|
+
return `translate3d(0, calc(100% - ${y}px), 0)`;
|
|
52
65
|
}
|
|
53
66
|
|
|
54
67
|
return rootState.props.isOpen
|
|
55
|
-
? `translate3d(0, calc(
|
|
56
|
-
: `translate3d(0, calc(100% - ${
|
|
68
|
+
? `translate3d(0, calc(100% - ${END_LIMIT}px), 0)`
|
|
69
|
+
: `translate3d(0, calc(100% - ${START_LIMIT}px), 0)`;
|
|
57
70
|
}
|
|
58
71
|
|
|
59
72
|
const styleTransform = $derived.by(() => {
|
|
@@ -73,27 +86,34 @@
|
|
|
73
86
|
<div
|
|
74
87
|
bind:this={ref}
|
|
75
88
|
class={classes}
|
|
89
|
+
style:max-height="calc(100% - {toCSSLength(rootState.props.range?.end ?? RANGE_START_DEFAULT)})"
|
|
76
90
|
style:transform={styleTransform}
|
|
77
91
|
style:transition={styleTransition}
|
|
92
|
+
data-drag-state={rootState.dragState}
|
|
78
93
|
>
|
|
79
94
|
{@render children?.()}
|
|
80
95
|
</div>
|
|
81
96
|
|
|
82
97
|
<style>
|
|
83
98
|
.sdd-content {
|
|
99
|
+
position: fixed;
|
|
100
|
+
inset: auto 0;
|
|
101
|
+
bottom: 0;
|
|
84
102
|
display: flex;
|
|
85
103
|
flex-direction: column;
|
|
86
|
-
width: calc(100% - 2rem);
|
|
87
|
-
height: 100%;
|
|
88
|
-
margin-inline: auto;
|
|
89
104
|
pointer-events: auto;
|
|
90
|
-
|
|
105
|
+
user-select: none;
|
|
91
106
|
z-index: 1;
|
|
92
|
-
}
|
|
93
107
|
|
|
94
|
-
|
|
95
|
-
.sdd-content {
|
|
96
|
-
|
|
108
|
+
&:not(:has(.sdd-content-handle)),
|
|
109
|
+
& :global(.sdd-content-handle) {
|
|
110
|
+
touch-action: none;
|
|
111
|
+
cursor: grab;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
&[data-drag-state*='drag'],
|
|
115
|
+
&[data-drag-state*='drag'] :global(.sdd-content-handle) {
|
|
116
|
+
cursor: grabbing;
|
|
97
117
|
}
|
|
98
118
|
}
|
|
99
119
|
</style>
|
package/dist/styles.css
CHANGED
|
@@ -1,16 +1,27 @@
|
|
|
1
1
|
.sdd-content {
|
|
2
|
+
width: calc(100% - 2rem);
|
|
3
|
+
padding: 1.25rem;
|
|
4
|
+
margin-inline: auto;
|
|
2
5
|
background-color: var(--sdd-white);
|
|
3
6
|
border-radius: 1rem 1rem 0 0;
|
|
4
7
|
|
|
8
|
+
&:has(.sdd-content-handle) {
|
|
9
|
+
padding-block-start: 0;
|
|
10
|
+
}
|
|
11
|
+
|
|
5
12
|
&::after {
|
|
6
13
|
content: "";
|
|
7
14
|
position: absolute;
|
|
8
|
-
top: 100
|
|
15
|
+
top: calc(100% - 1px);
|
|
9
16
|
left: 0;
|
|
10
17
|
width: 100%;
|
|
11
18
|
height: 50%;
|
|
12
19
|
background-color: inherit;
|
|
13
20
|
}
|
|
21
|
+
|
|
22
|
+
@media (min-width: 48em) {
|
|
23
|
+
width: calc(100% - 2.5rem);
|
|
24
|
+
}
|
|
14
25
|
}
|
|
15
26
|
|
|
16
27
|
.sdd-content-handle {
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const INTERACTIVE_ELEMENTS: Set<string>;
|
|
2
|
+
export declare const INTERACTIVE_ROLE_ATTRIBUTES: Set<string>;
|
|
3
|
+
export declare const INTERACTIVE_SELECTORS: string;
|
|
4
|
+
export declare function isOrResidesInInteractiveElement(target: HTMLElement, root: HTMLElement): boolean;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export const INTERACTIVE_ELEMENTS = new Set([
|
|
2
|
+
'a',
|
|
3
|
+
'audio',
|
|
4
|
+
'button',
|
|
5
|
+
'input',
|
|
6
|
+
'optgroup',
|
|
7
|
+
'option',
|
|
8
|
+
'select',
|
|
9
|
+
'summary',
|
|
10
|
+
'textarea',
|
|
11
|
+
'video',
|
|
12
|
+
]);
|
|
13
|
+
const INTERACTIVE_ROLES = new Set([
|
|
14
|
+
'button',
|
|
15
|
+
'checkbox',
|
|
16
|
+
'combobox',
|
|
17
|
+
'link',
|
|
18
|
+
'menuitem',
|
|
19
|
+
'menuitemcheckbox',
|
|
20
|
+
'menuitemradio',
|
|
21
|
+
'radio',
|
|
22
|
+
'searchbox',
|
|
23
|
+
'slider',
|
|
24
|
+
'spinbutton',
|
|
25
|
+
'switch',
|
|
26
|
+
'tab',
|
|
27
|
+
'textbox',
|
|
28
|
+
]);
|
|
29
|
+
export const INTERACTIVE_ROLE_ATTRIBUTES = new Set([...INTERACTIVE_ROLES].map((role) => `[role="${role}"]`));
|
|
30
|
+
export const INTERACTIVE_SELECTORS = [
|
|
31
|
+
...INTERACTIVE_ELEMENTS,
|
|
32
|
+
...INTERACTIVE_ROLE_ATTRIBUTES,
|
|
33
|
+
'[contenteditable]:not([contenteditable="false"])',
|
|
34
|
+
].join(', ');
|
|
35
|
+
// Thank you, Vojtech Miksu :)
|
|
36
|
+
// https://github.com/tajo/react-movable/blob/master/src/utils.ts
|
|
37
|
+
export function isOrResidesInInteractiveElement(target, root) {
|
|
38
|
+
while (target && target !== root) {
|
|
39
|
+
if (target.classList.contains('ssl-item-handle'))
|
|
40
|
+
return false;
|
|
41
|
+
const tagName = target.tagName.toLowerCase();
|
|
42
|
+
if (INTERACTIVE_ELEMENTS.has(tagName))
|
|
43
|
+
return true;
|
|
44
|
+
const role = target.getAttribute('role')?.toLowerCase();
|
|
45
|
+
if (role && INTERACTIVE_ROLES.has(role))
|
|
46
|
+
return true;
|
|
47
|
+
if (target.isContentEditable)
|
|
48
|
+
return true;
|
|
49
|
+
if (tagName === 'label' && target.hasAttribute('for'))
|
|
50
|
+
return true;
|
|
51
|
+
if (tagName)
|
|
52
|
+
target = target.parentElement;
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
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.
|
|
4
|
+
"version": "0.4.1",
|
|
5
5
|
"author": "Rodrigo D’Agostino <rodrigo.dagostino@proton.me>",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"draggable",
|
|
@@ -48,7 +48,8 @@
|
|
|
48
48
|
}
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
|
-
"svelte": "^5.0.0"
|
|
51
|
+
"svelte": "^5.0.0",
|
|
52
|
+
"esm-env": "^1.0.0"
|
|
52
53
|
},
|
|
53
54
|
"devDependencies": {
|
|
54
55
|
"@eslint/js": "10.0.1",
|