Repository: emilkowalski/vaul Branch: main Commit: 3e97aac6a38e Files: 64 Total size: 191.9 KB Directory structure: gitextract_xvflirfn/ ├── .eslintrc.js ├── .github/ │ └── workflows/ │ └── playwright.yml ├── .gitignore ├── .prettierrc.js ├── .vscode/ │ └── settings.json ├── FUNDING.yml ├── LICENSE.md ├── README.md ├── package.json ├── playwright.config.ts ├── pnpm-workspace.yaml ├── src/ │ ├── browser.ts │ ├── constants.ts │ ├── context.ts │ ├── helpers.ts │ ├── index.tsx │ ├── types.ts │ ├── use-composed-refs.ts │ ├── use-controllable-state.ts │ ├── use-position-fixed.ts │ ├── use-prevent-scroll.ts │ ├── use-scale-background.ts │ └── use-snap-points.ts ├── test/ │ ├── .eslintrc.json │ ├── .gitignore │ ├── README.md │ ├── next.config.js │ ├── package.json │ ├── postcss.config.js │ ├── src/ │ │ └── app/ │ │ ├── controlled/ │ │ │ └── page.tsx │ │ ├── default-open/ │ │ │ └── page.tsx │ │ ├── different-directions/ │ │ │ └── page.tsx │ │ ├── globals.css │ │ ├── initial-snap/ │ │ │ └── page.tsx │ │ ├── layout.tsx │ │ ├── nested-drawers/ │ │ │ └── page.tsx │ │ ├── non-dismissible/ │ │ │ └── page.tsx │ │ ├── open-another-drawer/ │ │ │ └── page.tsx │ │ ├── page.tsx │ │ ├── parent-container/ │ │ │ └── page.tsx │ │ ├── scrollable-page/ │ │ │ └── page.tsx │ │ ├── scrollable-with-inputs/ │ │ │ └── page.tsx │ │ ├── with-handle/ │ │ │ └── page.tsx │ │ ├── with-modal-false/ │ │ │ └── page.tsx │ │ ├── with-redirect/ │ │ │ ├── long-page/ │ │ │ │ └── page.tsx │ │ │ └── page.tsx │ │ ├── with-scaled-background/ │ │ │ └── page.tsx │ │ ├── with-snap-points/ │ │ │ └── page.tsx │ │ └── without-scaled-background/ │ │ └── page.tsx │ ├── tailwind.config.ts │ ├── tests/ │ │ ├── base.spec.ts │ │ ├── constants.ts │ │ ├── controlled.spec.ts │ │ ├── helpers.ts │ │ ├── initial-snap.spec.ts │ │ ├── nested.spec.ts │ │ ├── non-dismissible.spec.ts │ │ ├── with-handle.spec.ts │ │ ├── with-redirect.spec.ts │ │ ├── with-scaled-background.spec.ts │ │ └── without-scaled-background.spec.ts │ └── tsconfig.json ├── tsconfig.json └── turbo.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .eslintrc.js ================================================ module.exports = { root: true, // This tells ESLint to load the config from the package `eslint-config-custom` extends: ['custom'], settings: { next: { rootDir: ['apps/*/'], }, }, }; ================================================ FILE: .github/workflows/playwright.yml ================================================ name: Playwright Tests on: push: branches: [main, master] pull_request: branches: [main, master] jobs: test: timeout-minutes: 60 runs-on: ubuntu-latest container: image: mcr.microsoft.com/playwright:v1.41.2 steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v3 with: node-version: 20 - run: npm install -g pnpm@8.8.0 - run: pnpm install - run: pnpm build - run: npx playwright install - run: pnpm test || exit 1 - uses: actions/upload-artifact@v4 if: always() with: name: playwright-report path: playwright-report/ retention-days: 30 ================================================ FILE: .gitignore ================================================ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. dist # dependencies node_modules .pnp .pnp.js # testing coverage # next.js .next/ out/ build # misc .DS_Store *.pem # debug npm-debug.log* yarn-debug.log* yarn-error.log* .pnpm-debug.log* # local env files .env.local .env.development.local .env.test.local .env.production.local # turbo .turbo /test-results/ /playwright-report/ /playwright/.cache/ # styles style.css ================================================ FILE: .prettierrc.js ================================================ module.exports = { semi: true, singleQuote: true, tabWidth: 2, trailingComma: 'all', printWidth: 120, }; ================================================ FILE: .vscode/settings.json ================================================ { "typescript.tsdk": "node_modules/typescript/lib" } ================================================ FILE: FUNDING.yml ================================================ github: emilkowalski ================================================ FILE: LICENSE.md ================================================ MIT License Copyright (c) 2023 Emil Kowalski Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ > **Note** > This repo is unmaintained. I might come back to it at some point, but not in the near future. This was and always will be a hobby project and I simply don't have the time or will to work on it right now. ================================================ FILE: package.json ================================================ { "name": "vaul", "version": "1.1.2", "description": "Drawer component for React.", "main": "./dist/index.js", "module": "./dist/index.mjs", "types": "./dist/index.d.ts", "files": [ "dist", "style.css" ], "exports": { "import": { "types": "./dist/index.d.mts", "default": "./dist/index.mjs" }, "require": { "types": "./dist/index.d.ts", "default": "./dist/index.js" } }, "scripts": { "type-check": "tsc --noEmit", "build": "pnpm type-check && bunchee && pnpm copy-assets", "copy-assets": "cp -r ./src/style.css ./style.css", "dev": "bunchee --watch", "dev:test": "turbo run dev --filter=test...", "format": "prettier --write .", "test": "playwright test" }, "keywords": [ "react", "drawer", "dialog", "modal" ], "author": "Emil Kowalski ", "license": "MIT", "homepage": "https://vaul.emilkowal.ski/", "repository": { "type": "git", "url": "https://github.com/emilkowalski/vaul.git" }, "bugs": { "url": "https://github.com/emilkowalski/vaul/issues" }, "devDependencies": { "@playwright/test": "^1.37.1", "@radix-ui/react-dialog": "^1.0.4", "@types/node": "20.5.7", "@types/react": "18.2.55", "@types/react-dom": "18.2.18", "bunchee": "^5.1.5", "eslint": "^7.32.0", "prettier": "^2.5.1", "react": "^18.2.0", "react-dom": "^18.2.0", "turbo": "1.6", "typescript": "5.2.2" }, "peerDependencies": { "react": "^16.8 || ^17.0 || ^18.0 || ^19.0.0 || ^19.0.0-rc", "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0.0 || ^19.0.0-rc" }, "packageManager": "pnpm@8.8.0", "dependencies": { "@radix-ui/react-dialog": "^1.1.1" } } ================================================ FILE: playwright.config.ts ================================================ import { defineConfig, devices } from '@playwright/test'; /** * Read environment variables from file. * https://github.com/motdotla/dotenv */ // require('dotenv').config(); /** * See https://playwright.dev/docs/test-configuration. */ export default defineConfig({ testDir: './test', /* Maximum time one test can run for. */ timeout: 30 * 1000, expect: { /** * Maximum time expect() should wait for the condition to be met. * For example in `await expect(locator).toHaveText();` */ timeout: 5000, }, /* Run tests in files in parallel */ fullyParallel: true, /* Fail the build on CI if you accidentally left test.only in the source code. */ forbidOnly: !!process.env.CI, /* Retry on CI only */ retries: process.env.CI ? 2 : 0, /* Opt out of parallel tests on CI. */ workers: process.env.CI ? 1 : undefined, /* Reporter to use. See https://playwright.dev/docs/test-reporters */ reporter: 'html', /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ use: { trace: 'on-first-retry', baseURL: 'http://localhost:3000', }, webServer: { command: 'npm run dev', url: 'http://localhost:3000', cwd: './test', reuseExistingServer: !process.env.CI, }, /* Configure projects for major browsers */ projects: [ { name: 'iPhone', use: { ...devices['iPhone 13 Pro'] }, }, { name: 'Pixel', use: { ...devices['Pixel 5'] }, }, ], }); ================================================ FILE: pnpm-workspace.yaml ================================================ packages: - '.' - 'test' ================================================ FILE: src/browser.ts ================================================ export function isMobileFirefox(): boolean | undefined { const userAgent = navigator.userAgent; return ( typeof window !== 'undefined' && ((/Firefox/.test(userAgent) && /Mobile/.test(userAgent)) || // Android Firefox /FxiOS/.test(userAgent)) // iOS Firefox ); } export function isMac(): boolean | undefined { return testPlatform(/^Mac/); } export function isIPhone(): boolean | undefined { return testPlatform(/^iPhone/); } export function isSafari(): boolean | undefined { return /^((?!chrome|android).)*safari/i.test(navigator.userAgent); } export function isIPad(): boolean | undefined { return ( testPlatform(/^iPad/) || // iPadOS 13 lies and says it's a Mac, but we can distinguish by detecting touch support. (isMac() && navigator.maxTouchPoints > 1) ); } export function isIOS(): boolean | undefined { return isIPhone() || isIPad(); } export function testPlatform(re: RegExp): boolean | undefined { return typeof window !== 'undefined' && window.navigator != null ? re.test(window.navigator.platform) : undefined; } ================================================ FILE: src/constants.ts ================================================ export const TRANSITIONS = { DURATION: 0.5, EASE: [0.32, 0.72, 0, 1], }; export const VELOCITY_THRESHOLD = 0.4; export const CLOSE_THRESHOLD = 0.25; export const SCROLL_LOCK_TIMEOUT = 100; export const BORDER_RADIUS = 8; export const NESTED_DISPLACEMENT = 16; export const WINDOW_TOP_OFFSET = 26; export const DRAG_CLASS = 'vaul-dragging'; ================================================ FILE: src/context.ts ================================================ import React from 'react'; import { DrawerDirection } from './types'; interface DrawerContextValue { drawerRef: React.RefObject; overlayRef: React.RefObject; onPress: (event: React.PointerEvent) => void; onRelease: (event: React.PointerEvent | null) => void; onDrag: (event: React.PointerEvent) => void; onNestedDrag: (event: React.PointerEvent, percentageDragged: number) => void; onNestedOpenChange: (o: boolean) => void; onNestedRelease: (event: React.PointerEvent, open: boolean) => void; dismissible: boolean; isOpen: boolean; isDragging: boolean; keyboardIsOpen: React.MutableRefObject; snapPointsOffset: number[] | null; snapPoints?: (number | string)[] | null; activeSnapPointIndex?: number | null; modal: boolean; shouldFade: boolean; activeSnapPoint?: number | string | null; setActiveSnapPoint: (o: number | string | null) => void; closeDrawer: () => void; openProp?: boolean; onOpenChange?: (o: boolean) => void; direction: DrawerDirection; shouldScaleBackground: boolean; setBackgroundColorOnScale: boolean; noBodyStyles: boolean; handleOnly?: boolean; container?: HTMLElement | null; autoFocus?: boolean; shouldAnimate?: React.RefObject; } export const DrawerContext = React.createContext({ drawerRef: { current: null }, overlayRef: { current: null }, onPress: () => {}, onRelease: () => {}, onDrag: () => {}, onNestedDrag: () => {}, onNestedOpenChange: () => {}, onNestedRelease: () => {}, openProp: undefined, dismissible: false, isOpen: false, isDragging: false, keyboardIsOpen: { current: false }, snapPointsOffset: null, snapPoints: null, handleOnly: false, modal: false, shouldFade: false, activeSnapPoint: null, onOpenChange: () => {}, setActiveSnapPoint: () => {}, closeDrawer: () => {}, direction: 'bottom', shouldAnimate: { current: true }, shouldScaleBackground: false, setBackgroundColorOnScale: true, noBodyStyles: false, container: null, autoFocus: false, }); export const useDrawerContext = () => { const context = React.useContext(DrawerContext); if (!context) { throw new Error('useDrawerContext must be used within a Drawer.Root'); } return context; }; ================================================ FILE: src/helpers.ts ================================================ import { AnyFunction, DrawerDirection } from './types'; interface Style { [key: string]: string; } const cache = new WeakMap(); export function isInView(el: HTMLElement): boolean { const rect = el.getBoundingClientRect(); if (!window.visualViewport) return false; return ( rect.top >= 0 && rect.left >= 0 && // Need + 40 for safari detection rect.bottom <= window.visualViewport.height - 40 && rect.right <= window.visualViewport.width ); } export function set(el: Element | HTMLElement | null | undefined, styles: Style, ignoreCache = false) { if (!el || !(el instanceof HTMLElement)) return; let originalStyles: Style = {}; Object.entries(styles).forEach(([key, value]: [string, string]) => { if (key.startsWith('--')) { el.style.setProperty(key, value); return; } originalStyles[key] = (el.style as any)[key]; (el.style as any)[key] = value; }); if (ignoreCache) return; cache.set(el, originalStyles); } export function reset(el: Element | HTMLElement | null, prop?: string) { if (!el || !(el instanceof HTMLElement)) return; let originalStyles = cache.get(el); if (!originalStyles) { return; } if (prop) { (el.style as any)[prop] = originalStyles[prop]; } else { Object.entries(originalStyles).forEach(([key, value]) => { (el.style as any)[key] = value; }); } } export const isVertical = (direction: DrawerDirection) => { switch (direction) { case 'top': case 'bottom': return true; case 'left': case 'right': return false; default: return direction satisfies never; } }; export function getTranslate(element: HTMLElement, direction: DrawerDirection): number | null { if (!element) { return null; } const style = window.getComputedStyle(element); const transform = // @ts-ignore style.transform || style.webkitTransform || style.mozTransform; let mat = transform.match(/^matrix3d\((.+)\)$/); if (mat) { // https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix3d return parseFloat(mat[1].split(', ')[isVertical(direction) ? 13 : 12]); } // https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix mat = transform.match(/^matrix\((.+)\)$/); return mat ? parseFloat(mat[1].split(', ')[isVertical(direction) ? 5 : 4]) : null; } export function dampenValue(v: number) { return 8 * (Math.log(v + 1) - 2); } export function assignStyle(element: HTMLElement | null | undefined, style: Partial) { if (!element) return () => {}; const prevStyle = element.style.cssText; Object.assign(element.style, style); return () => { element.style.cssText = prevStyle; }; } /** * Receives functions as arguments and returns a new function that calls all. */ export function chain(...fns: T[]) { return (...args: T extends AnyFunction ? Parameters : never) => { for (const fn of fns) { if (typeof fn === 'function') { // @ts-ignore fn(...args); } } }; } ================================================ FILE: src/index.tsx ================================================ 'use client'; import * as DialogPrimitive from '@radix-ui/react-dialog'; import React from 'react'; import { DrawerContext, useDrawerContext } from './context'; import './style.css'; import { usePreventScroll, isInput } from './use-prevent-scroll'; import { useComposedRefs } from './use-composed-refs'; import { useSnapPoints } from './use-snap-points'; import { set, getTranslate, dampenValue, isVertical, reset } from './helpers'; import { TRANSITIONS, VELOCITY_THRESHOLD, CLOSE_THRESHOLD, SCROLL_LOCK_TIMEOUT, BORDER_RADIUS, NESTED_DISPLACEMENT, WINDOW_TOP_OFFSET, DRAG_CLASS, } from './constants'; import { DrawerDirection } from './types'; import { useControllableState } from './use-controllable-state'; import { useScaleBackground } from './use-scale-background'; import { usePositionFixed } from './use-position-fixed'; import { isIOS, isMobileFirefox } from './browser'; export interface WithFadeFromProps { /** * Array of numbers from 0 to 100 that corresponds to % of the screen a given snap point should take up. * Should go from least visible. Example `[0.2, 0.5, 0.8]`. * You can also use px values, which doesn't take screen height into account. */ snapPoints: (number | string)[]; /** * Index of a `snapPoint` from which the overlay fade should be applied. Defaults to the last snap point. */ fadeFromIndex: number; } export interface WithoutFadeFromProps { /** * Array of numbers from 0 to 100 that corresponds to % of the screen a given snap point should take up. * Should go from least visible. Example `[0.2, 0.5, 0.8]`. * You can also use px values, which doesn't take screen height into account. */ snapPoints?: (number | string)[]; fadeFromIndex?: never; } export type DialogProps = { activeSnapPoint?: number | string | null; setActiveSnapPoint?: (snapPoint: number | string | null) => void; children?: React.ReactNode; open?: boolean; /** * Number between 0 and 1 that determines when the drawer should be closed. * Example: threshold of 0.5 would close the drawer if the user swiped for 50% of the height of the drawer or more. * @default 0.25 */ closeThreshold?: number; /** * When `true` the `body` doesn't get any styles assigned from Vaul */ noBodyStyles?: boolean; onOpenChange?: (open: boolean) => void; shouldScaleBackground?: boolean; /** * When `false` we don't change body's background color when the drawer is open. * @default true */ setBackgroundColorOnScale?: boolean; /** * Duration for which the drawer is not draggable after scrolling content inside of the drawer. * @default 500ms */ scrollLockTimeout?: number; /** * 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 */ fixed?: boolean; /** * When `true` only allows the drawer to be dragged by the `` component. * @default false */ handleOnly?: boolean; /** * When `false` dragging, clicking outside, pressing esc, etc. will not close the drawer. * Use this in comination with the `open` prop, otherwise you won't be able to open/close the drawer. * @default true */ dismissible?: boolean; onDrag?: (event: React.PointerEvent, percentageDragged: number) => void; onRelease?: (event: React.PointerEvent, open: boolean) => void; /** * When `false` it allows to interact with elements outside of the drawer without closing it. * @default true */ modal?: boolean; nested?: boolean; onClose?: () => void; /** * Direction of the drawer. Can be `top` or `bottom`, `left`, `right`. * @default 'bottom' */ direction?: 'top' | 'bottom' | 'left' | 'right'; /** * Opened by default, skips initial enter animation. Still reacts to `open` state changes * @default false */ defaultOpen?: boolean; /** * When set to `true` prevents scrolling on the document body on mount, and restores it on unmount. * @default false */ disablePreventScroll?: boolean; /** * When `true` Vaul will reposition inputs rather than scroll then into view if the keyboard is in the way. * Setting it to `false` will fall back to the default browser behavior. * @default true when {@link snapPoints} is defined */ repositionInputs?: boolean; /** * Disabled velocity based swiping for snap points. * This means that a snap point won't be skipped even if the velocity is high enough. * Useful if each snap point in a drawer is equally important. * @default false */ snapToSequentialPoint?: boolean; container?: HTMLElement | null; /** * 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. * Useful to revert any state changes for example. */ onAnimationEnd?: (open: boolean) => void; preventScrollRestoration?: boolean; autoFocus?: boolean; } & (WithFadeFromProps | WithoutFadeFromProps); export function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRelease: onReleaseProp, snapPoints, shouldScaleBackground = false, setBackgroundColorOnScale = true, closeThreshold = CLOSE_THRESHOLD, scrollLockTimeout = SCROLL_LOCK_TIMEOUT, dismissible = true, handleOnly = false, fadeFromIndex = snapPoints && snapPoints.length - 1, activeSnapPoint: activeSnapPointProp, setActiveSnapPoint: setActiveSnapPointProp, fixed, modal = true, onClose, nested, noBodyStyles = false, direction = 'bottom', defaultOpen = false, disablePreventScroll = true, snapToSequentialPoint = false, preventScrollRestoration = false, repositionInputs = true, onAnimationEnd, container, autoFocus = false, }: DialogProps) { const [isOpen = false, setIsOpen] = useControllableState({ defaultProp: defaultOpen, prop: openProp, onChange: (o: boolean) => { onOpenChange?.(o); if (!o && !nested) { restorePositionSetting(); } setTimeout(() => { onAnimationEnd?.(o); }, TRANSITIONS.DURATION * 1000); if (o && !modal) { if (typeof window !== 'undefined') { window.requestAnimationFrame(() => { document.body.style.pointerEvents = 'auto'; }); } } if (!o) { // This will be removed when the exit animation ends (`500ms`) document.body.style.pointerEvents = 'auto'; } }, }); const [hasBeenOpened, setHasBeenOpened] = React.useState(false); const [isDragging, setIsDragging] = React.useState(false); const [justReleased, setJustReleased] = React.useState(false); const overlayRef = React.useRef(null); const openTime = React.useRef(null); const dragStartTime = React.useRef(null); const dragEndTime = React.useRef(null); const lastTimeDragPrevented = React.useRef(null); const isAllowedToDrag = React.useRef(false); const nestedOpenChangeTimer = React.useRef(null); const pointerStart = React.useRef(0); const keyboardIsOpen = React.useRef(false); const shouldAnimate = React.useRef(!defaultOpen); const previousDiffFromInitial = React.useRef(0); const drawerRef = React.useRef(null); const drawerHeightRef = React.useRef(drawerRef.current?.getBoundingClientRect().height || 0); const drawerWidthRef = React.useRef(drawerRef.current?.getBoundingClientRect().width || 0); const initialDrawerHeight = React.useRef(0); const onSnapPointChange = React.useCallback((activeSnapPointIndex: number) => { // Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable. if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) openTime.current = new Date(); }, []); const { activeSnapPoint, activeSnapPointIndex, setActiveSnapPoint, onRelease: onReleaseSnapPoints, snapPointsOffset, onDrag: onDragSnapPoints, shouldFade, getPercentageDragged: getSnapPointsPercentageDragged, } = useSnapPoints({ snapPoints, activeSnapPointProp, setActiveSnapPointProp, drawerRef, fadeFromIndex, overlayRef, onSnapPointChange, direction, container, snapToSequentialPoint, }); usePreventScroll({ isDisabled: !isOpen || isDragging || !modal || justReleased || !hasBeenOpened || !repositionInputs || !disablePreventScroll, }); const { restorePositionSetting } = usePositionFixed({ isOpen, modal, nested: nested ?? false, hasBeenOpened, preventScrollRestoration, noBodyStyles, }); function getScale() { return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth; } function onPress(event: React.PointerEvent) { if (!dismissible && !snapPoints) return; if (drawerRef.current && !drawerRef.current.contains(event.target as Node)) return; drawerHeightRef.current = drawerRef.current?.getBoundingClientRect().height || 0; drawerWidthRef.current = drawerRef.current?.getBoundingClientRect().width || 0; setIsDragging(true); dragStartTime.current = new Date(); // iOS doesn't trigger mouseUp after scrolling so we need to listen to touched in order to disallow dragging if (isIOS()) { window.addEventListener('touchend', () => (isAllowedToDrag.current = false), { once: true }); } // Ensure we maintain correct pointer capture even when going outside of the drawer (event.target as HTMLElement).setPointerCapture(event.pointerId); pointerStart.current = isVertical(direction) ? event.pageY : event.pageX; } function shouldDrag(el: EventTarget, isDraggingInDirection: boolean) { let element = el as HTMLElement; const highlightedText = window.getSelection()?.toString(); const swipeAmount = drawerRef.current ? getTranslate(drawerRef.current, direction) : null; const date = new Date(); // Fixes https://github.com/emilkowalski/vaul/issues/483 if (element.tagName === 'SELECT') { return false; } if (element.hasAttribute('data-vaul-no-drag') || element.closest('[data-vaul-no-drag]')) { return false; } if (direction === 'right' || direction === 'left') { return true; } // Allow scrolling when animating if (openTime.current && date.getTime() - openTime.current.getTime() < 500) { return false; } if (swipeAmount !== null) { if (direction === 'bottom' ? swipeAmount > 0 : swipeAmount < 0) { return true; } } // Don't drag if there's highlighted text if (highlightedText && highlightedText.length > 0) { return false; } // Disallow dragging if drawer was scrolled within `scrollLockTimeout` if ( lastTimeDragPrevented.current && date.getTime() - lastTimeDragPrevented.current.getTime() < scrollLockTimeout && swipeAmount === 0 ) { lastTimeDragPrevented.current = date; return false; } if (isDraggingInDirection) { lastTimeDragPrevented.current = date; // We are dragging down so we should allow scrolling return false; } // Keep climbing up the DOM tree as long as there's a parent while (element) { // Check if the element is scrollable if (element.scrollHeight > element.clientHeight) { if (element.scrollTop !== 0) { lastTimeDragPrevented.current = new Date(); // The element is scrollable and not scrolled to the top, so don't drag return false; } if (element.getAttribute('role') === 'dialog') { return true; } } // Move up to the parent element element = element.parentNode as HTMLElement; } // No scrollable parents not scrolled to the top found, so drag return true; } function onDrag(event: React.PointerEvent) { if (!drawerRef.current) { return; } // We need to know how much of the drawer has been dragged in percentages so that we can transform background accordingly if (isDragging) { const directionMultiplier = direction === 'bottom' || direction === 'right' ? 1 : -1; const draggedDistance = (pointerStart.current - (isVertical(direction) ? event.pageY : event.pageX)) * directionMultiplier; const isDraggingInDirection = draggedDistance > 0; // Pre condition for disallowing dragging in the close direction. const noCloseSnapPointsPreCondition = snapPoints && !dismissible && !isDraggingInDirection; // Disallow dragging down to close when first snap point is the active one and dismissible prop is set to false. if (noCloseSnapPointsPreCondition && activeSnapPointIndex === 0) return; // We need to capture last time when drag with scroll was triggered and have a timeout between const absDraggedDistance = Math.abs(draggedDistance); const wrapper = document.querySelector('[data-vaul-drawer-wrapper]'); const drawerDimension = direction === 'bottom' || direction === 'top' ? drawerHeightRef.current : drawerWidthRef.current; // Calculate the percentage dragged, where 1 is the closed position let percentageDragged = absDraggedDistance / drawerDimension; const snapPointPercentageDragged = getSnapPointsPercentageDragged(absDraggedDistance, isDraggingInDirection); if (snapPointPercentageDragged !== null) { percentageDragged = snapPointPercentageDragged; } // Disallow close dragging beyond the smallest snap point. if (noCloseSnapPointsPreCondition && percentageDragged >= 1) { return; } if (!isAllowedToDrag.current && !shouldDrag(event.target, isDraggingInDirection)) return; drawerRef.current.classList.add(DRAG_CLASS); // If shouldDrag gave true once after pressing down on the drawer, we set isAllowedToDrag to true and it will remain true until we let go, there's no reason to disable dragging mid way, ever, and that's the solution to it isAllowedToDrag.current = true; set(drawerRef.current, { transition: 'none', }); set(overlayRef.current, { transition: 'none', }); if (snapPoints) { onDragSnapPoints({ draggedDistance }); } // Run this only if snapPoints are not defined or if we are at the last snap point (highest one) if (isDraggingInDirection && !snapPoints) { const dampenedDraggedDistance = dampenValue(draggedDistance); const translateValue = Math.min(dampenedDraggedDistance * -1, 0) * directionMultiplier; set(drawerRef.current, { transform: isVertical(direction) ? `translate3d(0, ${translateValue}px, 0)` : `translate3d(${translateValue}px, 0, 0)`, }); return; } const opacityValue = 1 - percentageDragged; if (shouldFade || (fadeFromIndex && activeSnapPointIndex === fadeFromIndex - 1)) { onDragProp?.(event, percentageDragged); set( overlayRef.current, { opacity: `${opacityValue}`, transition: 'none', }, true, ); } if (wrapper && overlayRef.current && shouldScaleBackground) { // Calculate percentageDragged as a fraction (0 to 1) const scaleValue = Math.min(getScale() + percentageDragged * (1 - getScale()), 1); const borderRadiusValue = 8 - percentageDragged * 8; const translateValue = Math.max(0, 14 - percentageDragged * 14); set( wrapper, { borderRadius: `${borderRadiusValue}px`, transform: isVertical(direction) ? `scale(${scaleValue}) translate3d(0, ${translateValue}px, 0)` : `scale(${scaleValue}) translate3d(${translateValue}px, 0, 0)`, transition: 'none', }, true, ); } if (!snapPoints) { const translateValue = absDraggedDistance * directionMultiplier; set(drawerRef.current, { transform: isVertical(direction) ? `translate3d(0, ${translateValue}px, 0)` : `translate3d(${translateValue}px, 0, 0)`, }); } } } React.useEffect(() => { window.requestAnimationFrame(() => { shouldAnimate.current = true; }); }, []); React.useEffect(() => { function onVisualViewportChange() { if (!drawerRef.current || !repositionInputs) return; const focusedElement = document.activeElement as HTMLElement; if (isInput(focusedElement) || keyboardIsOpen.current) { const visualViewportHeight = window.visualViewport?.height || 0; const totalHeight = window.innerHeight; // This is the height of the keyboard let diffFromInitial = totalHeight - visualViewportHeight; const drawerHeight = drawerRef.current.getBoundingClientRect().height || 0; // Adjust drawer height only if it's tall enough const isTallEnough = drawerHeight > totalHeight * 0.8; if (!initialDrawerHeight.current) { initialDrawerHeight.current = drawerHeight; } const offsetFromTop = drawerRef.current.getBoundingClientRect().top; // visualViewport height may change due to somq e subtle changes to the keyboard. Checking if the height changed by 60 or more will make sure that they keyboard really changed its open state. if (Math.abs(previousDiffFromInitial.current - diffFromInitial) > 60) { keyboardIsOpen.current = !keyboardIsOpen.current; } if (snapPoints && snapPoints.length > 0 && snapPointsOffset && activeSnapPointIndex) { const activeSnapPointHeight = snapPointsOffset[activeSnapPointIndex] || 0; diffFromInitial += activeSnapPointHeight; } previousDiffFromInitial.current = diffFromInitial; // We don't have to change the height if the input is in view, when we are here we are in the opened keyboard state so we can correctly check if the input is in view if (drawerHeight > visualViewportHeight || keyboardIsOpen.current) { const height = drawerRef.current.getBoundingClientRect().height; let newDrawerHeight = height; if (height > visualViewportHeight) { newDrawerHeight = visualViewportHeight - (isTallEnough ? offsetFromTop : WINDOW_TOP_OFFSET); } // When fixed, 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 if (fixed) { drawerRef.current.style.height = `${height - Math.max(diffFromInitial, 0)}px`; } else { drawerRef.current.style.height = `${Math.max(newDrawerHeight, visualViewportHeight - offsetFromTop)}px`; } } else if (!isMobileFirefox()) { drawerRef.current.style.height = `${initialDrawerHeight.current}px`; } if (snapPoints && snapPoints.length > 0 && !keyboardIsOpen.current) { drawerRef.current.style.bottom = `0px`; } else { // Negative bottom value would never make sense drawerRef.current.style.bottom = `${Math.max(diffFromInitial, 0)}px`; } } } window.visualViewport?.addEventListener('resize', onVisualViewportChange); return () => window.visualViewport?.removeEventListener('resize', onVisualViewportChange); }, [activeSnapPointIndex, snapPoints, snapPointsOffset]); function closeDrawer(fromWithin?: boolean) { cancelDrag(); onClose?.(); if (!fromWithin) { setIsOpen(false); } setTimeout(() => { if (snapPoints) { setActiveSnapPoint(snapPoints[0]); } }, TRANSITIONS.DURATION * 1000); // seconds to ms } function resetDrawer() { if (!drawerRef.current) return; const wrapper = document.querySelector('[data-vaul-drawer-wrapper]'); const currentSwipeAmount = getTranslate(drawerRef.current, direction); set(drawerRef.current, { transform: 'translate3d(0, 0, 0)', transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`, }); set(overlayRef.current, { transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`, opacity: '1', }); // Don't reset background if swiped upwards if (shouldScaleBackground && currentSwipeAmount && currentSwipeAmount > 0 && isOpen) { set( wrapper, { borderRadius: `${BORDER_RADIUS}px`, overflow: 'hidden', ...(isVertical(direction) ? { transform: `scale(${getScale()}) translate3d(0, calc(env(safe-area-inset-top) + 14px), 0)`, transformOrigin: 'top', } : { transform: `scale(${getScale()}) translate3d(calc(env(safe-area-inset-top) + 14px), 0, 0)`, transformOrigin: 'left', }), transitionProperty: 'transform, border-radius', transitionDuration: `${TRANSITIONS.DURATION}s`, transitionTimingFunction: `cubic-bezier(${TRANSITIONS.EASE.join(',')})`, }, true, ); } } function cancelDrag() { if (!isDragging || !drawerRef.current) return; drawerRef.current.classList.remove(DRAG_CLASS); isAllowedToDrag.current = false; setIsDragging(false); dragEndTime.current = new Date(); } function onRelease(event: React.PointerEvent | null) { if (!isDragging || !drawerRef.current) return; drawerRef.current.classList.remove(DRAG_CLASS); isAllowedToDrag.current = false; setIsDragging(false); dragEndTime.current = new Date(); const swipeAmount = getTranslate(drawerRef.current, direction); if (!event || !shouldDrag(event.target, false) || !swipeAmount || Number.isNaN(swipeAmount)) return; if (dragStartTime.current === null) return; const timeTaken = dragEndTime.current.getTime() - dragStartTime.current.getTime(); const distMoved = pointerStart.current - (isVertical(direction) ? event.pageY : event.pageX); const velocity = Math.abs(distMoved) / timeTaken; if (velocity > 0.05) { // `justReleased` is needed to prevent the drawer from focusing on an input when the drag ends, as it's not the intent most of the time. setJustReleased(true); setTimeout(() => { setJustReleased(false); }, 200); } if (snapPoints) { const directionMultiplier = direction === 'bottom' || direction === 'right' ? 1 : -1; onReleaseSnapPoints({ draggedDistance: distMoved * directionMultiplier, closeDrawer, velocity, dismissible, }); onReleaseProp?.(event, true); return; } // Moved upwards, don't do anything if (direction === 'bottom' || direction === 'right' ? distMoved > 0 : distMoved < 0) { resetDrawer(); onReleaseProp?.(event, true); return; } if (velocity > VELOCITY_THRESHOLD) { closeDrawer(); onReleaseProp?.(event, false); return; } const visibleDrawerHeight = Math.min(drawerRef.current.getBoundingClientRect().height ?? 0, window.innerHeight); const visibleDrawerWidth = Math.min(drawerRef.current.getBoundingClientRect().width ?? 0, window.innerWidth); const isHorizontalSwipe = direction === 'left' || direction === 'right'; if (Math.abs(swipeAmount) >= (isHorizontalSwipe ? visibleDrawerWidth : visibleDrawerHeight) * closeThreshold) { closeDrawer(); onReleaseProp?.(event, false); return; } onReleaseProp?.(event, true); resetDrawer(); } React.useEffect(() => { // Trigger enter animation without using CSS animation if (isOpen) { set(document.documentElement, { scrollBehavior: 'auto', }); openTime.current = new Date(); } return () => { reset(document.documentElement, 'scrollBehavior'); }; }, [isOpen]); function onNestedOpenChange(o: boolean) { const scale = o ? (window.innerWidth - NESTED_DISPLACEMENT) / window.innerWidth : 1; const initialTranslate = o ? -NESTED_DISPLACEMENT : 0; if (nestedOpenChangeTimer.current) { window.clearTimeout(nestedOpenChangeTimer.current); } set(drawerRef.current, { transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`, transform: isVertical(direction) ? `scale(${scale}) translate3d(0, ${initialTranslate}px, 0)` : `scale(${scale}) translate3d(${initialTranslate}px, 0, 0)`, }); if (!o && drawerRef.current) { nestedOpenChangeTimer.current = setTimeout(() => { const translateValue = getTranslate(drawerRef.current as HTMLElement, direction); set(drawerRef.current, { transition: 'none', transform: isVertical(direction) ? `translate3d(0, ${translateValue}px, 0)` : `translate3d(${translateValue}px, 0, 0)`, }); }, 500); } } function onNestedDrag(_event: React.PointerEvent, percentageDragged: number) { if (percentageDragged < 0) return; const initialScale = (window.innerWidth - NESTED_DISPLACEMENT) / window.innerWidth; const newScale = initialScale + percentageDragged * (1 - initialScale); const newTranslate = -NESTED_DISPLACEMENT + percentageDragged * NESTED_DISPLACEMENT; set(drawerRef.current, { transform: isVertical(direction) ? `scale(${newScale}) translate3d(0, ${newTranslate}px, 0)` : `scale(${newScale}) translate3d(${newTranslate}px, 0, 0)`, transition: 'none', }); } function onNestedRelease(_event: React.PointerEvent, o: boolean) { const dim = isVertical(direction) ? window.innerHeight : window.innerWidth; const scale = o ? (dim - NESTED_DISPLACEMENT) / dim : 1; const translate = o ? -NESTED_DISPLACEMENT : 0; if (o) { set(drawerRef.current, { transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`, transform: isVertical(direction) ? `scale(${scale}) translate3d(0, ${translate}px, 0)` : `scale(${scale}) translate3d(${translate}px, 0, 0)`, }); } } React.useEffect(() => { if (!modal) { // Need to do this manually unfortunately window.requestAnimationFrame(() => { document.body.style.pointerEvents = 'auto'; }); } }, [modal]); return ( { if (!dismissible && !open) return; if (open) { setHasBeenOpened(true); } else { closeDrawer(true); } setIsOpen(open); }} open={isOpen} modal={modal} > {children} ); } export const Overlay = React.forwardRef>( function ({ ...rest }, ref) { const { overlayRef, snapPoints, onRelease, shouldFade, isOpen, modal, shouldAnimate } = useDrawerContext(); const composedRef = useComposedRefs(ref, overlayRef); const hasSnapPoints = snapPoints && snapPoints.length > 0; const onMouseUp = React.useCallback((event: React.PointerEvent) => onRelease(event), [onRelease]); // Overlay is the component that is locking scroll, removing it will unlock the scroll without having to dig into Radix's Dialog library if (!modal) { return null; } return ( ); }, ); Overlay.displayName = 'Drawer.Overlay'; export type ContentProps = React.ComponentPropsWithoutRef; export const Content = React.forwardRef(function ( { onPointerDownOutside, style, onOpenAutoFocus, ...rest }, ref, ) { const { drawerRef, onPress, onRelease, onDrag, keyboardIsOpen, snapPointsOffset, activeSnapPointIndex, modal, isOpen, direction, snapPoints, container, handleOnly, shouldAnimate, autoFocus, } = useDrawerContext(); // Needed to use transition instead of animations const [delayedSnapPoints, setDelayedSnapPoints] = React.useState(false); const composedRef = useComposedRefs(ref, drawerRef); const pointerStartRef = React.useRef<{ x: number; y: number } | null>(null); const lastKnownPointerEventRef = React.useRef | null>(null); const wasBeyondThePointRef = React.useRef(false); const hasSnapPoints = snapPoints && snapPoints.length > 0; useScaleBackground(); const isDeltaInDirection = (delta: { x: number; y: number }, direction: DrawerDirection, threshold = 0) => { if (wasBeyondThePointRef.current) return true; const deltaY = Math.abs(delta.y); const deltaX = Math.abs(delta.x); const isDeltaX = deltaX > deltaY; const dFactor = ['bottom', 'right'].includes(direction) ? 1 : -1; if (direction === 'left' || direction === 'right') { const isReverseDirection = delta.x * dFactor < 0; if (!isReverseDirection && deltaX >= 0 && deltaX <= threshold) { return isDeltaX; } } else { const isReverseDirection = delta.y * dFactor < 0; if (!isReverseDirection && deltaY >= 0 && deltaY <= threshold) { return !isDeltaX; } } wasBeyondThePointRef.current = true; return true; }; React.useEffect(() => { if (hasSnapPoints) { window.requestAnimationFrame(() => { setDelayedSnapPoints(true); }); } }, []); function handleOnPointerUp(event: React.PointerEvent | null) { pointerStartRef.current = null; wasBeyondThePointRef.current = false; onRelease(event); } return ( 0 ? ({ '--snap-point-height': `${snapPointsOffset[activeSnapPointIndex ?? 0]!}px`, ...style, } as React.CSSProperties) : style } onPointerDown={(event) => { if (handleOnly) return; rest.onPointerDown?.(event); pointerStartRef.current = { x: event.pageX, y: event.pageY }; onPress(event); }} onOpenAutoFocus={(e) => { onOpenAutoFocus?.(e); if (!autoFocus) { e.preventDefault(); } }} onPointerDownOutside={(e) => { onPointerDownOutside?.(e); if (!modal || e.defaultPrevented) { e.preventDefault(); return; } if (keyboardIsOpen.current) { keyboardIsOpen.current = false; } }} onFocusOutside={(e) => { if (!modal) { e.preventDefault(); return; } }} onPointerMove={(event) => { lastKnownPointerEventRef.current = event; if (handleOnly) return; rest.onPointerMove?.(event); if (!pointerStartRef.current) return; const yPosition = event.pageY - pointerStartRef.current.y; const xPosition = event.pageX - pointerStartRef.current.x; const swipeStartThreshold = event.pointerType === 'touch' ? 10 : 2; const delta = { x: xPosition, y: yPosition }; const isAllowedToSwipe = isDeltaInDirection(delta, direction, swipeStartThreshold); if (isAllowedToSwipe) onDrag(event); else if (Math.abs(xPosition) > swipeStartThreshold || Math.abs(yPosition) > swipeStartThreshold) { pointerStartRef.current = null; } }} onPointerUp={(event) => { rest.onPointerUp?.(event); pointerStartRef.current = null; wasBeyondThePointRef.current = false; onRelease(event); }} onPointerOut={(event) => { rest.onPointerOut?.(event); handleOnPointerUp(lastKnownPointerEventRef.current); }} onContextMenu={(event) => { rest.onContextMenu?.(event); if (lastKnownPointerEventRef.current) { handleOnPointerUp(lastKnownPointerEventRef.current); } }} /> ); }); Content.displayName = 'Drawer.Content'; export type HandleProps = React.ComponentPropsWithoutRef<'div'> & { preventCycle?: boolean; }; const LONG_HANDLE_PRESS_TIMEOUT = 250; const DOUBLE_TAP_TIMEOUT = 120; export const Handle = React.forwardRef(function ( { preventCycle = false, children, ...rest }, ref, ) { const { closeDrawer, isDragging, snapPoints, activeSnapPoint, setActiveSnapPoint, dismissible, handleOnly, isOpen, onPress, onDrag, } = useDrawerContext(); const closeTimeoutIdRef = React.useRef(null); const shouldCancelInteractionRef = React.useRef(false); function handleStartCycle() { // Stop if this is the second click of a double click if (shouldCancelInteractionRef.current) { handleCancelInteraction(); return; } window.setTimeout(() => { handleCycleSnapPoints(); }, DOUBLE_TAP_TIMEOUT); } function handleCycleSnapPoints() { // Prevent accidental taps while resizing drawer if (isDragging || preventCycle || shouldCancelInteractionRef.current) { handleCancelInteraction(); return; } // Make sure to clear the timeout id if the user releases the handle before the cancel timeout handleCancelInteraction(); if (!snapPoints || snapPoints.length === 0) { if (!dismissible) { closeDrawer(); } return; } const isLastSnapPoint = activeSnapPoint === snapPoints[snapPoints.length - 1]; if (isLastSnapPoint && dismissible) { closeDrawer(); return; } const currentSnapIndex = snapPoints.findIndex((point) => point === activeSnapPoint); if (currentSnapIndex === -1) return; // activeSnapPoint not found in snapPoints const nextSnapPoint = snapPoints[currentSnapIndex + 1]; setActiveSnapPoint(nextSnapPoint); } function handleStartInteraction() { closeTimeoutIdRef.current = window.setTimeout(() => { // Cancel click interaction on a long press shouldCancelInteractionRef.current = true; }, LONG_HANDLE_PRESS_TIMEOUT); } function handleCancelInteraction() { if (closeTimeoutIdRef.current) { window.clearTimeout(closeTimeoutIdRef.current); } shouldCancelInteractionRef.current = false; } return (
{ if (handleOnly) onPress(e); handleStartInteraction(); }} onPointerMove={(e) => { if (handleOnly) onDrag(e); }} // onPointerUp is already handled by the content component ref={ref} data-vaul-drawer-visible={isOpen ? 'true' : 'false'} data-vaul-handle="" aria-hidden="true" {...rest} > {/* Expand handle's hit area beyond what's visible to ensure a 44x44 tap target for touch devices */}
); }); Handle.displayName = 'Drawer.Handle'; export function NestedRoot({ onDrag, onOpenChange, open: nestedIsOpen, ...rest }: DialogProps) { const { onNestedDrag, onNestedOpenChange, onNestedRelease } = useDrawerContext(); if (!onNestedDrag) { throw new Error('Drawer.NestedRoot must be placed in another drawer'); } return ( { onNestedOpenChange(false); }} onDrag={(e, p) => { onNestedDrag(e, p); onDrag?.(e, p); }} onOpenChange={(o) => { if (o) { onNestedOpenChange(o); } onOpenChange?.(o); }} onRelease={onNestedRelease} {...rest} /> ); } type PortalProps = React.ComponentPropsWithoutRef; export function Portal(props: PortalProps) { const context = useDrawerContext(); const { container = context.container, ...portalProps } = props; return ; } export const Drawer = { Root, NestedRoot, Content, Overlay, Trigger: DialogPrimitive.Trigger, Portal, Handle, Close: DialogPrimitive.Close, Title: DialogPrimitive.Title, Description: DialogPrimitive.Description, }; ================================================ FILE: src/types.ts ================================================ export type DrawerDirection = 'top' | 'bottom' | 'left' | 'right'; export interface SnapPoint { fraction: number; height: number; } export type AnyFunction = (...args: any) => any; ================================================ FILE: src/use-composed-refs.ts ================================================ // This code comes from https://github.com/radix-ui/primitives/tree/main/packages/react/compose-refs import * as React from 'react'; type PossibleRef = React.Ref | undefined; /** * Set a given ref to a given value * This utility takes care of different types of refs: callback refs and RefObject(s) */ function setRef(ref: PossibleRef, value: T) { if (typeof ref === 'function') { ref(value); } else if (ref !== null && ref !== undefined) { (ref as React.MutableRefObject).current = value; } } /** * A utility to compose multiple refs together * Accepts callback refs and RefObject(s) */ function composeRefs(...refs: PossibleRef[]) { return (node: T) => refs.forEach((ref) => setRef(ref, node)); } /** * A custom hook that composes multiple refs * Accepts callback refs and RefObject(s) */ function useComposedRefs(...refs: PossibleRef[]) { // eslint-disable-next-line react-hooks/exhaustive-deps return React.useCallback(composeRefs(...refs), refs); } export { composeRefs, useComposedRefs }; ================================================ FILE: src/use-controllable-state.ts ================================================ // This code comes from https://github.com/radix-ui/primitives/blob/main/packages/react/use-controllable-state/src/useControllableState.tsx import React from 'react'; type UseControllableStateParams = { prop?: T | undefined; defaultProp?: T | undefined; onChange?: (state: T) => void; }; type SetStateFn = (prevState?: T) => T; function useCallbackRef any>(callback: T | undefined): T { const callbackRef = React.useRef(callback); React.useEffect(() => { callbackRef.current = callback; }); // https://github.com/facebook/react/issues/19240 return React.useMemo(() => ((...args) => callbackRef.current?.(...args)) as T, []); } function useUncontrolledState({ defaultProp, onChange }: Omit, 'prop'>) { const uncontrolledState = React.useState(defaultProp); const [value] = uncontrolledState; const prevValueRef = React.useRef(value); const handleChange = useCallbackRef(onChange); React.useEffect(() => { if (prevValueRef.current !== value) { handleChange(value as T); prevValueRef.current = value; } }, [value, prevValueRef, handleChange]); return uncontrolledState; } export function useControllableState({ prop, defaultProp, onChange = () => {} }: UseControllableStateParams) { const [uncontrolledProp, setUncontrolledProp] = useUncontrolledState({ defaultProp, onChange }); const isControlled = prop !== undefined; const value = isControlled ? prop : uncontrolledProp; const handleChange = useCallbackRef(onChange); const setValue: React.Dispatch> = React.useCallback( (nextValue) => { if (isControlled) { const setter = nextValue as SetStateFn; const value = typeof nextValue === 'function' ? setter(prop) : nextValue; if (value !== prop) handleChange(value as T); } else { setUncontrolledProp(nextValue); } }, [isControlled, prop, setUncontrolledProp, handleChange], ); return [value, setValue] as const; } ================================================ FILE: src/use-position-fixed.ts ================================================ import React from 'react'; import { isSafari } from './browser'; let previousBodyPosition: Record | null = null; /** * This hook is necessary to prevent buggy behavior on iOS devices (need to test on Android). * I won't get into too much detail about what bugs it solves, but so far I've found that setting the body to `position: fixed` is the most reliable way to prevent those bugs. * Issues that this hook solves: * https://github.com/emilkowalski/vaul/issues/435 * https://github.com/emilkowalski/vaul/issues/433 * And more that I discovered, but were just not reported. */ export function usePositionFixed({ isOpen, modal, nested, hasBeenOpened, preventScrollRestoration, noBodyStyles, }: { isOpen: boolean; modal: boolean; nested: boolean; hasBeenOpened: boolean; preventScrollRestoration: boolean; noBodyStyles: boolean; }) { const [activeUrl, setActiveUrl] = React.useState(() => (typeof window !== 'undefined' ? window.location.href : '')); const scrollPos = React.useRef(0); const setPositionFixed = React.useCallback(() => { // All browsers on iOS will return true here. if (!isSafari()) return; // If previousBodyPosition is already set, don't set it again. if (previousBodyPosition === null && isOpen && !noBodyStyles) { previousBodyPosition = { position: document.body.style.position, top: document.body.style.top, left: document.body.style.left, height: document.body.style.height, right: 'unset', }; // Update the dom inside an animation frame const { scrollX, innerHeight } = window; document.body.style.setProperty('position', 'fixed', 'important'); Object.assign(document.body.style, { top: `${-scrollPos.current}px`, left: `${-scrollX}px`, right: '0px', height: 'auto', }); window.setTimeout( () => window.requestAnimationFrame(() => { // Attempt to check if the bottom bar appeared due to the position change const bottomBarHeight = innerHeight - window.innerHeight; if (bottomBarHeight && scrollPos.current >= innerHeight) { // Move the content further up so that the bottom bar doesn't hide it document.body.style.top = `${-(scrollPos.current + bottomBarHeight)}px`; } }), 300, ); } }, [isOpen]); const restorePositionSetting = React.useCallback(() => { // All browsers on iOS will return true here. if (!isSafari()) return; if (previousBodyPosition !== null && !noBodyStyles) { // Convert the position from "px" to Int const y = -parseInt(document.body.style.top, 10); const x = -parseInt(document.body.style.left, 10); // Restore styles Object.assign(document.body.style, previousBodyPosition); window.requestAnimationFrame(() => { if (preventScrollRestoration && activeUrl !== window.location.href) { setActiveUrl(window.location.href); return; } window.scrollTo(x, y); }); previousBodyPosition = null; } }, [activeUrl]); React.useEffect(() => { function onScroll() { scrollPos.current = window.scrollY; } onScroll(); window.addEventListener('scroll', onScroll); return () => { window.removeEventListener('scroll', onScroll); }; }, []); React.useEffect(() => { if (!modal) return; return () => { if (typeof document === 'undefined') return; // Another drawer is opened, safe to ignore the execution const hasDrawerOpened = !!document.querySelector('[data-vaul-drawer]'); if (hasDrawerOpened) return; restorePositionSetting(); }; }, [modal, restorePositionSetting]); React.useEffect(() => { if (nested || !hasBeenOpened) return; // This is needed to force Safari toolbar to show **before** the drawer starts animating to prevent a gnarly shift from happening if (isOpen) { // avoid for standalone mode (PWA) const isStandalone = window.matchMedia('(display-mode: standalone)').matches; !isStandalone && setPositionFixed(); if (!modal) { window.setTimeout(() => { restorePositionSetting(); }, 500); } } else { restorePositionSetting(); } }, [isOpen, hasBeenOpened, activeUrl, modal, nested, setPositionFixed, restorePositionSetting]); return { restorePositionSetting }; } ================================================ FILE: src/use-prevent-scroll.ts ================================================ // This code comes from https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/overlays/src/usePreventScroll.ts import { useEffect, useLayoutEffect } from 'react'; import { isIOS } from './browser'; const KEYBOARD_BUFFER = 24; export const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect; interface PreventScrollOptions { /** Whether the scroll lock is disabled. */ isDisabled?: boolean; focusCallback?: () => void; } function chain(...callbacks: any[]): (...args: any[]) => void { return (...args: any[]) => { for (let callback of callbacks) { if (typeof callback === 'function') { callback(...args); } } }; } // @ts-ignore const visualViewport = typeof document !== 'undefined' && window.visualViewport; export function isScrollable(node: Element): boolean { let style = window.getComputedStyle(node); return /(auto|scroll)/.test(style.overflow + style.overflowX + style.overflowY); } export function getScrollParent(node: Element): Element { if (isScrollable(node)) { node = node.parentElement as HTMLElement; } while (node && !isScrollable(node)) { node = node.parentElement as HTMLElement; } return node || document.scrollingElement || document.documentElement; } // HTML input types that do not cause the software keyboard to appear. const nonTextInputTypes = new Set([ 'checkbox', 'radio', 'range', 'color', 'file', 'image', 'button', 'submit', 'reset', ]); // The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position let preventScrollCount = 0; let restore: () => void; /** * Prevents scrolling on the document body on mount, and * restores it on unmount. Also ensures that content does not * shift due to the scrollbars disappearing. */ export function usePreventScroll(options: PreventScrollOptions = {}) { let { isDisabled } = options; useIsomorphicLayoutEffect(() => { if (isDisabled) { return; } preventScrollCount++; if (preventScrollCount === 1) { if (isIOS()) { restore = preventScrollMobileSafari(); } } return () => { preventScrollCount--; if (preventScrollCount === 0) { restore?.(); } }; }, [isDisabled]); } // Mobile Safari is a whole different beast. Even with overflow: hidden, // it still scrolls the page in many situations: // // 1. When the bottom toolbar and address bar are collapsed, page scrolling is always allowed. // 2. When the keyboard is visible, the viewport does not resize. Instead, the keyboard covers part of // it, so it becomes scrollable. // 3. When tapping on an input, the page always scrolls so that the input is centered in the visual viewport. // This may cause even fixed position elements to scroll off the screen. // 4. When using the next/previous buttons in the keyboard to navigate between inputs, the whole page always // scrolls, even if the input is inside a nested scrollable element that could be scrolled instead. // // In order to work around these cases, and prevent scrolling without jankiness, we do a few things: // // 1. Prevent default on `touchmove` events that are not in a scrollable element. This prevents touch scrolling // on the window. // 2. Prevent default on `touchmove` events inside a scrollable element when the scroll position is at the // top or bottom. This avoids the whole page scrolling instead, but does prevent overscrolling. // 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves. // 4. When focusing an input, apply a transform to trick Safari into thinking the input is at the top // of the page, which prevents it from scrolling the page. After the input is focused, scroll the element // into view ourselves, without scrolling the whole page. // 5. Offset the body by the scroll position using a negative margin and scroll to the top. This should appear the // same visually, but makes the actual scroll position always zero. This is required to make all of the // above work or Safari will still try to scroll the page when focusing an input. // 6. As a last resort, handle window scroll events, and scroll back to the top. This can happen when attempting // to navigate to an input with the next/previous buttons that's outside a modal. function preventScrollMobileSafari() { let scrollable: Element; let lastY = 0; let onTouchStart = (e: TouchEvent) => { // Store the nearest scrollable parent element from the element that the user touched. scrollable = getScrollParent(e.target as Element); if (scrollable === document.documentElement && scrollable === document.body) { return; } lastY = e.changedTouches[0].pageY; }; let onTouchMove = (e: TouchEvent) => { // Prevent scrolling the window. if (!scrollable || scrollable === document.documentElement || scrollable === document.body) { e.preventDefault(); return; } // Prevent scrolling up when at the top and scrolling down when at the bottom // of a nested scrollable area, otherwise mobile Safari will start scrolling // the window instead. Unfortunately, this disables bounce scrolling when at // the top but it's the best we can do. let y = e.changedTouches[0].pageY; let scrollTop = scrollable.scrollTop; let bottom = scrollable.scrollHeight - scrollable.clientHeight; if (bottom === 0) { return; } if ((scrollTop <= 0 && y > lastY) || (scrollTop >= bottom && y < lastY)) { e.preventDefault(); } lastY = y; }; let onTouchEnd = (e: TouchEvent) => { let target = e.target as HTMLElement; // Apply this change if we're not already focused on the target element if (isInput(target) && target !== document.activeElement) { e.preventDefault(); // Apply a transform to trick Safari into thinking the input is at the top of the page // so it doesn't try to scroll it into view. When tapping on an input, this needs to // be done before the "focus" event, so we have to focus the element ourselves. target.style.transform = 'translateY(-2000px)'; target.focus(); requestAnimationFrame(() => { target.style.transform = ''; }); } }; let onFocus = (e: FocusEvent) => { let target = e.target as HTMLElement; if (isInput(target)) { // Transform also needs to be applied in the focus event in cases where focus moves // other than tapping on an input directly, e.g. the next/previous buttons in the // software keyboard. In these cases, it seems applying the transform in the focus event // is good enough, whereas when tapping an input, it must be done before the focus event. 🤷‍♂️ target.style.transform = 'translateY(-2000px)'; requestAnimationFrame(() => { target.style.transform = ''; // This will have prevented the browser from scrolling the focused element into view, // so we need to do this ourselves in a way that doesn't cause the whole page to scroll. if (visualViewport) { if (visualViewport.height < window.innerHeight) { // If the keyboard is already visible, do this after one additional frame // to wait for the transform to be removed. requestAnimationFrame(() => { scrollIntoView(target); }); } else { // Otherwise, wait for the visual viewport to resize before scrolling so we can // measure the correct position to scroll to. visualViewport.addEventListener('resize', () => scrollIntoView(target), { once: true }); } } }); } }; let onWindowScroll = () => { // Last resort. If the window scrolled, scroll it back to the top. // It should always be at the top because the body will have a negative margin (see below). window.scrollTo(0, 0); }; // Record the original scroll position so we can restore it. // Then apply a negative margin to the body to offset it by the scroll position. This will // enable us to scroll the window to the top, which is required for the rest of this to work. let scrollX = window.pageXOffset; let scrollY = window.pageYOffset; let restoreStyles = chain( setStyle(document.documentElement, 'paddingRight', `${window.innerWidth - document.documentElement.clientWidth}px`), // setStyle(document.documentElement, 'overflow', 'hidden'), // setStyle(document.body, 'marginTop', `-${scrollY}px`), ); // Scroll to the top. The negative margin on the body will make this appear the same. window.scrollTo(0, 0); let removeEvents = chain( addEvent(document, 'touchstart', onTouchStart, { passive: false, capture: true }), addEvent(document, 'touchmove', onTouchMove, { passive: false, capture: true }), addEvent(document, 'touchend', onTouchEnd, { passive: false, capture: true }), addEvent(document, 'focus', onFocus, true), addEvent(window, 'scroll', onWindowScroll), ); return () => { // Restore styles and scroll the page back to where it was. restoreStyles(); removeEvents(); window.scrollTo(scrollX, scrollY); }; } // Sets a CSS property on an element, and returns a function to revert it to the previous value. function setStyle(element: HTMLElement, style: keyof React.CSSProperties, value: string) { // https://github.com/microsoft/TypeScript/issues/17827#issuecomment-391663310 // @ts-ignore let cur = element.style[style]; // @ts-ignore element.style[style] = value; return () => { // @ts-ignore element.style[style] = cur; }; } // Adds an event listener to an element, and returns a function to remove it. function addEvent( target: EventTarget, event: K, handler: (this: Document, ev: GlobalEventHandlersEventMap[K]) => any, options?: boolean | AddEventListenerOptions, ) { // @ts-ignore target.addEventListener(event, handler, options); return () => { // @ts-ignore target.removeEventListener(event, handler, options); }; } function scrollIntoView(target: Element) { let root = document.scrollingElement || document.documentElement; while (target && target !== root) { // Find the parent scrollable element and adjust the scroll position if the target is not already in view. let scrollable = getScrollParent(target); if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== target) { let scrollableTop = scrollable.getBoundingClientRect().top; let targetTop = target.getBoundingClientRect().top; let targetBottom = target.getBoundingClientRect().bottom; // Buffer is needed for some edge cases const keyboardHeight = scrollable.getBoundingClientRect().bottom + KEYBOARD_BUFFER; if (targetBottom > keyboardHeight) { scrollable.scrollTop += targetTop - scrollableTop; } } // @ts-ignore target = scrollable.parentElement; } } export function isInput(target: Element) { return ( (target instanceof HTMLInputElement && !nonTextInputTypes.has(target.type)) || target instanceof HTMLTextAreaElement || (target instanceof HTMLElement && target.isContentEditable) ); } ================================================ FILE: src/use-scale-background.ts ================================================ import React, { useMemo } from 'react'; import { useDrawerContext } from './context'; import { assignStyle, chain, isVertical, reset } from './helpers'; import { BORDER_RADIUS, TRANSITIONS, WINDOW_TOP_OFFSET } from './constants'; const noop = () => () => {}; export function useScaleBackground() { const { direction, isOpen, shouldScaleBackground, setBackgroundColorOnScale, noBodyStyles } = useDrawerContext(); const timeoutIdRef = React.useRef(null); const initialBackgroundColor = useMemo(() => document.body.style.backgroundColor, []); function getScale() { return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth; } React.useEffect(() => { if (isOpen && shouldScaleBackground) { if (timeoutIdRef.current) clearTimeout(timeoutIdRef.current); const wrapper = (document.querySelector('[data-vaul-drawer-wrapper]') as HTMLElement) || (document.querySelector('[vaul-drawer-wrapper]') as HTMLElement); if (!wrapper) return; chain( setBackgroundColorOnScale && !noBodyStyles ? assignStyle(document.body, { background: 'black' }) : noop, assignStyle(wrapper, { transformOrigin: isVertical(direction) ? 'top' : 'left', transitionProperty: 'transform, border-radius', transitionDuration: `${TRANSITIONS.DURATION}s`, transitionTimingFunction: `cubic-bezier(${TRANSITIONS.EASE.join(',')})`, }), ); const wrapperStylesCleanup = assignStyle(wrapper, { borderRadius: `${BORDER_RADIUS}px`, overflow: 'hidden', ...(isVertical(direction) ? { transform: `scale(${getScale()}) translate3d(0, calc(env(safe-area-inset-top) + 14px), 0)`, } : { transform: `scale(${getScale()}) translate3d(calc(env(safe-area-inset-top) + 14px), 0, 0)`, }), }); return () => { wrapperStylesCleanup(); timeoutIdRef.current = window.setTimeout(() => { if (initialBackgroundColor) { document.body.style.background = initialBackgroundColor; } else { document.body.style.removeProperty('background'); } }, TRANSITIONS.DURATION * 1000); }; } }, [isOpen, shouldScaleBackground, initialBackgroundColor]); } ================================================ FILE: src/use-snap-points.ts ================================================ import React from 'react'; import { set, isVertical } from './helpers'; import { TRANSITIONS, VELOCITY_THRESHOLD } from './constants'; import { useControllableState } from './use-controllable-state'; import { DrawerDirection } from './types'; export function useSnapPoints({ activeSnapPointProp, setActiveSnapPointProp, snapPoints, drawerRef, overlayRef, fadeFromIndex, onSnapPointChange, direction = 'bottom', container, snapToSequentialPoint, }: { activeSnapPointProp?: number | string | null; setActiveSnapPointProp?(snapPoint: number | null | string): void; snapPoints?: (number | string)[]; fadeFromIndex?: number; drawerRef: React.RefObject; overlayRef: React.RefObject; onSnapPointChange(activeSnapPointIndex: number): void; direction?: DrawerDirection; container?: HTMLElement | null | undefined; snapToSequentialPoint?: boolean; }) { const [activeSnapPoint, setActiveSnapPoint] = useControllableState({ prop: activeSnapPointProp, defaultProp: snapPoints?.[0], onChange: setActiveSnapPointProp, }); const [windowDimensions, setWindowDimensions] = React.useState( typeof window !== 'undefined' ? { innerWidth: window.innerWidth, innerHeight: window.innerHeight, } : undefined, ); React.useEffect(() => { function onResize() { setWindowDimensions({ innerWidth: window.innerWidth, innerHeight: window.innerHeight, }); } window.addEventListener('resize', onResize); return () => window.removeEventListener('resize', onResize); }, []); const isLastSnapPoint = React.useMemo( () => activeSnapPoint === snapPoints?.[snapPoints.length - 1] || null, [snapPoints, activeSnapPoint], ); const activeSnapPointIndex = React.useMemo( () => snapPoints?.findIndex((snapPoint) => snapPoint === activeSnapPoint) ?? null, [snapPoints, activeSnapPoint], ); const shouldFade = (snapPoints && snapPoints.length > 0 && (fadeFromIndex || fadeFromIndex === 0) && !Number.isNaN(fadeFromIndex) && snapPoints[fadeFromIndex] === activeSnapPoint) || !snapPoints; const snapPointsOffset = React.useMemo(() => { const containerSize = container ? { width: container.getBoundingClientRect().width, height: container.getBoundingClientRect().height } : typeof window !== 'undefined' ? { width: window.innerWidth, height: window.innerHeight } : { width: 0, height: 0 }; return ( snapPoints?.map((snapPoint) => { const isPx = typeof snapPoint === 'string'; let snapPointAsNumber = 0; if (isPx) { snapPointAsNumber = parseInt(snapPoint, 10); } if (isVertical(direction)) { const height = isPx ? snapPointAsNumber : windowDimensions ? snapPoint * containerSize.height : 0; if (windowDimensions) { return direction === 'bottom' ? containerSize.height - height : -containerSize.height + height; } return height; } const width = isPx ? snapPointAsNumber : windowDimensions ? snapPoint * containerSize.width : 0; if (windowDimensions) { return direction === 'right' ? containerSize.width - width : -containerSize.width + width; } return width; }) ?? [] ); }, [snapPoints, windowDimensions, container]); const activeSnapPointOffset = React.useMemo( () => (activeSnapPointIndex !== null ? snapPointsOffset?.[activeSnapPointIndex] : null), [snapPointsOffset, activeSnapPointIndex], ); const snapToPoint = React.useCallback( (dimension: number) => { const newSnapPointIndex = snapPointsOffset?.findIndex((snapPointDim) => snapPointDim === dimension) ?? null; onSnapPointChange(newSnapPointIndex); set(drawerRef.current, { transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`, transform: isVertical(direction) ? `translate3d(0, ${dimension}px, 0)` : `translate3d(${dimension}px, 0, 0)`, }); if ( snapPointsOffset && newSnapPointIndex !== snapPointsOffset.length - 1 && fadeFromIndex !== undefined && newSnapPointIndex !== fadeFromIndex && newSnapPointIndex < fadeFromIndex ) { set(overlayRef.current, { transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`, opacity: '0', }); } else { set(overlayRef.current, { transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`, opacity: '1', }); } setActiveSnapPoint(snapPoints?.[Math.max(newSnapPointIndex, 0)]); }, [drawerRef.current, snapPoints, snapPointsOffset, fadeFromIndex, overlayRef, setActiveSnapPoint], ); React.useEffect(() => { if (activeSnapPoint || activeSnapPointProp) { const newIndex = snapPoints?.findIndex((snapPoint) => snapPoint === activeSnapPointProp || snapPoint === activeSnapPoint) ?? -1; if (snapPointsOffset && newIndex !== -1 && typeof snapPointsOffset[newIndex] === 'number') { snapToPoint(snapPointsOffset[newIndex] as number); } } }, [activeSnapPoint, activeSnapPointProp, snapPoints, snapPointsOffset, snapToPoint]); function onRelease({ draggedDistance, closeDrawer, velocity, dismissible, }: { draggedDistance: number; closeDrawer: () => void; velocity: number; dismissible: boolean; }) { if (fadeFromIndex === undefined) return; const currentPosition = direction === 'bottom' || direction === 'right' ? (activeSnapPointOffset ?? 0) - draggedDistance : (activeSnapPointOffset ?? 0) + draggedDistance; const isOverlaySnapPoint = activeSnapPointIndex === fadeFromIndex - 1; const isFirst = activeSnapPointIndex === 0; const hasDraggedUp = draggedDistance > 0; if (isOverlaySnapPoint) { set(overlayRef.current, { transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`, }); } if (!snapToSequentialPoint && velocity > 2 && !hasDraggedUp) { if (dismissible) closeDrawer(); else snapToPoint(snapPointsOffset[0]); // snap to initial point return; } if (!snapToSequentialPoint && velocity > 2 && hasDraggedUp && snapPointsOffset && snapPoints) { snapToPoint(snapPointsOffset[snapPoints.length - 1] as number); return; } // Find the closest snap point to the current position const closestSnapPoint = snapPointsOffset?.reduce((prev, curr) => { if (typeof prev !== 'number' || typeof curr !== 'number') return prev; return Math.abs(curr - currentPosition) < Math.abs(prev - currentPosition) ? curr : prev; }); const dim = isVertical(direction) ? window.innerHeight : window.innerWidth; if (velocity > VELOCITY_THRESHOLD && Math.abs(draggedDistance) < dim * 0.4) { const dragDirection = hasDraggedUp ? 1 : -1; // 1 = up, -1 = down // Don't do anything if we swipe upwards while being on the last snap point if (dragDirection > 0 && isLastSnapPoint && snapPoints) { snapToPoint(snapPointsOffset[snapPoints.length - 1]); return; } if (isFirst && dragDirection < 0 && dismissible) { closeDrawer(); } if (activeSnapPointIndex === null) return; snapToPoint(snapPointsOffset[activeSnapPointIndex + dragDirection]); return; } snapToPoint(closestSnapPoint); } function onDrag({ draggedDistance }: { draggedDistance: number }) { if (activeSnapPointOffset === null) return; const newValue = direction === 'bottom' || direction === 'right' ? activeSnapPointOffset - draggedDistance : activeSnapPointOffset + draggedDistance; // Don't do anything if we exceed the last(biggest) snap point if ((direction === 'bottom' || direction === 'right') && newValue < snapPointsOffset[snapPointsOffset.length - 1]) { return; } if ((direction === 'top' || direction === 'left') && newValue > snapPointsOffset[snapPointsOffset.length - 1]) { return; } set(drawerRef.current, { transform: isVertical(direction) ? `translate3d(0, ${newValue}px, 0)` : `translate3d(${newValue}px, 0, 0)`, }); } function getPercentageDragged(absDraggedDistance: number, isDraggingDown: boolean) { if (!snapPoints || typeof activeSnapPointIndex !== 'number' || !snapPointsOffset || fadeFromIndex === undefined) return null; // If this is true we are dragging to a snap point that is supposed to have an overlay const isOverlaySnapPoint = activeSnapPointIndex === fadeFromIndex - 1; const isOverlaySnapPointOrHigher = activeSnapPointIndex >= fadeFromIndex; if (isOverlaySnapPointOrHigher && isDraggingDown) { return 0; } // Don't animate, but still use this one if we are dragging away from the overlaySnapPoint if (isOverlaySnapPoint && !isDraggingDown) return 1; if (!shouldFade && !isOverlaySnapPoint) return null; // Either fadeFrom index or the one before const targetSnapPointIndex = isOverlaySnapPoint ? activeSnapPointIndex + 1 : activeSnapPointIndex - 1; // Get the distance from overlaySnapPoint to the one before or vice-versa to calculate the opacity percentage accordingly const snapPointDistance = isOverlaySnapPoint ? snapPointsOffset[targetSnapPointIndex] - snapPointsOffset[targetSnapPointIndex - 1] : snapPointsOffset[targetSnapPointIndex + 1] - snapPointsOffset[targetSnapPointIndex]; const percentageDragged = absDraggedDistance / Math.abs(snapPointDistance); if (isOverlaySnapPoint) { return 1 - percentageDragged; } else { return percentageDragged; } } return { isLastSnapPoint, activeSnapPoint, shouldFade, getPercentageDragged, setActiveSnapPoint, activeSnapPointIndex, onRelease, onDrag, snapPointsOffset, }; } ================================================ FILE: test/.eslintrc.json ================================================ { "extends": "next/core-web-vitals" } ================================================ FILE: test/.gitignore ================================================ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. # dependencies /node_modules /.pnp .pnp.js # testing /coverage # next.js /.next/ /out/ # production /build # misc .DS_Store *.pem # debug npm-debug.log* yarn-debug.log* yarn-error.log* # local env files .env*.local # vercel .vercel # typescript *.tsbuildinfo next-env.d.ts ================================================ FILE: test/README.md ================================================ This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). ## Getting Started First, run the development server: ```bash npm run dev # or yarn dev # or pnpm dev ``` Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font. ## Learn More To learn more about Next.js, take a look at the following resources: - [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. - [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! ## Deploy on Vercel The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. ================================================ FILE: test/next.config.js ================================================ /** @type {import('next').NextConfig} */ const nextConfig = { experimental: { typedRoutes: true, }, }; module.exports = nextConfig; ================================================ FILE: test/package.json ================================================ { "name": "test", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" }, "devDependencies": { "@types/node": "20.5.7", "@types/react": "18.2.55", "@types/react-dom": "18.2.18", "autoprefixer": "10.4.15", "eslint": "8.48.0", "eslint-config-next": "13.5.1", "postcss": "8.4.29", "typescript": "5.2.2" }, "dependencies": { "clsx": "^2.0.0", "next": "13.5.1", "react": "18.2.0", "react-dom": "18.2.0", "tailwindcss": "3.3.3", "vaul": "workspace:^" } } ================================================ FILE: test/postcss.config.js ================================================ module.exports = { plugins: { tailwindcss: {}, autoprefixer: {}, }, }; ================================================ FILE: test/src/app/controlled/page.tsx ================================================ 'use client'; import { useState } from 'react'; import { Drawer } from 'vaul'; export default function Page() { const [open, setOpen] = useState(false); const [fullyControlled, setFullyControlled] = useState(false); return (
setOpen(true)}> Close
Unstyled drawer for React.

