|
| 1 | +import type { ReadableBoxedValues, WithRefProps, WritableBoxedValues } from "svelte-toolbelt"; |
| 2 | + |
| 3 | +const CLOSE_THRESHOLD = 0.25; |
| 4 | +const SCROLL_LOCK_TIMEOUT = 100; |
| 5 | +const BORDER_RADIUS = 8; |
| 6 | +const NESTED_DISPLACEMENT = 16; |
| 7 | +const WINDOW_TOP_OFFSET = 26; |
| 8 | +const DRAG_CLASS = "vaul-dragging"; |
| 9 | + |
| 10 | +export type DrawerDirection = "left" | "right" | "top" | "bottom"; |
| 11 | + |
| 12 | +export type OnDragEvent = PointerEvent | TouchEvent; |
| 13 | +export type OnDrag = (event: OnDragEvent, percentageDragged: number) => void; |
| 14 | +export type OnReleaseEvent = PointerEvent | MouseEvent | TouchEvent; |
| 15 | +export type OnRelease = (event: OnReleaseEvent, open: boolean) => void; |
| 16 | + |
| 17 | +type DrawerRootStateProps = ReadableBoxedValues<{ |
| 18 | + closeThreshold: number; |
| 19 | + shouldScaleBackground: boolean; |
| 20 | + scrollLockTimeout: number; |
| 21 | + snapPoints: (string | number)[] | undefined; |
| 22 | + fadeFromIndex: number | undefined; |
| 23 | + fixed: boolean; |
| 24 | + dismissible: boolean; |
| 25 | + direction: DrawerDirection; |
| 26 | + onDrag: OnDrag; |
| 27 | + onRelease: OnRelease; |
| 28 | + nested: boolean; |
| 29 | + onClose: () => void; |
| 30 | + activeSnapPoint: number | string | null | undefined; |
| 31 | +}> & |
| 32 | + WritableBoxedValues<{ |
| 33 | + open: boolean; |
| 34 | + }>; |
| 35 | + |
| 36 | +class DrawerRootState { |
| 37 | + open: DrawerRootStateProps["open"]; |
| 38 | + closeThreshold: DrawerRootStateProps["closeThreshold"]; |
| 39 | + shouldScaleBackground: DrawerRootStateProps["shouldScaleBackground"]; |
| 40 | + scrollLockTimeout: DrawerRootStateProps["scrollLockTimeout"]; |
| 41 | + snapPoints: DrawerRootStateProps["snapPoints"]; |
| 42 | + fadeFromIndex: DrawerRootStateProps["fadeFromIndex"]; |
| 43 | + fixed: DrawerRootStateProps["fixed"]; |
| 44 | + dismissible: DrawerRootStateProps["dismissible"]; |
| 45 | + direction: DrawerRootStateProps["direction"]; |
| 46 | + onDrag: DrawerRootStateProps["onDrag"]; |
| 47 | + onRelease: DrawerRootStateProps["onRelease"]; |
| 48 | + nested: DrawerRootStateProps["nested"]; |
| 49 | + onClose: DrawerRootStateProps["onClose"]; |
| 50 | + activeSnapPoint: DrawerRootStateProps["activeSnapPoint"]; |
| 51 | + triggerNode = $state<HTMLElement | null>(null); |
| 52 | + overlayNode = $state<HTMLElement | null>(null); |
| 53 | + |
| 54 | + hasBeenOpened = $state(false); |
| 55 | + openTime = $state<Date | null>(null); |
| 56 | + keyboardIsOpen = $state(false); |
| 57 | + drawerNode = $state<HTMLElement | null>(null); |
| 58 | + drawerId = $state<string | null>(null); |
| 59 | + isDragging = false; |
| 60 | + dragStartTime: Date | null = null; |
| 61 | + isClosing = false; |
| 62 | + pointerStart = 0; |
| 63 | + dragEndTime: Date | null = null; |
| 64 | + lastTimeDragPrevented: Date | null = null; |
| 65 | + isAllowedToDrag = false; |
| 66 | + drawerHeight = 0; |
| 67 | + previousDiffFromInitial = 0; |
| 68 | + initialDrawerHeight = 0; |
| 69 | + nestedOpenChangeTimer: ReturnType<typeof setTimeout> | null = null; |
| 70 | + |
| 71 | + constructor(props: DrawerRootStateProps) { |
| 72 | + this.open = props.open; |
| 73 | + this.closeThreshold = props.closeThreshold; |
| 74 | + this.shouldScaleBackground = props.shouldScaleBackground; |
| 75 | + this.scrollLockTimeout = props.scrollLockTimeout; |
| 76 | + this.snapPoints = props.snapPoints; |
| 77 | + this.fadeFromIndex = props.fadeFromIndex; |
| 78 | + this.fixed = props.fixed; |
| 79 | + this.dismissible = props.dismissible; |
| 80 | + this.direction = props.direction; |
| 81 | + this.onDrag = props.onDrag; |
| 82 | + this.onRelease = props.onRelease; |
| 83 | + this.nested = props.nested; |
| 84 | + this.onClose = props.onClose; |
| 85 | + this.activeSnapPoint = props.activeSnapPoint; |
| 86 | + } |
| 87 | +} |
0 commit comments