@johly/vaul-svelte 1.0.0-next.8
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 +10 -0
- package/README.md +58 -0
- package/dist/components/drawer/drawer-content.svelte +60 -0
- package/dist/components/drawer/drawer-content.svelte.d.ts +5 -0
- package/dist/components/drawer/drawer-handle.svelte +31 -0
- package/dist/components/drawer/drawer-handle.svelte.d.ts +4 -0
- package/dist/components/drawer/drawer-nested.svelte +37 -0
- package/dist/components/drawer/drawer-nested.svelte.d.ts +3 -0
- package/dist/components/drawer/drawer-overlay.svelte +32 -0
- package/dist/components/drawer/drawer-overlay.svelte.d.ts +5 -0
- package/dist/components/drawer/drawer-portal.svelte +10 -0
- package/dist/components/drawer/drawer-portal.svelte.d.ts +3 -0
- package/dist/components/drawer/drawer.svelte +383 -0
- package/dist/components/drawer/drawer.svelte.d.ts +3 -0
- package/dist/components/drawer/index.d.ts +12 -0
- package/dist/components/drawer/index.js +11 -0
- package/dist/components/drawer/types.d.ts +126 -0
- package/dist/components/drawer/types.js +1 -0
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +1 -0
- package/dist/components/utils/mounted.svelte +12 -0
- package/dist/components/utils/mounted.svelte.d.ts +6 -0
- package/dist/context.d.ts +42 -0
- package/dist/context.js +2 -0
- package/dist/helpers.d.ts +16 -0
- package/dist/helpers.js +95 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/internal/browser.d.ts +8 -0
- package/dist/internal/browser.js +30 -0
- package/dist/internal/constants.d.ts +11 -0
- package/dist/internal/constants.js +11 -0
- package/dist/internal/noop.d.ts +1 -0
- package/dist/internal/noop.js +3 -0
- package/dist/internal/use-id.d.ts +4 -0
- package/dist/internal/use-id.js +8 -0
- package/dist/types.d.ts +12 -0
- package/dist/types.js +1 -0
- package/dist/use-drawer-content.svelte.js +187 -0
- package/dist/use-drawer-handle.svelte.d.ts +18 -0
- package/dist/use-drawer-handle.svelte.js +83 -0
- package/dist/use-drawer-overlay.svelte.d.ts +15 -0
- package/dist/use-drawer-overlay.svelte.js +40 -0
- package/dist/use-drawer-root.svelte.js +575 -0
- package/dist/use-position-fixed.svelte.d.ts +20 -0
- package/dist/use-position-fixed.svelte.js +114 -0
- package/dist/use-prevent-scroll.svelte.d.ts +15 -0
- package/dist/use-prevent-scroll.svelte.js +235 -0
- package/dist/use-scale-background.svelte.d.ts +1 -0
- package/dist/use-scale-background.svelte.js +57 -0
- package/dist/use-snap-points.svelte.d.ts +34 -0
- package/dist/use-snap-points.svelte.js +260 -0
- package/package.json +64 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { BitsPrimitiveDivAttributes, DialogContentPropsWithoutHTML, DialogOverlayPropsWithoutHTML, Dialog as DrawerPrimitive, WithChild, WithoutChildrenOrChild } from "bits-ui";
|
|
2
|
+
import type { WithChildren, Without } from "svelte-toolbelt";
|
|
3
|
+
export type WithFadeFromProps = {
|
|
4
|
+
/**
|
|
5
|
+
* Array of numbers from 0 to 100 that corresponds to % of the screen a given snap point should take up.
|
|
6
|
+
* Should go from least visible. Example `[0.2, 0.5, 0.8]`.
|
|
7
|
+
* You can also use px values, which doesn't take screen height into account.
|
|
8
|
+
*/
|
|
9
|
+
snapPoints: (number | string)[];
|
|
10
|
+
/**
|
|
11
|
+
* Index of a `snapPoint` from which the overlay fade should be applied. Defaults to the last snap point.
|
|
12
|
+
*/
|
|
13
|
+
fadeFromIndex: number;
|
|
14
|
+
};
|
|
15
|
+
export type WithoutFadeFromProps = {
|
|
16
|
+
/**
|
|
17
|
+
* Array of numbers from 0 to 100 that corresponds to % of the screen a given snap point should take up.
|
|
18
|
+
* Should go from least visible. Example `[0.2, 0.5, 0.8]`.
|
|
19
|
+
* You can also use px values, which doesn't take screen height into account.
|
|
20
|
+
*/
|
|
21
|
+
snapPoints?: (number | string)[];
|
|
22
|
+
fadeFromIndex?: never;
|
|
23
|
+
};
|
|
24
|
+
export type BaseDrawerRootPropsWithoutHTML = WithChildren<{
|
|
25
|
+
activeSnapPoint?: number | string | null;
|
|
26
|
+
onActiveSnapPointChange?: (snapPoint: number | string | null) => void;
|
|
27
|
+
open?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Number between 0 and 1 that determines when the drawer should be closed.
|
|
30
|
+
* Example: threshold of 0.5 would close the drawer if the user swiped for 50% of the height of the drawer or more.
|
|
31
|
+
* @default 0.25
|
|
32
|
+
*/
|
|
33
|
+
closeThreshold?: number;
|
|
34
|
+
/**
|
|
35
|
+
* When `true` the `body` doesn't get any styles assigned from Vaul
|
|
36
|
+
*/
|
|
37
|
+
noBodyStyles?: boolean;
|
|
38
|
+
onOpenChange?: (open: boolean) => void;
|
|
39
|
+
shouldScaleBackground?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* When `false` we don't change body's background color when the drawer is open.
|
|
42
|
+
* @default true
|
|
43
|
+
*/
|
|
44
|
+
setBackgroundColorOnScale?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Duration for which the drawer is not draggable after scrolling content inside of the drawer.
|
|
47
|
+
* @default 500ms
|
|
48
|
+
*/
|
|
49
|
+
scrollLockTimeout?: number;
|
|
50
|
+
/**
|
|
51
|
+
* When `true`, don't move the drawer upwards if there's space, but rather only change it's height so it's fully scrollable when the keyboard is open
|
|
52
|
+
*/
|
|
53
|
+
fixed?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* When `true` only allows the drawer to be dragged by the `<Drawer.Handle />` component.
|
|
56
|
+
* @default false
|
|
57
|
+
*/
|
|
58
|
+
handleOnly?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* When `false` dragging, clicking outside, pressing esc, etc. will not close the drawer.
|
|
61
|
+
* Use this in combination with the `open` prop, otherwise you won't be able to open/close the drawer.
|
|
62
|
+
* @default true
|
|
63
|
+
*/
|
|
64
|
+
dismissible?: boolean;
|
|
65
|
+
onDrag?: (event: PointerEvent, percentageDragged: number) => void;
|
|
66
|
+
onRelease?: (event: PointerEvent, open: boolean) => void;
|
|
67
|
+
/**
|
|
68
|
+
* When `false` it allows to interact with elements outside of the drawer without closing it.
|
|
69
|
+
* @default true
|
|
70
|
+
*/
|
|
71
|
+
modal?: boolean;
|
|
72
|
+
nested?: boolean;
|
|
73
|
+
onClose?: () => void;
|
|
74
|
+
/**
|
|
75
|
+
* Direction of the drawer. Can be `top` or `bottom`, `left`, `right`.
|
|
76
|
+
* @default 'bottom'
|
|
77
|
+
*/
|
|
78
|
+
direction?: "top" | "bottom" | "left" | "right";
|
|
79
|
+
/**
|
|
80
|
+
* Opened by default, skips initial enter animation. Still reacts to `open` state changes
|
|
81
|
+
* @default false
|
|
82
|
+
*/
|
|
83
|
+
defaultOpen?: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* When set to `true` prevents scrolling on the document body on mount, and restores it on unmount.
|
|
86
|
+
* @default false
|
|
87
|
+
*/
|
|
88
|
+
disablePreventScroll?: boolean;
|
|
89
|
+
/**
|
|
90
|
+
* When `true` Vaul will reposition inputs rather than scroll then into view if the keyboard is in the way.
|
|
91
|
+
* Setting it to `false` will fall back to the default browser behavior.
|
|
92
|
+
* @default true when {@link snapPoints} is defined
|
|
93
|
+
*/
|
|
94
|
+
repositionInputs?: boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Disabled velocity based swiping for snap points.
|
|
97
|
+
* This means that a snap point won't be skipped even if the velocity is high enough.
|
|
98
|
+
* Useful if each snap point in a drawer is equally important.
|
|
99
|
+
* @default false
|
|
100
|
+
*/
|
|
101
|
+
snapToSequentialPoint?: boolean;
|
|
102
|
+
container?: HTMLElement | null;
|
|
103
|
+
/**
|
|
104
|
+
* Gets triggered after the open or close animation ends, it receives an `open` argument with the `open` state of the drawer by the time the function was triggered.
|
|
105
|
+
* Useful to revert any state changes for example.
|
|
106
|
+
*/
|
|
107
|
+
onAnimationEnd?: (open: boolean) => void;
|
|
108
|
+
preventScrollRestoration?: boolean;
|
|
109
|
+
autoFocus?: boolean;
|
|
110
|
+
}> & (WithFadeFromProps | WithoutFadeFromProps);
|
|
111
|
+
export type DrawerRootPropsWithoutHTML = BaseDrawerRootPropsWithoutHTML & Without<DrawerPrimitive.RootProps, BaseDrawerRootPropsWithoutHTML>;
|
|
112
|
+
export type DrawerRootProps = DrawerRootPropsWithoutHTML;
|
|
113
|
+
export type DrawerHandlePropsWithoutHTML = Omit<WithChild<{
|
|
114
|
+
/**
|
|
115
|
+
* Whether to prevent cycling the snap points when the handle is pressed.
|
|
116
|
+
*
|
|
117
|
+
* @default false
|
|
118
|
+
*/
|
|
119
|
+
preventCycle?: boolean;
|
|
120
|
+
}>, "child">;
|
|
121
|
+
export type DrawerHandleProps = DrawerHandlePropsWithoutHTML & Without<BitsPrimitiveDivAttributes, DrawerHandlePropsWithoutHTML>;
|
|
122
|
+
export type DrawerContentPropsWithoutHTML = WithChildren<WithoutChildrenOrChild<Omit<DialogContentPropsWithoutHTML, "preventScroll">>>;
|
|
123
|
+
export type DrawerContentProps = DrawerContentPropsWithoutHTML & Without<BitsPrimitiveDivAttributes, DrawerContentPropsWithoutHTML>;
|
|
124
|
+
export type DrawerOverlayPropsWithoutHTML = WithChildren<WithoutChildrenOrChild<DialogOverlayPropsWithoutHTML>>;
|
|
125
|
+
export type DrawerOverlayProps = DrawerOverlayPropsWithoutHTML & Without<BitsPrimitiveDivAttributes, DrawerOverlayPropsWithoutHTML>;
|
|
126
|
+
export type { DialogPortalPropsWithoutHTML as DrawerPortalPropsWithoutHTML, DialogPortalProps as DrawerPortalProps, DialogTriggerProps as DrawerTriggerProps, DialogTitleProps as DrawerTitleProps, DialogDescriptionProps as DrawerDescriptionProps, DialogCloseProps as DrawerCloseProps, } from "bits-ui";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as Drawer from "./drawer/index.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as Drawer from "./drawer/index.js";
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Context } from "runed";
|
|
2
|
+
import type { DrawerDirection } from "./types.js";
|
|
3
|
+
import type { ReadableBoxedValues, WritableBoxedValues } from "svelte-toolbelt";
|
|
4
|
+
interface DrawerContextValue extends ReadableBoxedValues<{
|
|
5
|
+
dismissible: boolean;
|
|
6
|
+
snapPoints: (number | string)[] | null | undefined;
|
|
7
|
+
modal: boolean;
|
|
8
|
+
onOpenChange: (o: boolean) => void;
|
|
9
|
+
direction: DrawerDirection;
|
|
10
|
+
shouldScaleBackground: boolean;
|
|
11
|
+
setBackgroundColorOnScale: boolean;
|
|
12
|
+
noBodyStyles: boolean;
|
|
13
|
+
handleOnly: boolean;
|
|
14
|
+
container: HTMLElement | null;
|
|
15
|
+
autoFocus: boolean;
|
|
16
|
+
}>, WritableBoxedValues<{
|
|
17
|
+
keyboardIsOpen: boolean;
|
|
18
|
+
activeSnapPoint: number | string | null;
|
|
19
|
+
open: boolean;
|
|
20
|
+
}> {
|
|
21
|
+
drawerNode: HTMLElement | null;
|
|
22
|
+
setDrawerNode: (node: HTMLElement | null) => void;
|
|
23
|
+
overlayNode: HTMLElement | null;
|
|
24
|
+
setOverlayNode: (node: HTMLElement | null) => void;
|
|
25
|
+
onPress: (event: PointerEvent) => void;
|
|
26
|
+
onRelease: (event: PointerEvent | null) => void;
|
|
27
|
+
onDrag: (event: PointerEvent) => void;
|
|
28
|
+
onNestedDrag: (event: PointerEvent, percentageDragged: number) => void;
|
|
29
|
+
onNestedOpenChange: (o: boolean) => void;
|
|
30
|
+
onNestedRelease: (event: PointerEvent, open: boolean) => void;
|
|
31
|
+
closeDrawer: () => void;
|
|
32
|
+
restorePositionSetting: () => void;
|
|
33
|
+
onDialogOpenChange: (open: boolean) => void;
|
|
34
|
+
handleOpenChange: (open: boolean) => void;
|
|
35
|
+
readonly isDragging: boolean;
|
|
36
|
+
readonly snapPointsOffset: number[] | null;
|
|
37
|
+
readonly activeSnapPointIndex?: number | null;
|
|
38
|
+
readonly shouldAnimate: boolean;
|
|
39
|
+
readonly shouldFade: boolean;
|
|
40
|
+
}
|
|
41
|
+
export declare const DrawerContext: Context<DrawerContextValue>;
|
|
42
|
+
export {};
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AnyFunction, DrawerDirection } from "./types.js";
|
|
2
|
+
interface Style {
|
|
3
|
+
[key: string]: string;
|
|
4
|
+
}
|
|
5
|
+
export declare function isInView(el: HTMLElement): boolean;
|
|
6
|
+
export declare function set(el: Element | HTMLElement | null | undefined, styles: Style, ignoreCache?: boolean): void;
|
|
7
|
+
export declare function reset(el: Element | HTMLElement | null, prop?: string): void;
|
|
8
|
+
export declare const isVertical: (direction: DrawerDirection) => boolean;
|
|
9
|
+
export declare function getTranslate(element: HTMLElement, direction: DrawerDirection): number | null;
|
|
10
|
+
export declare function dampenValue(v: number): number;
|
|
11
|
+
export declare function assignStyle(element: HTMLElement | null | undefined, style: Partial<CSSStyleDeclaration>): () => void;
|
|
12
|
+
/**
|
|
13
|
+
* Receives functions as arguments and returns a new function that calls all.
|
|
14
|
+
*/
|
|
15
|
+
export declare function chain<T>(...fns: T[]): (...args: T extends AnyFunction ? Parameters<T> : never) => void;
|
|
16
|
+
export {};
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const cache = new WeakMap();
|
|
2
|
+
export function isInView(el) {
|
|
3
|
+
const rect = el.getBoundingClientRect();
|
|
4
|
+
if (!window.visualViewport)
|
|
5
|
+
return false;
|
|
6
|
+
return (rect.top >= 0 &&
|
|
7
|
+
rect.left >= 0 &&
|
|
8
|
+
// Need + 40 for safari detection
|
|
9
|
+
rect.bottom <= window.visualViewport.height - 40 &&
|
|
10
|
+
rect.right <= window.visualViewport.width);
|
|
11
|
+
}
|
|
12
|
+
export function set(el, styles, ignoreCache = false) {
|
|
13
|
+
if (!el || !(el instanceof HTMLElement))
|
|
14
|
+
return;
|
|
15
|
+
let originalStyles = {};
|
|
16
|
+
Object.entries(styles).forEach(([key, value]) => {
|
|
17
|
+
if (key.startsWith("--")) {
|
|
18
|
+
el.style.setProperty(key, value);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
originalStyles[key] = el.style[key];
|
|
22
|
+
el.style[key] = value;
|
|
23
|
+
});
|
|
24
|
+
if (ignoreCache)
|
|
25
|
+
return;
|
|
26
|
+
cache.set(el, originalStyles);
|
|
27
|
+
}
|
|
28
|
+
export function reset(el, prop) {
|
|
29
|
+
if (!el || !(el instanceof HTMLElement))
|
|
30
|
+
return;
|
|
31
|
+
let originalStyles = cache.get(el);
|
|
32
|
+
if (!originalStyles)
|
|
33
|
+
return;
|
|
34
|
+
if (prop) {
|
|
35
|
+
el.style[prop] = originalStyles[prop];
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
Object.entries(originalStyles).forEach(([key, value]) => {
|
|
39
|
+
el.style[key] = value;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export const isVertical = (direction) => {
|
|
44
|
+
switch (direction) {
|
|
45
|
+
case "top":
|
|
46
|
+
case "bottom":
|
|
47
|
+
return true;
|
|
48
|
+
case "left":
|
|
49
|
+
case "right":
|
|
50
|
+
return false;
|
|
51
|
+
default:
|
|
52
|
+
return direction;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
export function getTranslate(element, direction) {
|
|
56
|
+
if (!element) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
const style = window.getComputedStyle(element);
|
|
60
|
+
const transform =
|
|
61
|
+
// @ts-expect-error - shh
|
|
62
|
+
style.transform || style.webkitTransform || style.mozTransform;
|
|
63
|
+
let mat = transform.match(/^matrix3d\((.+)\)$/);
|
|
64
|
+
if (mat) {
|
|
65
|
+
// https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix3d
|
|
66
|
+
return parseFloat(mat[1].split(", ")[isVertical(direction) ? 13 : 12]);
|
|
67
|
+
}
|
|
68
|
+
// https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix
|
|
69
|
+
mat = transform.match(/^matrix\((.+)\)$/);
|
|
70
|
+
return mat ? parseFloat(mat[1].split(", ")[isVertical(direction) ? 5 : 4]) : null;
|
|
71
|
+
}
|
|
72
|
+
export function dampenValue(v) {
|
|
73
|
+
return 8 * (Math.log(v + 1) - 2);
|
|
74
|
+
}
|
|
75
|
+
export function assignStyle(element, style) {
|
|
76
|
+
if (!element)
|
|
77
|
+
return () => { };
|
|
78
|
+
const prevStyle = element.style.cssText;
|
|
79
|
+
Object.assign(element.style, style);
|
|
80
|
+
return () => {
|
|
81
|
+
element.style.cssText = prevStyle;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Receives functions as arguments and returns a new function that calls all.
|
|
86
|
+
*/
|
|
87
|
+
export function chain(...fns) {
|
|
88
|
+
return (...args) => {
|
|
89
|
+
for (const fn of fns) {
|
|
90
|
+
if (typeof fn === "function") {
|
|
91
|
+
fn(...args);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const isBrowser: boolean;
|
|
2
|
+
export declare function isMobileFirefox(): boolean | undefined;
|
|
3
|
+
export declare function isMac(): boolean | undefined;
|
|
4
|
+
export declare function isIPhone(): boolean | undefined;
|
|
5
|
+
export declare function isSafari(): boolean | undefined;
|
|
6
|
+
export declare function isIPad(): boolean | undefined;
|
|
7
|
+
export declare function isIOS(): boolean | undefined;
|
|
8
|
+
export declare function testPlatform(re: RegExp): boolean | undefined;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const isBrowser = typeof document !== "undefined";
|
|
2
|
+
export function isMobileFirefox() {
|
|
3
|
+
const userAgent = navigator.userAgent;
|
|
4
|
+
return (typeof window !== "undefined" &&
|
|
5
|
+
((/Firefox/.test(userAgent) && /Mobile/.test(userAgent)) || // Android Firefox
|
|
6
|
+
/FxiOS/.test(userAgent)) // iOS Firefox
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
export function isMac() {
|
|
10
|
+
return testPlatform(/^Mac/);
|
|
11
|
+
}
|
|
12
|
+
export function isIPhone() {
|
|
13
|
+
return testPlatform(/^iPhone/);
|
|
14
|
+
}
|
|
15
|
+
export function isSafari() {
|
|
16
|
+
return /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
|
|
17
|
+
}
|
|
18
|
+
export function isIPad() {
|
|
19
|
+
return (testPlatform(/^iPad/) ||
|
|
20
|
+
// iPadOS 13 lies and says it's a Mac, but we can distinguish by detecting touch support.
|
|
21
|
+
(isMac() && navigator.maxTouchPoints > 1));
|
|
22
|
+
}
|
|
23
|
+
export function isIOS() {
|
|
24
|
+
return isIPhone() || isIPad();
|
|
25
|
+
}
|
|
26
|
+
export function testPlatform(re) {
|
|
27
|
+
return typeof window !== "undefined" && window.navigator != null
|
|
28
|
+
? re.test(window.navigator.platform)
|
|
29
|
+
: undefined;
|
|
30
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const TRANSITIONS: {
|
|
2
|
+
DURATION: number;
|
|
3
|
+
EASE: number[];
|
|
4
|
+
};
|
|
5
|
+
export declare const VELOCITY_THRESHOLD = 0.4;
|
|
6
|
+
export declare const CLOSE_THRESHOLD = 0.25;
|
|
7
|
+
export declare const SCROLL_LOCK_TIMEOUT = 100;
|
|
8
|
+
export declare const BORDER_RADIUS = 8;
|
|
9
|
+
export declare const NESTED_DISPLACEMENT = 16;
|
|
10
|
+
export declare const WINDOW_TOP_OFFSET = 26;
|
|
11
|
+
export declare const DRAG_CLASS = "vaul-dragging";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const TRANSITIONS = {
|
|
2
|
+
DURATION: 0.5,
|
|
3
|
+
EASE: [0.32, 0.72, 0, 1],
|
|
4
|
+
};
|
|
5
|
+
export const VELOCITY_THRESHOLD = 0.4;
|
|
6
|
+
export const CLOSE_THRESHOLD = 0.25;
|
|
7
|
+
export const SCROLL_LOCK_TIMEOUT = 100;
|
|
8
|
+
export const BORDER_RADIUS = 8;
|
|
9
|
+
export const NESTED_DISPLACEMENT = 16;
|
|
10
|
+
export const WINDOW_TOP_OFFSET = 26;
|
|
11
|
+
export const DRAG_CLASS = "vaul-dragging";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function noop(): void;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type * from "./components/drawer/types.js";
|
|
2
|
+
export type DrawerDirection = "left" | "right" | "top" | "bottom";
|
|
3
|
+
export type OnChangeFn<T> = (value: T) => void;
|
|
4
|
+
export type OnDrag = (event: PointerEvent | MouseEvent, percentageDragged: number) => void;
|
|
5
|
+
export type OnRelease = (event: PointerEvent | MouseEvent, open: boolean) => void;
|
|
6
|
+
export type AnyFunction = (...args: any) => any;
|
|
7
|
+
export type Getters<T> = {
|
|
8
|
+
[K in keyof T]: () => T[K];
|
|
9
|
+
};
|
|
10
|
+
export type VaulPointerEvent<T extends Element = HTMLElement> = PointerEvent & {
|
|
11
|
+
currentTarget: T;
|
|
12
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import {} from "svelte";
|
|
2
|
+
import { useRefById } from "svelte-toolbelt";
|
|
3
|
+
import { Dialog as DrawerPrimitive } from "bits-ui";
|
|
4
|
+
import { DrawerContext } from "./context.js";
|
|
5
|
+
import { watch } from "runed";
|
|
6
|
+
import { useScaleBackground } from "./use-scale-background.svelte.js";
|
|
7
|
+
export function useDrawerContent(opts) {
|
|
8
|
+
const ctx = DrawerContext.get();
|
|
9
|
+
let mounted = $state(false);
|
|
10
|
+
useRefById({
|
|
11
|
+
id: opts.id,
|
|
12
|
+
ref: opts.ref,
|
|
13
|
+
deps: () => [mounted, ctx.open.current],
|
|
14
|
+
onRefChange: (node) => {
|
|
15
|
+
if (!mounted) {
|
|
16
|
+
ctx.setDrawerNode(null);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
ctx.setDrawerNode(node);
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
let delayedSnapPoints = $state(false);
|
|
24
|
+
let pointerStart = null;
|
|
25
|
+
let lastKnownPointerEvent = null;
|
|
26
|
+
let wasBeyondThePoint = false;
|
|
27
|
+
const hasSnapPoints = $derived(ctx.snapPoints.current && ctx.snapPoints.current.length > 0);
|
|
28
|
+
useScaleBackground();
|
|
29
|
+
function isDeltaInDirection(delta, direction, threshold = 0) {
|
|
30
|
+
if (wasBeyondThePoint)
|
|
31
|
+
return true;
|
|
32
|
+
const deltaY = Math.abs(delta.y);
|
|
33
|
+
const deltaX = Math.abs(delta.x);
|
|
34
|
+
const isDeltaX = deltaX > deltaY;
|
|
35
|
+
const dFactor = ["bottom", "right"].includes(direction) ? 1 : -1;
|
|
36
|
+
if (direction === "left" || direction === "right") {
|
|
37
|
+
const isReverseDirection = delta.x * dFactor < 0;
|
|
38
|
+
if (!isReverseDirection && deltaX >= 0 && deltaX <= threshold) {
|
|
39
|
+
return isDeltaX;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
const isReverseDirection = delta.y * dFactor < 0;
|
|
44
|
+
if (!isReverseDirection && deltaY >= 0 && deltaY <= threshold) {
|
|
45
|
+
return !isDeltaX;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
wasBeyondThePoint = true;
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
watch([() => hasSnapPoints, () => ctx.open.current], () => {
|
|
52
|
+
if (hasSnapPoints && ctx.open.current) {
|
|
53
|
+
window.requestAnimationFrame(() => {
|
|
54
|
+
delayedSnapPoints = true;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
delayedSnapPoints = false;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
function handleOnPointerUp(e) {
|
|
62
|
+
pointerStart = null;
|
|
63
|
+
wasBeyondThePoint = false;
|
|
64
|
+
ctx.onRelease(e);
|
|
65
|
+
}
|
|
66
|
+
function onpointerdown(e) {
|
|
67
|
+
if (ctx.handleOnly.current)
|
|
68
|
+
return;
|
|
69
|
+
opts.onpointerdown.current?.(e);
|
|
70
|
+
pointerStart = { x: e.pageX, y: e.pageY };
|
|
71
|
+
ctx.onPress(e);
|
|
72
|
+
}
|
|
73
|
+
function onOpenAutoFocus(e) {
|
|
74
|
+
opts.onOpenAutoFocus.current?.(e);
|
|
75
|
+
if (!ctx.autoFocus.current) {
|
|
76
|
+
e.preventDefault();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function onInteractOutside(e) {
|
|
80
|
+
opts.onInteractOutside.current?.(e);
|
|
81
|
+
if (!ctx.modal.current || e.defaultPrevented) {
|
|
82
|
+
e.preventDefault();
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (ctx.keyboardIsOpen.current) {
|
|
86
|
+
ctx.keyboardIsOpen.current = false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function onFocusOutside(e) {
|
|
90
|
+
if (!ctx.modal.current) {
|
|
91
|
+
e.preventDefault();
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function onpointermove(e) {
|
|
96
|
+
lastKnownPointerEvent = e;
|
|
97
|
+
if (ctx.handleOnly.current)
|
|
98
|
+
return;
|
|
99
|
+
opts.onpointermove.current?.(e);
|
|
100
|
+
if (!pointerStart)
|
|
101
|
+
return;
|
|
102
|
+
const yPosition = e.pageY - pointerStart.y;
|
|
103
|
+
const xPosition = e.pageX - pointerStart.x;
|
|
104
|
+
const swipeStartThreshold = e.pointerType === "touch" ? 10 : 2;
|
|
105
|
+
const delta = { x: xPosition, y: yPosition };
|
|
106
|
+
const isAllowedToSwipe = isDeltaInDirection(delta, ctx.direction.current, swipeStartThreshold);
|
|
107
|
+
if (isAllowedToSwipe) {
|
|
108
|
+
ctx.onDrag(e);
|
|
109
|
+
}
|
|
110
|
+
else if (Math.abs(xPosition) > swipeStartThreshold ||
|
|
111
|
+
Math.abs(yPosition) > swipeStartThreshold) {
|
|
112
|
+
pointerStart = null;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function onpointerup(e) {
|
|
116
|
+
opts.onpointerup.current?.(e);
|
|
117
|
+
pointerStart = null;
|
|
118
|
+
wasBeyondThePoint = false;
|
|
119
|
+
ctx.onRelease(e);
|
|
120
|
+
}
|
|
121
|
+
function onpointerout(e) {
|
|
122
|
+
opts.onpointerout.current?.(e);
|
|
123
|
+
if (e.pointerType === "touch")
|
|
124
|
+
return;
|
|
125
|
+
handleOnPointerUp(lastKnownPointerEvent);
|
|
126
|
+
}
|
|
127
|
+
function oncontextmenu(e) {
|
|
128
|
+
opts.oncontextmenu.current?.(e);
|
|
129
|
+
if (lastKnownPointerEvent) {
|
|
130
|
+
handleOnPointerUp(lastKnownPointerEvent);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function ontouchmove(e) {
|
|
134
|
+
if (!pointerStart || e.touches.length !== 1)
|
|
135
|
+
return;
|
|
136
|
+
const touch = e.touches[0];
|
|
137
|
+
const yPosition = touch.pageY - pointerStart.y;
|
|
138
|
+
const xPosition = touch.pageX - pointerStart.x;
|
|
139
|
+
const syntheticEvent = {
|
|
140
|
+
pageX: touch.pageX,
|
|
141
|
+
pageY: touch.pageY,
|
|
142
|
+
pointerType: "touch",
|
|
143
|
+
target: e.target,
|
|
144
|
+
};
|
|
145
|
+
lastKnownPointerEvent = syntheticEvent;
|
|
146
|
+
const swipeStartThreshold = 10;
|
|
147
|
+
const delta = { x: xPosition, y: yPosition };
|
|
148
|
+
const isAllowedToSwipe = isDeltaInDirection(delta, ctx.direction.current, swipeStartThreshold);
|
|
149
|
+
if (isAllowedToSwipe) {
|
|
150
|
+
ctx.onDrag(syntheticEvent);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function ontouchend(_e) {
|
|
154
|
+
if (lastKnownPointerEvent) {
|
|
155
|
+
handleOnPointerUp(lastKnownPointerEvent);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const props = $derived({
|
|
159
|
+
id: opts.id.current,
|
|
160
|
+
"data-vaul-drawer-direction": ctx.direction.current,
|
|
161
|
+
"data-vaul-drawer": "",
|
|
162
|
+
"data-vaul-delayed-snap-points": delayedSnapPoints ? "true" : "false",
|
|
163
|
+
"data-vaul-snap-points": ctx.open.current && hasSnapPoints ? "true" : "false",
|
|
164
|
+
"data-vaul-custom-container": ctx.container.current ? "true" : "false",
|
|
165
|
+
"data-vaul-animate": ctx.shouldAnimate ? "true" : "false",
|
|
166
|
+
onpointerdown,
|
|
167
|
+
onOpenAutoFocus,
|
|
168
|
+
onInteractOutside,
|
|
169
|
+
onFocusOutside,
|
|
170
|
+
onpointerup,
|
|
171
|
+
onpointermove,
|
|
172
|
+
onpointerout,
|
|
173
|
+
oncontextmenu,
|
|
174
|
+
ontouchmove,
|
|
175
|
+
ontouchend,
|
|
176
|
+
preventScroll: ctx.modal.current,
|
|
177
|
+
});
|
|
178
|
+
return {
|
|
179
|
+
get props() {
|
|
180
|
+
return props;
|
|
181
|
+
},
|
|
182
|
+
ctx,
|
|
183
|
+
setMounted: (value) => {
|
|
184
|
+
mounted = value;
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type ReadableBoxedValues, type WithRefProps } from "svelte-toolbelt";
|
|
2
|
+
interface UseDrawerHandleOpts extends WithRefProps, ReadableBoxedValues<{
|
|
3
|
+
preventCycle: boolean;
|
|
4
|
+
}> {
|
|
5
|
+
}
|
|
6
|
+
export declare function useDrawerHandle(opts: UseDrawerHandleOpts): {
|
|
7
|
+
readonly props: {
|
|
8
|
+
id: string;
|
|
9
|
+
"data-vaul-drawer-visible": string;
|
|
10
|
+
"data-vaul-handle": string;
|
|
11
|
+
"aria-hidden": string;
|
|
12
|
+
onclick: () => void;
|
|
13
|
+
onpointercancel: () => void;
|
|
14
|
+
onpointerdown: (e: PointerEvent) => void;
|
|
15
|
+
onpointermove: (e: PointerEvent) => void;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export {};
|