This component can be used as a replacement for a Dialog on mobile and tablet devices.

It uses{' '} Radix's Dialog primitive {' '} under the hood and is inspired by{' '} this tweet.

setFullyControlled(o)}> Close
Unstyled drawer for React.

This component can be used as a replacement for a Dialog on mobile and tablet devices.

It uses{' '} Radix's Dialog primitive {' '} under the hood and is inspired by{' '} this tweet.

); } ================================================ FILE: test/src/app/default-open/page.tsx ================================================ 'use client'; import { useState } from 'react'; import { Drawer } from 'vaul'; export default function Page() { return (
Close
Unstyled drawer for React.

This component can be used as a replacement for a Dialog on mobile and tablet devices.

It uses{' '} Radix's Dialog primitive {' '} under the hood and is inspired by{' '} this tweet.

Close
Unstyled drawer for React.

This component can be used as a replacement for a Dialog on mobile and tablet devices.

It uses{' '} Radix's Dialog primitive {' '} under the hood and is inspired by{' '} this tweet.

); } ================================================ FILE: test/src/app/different-directions/page.tsx ================================================ 'use client'; import clsx from 'clsx'; import { Drawer, DialogProps } from 'vaul'; function DirectionalDrawer({ direction, children, }: { direction: DialogProps['direction']; children: React.ReactNode; }) { return ( Close
Unstyled drawer for React.

This component can be used as a replacement for a Dialog on mobile and tablet devices.

It uses{' '} Radix's Dialog primitive {' '} under the hood and is inspired by{' '} this tweet.

); } export default function Page() { return (
Top Right Bottom Left
); } ================================================ FILE: test/src/app/globals.css ================================================ @tailwind base; @tailwind components; @tailwind utilities; body, main { min-height: 500vh; } html { height: -webkit-fill-available; } a { text-decoration-thickness: 1px; text-underline-offset: 2px; text-decoration: underline; } ================================================ FILE: test/src/app/initial-snap/page.tsx ================================================ 'use client'; import { clsx } from 'clsx'; import { useState } from 'react'; import { Drawer } from 'vaul'; const snapPoints = [0, '148px', '355px', 1]; export default function Page() { const [snap, setSnap] = useState(snapPoints[1]); const activeSnapPointIndex = snapPoints.indexOf(snap as string); return (
{activeSnapPointIndex}
{' '}

The Hidden Details

2 modules, 27 hours of video

The world of user interface design is an intricate landscape filled with hidden details and nuance. In this course, you will learn something cool. To the untrained eye, a beautifully designed UI.

Module 01. The Details

Layers of UI A basic introduction to Layers of Design.
Typography The fundamentals of type.
UI Animations Going through the right easings and durations.
“I especially loved the hidden details video. That was so useful, learned a lot by just reading it. Can’t wait for more course content!”
Yvonne Ray, Frontend Developer

Module 02. The Process

Build Create cool components to practice.
User Insight Find out what users think and fine-tune.
Putting it all together Let's build an app together and apply everything.
); } ================================================ FILE: test/src/app/layout.tsx ================================================ import './globals.css'; import type { Metadata } from 'next'; import { Inter } from 'next/font/google'; const inter = Inter({ subsets: ['latin'] }); export const metadata: Metadata = { title: 'Create Next App', description: 'Generated by create next app', }; export default function RootLayout({ children }: { children: React.ReactNode }) { return ( {children} ); } ================================================ FILE: test/src/app/nested-drawers/page.tsx ================================================ 'use client'; import { Drawer } from 'vaul'; export default function Page() { return (
Drawer for React.

This component can be used as a Dialog replacement on mobile and tablet devices.

It comes unstyled and has gesture-driven animations.

It uses{' '} Radix’s Dialog primitive {' '} under the hood and is inspired by{' '} this tweet.

Open Second Drawer Close
This drawer is nested.

Place a `Drawer.NestedRoot`{' '} inside another drawer and it will be nested automatically for you.

You can view more examples{' '} here .

); } ================================================ FILE: test/src/app/non-dismissible/page.tsx ================================================ 'use client'; import { useState } from 'react'; import { Drawer } from 'vaul'; export default function Page() { const [open, setOpen] = useState(false); return (
setOpen(true)}>
Unstyled drawer for React.

This component can be used as a replacement for a Dialog on mobile and tablet devices.

It uses{' '} Radix’s Dialog primitive {' '} under the hood and is inspired by{' '} this tweet.

); } ================================================ FILE: test/src/app/open-another-drawer/page.tsx ================================================ 'use client'; import { Drawer } from 'vaul'; import { useState } from 'react'; export function MyDrawer({ open, setOpen, setOpen2, }: { open: boolean; setOpen: (open: boolean) => void; setOpen2: (open: boolean) => void; }) { return ( setOpen(true)}>
Unstyled drawer for React.
); } export function MyDrawer2({ open, setOpen }: { open: boolean; setOpen: (open: boolean) => void }) { return (
Unstyled drawer for React.

This component can be used as a replacement for a Dialog on mobile and tablet devices.

It uses{' '} Radix’s Dialog primitive {' '} under the hood and is inspired by{' '} this tweet.

); } export default function Home() { const [open, setOpen] = useState(false); const [open2, setOpen2] = useState(false); return (

scroll down

scroll down

); } ================================================ FILE: test/src/app/page.tsx ================================================ 'use client'; import Link from 'next/link'; export default function Page() { return (
With scaled background Without scaled background With snap points With modal false Scrollable with inputs Nested drawers Non-dismissible Initial snap Controlled Default open With redirect Different directions
); } ================================================ FILE: test/src/app/parent-container/page.tsx ================================================ 'use client'; import clsx from 'clsx'; import { useState } from 'react'; import { Drawer } from 'vaul'; export default function Page() { return (
); } function Default() { const [parent, setParent] = useState(null); return (

Default

Open Drawer Unstyled drawer for React.
); } function WithNested() { const [parent, setParent] = useState(null); return (

With Nested

Open Drawer Unstyled drawer for React. Open nested drawer Unstyled drawer for React.
); } ================================================ FILE: test/src/app/scrollable-page/page.tsx ================================================ 'use client'; import { useState } from 'react'; import { Drawer } from 'vaul'; export default function Page() { const [open, setOpen] = useState(false); return (

Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas faucibus mollis interdum.

Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas faucibus mollis interdum.

Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas faucibus mollis interdum.

Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas faucibus mollis interdum.

Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas faucibus mollis interdum.

Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas faucibus mollis interdum.

Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas faucibus mollis interdum.

Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas faucibus mollis interdum.

Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas faucibus mollis interdum.

Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas faucibus mollis interdum.

Aenean lacinia bibendum nulla sed consectetur. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas faucibus mollis interdum.

setOpen(true)}>
Unstyled drawer for React.

This component can be used as a replacement for a Dialog on mobile and tablet devices.

It uses{' '} Radix's Dialog primitive {' '} under the hood and is inspired by{' '} this tweet.

); } ================================================ FILE: test/src/app/scrollable-with-inputs/page.tsx ================================================ 'use client'; import { Drawer } from 'vaul'; export default function Page() { return (

But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?

On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.

); } ================================================ FILE: test/src/app/with-handle/page.tsx ================================================ 'use client'; import { clsx } from 'clsx'; import { useState } from 'react'; import { Drawer } from 'vaul'; const snapPoints = ['148px', '355px']; export default function Page() { const [snap, setSnap] = useState(snapPoints[0]); const activeSnapPointIndex = snapPoints.indexOf(snap as string); return (
{activeSnapPointIndex}
{' '}

The Hidden Details

2 modules, 27 hours of video

The world of user interface design is an intricate landscape filled with hidden details and nuance. In this course, you will learn something cool. To the untrained eye, a beautifully designed UI.

Module 01. The Details

Layers of UI A basic introduction to Layers of Design.
Typography The fundamentals of type.
UI Animations Going through the right easings and durations.
“I especially loved the hidden details video. That was so useful, learned a lot by just reading it. Can’t wait for more course content!”
Yvonne Ray, Frontend Developer

Module 02. The Process

Build Create cool components to practice.
User Insight Find out what users think and fine-tune.
Putting it all together Let's build an app together and apply everything.
); } ================================================ FILE: test/src/app/with-modal-false/page.tsx ================================================ 'use client'; import { clsx } from 'clsx'; import { useState } from 'react'; import { Drawer } from 'vaul'; const snapPoints = ['148px', '355px', 1]; export default function Page() { const [snap, setSnap] = useState(snapPoints[0]); const activeSnapPointIndex = snapPoints.indexOf(snap as string); return (
{activeSnapPointIndex}
{' '}

The Hidden Details

2 modules, 27 hours of video

The world of user interface design is an intricate landscape filled with hidden details and nuance. In this course, you will learn something cool. To the untrained eye, a beautifully designed UI.

Module 01. The Details

Layers of UI A basic introduction to Layers of Design.
Typography The fundamentals of type.
UI Animations Going through the right easings and durations.
“I especially loved the hidden details video. That was so useful, learned a lot by just reading it. Can’t wait for more course content!”
Yvonne Ray, Frontend Developer

Module 02. The Process

Build Create cool components to practice.
User Insight Find out what users think and fine-tune.
Putting it all together Let's build an app together and apply everything.
); } ================================================ FILE: test/src/app/with-redirect/long-page/page.tsx ================================================ 'use client'; export default function Page() { return (

scroll down

content only visible after scroll

); } ================================================ FILE: test/src/app/with-redirect/page.tsx ================================================ 'use client'; import Link from 'next/link'; import { Drawer } from 'vaul'; export default function Page() { return (
Close
Redirect to another route.

This route is only used to test the body reset position.

Go to{' '} another route {' '}

); } ================================================ FILE: test/src/app/with-scaled-background/page.tsx ================================================ 'use client'; import { useState } from 'react'; import clsx from 'clsx'; import { Drawer } from 'vaul'; import { DrawerDirection } from 'vaul/src/types'; const CenteredContent = () => { return (
Unstyled drawer for React.

This component can be used as a replacement for a Dialog on mobile and tablet devices.

It uses{' '} Radix's Dialog primitive {' '} under the hood and is inspired by{' '} this tweet.

); }; const DrawerContent = ({ drawerDirection }: { drawerDirection: DrawerDirection }) => { return (
); }; export default function Page() { const [direction, setDirection] = useState('bottom'); return (
); } ================================================ FILE: test/src/app/with-snap-points/page.tsx ================================================ 'use client'; import { clsx } from 'clsx'; import { useState } from 'react'; import { Drawer } from 'vaul'; const snapPoints = ['148px', '355px', 1]; export default function Page() { const [snap, setSnap] = useState(snapPoints[0]); const activeSnapPointIndex = snapPoints.indexOf(snap as string); return (
{activeSnapPointIndex}
{' '}

The Hidden Details

2 modules, 27 hours of video

The world of user interface design is an intricate landscape filled with hidden details and nuance. In this course, you will learn something cool. To the untrained eye, a beautifully designed UI.

Module 01. The Details

Layers of UI A basic introduction to Layers of Design.
Typography The fundamentals of type.
UI Animations Going through the right easings and durations.
“I especially loved the hidden details video. That was so useful, learned a lot by just reading it. Can’t wait for more course content!”
Yvonne Ray, Frontend Developer

Module 02. The Process

Build Create cool components to practice.
User Insight Find out what users think and fine-tune.
Putting it all together Let's build an app together and apply everything.
); } ================================================ FILE: test/src/app/without-scaled-background/page.tsx ================================================ 'use client'; import { useState } from 'react'; import { Drawer } from 'vaul'; export default function Page() { const [open, setOpen] = useState(false); const [parent, setParent] = useState(null); return (
Close
Unstyled drawer for React.

This component can be used as a replacement for a Dialog on mobile and tablet devices.

It uses{' '} Radix's Dialog primitive {' '} under the hood and is inspired by{' '} this tweet.

); } ================================================ FILE: test/tailwind.config.ts ================================================ import type { Config } from 'tailwindcss'; const config: Config = { content: [ './src/pages/**/*.{js,ts,jsx,tsx,mdx}', './src/components/**/*.{js,ts,jsx,tsx,mdx}', './src/app/**/*.{js,ts,jsx,tsx,mdx}', ], theme: { extend: { backgroundImage: { 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 'gradient-conic': 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', }, }, }, plugins: [], }; export default config; ================================================ FILE: test/tests/base.spec.ts ================================================ import { test, expect } from '@playwright/test'; import { ANIMATION_DURATION } from './constants'; import { openDrawer } from './helpers'; test.beforeEach(async ({ page }) => { await page.goto('/without-scaled-background'); }); test.describe('Base tests', () => { test('should open drawer', async ({ page }) => { await expect(page.getByTestId('content')).not.toBeVisible(); await page.getByTestId('trigger').click(); await expect(page.getByTestId('content')).toBeVisible(); }); test('should close on background interaction', async ({ page }) => { await openDrawer(page); // Click on the background await page.mouse.click(0, 0); await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('content')).not.toBeVisible(); }); test('should close when `Drawer.Close` is clicked', async ({ page }) => { await openDrawer(page); await page.getByTestId('drawer-close').click(); await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('content')).not.toBeVisible(); }); test('should close when controlled', async ({ page }) => { await openDrawer(page); await page.getByTestId('controlled-close').click(); await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('content')).not.toBeVisible(); }); test('should be open by defafult when `defaultOpen` is true', async ({ page }) => { await page.goto('/default-open'); await expect(page.getByTestId('content')).toBeVisible(); }); test('should close when dragged down', async ({ page }) => { await openDrawer(page); await page.hover('[data-vaul-drawer]'); await page.mouse.down(); await page.mouse.move(0, 800); await page.mouse.up(); await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('content')).not.toBeVisible(); }); test('should not close when dragged up', async ({ page }) => { await openDrawer(page); await page.hover('[data-vaul-drawer]'); await page.mouse.down(); await page.mouse.move(0, -800); await page.mouse.up(); await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('content')).toBeVisible(); }); }); test('should close when dragged down and cancelled', async ({ page }) => { await openDrawer(page); await page.hover('[data-vaul-drawer]'); await page.mouse.down(); await page.mouse.move(0, 800); await page.dispatchEvent('[data-vaul-drawer]', 'contextmenu'); await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('content')).not.toBeVisible(); }); ================================================ FILE: test/tests/constants.ts ================================================ export const ANIMATION_DURATION = 500; ================================================ FILE: test/tests/controlled.spec.ts ================================================ import { expect, test } from '@playwright/test'; import { ANIMATION_DURATION } from './constants'; test.beforeEach(async ({ page }) => { await page.goto('/controlled'); }); test.describe('Initial-snap', () => { test('should not close when clicked on overlay and only the open prop is passsed', async ({ page }) => { await expect(page.getByTestId('content')).not.toBeVisible(); await page.getByTestId('trigger').click(); await expect(page.getByTestId('content')).toBeVisible(); // Click on the background await page.mouse.click(0, 0); await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('content')).toBeVisible(); }); test('should close when clicked on overlay and open and onOpenChange props are passed', async ({ page }) => { await expect(page.getByTestId('fully-controlled-content')).not.toBeVisible(); await page.getByTestId('fully-controlled-trigger').click(); await expect(page.getByTestId('fully-controlled-content')).toBeVisible(); // Click on the background await page.mouse.click(0, 0); await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('fully-controlled-content')).not.toBeVisible(); }); }); ================================================ FILE: test/tests/helpers.ts ================================================ import { expect, Page } from '@playwright/test'; import { ANIMATION_DURATION } from './constants'; export async function openDrawer(page: Page) { await expect(page.getByTestId('content')).not.toBeVisible(); await page.getByTestId('trigger').click(); await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('content')).toBeVisible(); } export async function dragWithSpeed( page: Page, selector: string, startY: number, endY: number, speed: number = 10, ): Promise { const startX = 0; const distance = Math.abs(endY - startY); const steps = distance / speed; const delayPerStep = 10; // in milliseconds const yOffset = (endY - startY) / steps; await page.hover(selector); await page.mouse.down(); await page.mouse.move(0, -200); await page.mouse.up(); } ================================================ FILE: test/tests/initial-snap.spec.ts ================================================ import { Page, expect, test } from '@playwright/test'; import { ANIMATION_DURATION } from './constants'; test.beforeEach(async ({ page }) => { await page.goto('/initial-snap'); }); const snapPointYPositions = { 0: 800, 1: 600, 2: 590, 3: 200, } as const; const snapTo = async (page: Page, snapPointIndex: keyof typeof snapPointYPositions) => { await page.hover('[data-vaul-drawer]'); await page.mouse.down(); await page.mouse.move(0, snapPointYPositions[snapPointIndex]); await page.mouse.up(); await page.waitForTimeout(ANIMATION_DURATION); }; test.describe('Initial-snap', () => { test('should be open and snapped on initial load', async ({ page }) => { await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('content')).toBeVisible(); await expect(page.getByTestId('active-snap-index')).toHaveText('1'); }); // test('should snap to next snap point when dragged up', async ({ page }) => { // snapTo(page, 2); // await expect(page.getByTestId('active-snap-index')).toHaveText('2'); // }); // test('should snap to last snap point when dragged up', async ({ page }) => { // snapTo(page, 3); // await expect(page.getByTestId('active-snap-index')).toHaveText('3'); // }); // test('should snap to 0 when dragged down', async ({ page }) => { // snapTo(page, 0); // await expect(page.getByTestId('active-snap-index')).toHaveText('0'); // }); }); ================================================ FILE: test/tests/nested.spec.ts ================================================ import { test, expect } from '@playwright/test'; import { ANIMATION_DURATION } from './constants'; import { openDrawer } from './helpers'; test.beforeEach(async ({ page }) => { await page.goto('/nested-drawers'); }); test.describe('Nested tests', () => { test('should open and close nested drawer', async ({ page }) => { await openDrawer(page); await page.getByTestId('nested-trigger').click(); await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('nested-content')).toBeVisible(); await page.getByTestId('nested-close').click(); await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('nested-content')).not.toBeVisible(); await await expect(page.getByTestId('content')).toBeVisible(); }); }); ================================================ FILE: test/tests/non-dismissible.spec.ts ================================================ import { test, expect } from '@playwright/test'; import { openDrawer } from './helpers'; import { ANIMATION_DURATION } from './constants'; test.beforeEach(async ({ page }) => { await page.goto('/non-dismissible'); }); test.describe('Non-dismissible', () => { test('should not close on background interaction', async ({ page }) => { await openDrawer(page); // Click on the background await page.mouse.click(0, 0); await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('content')).toBeVisible(); }); test('should not close when dragged down', async ({ page }) => { await openDrawer(page); await page.hover('[data-vaul-drawer]'); await page.mouse.down(); await page.mouse.move(0, 800); await page.mouse.up(); await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('content')).toBeVisible(); }); test('should close when the dismiss button is clicked', async ({ page }) => { await openDrawer(page); await page.getByTestId('dismiss-button').click(); await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('content')).not.toBeVisible(); }); }); ================================================ FILE: test/tests/with-handle.spec.ts ================================================ import { test, expect } from '@playwright/test'; import { ANIMATION_DURATION } from './constants'; test.beforeEach(async ({ page }) => { await page.goto('/with-handle'); }); test.describe('With handle', () => { test('click should cycle to the next snap point', async ({ page }) => { await page.waitForTimeout(ANIMATION_DURATION); await expect(page.getByTestId('content')).toBeVisible(); await expect(page.getByTestId('active-snap-index')).toHaveText('0'); await page.getByTestId('handle').click(); await expect(page.getByTestId('active-snap-index')).toHaveText('1'); }); }); ================================================ FILE: test/tests/with-redirect.spec.ts ================================================ import { test, expect } from '@playwright/test'; import { openDrawer } from './helpers'; test.beforeEach(async ({ page }) => { await page.goto('/with-redirect'); }); test.describe('With redirect', () => { test('should restore body position settings', async ({ page }) => { await openDrawer(page); await page.getByTestId('link').click(); await page.waitForURL('**/with-redirect/long-page'); const content = page.getByTestId('content'); // safe check await expect(content).toBeVisible(); content.scrollIntoViewIfNeeded(); await expect(page.getByTestId('content')).toBeInViewport(); }); }); ================================================ FILE: test/tests/with-scaled-background.spec.ts ================================================ import { test, expect } from '@playwright/test'; import { openDrawer } from './helpers'; test.beforeEach(async ({ page }) => { await page.goto('/with-scaled-background'); }); test.describe('With scaled background', () => { test('should scale background', async ({ page }) => { await expect(page.locator('[data-vaul-drawer-wrapper]')).not.toHaveCSS('transform', ''); await page.getByTestId('trigger').click(); await expect(page.locator('[data-vaul-drawer-wrapper]')).toHaveCSS('transform', /matrix/); }); test('should scale background when dragging', async ({ page }) => { await expect(page.locator('[data-vaul-drawer-wrapper]')).not.toHaveCSS('transform', ''); await openDrawer(page); await page.hover('[data-vaul-drawer]'); await page.mouse.down(); await page.mouse.move(0, 100); await expect(page.locator('[data-vaul-drawer-wrapper]')).toHaveCSS('transform', /matrix/); await page.mouse.up(); await expect(page.locator('[data-vaul-drawer-wrapper]')).not.toHaveCSS('transform', ''); }); }); ================================================ FILE: test/tests/without-scaled-background.spec.ts ================================================ import { test, expect } from '@playwright/test'; test.beforeEach(async ({ page }) => { await page.goto('/without-scaled-background'); }); test.describe('Without scaled background', () => { test('should not scale background', async ({ page }) => { await expect(page.locator('[data-vaul-drawer-wrapper]')).not.toHaveCSS('transform', ''); await page.getByTestId('trigger').click(); await expect(page.locator('[data-vaul-drawer-wrapper]')).not.toHaveCSS('transform', ''); }); }); ================================================ FILE: test/tsconfig.json ================================================ { "compilerOptions": { "target": "es5", "baseUrl": ".", "lib": [ "dom", "dom.iterable", "esnext" ], "allowJs": true, "skipLibCheck": true, "strict": true, "noEmit": true, "esModuleInterop": true, "module": "esnext", "moduleResolution": "bundler", "resolveJsonModule": true, "isolatedModules": true, "jsx": "preserve", "incremental": true, "plugins": [ { "name": "next" } ], "paths": { "@/*": [ "./src/*" ] } }, "include": [ "next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts" ], "exclude": [ "node_modules" ] } ================================================ FILE: tsconfig.json ================================================ { "compilerOptions": { "jsx": "react", "target": "es2018", "moduleResolution": "node", "esModuleInterop": true, "lib": ["es2015", "dom"], "strict": true }, "include": ["src"], } ================================================ FILE: turbo.json ================================================ { "$schema": "https://turbo.build/schema.json", "pipeline": { "build": { "dependsOn": ["^build"], "outputs": ["dist/**", ".next/**"] }, "dev": { "cache": false } } }