├── .eslintrc.js ├── .github └── workflows │ └── playwright.yml ├── .gitignore ├── .prettierrc.js ├── .vscode └── settings.json ├── FUNDING.yml ├── LICENSE.md ├── README.md ├── package.json ├── playwright.config.ts ├── pnpm-lock.yaml ├── pnpm-workspace.yaml ├── src ├── browser.ts ├── constants.ts ├── context.ts ├── helpers.ts ├── index.tsx ├── style.css ├── 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 ├── public │ ├── next.svg │ └── vercel.svg ├── 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 /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | // This tells ESLint to load the config from the package `eslint-config-custom` 4 | extends: ['custom'], 5 | settings: { 6 | next: { 7 | rootDir: ['apps/*/'], 8 | }, 9 | }, 10 | }; 11 | -------------------------------------------------------------------------------- /.github/workflows/playwright.yml: -------------------------------------------------------------------------------- 1 | name: Playwright Tests 2 | on: 3 | push: 4 | branches: [main, master] 5 | pull_request: 6 | branches: [main, master] 7 | jobs: 8 | test: 9 | timeout-minutes: 60 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/checkout@v3 13 | - uses: actions/setup-node@v3 14 | with: 15 | node-version: 16 16 | - run: npm install -g pnpm@8.8.0 17 | - run: pnpm install 18 | - run: pnpm build 19 | - run: npx playwright install --with-deps 20 | - run: pnpm test || exit 1 21 | - uses: actions/upload-artifact@v3 22 | if: always() 23 | with: 24 | name: playwright-report 25 | path: playwright-report/ 26 | retention-days: 30 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | dist 3 | 4 | 5 | # dependencies 6 | node_modules 7 | .pnp 8 | .pnp.js 9 | 10 | # testing 11 | coverage 12 | 13 | # next.js 14 | .next/ 15 | out/ 16 | build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | .pnpm-debug.log* 27 | 28 | # local env files 29 | .env.local 30 | .env.development.local 31 | .env.test.local 32 | .env.production.local 33 | 34 | # turbo 35 | .turbo 36 | /test-results/ 37 | /playwright-report/ 38 | /playwright/.cache/ 39 | 40 | # styles 41 | style.css 42 | -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | semi: true, 3 | singleQuote: true, 4 | tabWidth: 2, 5 | trailingComma: 'all', 6 | printWidth: 120, 7 | }; 8 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "typescript.tsdk": "node_modules/typescript/lib" 3 | } 4 | -------------------------------------------------------------------------------- /FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: emilkowalski 2 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Emil Kowalski 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | https://github.com/emilkowalski/vaul/assets/36730035/fdf8c5e8-ade8-433b-8bb0-4ce10e722516 2 | 3 | Vaul is an unstyled drawer component for React that can be used as a Dialog replacement on tablet and mobile devices. You can read about why and how it was built [here](https://emilkowal.ski/ui/building-a-drawer-component). 4 | 5 | ## Usage 6 | 7 | To start using the library, install it in your project:, 8 | 9 | ```bash 10 | npm install vaul 11 | ``` 12 | 13 | Use the drawer in your app. 14 | 15 | ```jsx 16 | import { Drawer } from 'vaul'; 17 | 18 | function MyComponent() { 19 | return ( 20 | 21 | Open 22 | 23 | 24 | Title 25 | 26 | 27 | 28 | 29 | ); 30 | } 31 | ``` 32 | 33 | ## Documentation 34 | 35 | Find the full API reference and examples in the [documentation](https://vaul.emilkowal.ski/getting-started). 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vaul", 3 | "version": "1.1.2", 4 | "description": "Drawer component for React.", 5 | "main": "./dist/index.js", 6 | "module": "./dist/index.mjs", 7 | "types": "./dist/index.d.ts", 8 | "files": [ 9 | "dist", 10 | "style.css" 11 | ], 12 | "exports": { 13 | "import": { 14 | "types": "./dist/index.d.mts", 15 | "default": "./dist/index.mjs" 16 | }, 17 | "require": { 18 | "types": "./dist/index.d.ts", 19 | "default": "./dist/index.js" 20 | } 21 | }, 22 | "scripts": { 23 | "type-check": "tsc --noEmit", 24 | "build": "pnpm type-check && bunchee && pnpm copy-assets", 25 | "copy-assets": "cp -r ./src/style.css ./style.css", 26 | "dev": "bunchee --watch", 27 | "dev:test": "turbo run dev --filter=test...", 28 | "format": "prettier --write .", 29 | "test": "playwright test" 30 | }, 31 | "keywords": [ 32 | "react", 33 | "drawer", 34 | "dialog", 35 | "modal" 36 | ], 37 | "author": "Emil Kowalski ", 38 | "license": "MIT", 39 | "homepage": "https://vaul.emilkowal.ski/", 40 | "repository": { 41 | "type": "git", 42 | "url": "https://github.com/emilkowalski/vaul.git" 43 | }, 44 | "bugs": { 45 | "url": "https://github.com/emilkowalski/vaul/issues" 46 | }, 47 | "devDependencies": { 48 | "@playwright/test": "^1.37.1", 49 | "@radix-ui/react-dialog": "^1.0.4", 50 | "@types/node": "20.5.7", 51 | "@types/react": "18.2.55", 52 | "@types/react-dom": "18.2.18", 53 | "bunchee": "^5.1.5", 54 | "eslint": "^7.32.0", 55 | "prettier": "^2.5.1", 56 | "react": "^18.2.0", 57 | "react-dom": "^18.2.0", 58 | "turbo": "1.6", 59 | "typescript": "5.2.2" 60 | }, 61 | "peerDependencies": { 62 | "react": "^16.8 || ^17.0 || ^18.0 || ^19.0.0 || ^19.0.0-rc", 63 | "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0.0 || ^19.0.0-rc" 64 | }, 65 | "packageManager": "pnpm@8.8.0", 66 | "dependencies": { 67 | "@radix-ui/react-dialog": "^1.1.1" 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /playwright.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig, devices } from '@playwright/test'; 2 | 3 | /** 4 | * Read environment variables from file. 5 | * https://github.com/motdotla/dotenv 6 | */ 7 | // require('dotenv').config(); 8 | 9 | /** 10 | * See https://playwright.dev/docs/test-configuration. 11 | */ 12 | export default defineConfig({ 13 | testDir: './test', 14 | /* Maximum time one test can run for. */ 15 | timeout: 30 * 1000, 16 | expect: { 17 | /** 18 | * Maximum time expect() should wait for the condition to be met. 19 | * For example in `await expect(locator).toHaveText();` 20 | */ 21 | timeout: 5000, 22 | }, 23 | /* Run tests in files in parallel */ 24 | fullyParallel: true, 25 | /* Fail the build on CI if you accidentally left test.only in the source code. */ 26 | forbidOnly: !!process.env.CI, 27 | /* Retry on CI only */ 28 | retries: process.env.CI ? 2 : 0, 29 | /* Opt out of parallel tests on CI. */ 30 | workers: process.env.CI ? 1 : undefined, 31 | /* Reporter to use. See https://playwright.dev/docs/test-reporters */ 32 | reporter: 'html', 33 | /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ 34 | use: { 35 | trace: 'on-first-retry', 36 | baseURL: 'http://localhost:3000', 37 | }, 38 | webServer: { 39 | command: 'npm run dev', 40 | url: 'http://localhost:3000', 41 | cwd: './test', 42 | reuseExistingServer: !process.env.CI, 43 | }, 44 | /* Configure projects for major browsers */ 45 | projects: [ 46 | { 47 | name: 'iPhone', 48 | use: { ...devices['iPhone 13 Pro'] }, 49 | }, 50 | { 51 | name: 'Pixel', 52 | use: { ...devices['Pixel 5'] }, 53 | }, 54 | ], 55 | }); 56 | -------------------------------------------------------------------------------- /pnpm-workspace.yaml: -------------------------------------------------------------------------------- 1 | packages: 2 | - '.' 3 | - 'test' 4 | -------------------------------------------------------------------------------- /src/browser.ts: -------------------------------------------------------------------------------- 1 | export function isMobileFirefox(): boolean | undefined { 2 | const userAgent = navigator.userAgent; 3 | return ( 4 | typeof window !== 'undefined' && 5 | ((/Firefox/.test(userAgent) && /Mobile/.test(userAgent)) || // Android Firefox 6 | /FxiOS/.test(userAgent)) // iOS Firefox 7 | ); 8 | } 9 | 10 | export function isMac(): boolean | undefined { 11 | return testPlatform(/^Mac/); 12 | } 13 | 14 | export function isIPhone(): boolean | undefined { 15 | return testPlatform(/^iPhone/); 16 | } 17 | 18 | export function isSafari(): boolean | undefined { 19 | return /^((?!chrome|android).)*safari/i.test(navigator.userAgent); 20 | } 21 | 22 | export function isIPad(): boolean | undefined { 23 | return ( 24 | testPlatform(/^iPad/) || 25 | // iPadOS 13 lies and says it's a Mac, but we can distinguish by detecting touch support. 26 | (isMac() && navigator.maxTouchPoints > 1) 27 | ); 28 | } 29 | 30 | export function isIOS(): boolean | undefined { 31 | return isIPhone() || isIPad(); 32 | } 33 | 34 | export function testPlatform(re: RegExp): boolean | undefined { 35 | return typeof window !== 'undefined' && window.navigator != null ? re.test(window.navigator.platform) : undefined; 36 | } 37 | -------------------------------------------------------------------------------- /src/constants.ts: -------------------------------------------------------------------------------- 1 | export const TRANSITIONS = { 2 | DURATION: 0.5, 3 | EASE: [0.32, 0.72, 0, 1], 4 | }; 5 | 6 | export const VELOCITY_THRESHOLD = 0.4; 7 | 8 | export const CLOSE_THRESHOLD = 0.25; 9 | 10 | export const SCROLL_LOCK_TIMEOUT = 100; 11 | 12 | export const BORDER_RADIUS = 8; 13 | 14 | export const NESTED_DISPLACEMENT = 16; 15 | 16 | export const WINDOW_TOP_OFFSET = 26; 17 | 18 | export const DRAG_CLASS = 'vaul-dragging'; 19 | -------------------------------------------------------------------------------- /src/context.ts: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { DrawerDirection } from './types'; 3 | 4 | interface DrawerContextValue { 5 | drawerRef: React.RefObject; 6 | overlayRef: React.RefObject; 7 | onPress: (event: React.PointerEvent) => void; 8 | onRelease: (event: React.PointerEvent | null) => void; 9 | onDrag: (event: React.PointerEvent) => void; 10 | onNestedDrag: (event: React.PointerEvent, percentageDragged: number) => void; 11 | onNestedOpenChange: (o: boolean) => void; 12 | onNestedRelease: (event: React.PointerEvent, open: boolean) => void; 13 | dismissible: boolean; 14 | isOpen: boolean; 15 | isDragging: boolean; 16 | keyboardIsOpen: React.MutableRefObject; 17 | snapPointsOffset: number[] | null; 18 | snapPoints?: (number | string)[] | null; 19 | activeSnapPointIndex?: number | null; 20 | modal: boolean; 21 | shouldFade: boolean; 22 | activeSnapPoint?: number | string | null; 23 | setActiveSnapPoint: (o: number | string | null) => void; 24 | closeDrawer: () => void; 25 | openProp?: boolean; 26 | onOpenChange?: (o: boolean) => void; 27 | direction: DrawerDirection; 28 | shouldScaleBackground: boolean; 29 | setBackgroundColorOnScale: boolean; 30 | noBodyStyles: boolean; 31 | handleOnly?: boolean; 32 | container?: HTMLElement | null; 33 | autoFocus?: boolean; 34 | shouldAnimate?: React.RefObject; 35 | } 36 | 37 | export const DrawerContext = React.createContext({ 38 | drawerRef: { current: null }, 39 | overlayRef: { current: null }, 40 | onPress: () => {}, 41 | onRelease: () => {}, 42 | onDrag: () => {}, 43 | onNestedDrag: () => {}, 44 | onNestedOpenChange: () => {}, 45 | onNestedRelease: () => {}, 46 | openProp: undefined, 47 | dismissible: false, 48 | isOpen: false, 49 | isDragging: false, 50 | keyboardIsOpen: { current: false }, 51 | snapPointsOffset: null, 52 | snapPoints: null, 53 | handleOnly: false, 54 | modal: false, 55 | shouldFade: false, 56 | activeSnapPoint: null, 57 | onOpenChange: () => {}, 58 | setActiveSnapPoint: () => {}, 59 | closeDrawer: () => {}, 60 | direction: 'bottom', 61 | shouldAnimate: { current: true }, 62 | shouldScaleBackground: false, 63 | setBackgroundColorOnScale: true, 64 | noBodyStyles: false, 65 | container: null, 66 | autoFocus: false, 67 | }); 68 | 69 | export const useDrawerContext = () => { 70 | const context = React.useContext(DrawerContext); 71 | if (!context) { 72 | throw new Error('useDrawerContext must be used within a Drawer.Root'); 73 | } 74 | return context; 75 | }; 76 | -------------------------------------------------------------------------------- /src/helpers.ts: -------------------------------------------------------------------------------- 1 | import { AnyFunction, DrawerDirection } from './types'; 2 | 3 | interface Style { 4 | [key: string]: string; 5 | } 6 | 7 | const cache = new WeakMap(); 8 | 9 | export function isInView(el: HTMLElement): boolean { 10 | const rect = el.getBoundingClientRect(); 11 | 12 | if (!window.visualViewport) return false; 13 | 14 | return ( 15 | rect.top >= 0 && 16 | rect.left >= 0 && 17 | // Need + 40 for safari detection 18 | rect.bottom <= window.visualViewport.height - 40 && 19 | rect.right <= window.visualViewport.width 20 | ); 21 | } 22 | 23 | export function set(el: Element | HTMLElement | null | undefined, styles: Style, ignoreCache = false) { 24 | if (!el || !(el instanceof HTMLElement)) return; 25 | let originalStyles: Style = {}; 26 | 27 | Object.entries(styles).forEach(([key, value]: [string, string]) => { 28 | if (key.startsWith('--')) { 29 | el.style.setProperty(key, value); 30 | return; 31 | } 32 | 33 | originalStyles[key] = (el.style as any)[key]; 34 | (el.style as any)[key] = value; 35 | }); 36 | 37 | if (ignoreCache) return; 38 | 39 | cache.set(el, originalStyles); 40 | } 41 | 42 | export function reset(el: Element | HTMLElement | null, prop?: string) { 43 | if (!el || !(el instanceof HTMLElement)) return; 44 | let originalStyles = cache.get(el); 45 | 46 | if (!originalStyles) { 47 | return; 48 | } 49 | 50 | if (prop) { 51 | (el.style as any)[prop] = originalStyles[prop]; 52 | } else { 53 | Object.entries(originalStyles).forEach(([key, value]) => { 54 | (el.style as any)[key] = value; 55 | }); 56 | } 57 | } 58 | 59 | export const isVertical = (direction: DrawerDirection) => { 60 | switch (direction) { 61 | case 'top': 62 | case 'bottom': 63 | return true; 64 | case 'left': 65 | case 'right': 66 | return false; 67 | default: 68 | return direction satisfies never; 69 | } 70 | }; 71 | 72 | export function getTranslate(element: HTMLElement, direction: DrawerDirection): number | null { 73 | if (!element) { 74 | return null; 75 | } 76 | const style = window.getComputedStyle(element); 77 | const transform = 78 | // @ts-ignore 79 | style.transform || style.webkitTransform || style.mozTransform; 80 | let mat = transform.match(/^matrix3d\((.+)\)$/); 81 | if (mat) { 82 | // https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix3d 83 | return parseFloat(mat[1].split(', ')[isVertical(direction) ? 13 : 12]); 84 | } 85 | // https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix 86 | mat = transform.match(/^matrix\((.+)\)$/); 87 | return mat ? parseFloat(mat[1].split(', ')[isVertical(direction) ? 5 : 4]) : null; 88 | } 89 | 90 | export function dampenValue(v: number) { 91 | return 8 * (Math.log(v + 1) - 2); 92 | } 93 | 94 | export function assignStyle(element: HTMLElement | null | undefined, style: Partial) { 95 | if (!element) return () => {}; 96 | 97 | const prevStyle = element.style.cssText; 98 | Object.assign(element.style, style); 99 | 100 | return () => { 101 | element.style.cssText = prevStyle; 102 | }; 103 | } 104 | 105 | /** 106 | * Receives functions as arguments and returns a new function that calls all. 107 | */ 108 | export function chain(...fns: T[]) { 109 | return (...args: T extends AnyFunction ? Parameters : never) => { 110 | for (const fn of fns) { 111 | if (typeof fn === 'function') { 112 | // @ts-ignore 113 | fn(...args); 114 | } 115 | } 116 | }; 117 | } 118 | -------------------------------------------------------------------------------- /src/style.css: -------------------------------------------------------------------------------- 1 | [data-vaul-drawer] { 2 | touch-action: none; 3 | will-change: transform; 4 | transition: transform 0.5s cubic-bezier(0.32, 0.72, 0, 1); 5 | animation-duration: 0.5s; 6 | animation-timing-function: cubic-bezier(0.32, 0.72, 0, 1); 7 | } 8 | 9 | [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='bottom'][data-state='open'] { 10 | animation-name: slideFromBottom; 11 | } 12 | [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='bottom'][data-state='closed'] { 13 | animation-name: slideToBottom; 14 | } 15 | 16 | [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='top'][data-state='open'] { 17 | animation-name: slideFromTop; 18 | } 19 | [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='top'][data-state='closed'] { 20 | animation-name: slideToTop; 21 | } 22 | 23 | [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='left'][data-state='open'] { 24 | animation-name: slideFromLeft; 25 | } 26 | [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='left'][data-state='closed'] { 27 | animation-name: slideToLeft; 28 | } 29 | 30 | [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='right'][data-state='open'] { 31 | animation-name: slideFromRight; 32 | } 33 | [data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='right'][data-state='closed'] { 34 | animation-name: slideToRight; 35 | } 36 | 37 | [data-vaul-drawer][data-vaul-snap-points='true'][data-vaul-drawer-direction='bottom'] { 38 | transform: translate3d(0, var(--initial-transform, 100%), 0); 39 | } 40 | 41 | [data-vaul-drawer][data-vaul-snap-points='true'][data-vaul-drawer-direction='top'] { 42 | transform: translate3d(0, calc(var(--initial-transform, 100%) * -1), 0); 43 | } 44 | 45 | [data-vaul-drawer][data-vaul-snap-points='true'][data-vaul-drawer-direction='left'] { 46 | transform: translate3d(calc(var(--initial-transform, 100%) * -1), 0, 0); 47 | } 48 | 49 | [data-vaul-drawer][data-vaul-snap-points='true'][data-vaul-drawer-direction='right'] { 50 | transform: translate3d(var(--initial-transform, 100%), 0, 0); 51 | } 52 | 53 | [data-vaul-drawer][data-vaul-delayed-snap-points='true'][data-vaul-drawer-direction='top'] { 54 | transform: translate3d(0, var(--snap-point-height, 0), 0); 55 | } 56 | 57 | [data-vaul-drawer][data-vaul-delayed-snap-points='true'][data-vaul-drawer-direction='bottom'] { 58 | transform: translate3d(0, var(--snap-point-height, 0), 0); 59 | } 60 | 61 | [data-vaul-drawer][data-vaul-delayed-snap-points='true'][data-vaul-drawer-direction='left'] { 62 | transform: translate3d(var(--snap-point-height, 0), 0, 0); 63 | } 64 | 65 | [data-vaul-drawer][data-vaul-delayed-snap-points='true'][data-vaul-drawer-direction='right'] { 66 | transform: translate3d(var(--snap-point-height, 0), 0, 0); 67 | } 68 | 69 | [data-vaul-overlay][data-vaul-snap-points='false'] { 70 | animation-duration: 0.5s; 71 | animation-timing-function: cubic-bezier(0.32, 0.72, 0, 1); 72 | } 73 | [data-vaul-overlay][data-vaul-snap-points='false'][data-state='open'] { 74 | animation-name: fadeIn; 75 | } 76 | [data-vaul-overlay][data-state='closed'] { 77 | animation-name: fadeOut; 78 | } 79 | 80 | [data-vaul-animate='false'] { 81 | animation: none !important; 82 | } 83 | 84 | [data-vaul-overlay][data-vaul-snap-points='true'] { 85 | opacity: 0; 86 | transition: opacity 0.5s cubic-bezier(0.32, 0.72, 0, 1); 87 | } 88 | 89 | [data-vaul-overlay][data-vaul-snap-points='true'] { 90 | opacity: 1; 91 | } 92 | 93 | [data-vaul-drawer]:not([data-vaul-custom-container='true'])::after { 94 | content: ''; 95 | position: absolute; 96 | background: inherit; 97 | background-color: inherit; 98 | } 99 | 100 | [data-vaul-drawer][data-vaul-drawer-direction='top']::after { 101 | top: initial; 102 | bottom: 100%; 103 | left: 0; 104 | right: 0; 105 | height: 200%; 106 | } 107 | 108 | [data-vaul-drawer][data-vaul-drawer-direction='bottom']::after { 109 | top: 100%; 110 | bottom: initial; 111 | left: 0; 112 | right: 0; 113 | height: 200%; 114 | } 115 | 116 | [data-vaul-drawer][data-vaul-drawer-direction='left']::after { 117 | left: initial; 118 | right: 100%; 119 | top: 0; 120 | bottom: 0; 121 | width: 200%; 122 | } 123 | 124 | [data-vaul-drawer][data-vaul-drawer-direction='right']::after { 125 | left: 100%; 126 | right: initial; 127 | top: 0; 128 | bottom: 0; 129 | width: 200%; 130 | } 131 | 132 | [data-vaul-overlay][data-vaul-snap-points='true']:not([data-vaul-snap-points-overlay='true']):not( 133 | [data-state='closed'] 134 | ) { 135 | opacity: 0; 136 | } 137 | 138 | [data-vaul-overlay][data-vaul-snap-points-overlay='true'] { 139 | opacity: 1; 140 | } 141 | 142 | [data-vaul-handle] { 143 | display: block; 144 | position: relative; 145 | opacity: 0.7; 146 | background: #e2e2e4; 147 | margin-left: auto; 148 | margin-right: auto; 149 | height: 5px; 150 | width: 32px; 151 | border-radius: 1rem; 152 | touch-action: pan-y; 153 | } 154 | 155 | [data-vaul-handle]:hover, 156 | [data-vaul-handle]:active { 157 | opacity: 1; 158 | } 159 | 160 | [data-vaul-handle-hitarea] { 161 | position: absolute; 162 | left: 50%; 163 | top: 50%; 164 | transform: translate(-50%, -50%); 165 | width: max(100%, 2.75rem); /* 44px */ 166 | height: max(100%, 2.75rem); /* 44px */ 167 | touch-action: inherit; 168 | } 169 | 170 | @media (hover: hover) and (pointer: fine) { 171 | [data-vaul-drawer] { 172 | user-select: none; 173 | } 174 | } 175 | 176 | @media (pointer: fine) { 177 | [data-vaul-handle-hitarea]: { 178 | width: 100%; 179 | height: 100%; 180 | } 181 | } 182 | 183 | @keyframes fadeIn { 184 | from { 185 | opacity: 0; 186 | } 187 | to { 188 | opacity: 1; 189 | } 190 | } 191 | 192 | @keyframes fadeOut { 193 | to { 194 | opacity: 0; 195 | } 196 | } 197 | 198 | @keyframes slideFromBottom { 199 | from { 200 | transform: translate3d(0, var(--initial-transform, 100%), 0); 201 | } 202 | to { 203 | transform: translate3d(0, 0, 0); 204 | } 205 | } 206 | 207 | @keyframes slideToBottom { 208 | to { 209 | transform: translate3d(0, var(--initial-transform, 100%), 0); 210 | } 211 | } 212 | 213 | @keyframes slideFromTop { 214 | from { 215 | transform: translate3d(0, calc(var(--initial-transform, 100%) * -1), 0); 216 | } 217 | to { 218 | transform: translate3d(0, 0, 0); 219 | } 220 | } 221 | 222 | @keyframes slideToTop { 223 | to { 224 | transform: translate3d(0, calc(var(--initial-transform, 100%) * -1), 0); 225 | } 226 | } 227 | 228 | @keyframes slideFromLeft { 229 | from { 230 | transform: translate3d(calc(var(--initial-transform, 100%) * -1), 0, 0); 231 | } 232 | to { 233 | transform: translate3d(0, 0, 0); 234 | } 235 | } 236 | 237 | @keyframes slideToLeft { 238 | to { 239 | transform: translate3d(calc(var(--initial-transform, 100%) * -1), 0, 0); 240 | } 241 | } 242 | 243 | @keyframes slideFromRight { 244 | from { 245 | transform: translate3d(var(--initial-transform, 100%), 0, 0); 246 | } 247 | to { 248 | transform: translate3d(0, 0, 0); 249 | } 250 | } 251 | 252 | @keyframes slideToRight { 253 | to { 254 | transform: translate3d(var(--initial-transform, 100%), 0, 0); 255 | } 256 | } 257 | -------------------------------------------------------------------------------- /src/types.ts: -------------------------------------------------------------------------------- 1 | export type DrawerDirection = 'top' | 'bottom' | 'left' | 'right'; 2 | export interface SnapPoint { 3 | fraction: number; 4 | height: number; 5 | } 6 | 7 | export type AnyFunction = (...args: any) => any; 8 | -------------------------------------------------------------------------------- /src/use-composed-refs.ts: -------------------------------------------------------------------------------- 1 | // This code comes from https://github.com/radix-ui/primitives/tree/main/packages/react/compose-refs 2 | 3 | import * as React from 'react'; 4 | 5 | type PossibleRef = React.Ref | undefined; 6 | 7 | /** 8 | * Set a given ref to a given value 9 | * This utility takes care of different types of refs: callback refs and RefObject(s) 10 | */ 11 | function setRef(ref: PossibleRef, value: T) { 12 | if (typeof ref === 'function') { 13 | ref(value); 14 | } else if (ref !== null && ref !== undefined) { 15 | (ref as React.MutableRefObject).current = value; 16 | } 17 | } 18 | 19 | /** 20 | * A utility to compose multiple refs together 21 | * Accepts callback refs and RefObject(s) 22 | */ 23 | function composeRefs(...refs: PossibleRef[]) { 24 | return (node: T) => refs.forEach((ref) => setRef(ref, node)); 25 | } 26 | 27 | /** 28 | * A custom hook that composes multiple refs 29 | * Accepts callback refs and RefObject(s) 30 | */ 31 | function useComposedRefs(...refs: PossibleRef[]) { 32 | // eslint-disable-next-line react-hooks/exhaustive-deps 33 | return React.useCallback(composeRefs(...refs), refs); 34 | } 35 | 36 | export { composeRefs, useComposedRefs }; 37 | -------------------------------------------------------------------------------- /src/use-controllable-state.ts: -------------------------------------------------------------------------------- 1 | // This code comes from https://github.com/radix-ui/primitives/blob/main/packages/react/use-controllable-state/src/useControllableState.tsx 2 | 3 | import React from 'react'; 4 | 5 | type UseControllableStateParams = { 6 | prop?: T | undefined; 7 | defaultProp?: T | undefined; 8 | onChange?: (state: T) => void; 9 | }; 10 | 11 | type SetStateFn = (prevState?: T) => T; 12 | 13 | function useCallbackRef any>(callback: T | undefined): T { 14 | const callbackRef = React.useRef(callback); 15 | 16 | React.useEffect(() => { 17 | callbackRef.current = callback; 18 | }); 19 | 20 | // https://github.com/facebook/react/issues/19240 21 | return React.useMemo(() => ((...args) => callbackRef.current?.(...args)) as T, []); 22 | } 23 | 24 | function useUncontrolledState({ defaultProp, onChange }: Omit, 'prop'>) { 25 | const uncontrolledState = React.useState(defaultProp); 26 | const [value] = uncontrolledState; 27 | const prevValueRef = React.useRef(value); 28 | const handleChange = useCallbackRef(onChange); 29 | 30 | React.useEffect(() => { 31 | if (prevValueRef.current !== value) { 32 | handleChange(value as T); 33 | prevValueRef.current = value; 34 | } 35 | }, [value, prevValueRef, handleChange]); 36 | 37 | return uncontrolledState; 38 | } 39 | export function useControllableState({ prop, defaultProp, onChange = () => {} }: UseControllableStateParams) { 40 | const [uncontrolledProp, setUncontrolledProp] = useUncontrolledState({ defaultProp, onChange }); 41 | const isControlled = prop !== undefined; 42 | const value = isControlled ? prop : uncontrolledProp; 43 | const handleChange = useCallbackRef(onChange); 44 | 45 | const setValue: React.Dispatch> = React.useCallback( 46 | (nextValue) => { 47 | if (isControlled) { 48 | const setter = nextValue as SetStateFn; 49 | const value = typeof nextValue === 'function' ? setter(prop) : nextValue; 50 | if (value !== prop) handleChange(value as T); 51 | } else { 52 | setUncontrolledProp(nextValue); 53 | } 54 | }, 55 | [isControlled, prop, setUncontrolledProp, handleChange], 56 | ); 57 | 58 | return [value, setValue] as const; 59 | } 60 | -------------------------------------------------------------------------------- /src/use-position-fixed.ts: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { isSafari } from './browser'; 3 | 4 | let previousBodyPosition: Record | null = null; 5 | 6 | /** 7 | * This hook is necessary to prevent buggy behavior on iOS devices (need to test on Android). 8 | * 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. 9 | * Issues that this hook solves: 10 | * https://github.com/emilkowalski/vaul/issues/435 11 | * https://github.com/emilkowalski/vaul/issues/433 12 | * And more that I discovered, but were just not reported. 13 | */ 14 | 15 | export function usePositionFixed({ 16 | isOpen, 17 | modal, 18 | nested, 19 | hasBeenOpened, 20 | preventScrollRestoration, 21 | noBodyStyles, 22 | }: { 23 | isOpen: boolean; 24 | modal: boolean; 25 | nested: boolean; 26 | hasBeenOpened: boolean; 27 | preventScrollRestoration: boolean; 28 | noBodyStyles: boolean; 29 | }) { 30 | const [activeUrl, setActiveUrl] = React.useState(() => (typeof window !== 'undefined' ? window.location.href : '')); 31 | const scrollPos = React.useRef(0); 32 | 33 | const setPositionFixed = React.useCallback(() => { 34 | // All browsers on iOS will return true here. 35 | if (!isSafari()) return; 36 | 37 | // If previousBodyPosition is already set, don't set it again. 38 | if (previousBodyPosition === null && isOpen && !noBodyStyles) { 39 | previousBodyPosition = { 40 | position: document.body.style.position, 41 | top: document.body.style.top, 42 | left: document.body.style.left, 43 | height: document.body.style.height, 44 | right: 'unset', 45 | }; 46 | 47 | // Update the dom inside an animation frame 48 | const { scrollX, innerHeight } = window; 49 | 50 | document.body.style.setProperty('position', 'fixed', 'important'); 51 | Object.assign(document.body.style, { 52 | top: `${-scrollPos.current}px`, 53 | left: `${-scrollX}px`, 54 | right: '0px', 55 | height: 'auto', 56 | }); 57 | 58 | window.setTimeout( 59 | () => 60 | window.requestAnimationFrame(() => { 61 | // Attempt to check if the bottom bar appeared due to the position change 62 | const bottomBarHeight = innerHeight - window.innerHeight; 63 | if (bottomBarHeight && scrollPos.current >= innerHeight) { 64 | // Move the content further up so that the bottom bar doesn't hide it 65 | document.body.style.top = `${-(scrollPos.current + bottomBarHeight)}px`; 66 | } 67 | }), 68 | 300, 69 | ); 70 | } 71 | }, [isOpen]); 72 | 73 | const restorePositionSetting = React.useCallback(() => { 74 | // All browsers on iOS will return true here. 75 | if (!isSafari()) return; 76 | 77 | if (previousBodyPosition !== null && !noBodyStyles) { 78 | // Convert the position from "px" to Int 79 | const y = -parseInt(document.body.style.top, 10); 80 | const x = -parseInt(document.body.style.left, 10); 81 | 82 | // Restore styles 83 | Object.assign(document.body.style, previousBodyPosition); 84 | 85 | window.requestAnimationFrame(() => { 86 | if (preventScrollRestoration && activeUrl !== window.location.href) { 87 | setActiveUrl(window.location.href); 88 | return; 89 | } 90 | 91 | window.scrollTo(x, y); 92 | }); 93 | 94 | previousBodyPosition = null; 95 | } 96 | }, [activeUrl]); 97 | 98 | React.useEffect(() => { 99 | function onScroll() { 100 | scrollPos.current = window.scrollY; 101 | } 102 | 103 | onScroll(); 104 | 105 | window.addEventListener('scroll', onScroll); 106 | 107 | return () => { 108 | window.removeEventListener('scroll', onScroll); 109 | }; 110 | }, []); 111 | 112 | React.useEffect(() => { 113 | if (!modal) return; 114 | 115 | return () => { 116 | if (typeof document === 'undefined') return; 117 | 118 | // Another drawer is opened, safe to ignore the execution 119 | const hasDrawerOpened = !!document.querySelector('[data-vaul-drawer]'); 120 | if (hasDrawerOpened) return; 121 | 122 | restorePositionSetting(); 123 | }; 124 | }, [modal, restorePositionSetting]); 125 | 126 | React.useEffect(() => { 127 | if (nested || !hasBeenOpened) return; 128 | // This is needed to force Safari toolbar to show **before** the drawer starts animating to prevent a gnarly shift from happening 129 | if (isOpen) { 130 | // avoid for standalone mode (PWA) 131 | const isStandalone = window.matchMedia('(display-mode: standalone)').matches; 132 | !isStandalone && setPositionFixed(); 133 | 134 | if (!modal) { 135 | window.setTimeout(() => { 136 | restorePositionSetting(); 137 | }, 500); 138 | } 139 | } else { 140 | restorePositionSetting(); 141 | } 142 | }, [isOpen, hasBeenOpened, activeUrl, modal, nested, setPositionFixed, restorePositionSetting]); 143 | 144 | return { restorePositionSetting }; 145 | } 146 | -------------------------------------------------------------------------------- /src/use-prevent-scroll.ts: -------------------------------------------------------------------------------- 1 | // This code comes from https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/overlays/src/usePreventScroll.ts 2 | 3 | import { useEffect, useLayoutEffect } from 'react'; 4 | import { isIOS } from './browser'; 5 | 6 | const KEYBOARD_BUFFER = 24; 7 | 8 | export const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect; 9 | 10 | interface PreventScrollOptions { 11 | /** Whether the scroll lock is disabled. */ 12 | isDisabled?: boolean; 13 | focusCallback?: () => void; 14 | } 15 | 16 | function chain(...callbacks: any[]): (...args: any[]) => void { 17 | return (...args: any[]) => { 18 | for (let callback of callbacks) { 19 | if (typeof callback === 'function') { 20 | callback(...args); 21 | } 22 | } 23 | }; 24 | } 25 | 26 | // @ts-ignore 27 | const visualViewport = typeof document !== 'undefined' && window.visualViewport; 28 | 29 | export function isScrollable(node: Element): boolean { 30 | let style = window.getComputedStyle(node); 31 | return /(auto|scroll)/.test(style.overflow + style.overflowX + style.overflowY); 32 | } 33 | 34 | export function getScrollParent(node: Element): Element { 35 | if (isScrollable(node)) { 36 | node = node.parentElement as HTMLElement; 37 | } 38 | 39 | while (node && !isScrollable(node)) { 40 | node = node.parentElement as HTMLElement; 41 | } 42 | 43 | return node || document.scrollingElement || document.documentElement; 44 | } 45 | 46 | // HTML input types that do not cause the software keyboard to appear. 47 | const nonTextInputTypes = new Set([ 48 | 'checkbox', 49 | 'radio', 50 | 'range', 51 | 'color', 52 | 'file', 53 | 'image', 54 | 'button', 55 | 'submit', 56 | 'reset', 57 | ]); 58 | 59 | // The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position 60 | let preventScrollCount = 0; 61 | let restore: () => void; 62 | 63 | /** 64 | * Prevents scrolling on the document body on mount, and 65 | * restores it on unmount. Also ensures that content does not 66 | * shift due to the scrollbars disappearing. 67 | */ 68 | export function usePreventScroll(options: PreventScrollOptions = {}) { 69 | let { isDisabled } = options; 70 | 71 | useIsomorphicLayoutEffect(() => { 72 | if (isDisabled) { 73 | return; 74 | } 75 | 76 | preventScrollCount++; 77 | if (preventScrollCount === 1) { 78 | if (isIOS()) { 79 | restore = preventScrollMobileSafari(); 80 | } 81 | } 82 | 83 | return () => { 84 | preventScrollCount--; 85 | if (preventScrollCount === 0) { 86 | restore?.(); 87 | } 88 | }; 89 | }, [isDisabled]); 90 | } 91 | 92 | // Mobile Safari is a whole different beast. Even with overflow: hidden, 93 | // it still scrolls the page in many situations: 94 | // 95 | // 1. When the bottom toolbar and address bar are collapsed, page scrolling is always allowed. 96 | // 2. When the keyboard is visible, the viewport does not resize. Instead, the keyboard covers part of 97 | // it, so it becomes scrollable. 98 | // 3. When tapping on an input, the page always scrolls so that the input is centered in the visual viewport. 99 | // This may cause even fixed position elements to scroll off the screen. 100 | // 4. When using the next/previous buttons in the keyboard to navigate between inputs, the whole page always 101 | // scrolls, even if the input is inside a nested scrollable element that could be scrolled instead. 102 | // 103 | // In order to work around these cases, and prevent scrolling without jankiness, we do a few things: 104 | // 105 | // 1. Prevent default on `touchmove` events that are not in a scrollable element. This prevents touch scrolling 106 | // on the window. 107 | // 2. Prevent default on `touchmove` events inside a scrollable element when the scroll position is at the 108 | // top or bottom. This avoids the whole page scrolling instead, but does prevent overscrolling. 109 | // 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves. 110 | // 4. When focusing an input, apply a transform to trick Safari into thinking the input is at the top 111 | // of the page, which prevents it from scrolling the page. After the input is focused, scroll the element 112 | // into view ourselves, without scrolling the whole page. 113 | // 5. Offset the body by the scroll position using a negative margin and scroll to the top. This should appear the 114 | // same visually, but makes the actual scroll position always zero. This is required to make all of the 115 | // above work or Safari will still try to scroll the page when focusing an input. 116 | // 6. As a last resort, handle window scroll events, and scroll back to the top. This can happen when attempting 117 | // to navigate to an input with the next/previous buttons that's outside a modal. 118 | function preventScrollMobileSafari() { 119 | let scrollable: Element; 120 | let lastY = 0; 121 | let onTouchStart = (e: TouchEvent) => { 122 | // Store the nearest scrollable parent element from the element that the user touched. 123 | scrollable = getScrollParent(e.target as Element); 124 | if (scrollable === document.documentElement && scrollable === document.body) { 125 | return; 126 | } 127 | 128 | lastY = e.changedTouches[0].pageY; 129 | }; 130 | 131 | let onTouchMove = (e: TouchEvent) => { 132 | // Prevent scrolling the window. 133 | if (!scrollable || scrollable === document.documentElement || scrollable === document.body) { 134 | e.preventDefault(); 135 | return; 136 | } 137 | 138 | // Prevent scrolling up when at the top and scrolling down when at the bottom 139 | // of a nested scrollable area, otherwise mobile Safari will start scrolling 140 | // the window instead. Unfortunately, this disables bounce scrolling when at 141 | // the top but it's the best we can do. 142 | let y = e.changedTouches[0].pageY; 143 | let scrollTop = scrollable.scrollTop; 144 | let bottom = scrollable.scrollHeight - scrollable.clientHeight; 145 | 146 | if (bottom === 0) { 147 | return; 148 | } 149 | 150 | if ((scrollTop <= 0 && y > lastY) || (scrollTop >= bottom && y < lastY)) { 151 | e.preventDefault(); 152 | } 153 | 154 | lastY = y; 155 | }; 156 | 157 | let onTouchEnd = (e: TouchEvent) => { 158 | let target = e.target as HTMLElement; 159 | 160 | // Apply this change if we're not already focused on the target element 161 | if (isInput(target) && target !== document.activeElement) { 162 | e.preventDefault(); 163 | 164 | // Apply a transform to trick Safari into thinking the input is at the top of the page 165 | // so it doesn't try to scroll it into view. When tapping on an input, this needs to 166 | // be done before the "focus" event, so we have to focus the element ourselves. 167 | target.style.transform = 'translateY(-2000px)'; 168 | target.focus(); 169 | requestAnimationFrame(() => { 170 | target.style.transform = ''; 171 | }); 172 | } 173 | }; 174 | 175 | let onFocus = (e: FocusEvent) => { 176 | let target = e.target as HTMLElement; 177 | if (isInput(target)) { 178 | // Transform also needs to be applied in the focus event in cases where focus moves 179 | // other than tapping on an input directly, e.g. the next/previous buttons in the 180 | // software keyboard. In these cases, it seems applying the transform in the focus event 181 | // is good enough, whereas when tapping an input, it must be done before the focus event. 🤷‍♂️ 182 | target.style.transform = 'translateY(-2000px)'; 183 | requestAnimationFrame(() => { 184 | target.style.transform = ''; 185 | 186 | // This will have prevented the browser from scrolling the focused element into view, 187 | // so we need to do this ourselves in a way that doesn't cause the whole page to scroll. 188 | if (visualViewport) { 189 | if (visualViewport.height < window.innerHeight) { 190 | // If the keyboard is already visible, do this after one additional frame 191 | // to wait for the transform to be removed. 192 | requestAnimationFrame(() => { 193 | scrollIntoView(target); 194 | }); 195 | } else { 196 | // Otherwise, wait for the visual viewport to resize before scrolling so we can 197 | // measure the correct position to scroll to. 198 | visualViewport.addEventListener('resize', () => scrollIntoView(target), { once: true }); 199 | } 200 | } 201 | }); 202 | } 203 | }; 204 | 205 | let onWindowScroll = () => { 206 | // Last resort. If the window scrolled, scroll it back to the top. 207 | // It should always be at the top because the body will have a negative margin (see below). 208 | window.scrollTo(0, 0); 209 | }; 210 | 211 | // Record the original scroll position so we can restore it. 212 | // Then apply a negative margin to the body to offset it by the scroll position. This will 213 | // enable us to scroll the window to the top, which is required for the rest of this to work. 214 | let scrollX = window.pageXOffset; 215 | let scrollY = window.pageYOffset; 216 | 217 | let restoreStyles = chain( 218 | setStyle(document.documentElement, 'paddingRight', `${window.innerWidth - document.documentElement.clientWidth}px`), 219 | // setStyle(document.documentElement, 'overflow', 'hidden'), 220 | // setStyle(document.body, 'marginTop', `-${scrollY}px`), 221 | ); 222 | 223 | // Scroll to the top. The negative margin on the body will make this appear the same. 224 | window.scrollTo(0, 0); 225 | 226 | let removeEvents = chain( 227 | addEvent(document, 'touchstart', onTouchStart, { passive: false, capture: true }), 228 | addEvent(document, 'touchmove', onTouchMove, { passive: false, capture: true }), 229 | addEvent(document, 'touchend', onTouchEnd, { passive: false, capture: true }), 230 | addEvent(document, 'focus', onFocus, true), 231 | addEvent(window, 'scroll', onWindowScroll), 232 | ); 233 | 234 | return () => { 235 | // Restore styles and scroll the page back to where it was. 236 | restoreStyles(); 237 | removeEvents(); 238 | window.scrollTo(scrollX, scrollY); 239 | }; 240 | } 241 | 242 | // Sets a CSS property on an element, and returns a function to revert it to the previous value. 243 | function setStyle(element: HTMLElement, style: keyof React.CSSProperties, value: string) { 244 | // https://github.com/microsoft/TypeScript/issues/17827#issuecomment-391663310 245 | // @ts-ignore 246 | let cur = element.style[style]; 247 | // @ts-ignore 248 | element.style[style] = value; 249 | 250 | return () => { 251 | // @ts-ignore 252 | element.style[style] = cur; 253 | }; 254 | } 255 | 256 | // Adds an event listener to an element, and returns a function to remove it. 257 | function addEvent( 258 | target: EventTarget, 259 | event: K, 260 | handler: (this: Document, ev: GlobalEventHandlersEventMap[K]) => any, 261 | options?: boolean | AddEventListenerOptions, 262 | ) { 263 | // @ts-ignore 264 | target.addEventListener(event, handler, options); 265 | 266 | return () => { 267 | // @ts-ignore 268 | target.removeEventListener(event, handler, options); 269 | }; 270 | } 271 | 272 | function scrollIntoView(target: Element) { 273 | let root = document.scrollingElement || document.documentElement; 274 | while (target && target !== root) { 275 | // Find the parent scrollable element and adjust the scroll position if the target is not already in view. 276 | let scrollable = getScrollParent(target); 277 | if (scrollable !== document.documentElement && scrollable !== document.body && scrollable !== target) { 278 | let scrollableTop = scrollable.getBoundingClientRect().top; 279 | let targetTop = target.getBoundingClientRect().top; 280 | let targetBottom = target.getBoundingClientRect().bottom; 281 | // Buffer is needed for some edge cases 282 | const keyboardHeight = scrollable.getBoundingClientRect().bottom + KEYBOARD_BUFFER; 283 | 284 | if (targetBottom > keyboardHeight) { 285 | scrollable.scrollTop += targetTop - scrollableTop; 286 | } 287 | } 288 | 289 | // @ts-ignore 290 | target = scrollable.parentElement; 291 | } 292 | } 293 | 294 | export function isInput(target: Element) { 295 | return ( 296 | (target instanceof HTMLInputElement && !nonTextInputTypes.has(target.type)) || 297 | target instanceof HTMLTextAreaElement || 298 | (target instanceof HTMLElement && target.isContentEditable) 299 | ); 300 | } 301 | -------------------------------------------------------------------------------- /src/use-scale-background.ts: -------------------------------------------------------------------------------- 1 | import React, { useMemo } from 'react'; 2 | import { useDrawerContext } from './context'; 3 | import { assignStyle, chain, isVertical, reset } from './helpers'; 4 | import { BORDER_RADIUS, TRANSITIONS, WINDOW_TOP_OFFSET } from './constants'; 5 | 6 | const noop = () => () => {}; 7 | 8 | export function useScaleBackground() { 9 | const { direction, isOpen, shouldScaleBackground, setBackgroundColorOnScale, noBodyStyles } = useDrawerContext(); 10 | const timeoutIdRef = React.useRef(null); 11 | const initialBackgroundColor = useMemo(() => document.body.style.backgroundColor, []); 12 | 13 | function getScale() { 14 | return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth; 15 | } 16 | 17 | React.useEffect(() => { 18 | if (isOpen && shouldScaleBackground) { 19 | if (timeoutIdRef.current) clearTimeout(timeoutIdRef.current); 20 | const wrapper = 21 | (document.querySelector('[data-vaul-drawer-wrapper]') as HTMLElement) || 22 | (document.querySelector('[vaul-drawer-wrapper]') as HTMLElement); 23 | 24 | if (!wrapper) return; 25 | 26 | chain( 27 | setBackgroundColorOnScale && !noBodyStyles ? assignStyle(document.body, { background: 'black' }) : noop, 28 | assignStyle(wrapper, { 29 | transformOrigin: isVertical(direction) ? 'top' : 'left', 30 | transitionProperty: 'transform, border-radius', 31 | transitionDuration: `${TRANSITIONS.DURATION}s`, 32 | transitionTimingFunction: `cubic-bezier(${TRANSITIONS.EASE.join(',')})`, 33 | }), 34 | ); 35 | 36 | const wrapperStylesCleanup = assignStyle(wrapper, { 37 | borderRadius: `${BORDER_RADIUS}px`, 38 | overflow: 'hidden', 39 | ...(isVertical(direction) 40 | ? { 41 | transform: `scale(${getScale()}) translate3d(0, calc(env(safe-area-inset-top) + 14px), 0)`, 42 | } 43 | : { 44 | transform: `scale(${getScale()}) translate3d(calc(env(safe-area-inset-top) + 14px), 0, 0)`, 45 | }), 46 | }); 47 | 48 | return () => { 49 | wrapperStylesCleanup(); 50 | timeoutIdRef.current = window.setTimeout(() => { 51 | if (initialBackgroundColor) { 52 | document.body.style.background = initialBackgroundColor; 53 | } else { 54 | document.body.style.removeProperty('background'); 55 | } 56 | }, TRANSITIONS.DURATION * 1000); 57 | }; 58 | } 59 | }, [isOpen, shouldScaleBackground, initialBackgroundColor]); 60 | } 61 | -------------------------------------------------------------------------------- /src/use-snap-points.ts: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { set, isVertical } from './helpers'; 3 | import { TRANSITIONS, VELOCITY_THRESHOLD } from './constants'; 4 | import { useControllableState } from './use-controllable-state'; 5 | import { DrawerDirection } from './types'; 6 | 7 | export function useSnapPoints({ 8 | activeSnapPointProp, 9 | setActiveSnapPointProp, 10 | snapPoints, 11 | drawerRef, 12 | overlayRef, 13 | fadeFromIndex, 14 | onSnapPointChange, 15 | direction = 'bottom', 16 | container, 17 | snapToSequentialPoint, 18 | }: { 19 | activeSnapPointProp?: number | string | null; 20 | setActiveSnapPointProp?(snapPoint: number | null | string): void; 21 | snapPoints?: (number | string)[]; 22 | fadeFromIndex?: number; 23 | drawerRef: React.RefObject; 24 | overlayRef: React.RefObject; 25 | onSnapPointChange(activeSnapPointIndex: number): void; 26 | direction?: DrawerDirection; 27 | container?: HTMLElement | null | undefined; 28 | snapToSequentialPoint?: boolean; 29 | }) { 30 | const [activeSnapPoint, setActiveSnapPoint] = useControllableState({ 31 | prop: activeSnapPointProp, 32 | defaultProp: snapPoints?.[0], 33 | onChange: setActiveSnapPointProp, 34 | }); 35 | 36 | const [windowDimensions, setWindowDimensions] = React.useState( 37 | typeof window !== 'undefined' 38 | ? { 39 | innerWidth: window.innerWidth, 40 | innerHeight: window.innerHeight, 41 | } 42 | : undefined, 43 | ); 44 | 45 | React.useEffect(() => { 46 | function onResize() { 47 | setWindowDimensions({ 48 | innerWidth: window.innerWidth, 49 | innerHeight: window.innerHeight, 50 | }); 51 | } 52 | window.addEventListener('resize', onResize); 53 | 54 | return () => window.removeEventListener('resize', onResize); 55 | }, []); 56 | 57 | const isLastSnapPoint = React.useMemo( 58 | () => activeSnapPoint === snapPoints?.[snapPoints.length - 1] || null, 59 | [snapPoints, activeSnapPoint], 60 | ); 61 | 62 | const activeSnapPointIndex = React.useMemo( 63 | () => snapPoints?.findIndex((snapPoint) => snapPoint === activeSnapPoint) ?? null, 64 | [snapPoints, activeSnapPoint], 65 | ); 66 | 67 | const shouldFade = 68 | (snapPoints && 69 | snapPoints.length > 0 && 70 | (fadeFromIndex || fadeFromIndex === 0) && 71 | !Number.isNaN(fadeFromIndex) && 72 | snapPoints[fadeFromIndex] === activeSnapPoint) || 73 | !snapPoints; 74 | 75 | const snapPointsOffset = React.useMemo(() => { 76 | const containerSize = container 77 | ? { width: container.getBoundingClientRect().width, height: container.getBoundingClientRect().height } 78 | : typeof window !== 'undefined' 79 | ? { width: window.innerWidth, height: window.innerHeight } 80 | : { width: 0, height: 0 }; 81 | 82 | return ( 83 | snapPoints?.map((snapPoint) => { 84 | const isPx = typeof snapPoint === 'string'; 85 | let snapPointAsNumber = 0; 86 | 87 | if (isPx) { 88 | snapPointAsNumber = parseInt(snapPoint, 10); 89 | } 90 | 91 | if (isVertical(direction)) { 92 | const height = isPx ? snapPointAsNumber : windowDimensions ? snapPoint * containerSize.height : 0; 93 | 94 | if (windowDimensions) { 95 | return direction === 'bottom' ? containerSize.height - height : -containerSize.height + height; 96 | } 97 | 98 | return height; 99 | } 100 | const width = isPx ? snapPointAsNumber : windowDimensions ? snapPoint * containerSize.width : 0; 101 | 102 | if (windowDimensions) { 103 | return direction === 'right' ? containerSize.width - width : -containerSize.width + width; 104 | } 105 | 106 | return width; 107 | }) ?? [] 108 | ); 109 | }, [snapPoints, windowDimensions, container]); 110 | 111 | const activeSnapPointOffset = React.useMemo( 112 | () => (activeSnapPointIndex !== null ? snapPointsOffset?.[activeSnapPointIndex] : null), 113 | [snapPointsOffset, activeSnapPointIndex], 114 | ); 115 | 116 | const snapToPoint = React.useCallback( 117 | (dimension: number) => { 118 | const newSnapPointIndex = snapPointsOffset?.findIndex((snapPointDim) => snapPointDim === dimension) ?? null; 119 | onSnapPointChange(newSnapPointIndex); 120 | 121 | set(drawerRef.current, { 122 | transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`, 123 | transform: isVertical(direction) ? `translate3d(0, ${dimension}px, 0)` : `translate3d(${dimension}px, 0, 0)`, 124 | }); 125 | 126 | if ( 127 | snapPointsOffset && 128 | newSnapPointIndex !== snapPointsOffset.length - 1 && 129 | fadeFromIndex !== undefined && 130 | newSnapPointIndex !== fadeFromIndex && 131 | newSnapPointIndex < fadeFromIndex 132 | ) { 133 | set(overlayRef.current, { 134 | transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`, 135 | opacity: '0', 136 | }); 137 | } else { 138 | set(overlayRef.current, { 139 | transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`, 140 | opacity: '1', 141 | }); 142 | } 143 | 144 | setActiveSnapPoint(snapPoints?.[Math.max(newSnapPointIndex, 0)]); 145 | }, 146 | [drawerRef.current, snapPoints, snapPointsOffset, fadeFromIndex, overlayRef, setActiveSnapPoint], 147 | ); 148 | 149 | React.useEffect(() => { 150 | if (activeSnapPoint || activeSnapPointProp) { 151 | const newIndex = 152 | snapPoints?.findIndex((snapPoint) => snapPoint === activeSnapPointProp || snapPoint === activeSnapPoint) ?? -1; 153 | if (snapPointsOffset && newIndex !== -1 && typeof snapPointsOffset[newIndex] === 'number') { 154 | snapToPoint(snapPointsOffset[newIndex] as number); 155 | } 156 | } 157 | }, [activeSnapPoint, activeSnapPointProp, snapPoints, snapPointsOffset, snapToPoint]); 158 | 159 | function onRelease({ 160 | draggedDistance, 161 | closeDrawer, 162 | velocity, 163 | dismissible, 164 | }: { 165 | draggedDistance: number; 166 | closeDrawer: () => void; 167 | velocity: number; 168 | dismissible: boolean; 169 | }) { 170 | if (fadeFromIndex === undefined) return; 171 | 172 | const currentPosition = 173 | direction === 'bottom' || direction === 'right' 174 | ? (activeSnapPointOffset ?? 0) - draggedDistance 175 | : (activeSnapPointOffset ?? 0) + draggedDistance; 176 | const isOverlaySnapPoint = activeSnapPointIndex === fadeFromIndex - 1; 177 | const isFirst = activeSnapPointIndex === 0; 178 | const hasDraggedUp = draggedDistance > 0; 179 | 180 | if (isOverlaySnapPoint) { 181 | set(overlayRef.current, { 182 | transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`, 183 | }); 184 | } 185 | 186 | if (!snapToSequentialPoint && velocity > 2 && !hasDraggedUp) { 187 | if (dismissible) closeDrawer(); 188 | else snapToPoint(snapPointsOffset[0]); // snap to initial point 189 | return; 190 | } 191 | 192 | if (!snapToSequentialPoint && velocity > 2 && hasDraggedUp && snapPointsOffset && snapPoints) { 193 | snapToPoint(snapPointsOffset[snapPoints.length - 1] as number); 194 | return; 195 | } 196 | 197 | // Find the closest snap point to the current position 198 | const closestSnapPoint = snapPointsOffset?.reduce((prev, curr) => { 199 | if (typeof prev !== 'number' || typeof curr !== 'number') return prev; 200 | 201 | return Math.abs(curr - currentPosition) < Math.abs(prev - currentPosition) ? curr : prev; 202 | }); 203 | 204 | const dim = isVertical(direction) ? window.innerHeight : window.innerWidth; 205 | if (velocity > VELOCITY_THRESHOLD && Math.abs(draggedDistance) < dim * 0.4) { 206 | const dragDirection = hasDraggedUp ? 1 : -1; // 1 = up, -1 = down 207 | 208 | // Don't do anything if we swipe upwards while being on the last snap point 209 | if (dragDirection > 0 && isLastSnapPoint && snapPoints) { 210 | snapToPoint(snapPointsOffset[snapPoints.length - 1]); 211 | return; 212 | } 213 | 214 | if (isFirst && dragDirection < 0 && dismissible) { 215 | closeDrawer(); 216 | } 217 | 218 | if (activeSnapPointIndex === null) return; 219 | 220 | snapToPoint(snapPointsOffset[activeSnapPointIndex + dragDirection]); 221 | return; 222 | } 223 | 224 | snapToPoint(closestSnapPoint); 225 | } 226 | 227 | function onDrag({ draggedDistance }: { draggedDistance: number }) { 228 | if (activeSnapPointOffset === null) return; 229 | const newValue = 230 | direction === 'bottom' || direction === 'right' 231 | ? activeSnapPointOffset - draggedDistance 232 | : activeSnapPointOffset + draggedDistance; 233 | 234 | // Don't do anything if we exceed the last(biggest) snap point 235 | if ((direction === 'bottom' || direction === 'right') && newValue < snapPointsOffset[snapPointsOffset.length - 1]) { 236 | return; 237 | } 238 | if ((direction === 'top' || direction === 'left') && newValue > snapPointsOffset[snapPointsOffset.length - 1]) { 239 | return; 240 | } 241 | 242 | set(drawerRef.current, { 243 | transform: isVertical(direction) ? `translate3d(0, ${newValue}px, 0)` : `translate3d(${newValue}px, 0, 0)`, 244 | }); 245 | } 246 | 247 | function getPercentageDragged(absDraggedDistance: number, isDraggingDown: boolean) { 248 | if (!snapPoints || typeof activeSnapPointIndex !== 'number' || !snapPointsOffset || fadeFromIndex === undefined) 249 | return null; 250 | 251 | // If this is true we are dragging to a snap point that is supposed to have an overlay 252 | const isOverlaySnapPoint = activeSnapPointIndex === fadeFromIndex - 1; 253 | const isOverlaySnapPointOrHigher = activeSnapPointIndex >= fadeFromIndex; 254 | 255 | if (isOverlaySnapPointOrHigher && isDraggingDown) { 256 | return 0; 257 | } 258 | 259 | // Don't animate, but still use this one if we are dragging away from the overlaySnapPoint 260 | if (isOverlaySnapPoint && !isDraggingDown) return 1; 261 | if (!shouldFade && !isOverlaySnapPoint) return null; 262 | 263 | // Either fadeFrom index or the one before 264 | const targetSnapPointIndex = isOverlaySnapPoint ? activeSnapPointIndex + 1 : activeSnapPointIndex - 1; 265 | 266 | // Get the distance from overlaySnapPoint to the one before or vice-versa to calculate the opacity percentage accordingly 267 | const snapPointDistance = isOverlaySnapPoint 268 | ? snapPointsOffset[targetSnapPointIndex] - snapPointsOffset[targetSnapPointIndex - 1] 269 | : snapPointsOffset[targetSnapPointIndex + 1] - snapPointsOffset[targetSnapPointIndex]; 270 | 271 | const percentageDragged = absDraggedDistance / Math.abs(snapPointDistance); 272 | 273 | if (isOverlaySnapPoint) { 274 | return 1 - percentageDragged; 275 | } else { 276 | return percentageDragged; 277 | } 278 | } 279 | 280 | return { 281 | isLastSnapPoint, 282 | activeSnapPoint, 283 | shouldFade, 284 | getPercentageDragged, 285 | setActiveSnapPoint, 286 | activeSnapPointIndex, 287 | onRelease, 288 | onDrag, 289 | snapPointsOffset, 290 | }; 291 | } 292 | -------------------------------------------------------------------------------- /test/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "next/core-web-vitals" 3 | } 4 | -------------------------------------------------------------------------------- /test/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env*.local 29 | 30 | # vercel 31 | .vercel 32 | 33 | # typescript 34 | *.tsbuildinfo 35 | next-env.d.ts 36 | -------------------------------------------------------------------------------- /test/README.md: -------------------------------------------------------------------------------- 1 | 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). 2 | 3 | ## Getting Started 4 | 5 | First, run the development server: 6 | 7 | ```bash 8 | npm run dev 9 | # or 10 | yarn dev 11 | # or 12 | pnpm dev 13 | ``` 14 | 15 | Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. 16 | 17 | You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. 18 | 19 | This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font. 20 | 21 | ## Learn More 22 | 23 | To learn more about Next.js, take a look at the following resources: 24 | 25 | - [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. 26 | - [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. 27 | 28 | You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! 29 | 30 | ## Deploy on Vercel 31 | 32 | 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. 33 | 34 | Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. 35 | -------------------------------------------------------------------------------- /test/next.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('next').NextConfig} */ 2 | const nextConfig = { 3 | experimental: { 4 | typedRoutes: true, 5 | }, 6 | }; 7 | 8 | module.exports = nextConfig; 9 | -------------------------------------------------------------------------------- /test/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "test", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "dev": "next dev", 7 | "build": "next build", 8 | "start": "next start", 9 | "lint": "next lint" 10 | }, 11 | "devDependencies": { 12 | "@types/node": "20.5.7", 13 | "@types/react": "18.2.55", 14 | "@types/react-dom": "18.2.18", 15 | "autoprefixer": "10.4.15", 16 | "eslint": "8.48.0", 17 | "eslint-config-next": "13.5.1", 18 | "postcss": "8.4.29", 19 | "typescript": "5.2.2" 20 | }, 21 | "dependencies": { 22 | "clsx": "^2.0.0", 23 | "next": "13.5.1", 24 | "react": "18.2.0", 25 | "react-dom": "18.2.0", 26 | "tailwindcss": "3.3.3", 27 | "vaul": "workspace:^" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test/postcss.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | }; 7 | -------------------------------------------------------------------------------- /test/public/next.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/public/vercel.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test/src/app/controlled/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { useState } from 'react'; 4 | import { Drawer } from 'vaul'; 5 | 6 | export default function Page() { 7 | const [open, setOpen] = useState(false); 8 | const [fullyControlled, setFullyControlled] = useState(false); 9 | 10 | return ( 11 |
12 | 13 | setOpen(true)}> 14 | 17 | 18 | 19 | 20 | 24 | Close 25 | 28 |
29 |
30 |
31 | Unstyled drawer for React. 32 |

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

35 |

36 | It uses{' '} 37 | 42 | Radix's Dialog primitive 43 | {' '} 44 | under the hood and is inspired by{' '} 45 | 50 | this tweet. 51 | 52 |

53 |
54 |
55 | 105 | 106 | 107 | 108 | setFullyControlled(o)}> 109 | 110 | 113 | 114 | 115 | 116 | 120 | Close 121 | 124 |
125 |
126 |
127 | Unstyled drawer for React. 128 |

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

131 |

132 | It uses{' '} 133 | 138 | Radix's Dialog primitive 139 | {' '} 140 | under the hood and is inspired by{' '} 141 | 146 | this tweet. 147 | 148 |

149 |
150 |
151 | 201 | 202 | 203 | 204 |
205 | ); 206 | } 207 | -------------------------------------------------------------------------------- /test/src/app/default-open/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { useState } from 'react'; 4 | import { Drawer } from 'vaul'; 5 | 6 | export default function Page() { 7 | return ( 8 |
9 | 10 | 11 | 14 | 15 | 16 | 17 | 21 | Close 22 | 25 |
26 |
27 |
28 | Unstyled drawer for React. 29 |

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

32 |

33 | It uses{' '} 34 | 39 | Radix's Dialog primitive 40 | {' '} 41 | under the hood and is inspired by{' '} 42 | 47 | this tweet. 48 | 49 |

50 |
51 |
52 | 102 | 103 | 104 | 105 | 106 | 107 | 110 | 111 | 112 | 113 | 117 | Close 118 | 121 |
122 |
123 |
124 | Unstyled drawer for React. 125 |

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

128 |

129 | It uses{' '} 130 | 135 | Radix's Dialog primitive 136 | {' '} 137 | under the hood and is inspired by{' '} 138 | 143 | this tweet. 144 | 145 |

146 |
147 |
148 | 198 | 199 | 200 | 201 |
202 | ); 203 | } 204 | -------------------------------------------------------------------------------- /test/src/app/different-directions/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import clsx from 'clsx'; 4 | import { Drawer, DialogProps } from 'vaul'; 5 | 6 | function DirectionalDrawer({ 7 | direction, 8 | children, 9 | }: { 10 | direction: DialogProps['direction']; 11 | children: React.ReactNode; 12 | }) { 13 | return ( 14 | 15 | 16 | 19 | 20 | 21 | 22 | 31 | Close 32 | 35 |
36 |
37 |
38 | Unstyled drawer for React. 39 |

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

42 |

43 | It uses{' '} 44 | 49 | Radix's Dialog primitive 50 | {' '} 51 | under the hood and is inspired by{' '} 52 | 57 | this tweet. 58 | 59 |

60 |
61 |
62 | 112 | 113 | 114 | 115 | ); 116 | } 117 | 118 | export default function Page() { 119 | return ( 120 |
121 | Top 122 | Right 123 | Bottom 124 | Left 125 |
126 | ); 127 | } 128 | -------------------------------------------------------------------------------- /test/src/app/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | 5 | body, 6 | main { 7 | min-height: 500vh; 8 | } 9 | 10 | html { 11 | height: -webkit-fill-available; 12 | } 13 | 14 | a { 15 | text-decoration-thickness: 1px; 16 | text-underline-offset: 2px; 17 | text-decoration: underline; 18 | } 19 | -------------------------------------------------------------------------------- /test/src/app/initial-snap/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { clsx } from 'clsx'; 4 | import { useState } from 'react'; 5 | import { Drawer } from 'vaul'; 6 | 7 | const snapPoints = [0, '148px', '355px', 1]; 8 | 9 | export default function Page() { 10 | const [snap, setSnap] = useState(snapPoints[1]); 11 | 12 | const activeSnapPointIndex = snapPoints.indexOf(snap as string); 13 | 14 | return ( 15 |
16 |
{activeSnapPointIndex}
17 | 18 | 19 | 20 | 21 | 22 | 23 | 27 |
33 |
34 | 46 | 58 | 70 | 82 | 94 |
{' '} 95 |

The Hidden Details

96 |

2 modules, 27 hours of video

97 |

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

101 | 104 |
105 |

Module 01. The Details

106 |
107 |
108 | Layers of UI 109 | A basic introduction to Layers of Design. 110 |
111 |
112 | Typography 113 | The fundamentals of type. 114 |
115 |
116 | UI Animations 117 | Going through the right easings and durations. 118 |
119 |
120 |
121 |
122 |
123 |
124 | “I especially loved the hidden details video. That was so useful, learned a lot by just reading it. 125 | Can’t wait for more course content!” 126 |
127 |
128 | Yvonne Ray, Frontend Developer 129 |
130 |
131 |
132 |
133 |

Module 02. The Process

134 |
135 |
136 | Build 137 | Create cool components to practice. 138 |
139 |
140 | User Insight 141 | Find out what users think and fine-tune. 142 |
143 |
144 | Putting it all together 145 | Let's build an app together and apply everything. 146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 | ); 155 | } 156 | -------------------------------------------------------------------------------- /test/src/app/layout.tsx: -------------------------------------------------------------------------------- 1 | import './globals.css'; 2 | import type { Metadata } from 'next'; 3 | import { Inter } from 'next/font/google'; 4 | 5 | const inter = Inter({ subsets: ['latin'] }); 6 | 7 | export const metadata: Metadata = { 8 | title: 'Create Next App', 9 | description: 'Generated by create next app', 10 | }; 11 | 12 | export default function RootLayout({ children }: { children: React.ReactNode }) { 13 | return ( 14 | 15 | {children} 16 | 17 | ); 18 | } 19 | -------------------------------------------------------------------------------- /test/src/app/nested-drawers/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { Drawer } from 'vaul'; 4 | 5 | export default function Page() { 6 | return ( 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 | 18 |
19 |
20 |
21 | Drawer for React. 22 |

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

25 |

It comes unstyled and has gesture-driven animations.

26 |

27 | It uses{' '} 28 | 33 | Radix’s Dialog primitive 34 | {' '} 35 | under the hood and is inspired by{' '} 36 | 41 | this tweet. 42 | 43 |

44 | 45 | 49 | Open Second Drawer 50 | 51 | 52 | 53 | 57 | Close 58 |
59 |
60 |
61 | This drawer is nested. 62 |

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

66 |

67 | You can view more examples{' '} 68 | 73 | here 74 | 75 | . 76 |

77 |
78 |
79 | 129 | 130 | 131 | 132 |
133 |
134 | 184 | 185 | 186 | 187 |
188 | ); 189 | } 190 | -------------------------------------------------------------------------------- /test/src/app/non-dismissible/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { useState } from 'react'; 4 | import { Drawer } from 'vaul'; 5 | 6 | export default function Page() { 7 | const [open, setOpen] = useState(false); 8 | return ( 9 |
10 | 11 | setOpen(true)}> 12 | 13 | 14 | 15 | 16 | 20 |
21 |
22 |
23 | Unstyled drawer for React. 24 |

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

27 |

28 | It uses{' '} 29 | 34 | Radix’s Dialog primitive 35 | {' '} 36 | under the hood and is inspired by{' '} 37 | 42 | this tweet. 43 | 44 |

45 | 46 | 54 |
55 |
56 | 106 | 107 | 108 | 109 |
110 | ); 111 | } 112 | -------------------------------------------------------------------------------- /test/src/app/open-another-drawer/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { Drawer } from 'vaul'; 4 | import { useState } from 'react'; 5 | 6 | export function MyDrawer({ 7 | open, 8 | setOpen, 9 | setOpen2, 10 | }: { 11 | open: boolean; 12 | setOpen: (open: boolean) => void; 13 | setOpen2: (open: boolean) => void; 14 | }) { 15 | return ( 16 | 17 | setOpen(true)}> 18 | 19 | 20 | 21 | 22 | 23 |
24 |
25 |
26 | Unstyled drawer for React. 27 | 28 | 38 |
39 |
40 | 90 | 91 | 92 | 93 | ); 94 | } 95 | 96 | export function MyDrawer2({ open, setOpen }: { open: boolean; setOpen: (open: boolean) => void }) { 97 | return ( 98 | 99 | 100 | 101 | 102 |
103 |
104 |
105 | Unstyled drawer for React. 106 |

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

109 |

110 | It uses{' '} 111 | 116 | Radix’s Dialog primitive 117 | {' '} 118 | under the hood and is inspired by{' '} 119 | 124 | this tweet. 125 | 126 |

127 | 128 | 137 |
138 |
139 | 189 | 190 | 191 | 192 | ); 193 | } 194 | 195 | export default function Home() { 196 | const [open, setOpen] = useState(false); 197 | const [open2, setOpen2] = useState(false); 198 | 199 | return ( 200 |
201 |

scroll down

202 | 203 | 204 |

scroll down

205 |
206 | ); 207 | } 208 | -------------------------------------------------------------------------------- /test/src/app/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import Link from 'next/link'; 4 | 5 | export default function Page() { 6 | return ( 7 |
8 | With scaled background 9 | Without scaled background 10 | With snap points 11 | With modal false 12 | Scrollable with inputs 13 | Nested drawers 14 | Non-dismissible 15 | Initial snap 16 | Controlled 17 | Default open 18 | With redirect 19 | Different directions 20 |
21 | ); 22 | } 23 | -------------------------------------------------------------------------------- /test/src/app/parent-container/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | import clsx from 'clsx'; 3 | import { useState } from 'react'; 4 | import { Drawer } from 'vaul'; 5 | 6 | export default function Page() { 7 | return ( 8 |
9 | 10 | 11 |
12 | ); 13 | } 14 | 15 | function Default() { 16 | const [parent, setParent] = useState(null); 17 | 18 | return ( 19 |
20 |

Default

21 |
25 | 26 | Open Drawer 27 | 28 | 29 | 30 | Unstyled drawer for React. 31 | 32 | 33 | 34 |
35 |
36 | ); 37 | } 38 | 39 | function WithNested() { 40 | const [parent, setParent] = useState(null); 41 | 42 | return ( 43 |
44 |

With Nested

45 |
49 | 50 | Open Drawer 51 | 52 | 53 | 54 | Unstyled drawer for React. 55 | 56 | Open nested drawer 57 | 58 | 59 | 60 | Unstyled drawer for React. 61 | 62 | 63 | 64 | 65 | 66 | 67 |
68 |
69 | ); 70 | } 71 | -------------------------------------------------------------------------------- /test/src/app/scrollable-page/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { useState } from 'react'; 4 | import { Drawer } from 'vaul'; 5 | 6 | export default function Page() { 7 | const [open, setOpen] = useState(false); 8 | 9 | return ( 10 |
14 |

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

19 |

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

24 |

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

29 |

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

34 |

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

39 |

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

44 |

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

49 |

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

54 |

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

59 |

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

64 |

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

69 | 70 | setOpen(true)}> 71 | 74 | 75 | 76 | 77 | 81 |
82 |
83 |
84 | Unstyled drawer for React. 85 |

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

88 |

89 | It uses{' '} 90 | 95 | Radix's Dialog primitive 96 | {' '} 97 | under the hood and is inspired by{' '} 98 | 103 | this tweet. 104 | 105 |

106 |
107 |
108 | 158 | 159 | 160 | 161 |
162 | ); 163 | } 164 | -------------------------------------------------------------------------------- /test/src/app/scrollable-with-inputs/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { Drawer } from 'vaul'; 4 | 5 | export default function Page() { 6 | return ( 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 |

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

29 | 30 |

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

42 | 43 |
44 |
45 |
46 |
47 |
48 | ); 49 | } 50 | -------------------------------------------------------------------------------- /test/src/app/with-handle/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { clsx } from 'clsx'; 4 | import { useState } from 'react'; 5 | import { Drawer } from 'vaul'; 6 | 7 | const snapPoints = ['148px', '355px']; 8 | 9 | export default function Page() { 10 | const [snap, setSnap] = useState(snapPoints[0]); 11 | 12 | const activeSnapPointIndex = snapPoints.indexOf(snap as string); 13 | 14 | return ( 15 |
16 |
{activeSnapPointIndex}
17 | 18 | 19 | 20 | 21 | 22 | 23 | 27 | 28 |
34 |
35 | 47 | 59 | 71 | 83 | 95 |
{' '} 96 |

The Hidden Details

97 |

2 modules, 27 hours of video

98 |

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

102 | 105 |
106 |

Module 01. The Details

107 |
108 |
109 | Layers of UI 110 | A basic introduction to Layers of Design. 111 |
112 |
113 | Typography 114 | The fundamentals of type. 115 |
116 |
117 | UI Animations 118 | Going through the right easings and durations. 119 |
120 |
121 |
122 |
123 |
124 |
125 | “I especially loved the hidden details video. That was so useful, learned a lot by just reading it. 126 | Can’t wait for more course content!” 127 |
128 |
129 | Yvonne Ray, Frontend Developer 130 |
131 |
132 |
133 |
134 |

Module 02. The Process

135 |
136 |
137 | Build 138 | Create cool components to practice. 139 |
140 |
141 | User Insight 142 | Find out what users think and fine-tune. 143 |
144 |
145 | Putting it all together 146 | Let's build an app together and apply everything. 147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 | ); 156 | } 157 | -------------------------------------------------------------------------------- /test/src/app/with-modal-false/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { clsx } from 'clsx'; 4 | import { useState } from 'react'; 5 | import { Drawer } from 'vaul'; 6 | 7 | const snapPoints = ['148px', '355px', 1]; 8 | 9 | export default function Page() { 10 | const [snap, setSnap] = useState(snapPoints[0]); 11 | 12 | const activeSnapPointIndex = snapPoints.indexOf(snap as string); 13 | 14 | return ( 15 |
16 |
{activeSnapPointIndex}
17 | 18 | 19 | 20 | 21 | 22 | 23 | 27 |
33 |
34 | 46 | 58 | 70 | 82 | 94 |
{' '} 95 |

The Hidden Details

96 |

2 modules, 27 hours of video

97 |

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

101 | 104 |
105 |

Module 01. The Details

106 |
107 |
108 | Layers of UI 109 | A basic introduction to Layers of Design. 110 |
111 |
112 | Typography 113 | The fundamentals of type. 114 |
115 |
116 | UI Animations 117 | Going through the right easings and durations. 118 |
119 |
120 |
121 |
122 |
123 |
124 | “I especially loved the hidden details video. That was so useful, learned a lot by just reading it. 125 | Can’t wait for more course content!” 126 |
127 |
128 | Yvonne Ray, Frontend Developer 129 |
130 |
131 |
132 |
133 |

Module 02. The Process

134 |
135 |
136 | Build 137 | Create cool components to practice. 138 |
139 |
140 | User Insight 141 | Find out what users think and fine-tune. 142 |
143 |
144 | Putting it all together 145 | Let's build an app together and apply everything. 146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 | ); 155 | } 156 | -------------------------------------------------------------------------------- /test/src/app/with-redirect/long-page/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | export default function Page() { 4 | return ( 5 |
6 |

scroll down

7 |

content only visible after scroll

8 |
9 | ); 10 | } 11 | -------------------------------------------------------------------------------- /test/src/app/with-redirect/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import Link from 'next/link'; 4 | import { Drawer } from 'vaul'; 5 | 6 | export default function Page() { 7 | return ( 8 |
9 | 10 | 11 | 14 | 15 | 16 | 17 | 21 | Close 22 |
23 |
24 |
25 | Redirect to another route. 26 |

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

27 |

28 | Go to{' '} 29 | 30 | another route 31 | {' '} 32 |

33 |
34 |
35 | 36 | 37 | 38 |
39 | ); 40 | } 41 | -------------------------------------------------------------------------------- /test/src/app/with-scaled-background/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { useState } from 'react'; 4 | import clsx from 'clsx'; 5 | import { Drawer } from 'vaul'; 6 | import { DrawerDirection } from 'vaul/src/types'; 7 | 8 | const CenteredContent = () => { 9 | return ( 10 |
11 | Unstyled drawer for React. 12 |

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

15 |

16 | It uses{' '} 17 | 18 | Radix's Dialog primitive 19 | {' '} 20 | under the hood and is inspired by{' '} 21 | 22 | this tweet. 23 | 24 |

25 |
26 | ); 27 | }; 28 | 29 | const DrawerContent = ({ drawerDirection }: { drawerDirection: DrawerDirection }) => { 30 | return ( 31 | 41 |
50 |
57 |
58 | 59 |
60 |
61 | 62 | ); 63 | }; 64 | 65 | export default function Page() { 66 | const [direction, setDirection] = useState('bottom'); 67 | 68 | return ( 69 |
73 | 83 | 84 | 85 | 88 | 89 | 90 | 91 | 92 | 93 | 94 |
95 | ); 96 | } 97 | -------------------------------------------------------------------------------- /test/src/app/with-snap-points/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { clsx } from 'clsx'; 4 | import { useState } from 'react'; 5 | import { Drawer } from 'vaul'; 6 | 7 | const snapPoints = ['148px', '355px', 1]; 8 | 9 | export default function Page() { 10 | const [snap, setSnap] = useState(snapPoints[0]); 11 | 12 | const activeSnapPointIndex = snapPoints.indexOf(snap as string); 13 | 14 | return ( 15 |
16 |
{activeSnapPointIndex}
17 | 18 | 19 | 20 | 21 | 22 | 23 | 27 |
33 |
34 | 46 | 58 | 70 | 82 | 94 |
{' '} 95 |

The Hidden Details

96 |

2 modules, 27 hours of video

97 |

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

101 | 104 |
105 |

Module 01. The Details

106 |
107 |
108 | Layers of UI 109 | A basic introduction to Layers of Design. 110 |
111 |
112 | Typography 113 | The fundamentals of type. 114 |
115 |
116 | UI Animations 117 | Going through the right easings and durations. 118 |
119 |
120 |
121 |
122 |
123 |
124 | “I especially loved the hidden details video. That was so useful, learned a lot by just reading it. 125 | Can’t wait for more course content!” 126 |
127 |
128 | Yvonne Ray, Frontend Developer 129 |
130 |
131 |
132 |
133 |

Module 02. The Process

134 |
135 |
136 | Build 137 | Create cool components to practice. 138 |
139 |
140 | User Insight 141 | Find out what users think and fine-tune. 142 |
143 |
144 | Putting it all together 145 | Let's build an app together and apply everything. 146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 | ); 155 | } 156 | -------------------------------------------------------------------------------- /test/src/app/without-scaled-background/page.tsx: -------------------------------------------------------------------------------- 1 | 'use client'; 2 | 3 | import { useState } from 'react'; 4 | import { Drawer } from 'vaul'; 5 | 6 | export default function Page() { 7 | const [open, setOpen] = useState(false); 8 | const [parent, setParent] = useState(null); 9 | 10 | return ( 11 |
12 |
13 | 14 | 15 | 18 | 19 | 20 | 21 | 25 | Close 26 | 29 |
30 |
31 |
32 | Unstyled drawer for React. 33 |

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

36 |

37 | It uses{' '} 38 | 43 | Radix's Dialog primitive 44 | {' '} 45 | under the hood and is inspired by{' '} 46 | 51 | this tweet. 52 | 53 |

54 |
55 |
56 | 106 | 107 | 108 | 109 |
110 | ); 111 | } 112 | -------------------------------------------------------------------------------- /test/tailwind.config.ts: -------------------------------------------------------------------------------- 1 | import type { Config } from 'tailwindcss'; 2 | 3 | const config: Config = { 4 | content: [ 5 | './src/pages/**/*.{js,ts,jsx,tsx,mdx}', 6 | './src/components/**/*.{js,ts,jsx,tsx,mdx}', 7 | './src/app/**/*.{js,ts,jsx,tsx,mdx}', 8 | ], 9 | theme: { 10 | extend: { 11 | backgroundImage: { 12 | 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 13 | 'gradient-conic': 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', 14 | }, 15 | }, 16 | }, 17 | plugins: [], 18 | }; 19 | export default config; 20 | -------------------------------------------------------------------------------- /test/tests/base.spec.ts: -------------------------------------------------------------------------------- 1 | import { test, expect } from '@playwright/test'; 2 | import { ANIMATION_DURATION } from './constants'; 3 | import { openDrawer } from './helpers'; 4 | 5 | test.beforeEach(async ({ page }) => { 6 | await page.goto('/without-scaled-background'); 7 | }); 8 | 9 | test.describe('Base tests', () => { 10 | test('should open drawer', async ({ page }) => { 11 | await expect(page.getByTestId('content')).not.toBeVisible(); 12 | 13 | await page.getByTestId('trigger').click(); 14 | 15 | await expect(page.getByTestId('content')).toBeVisible(); 16 | }); 17 | 18 | test('should close on background interaction', async ({ page }) => { 19 | await openDrawer(page); 20 | // Click on the background 21 | await page.mouse.click(0, 0); 22 | 23 | await page.waitForTimeout(ANIMATION_DURATION); 24 | await expect(page.getByTestId('content')).not.toBeVisible(); 25 | }); 26 | 27 | test('should close when `Drawer.Close` is clicked', async ({ page }) => { 28 | await openDrawer(page); 29 | 30 | await page.getByTestId('drawer-close').click(); 31 | await page.waitForTimeout(ANIMATION_DURATION); 32 | await expect(page.getByTestId('content')).not.toBeVisible(); 33 | }); 34 | 35 | test('should close when controlled', async ({ page }) => { 36 | await openDrawer(page); 37 | 38 | await page.getByTestId('controlled-close').click(); 39 | await page.waitForTimeout(ANIMATION_DURATION); 40 | await expect(page.getByTestId('content')).not.toBeVisible(); 41 | }); 42 | 43 | test('should be open by defafult when `defaultOpen` is true', async ({ page }) => { 44 | await page.goto('/default-open'); 45 | 46 | await expect(page.getByTestId('content')).toBeVisible(); 47 | }); 48 | 49 | test('should close when dragged down', async ({ page }) => { 50 | await openDrawer(page); 51 | await page.hover('[data-vaul-drawer]'); 52 | await page.mouse.down(); 53 | await page.mouse.move(0, 800); 54 | await page.mouse.up(); 55 | await page.waitForTimeout(ANIMATION_DURATION); 56 | await expect(page.getByTestId('content')).not.toBeVisible(); 57 | }); 58 | 59 | test('should not close when dragged up', async ({ page }) => { 60 | await openDrawer(page); 61 | await page.hover('[data-vaul-drawer]'); 62 | await page.mouse.down(); 63 | await page.mouse.move(0, -800); 64 | await page.mouse.up(); 65 | await page.waitForTimeout(ANIMATION_DURATION); 66 | await expect(page.getByTestId('content')).toBeVisible(); 67 | }); 68 | }); 69 | 70 | test('should close when dragged down and cancelled', async ({ page }) => { 71 | await openDrawer(page); 72 | await page.hover('[data-vaul-drawer]'); 73 | await page.mouse.down(); 74 | await page.mouse.move(0, 800); 75 | await page.dispatchEvent('[data-vaul-drawer]', 'contextmenu'); 76 | await page.waitForTimeout(ANIMATION_DURATION); 77 | await expect(page.getByTestId('content')).not.toBeVisible(); 78 | }); 79 | -------------------------------------------------------------------------------- /test/tests/constants.ts: -------------------------------------------------------------------------------- 1 | export const ANIMATION_DURATION = 500; 2 | -------------------------------------------------------------------------------- /test/tests/controlled.spec.ts: -------------------------------------------------------------------------------- 1 | import { expect, test } from '@playwright/test'; 2 | import { ANIMATION_DURATION } from './constants'; 3 | 4 | test.beforeEach(async ({ page }) => { 5 | await page.goto('/controlled'); 6 | }); 7 | 8 | test.describe('Initial-snap', () => { 9 | test('should not close when clicked on overlay and only the open prop is passsed', async ({ page }) => { 10 | await expect(page.getByTestId('content')).not.toBeVisible(); 11 | await page.getByTestId('trigger').click(); 12 | await expect(page.getByTestId('content')).toBeVisible(); 13 | // Click on the background 14 | await page.mouse.click(0, 0); 15 | 16 | await page.waitForTimeout(ANIMATION_DURATION); 17 | await expect(page.getByTestId('content')).toBeVisible(); 18 | }); 19 | 20 | test('should close when clicked on overlay and open and onOpenChange props are passed', async ({ page }) => { 21 | await expect(page.getByTestId('fully-controlled-content')).not.toBeVisible(); 22 | await page.getByTestId('fully-controlled-trigger').click(); 23 | await expect(page.getByTestId('fully-controlled-content')).toBeVisible(); 24 | // Click on the background 25 | await page.mouse.click(0, 0); 26 | 27 | await page.waitForTimeout(ANIMATION_DURATION); 28 | await expect(page.getByTestId('fully-controlled-content')).not.toBeVisible(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/tests/helpers.ts: -------------------------------------------------------------------------------- 1 | import { expect, Page } from '@playwright/test'; 2 | import { ANIMATION_DURATION } from './constants'; 3 | 4 | export async function openDrawer(page: Page) { 5 | await expect(page.getByTestId('content')).not.toBeVisible(); 6 | await page.getByTestId('trigger').click(); 7 | await page.waitForTimeout(ANIMATION_DURATION); 8 | await expect(page.getByTestId('content')).toBeVisible(); 9 | } 10 | 11 | export async function dragWithSpeed( 12 | page: Page, 13 | selector: string, 14 | startY: number, 15 | endY: number, 16 | speed: number = 10, 17 | ): Promise { 18 | const startX = 0; 19 | const distance = Math.abs(endY - startY); 20 | const steps = distance / speed; 21 | const delayPerStep = 10; // in milliseconds 22 | const yOffset = (endY - startY) / steps; 23 | 24 | await page.hover(selector); 25 | await page.mouse.down(); 26 | await page.mouse.move(0, -200); 27 | await page.mouse.up(); 28 | } 29 | -------------------------------------------------------------------------------- /test/tests/initial-snap.spec.ts: -------------------------------------------------------------------------------- 1 | import { Page, expect, test } from '@playwright/test'; 2 | import { ANIMATION_DURATION } from './constants'; 3 | 4 | test.beforeEach(async ({ page }) => { 5 | await page.goto('/initial-snap'); 6 | }); 7 | 8 | const snapPointYPositions = { 9 | 0: 800, 10 | 1: 600, 11 | 2: 590, 12 | 3: 200, 13 | } as const; 14 | 15 | const snapTo = async (page: Page, snapPointIndex: keyof typeof snapPointYPositions) => { 16 | await page.hover('[data-vaul-drawer]'); 17 | await page.mouse.down(); 18 | await page.mouse.move(0, snapPointYPositions[snapPointIndex]); 19 | await page.mouse.up(); 20 | await page.waitForTimeout(ANIMATION_DURATION); 21 | }; 22 | 23 | test.describe('Initial-snap', () => { 24 | test('should be open and snapped on initial load', async ({ page }) => { 25 | await page.waitForTimeout(ANIMATION_DURATION); 26 | 27 | await expect(page.getByTestId('content')).toBeVisible(); 28 | await expect(page.getByTestId('active-snap-index')).toHaveText('1'); 29 | }); 30 | 31 | // test('should snap to next snap point when dragged up', async ({ page }) => { 32 | // snapTo(page, 2); 33 | 34 | // await expect(page.getByTestId('active-snap-index')).toHaveText('2'); 35 | // }); 36 | 37 | // test('should snap to last snap point when dragged up', async ({ page }) => { 38 | // snapTo(page, 3); 39 | 40 | // await expect(page.getByTestId('active-snap-index')).toHaveText('3'); 41 | // }); 42 | 43 | // test('should snap to 0 when dragged down', async ({ page }) => { 44 | // snapTo(page, 0); 45 | 46 | // await expect(page.getByTestId('active-snap-index')).toHaveText('0'); 47 | // }); 48 | }); 49 | -------------------------------------------------------------------------------- /test/tests/nested.spec.ts: -------------------------------------------------------------------------------- 1 | import { test, expect } from '@playwright/test'; 2 | import { ANIMATION_DURATION } from './constants'; 3 | import { openDrawer } from './helpers'; 4 | 5 | test.beforeEach(async ({ page }) => { 6 | await page.goto('/nested-drawers'); 7 | }); 8 | 9 | test.describe('Nested tests', () => { 10 | test('should open and close nested drawer', async ({ page }) => { 11 | await openDrawer(page); 12 | await page.getByTestId('nested-trigger').click(); 13 | await page.waitForTimeout(ANIMATION_DURATION); 14 | await expect(page.getByTestId('nested-content')).toBeVisible(); 15 | await page.getByTestId('nested-close').click(); 16 | await page.waitForTimeout(ANIMATION_DURATION); 17 | await expect(page.getByTestId('nested-content')).not.toBeVisible(); 18 | await await expect(page.getByTestId('content')).toBeVisible(); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /test/tests/non-dismissible.spec.ts: -------------------------------------------------------------------------------- 1 | import { test, expect } from '@playwright/test'; 2 | import { openDrawer } from './helpers'; 3 | import { ANIMATION_DURATION } from './constants'; 4 | 5 | test.beforeEach(async ({ page }) => { 6 | await page.goto('/non-dismissible'); 7 | }); 8 | 9 | test.describe('Non-dismissible', () => { 10 | test('should not close on background interaction', async ({ page }) => { 11 | await openDrawer(page); 12 | // Click on the background 13 | await page.mouse.click(0, 0); 14 | await page.waitForTimeout(ANIMATION_DURATION); 15 | await expect(page.getByTestId('content')).toBeVisible(); 16 | }); 17 | 18 | test('should not close when dragged down', async ({ page }) => { 19 | await openDrawer(page); 20 | await page.hover('[data-vaul-drawer]'); 21 | await page.mouse.down(); 22 | await page.mouse.move(0, 800); 23 | await page.mouse.up(); 24 | await page.waitForTimeout(ANIMATION_DURATION); 25 | await expect(page.getByTestId('content')).toBeVisible(); 26 | }); 27 | 28 | test('should close when the dismiss button is clicked', async ({ page }) => { 29 | await openDrawer(page); 30 | 31 | await page.getByTestId('dismiss-button').click(); 32 | await page.waitForTimeout(ANIMATION_DURATION); 33 | await expect(page.getByTestId('content')).not.toBeVisible(); 34 | }); 35 | }); 36 | -------------------------------------------------------------------------------- /test/tests/with-handle.spec.ts: -------------------------------------------------------------------------------- 1 | import { test, expect } from '@playwright/test'; 2 | import { ANIMATION_DURATION } from './constants'; 3 | 4 | test.beforeEach(async ({ page }) => { 5 | await page.goto('/with-handle'); 6 | }); 7 | 8 | test.describe('With handle', () => { 9 | test('click should cycle to the next snap point', async ({ page }) => { 10 | await page.waitForTimeout(ANIMATION_DURATION); 11 | 12 | await expect(page.getByTestId('content')).toBeVisible(); 13 | await expect(page.getByTestId('active-snap-index')).toHaveText('0'); 14 | 15 | await page.getByTestId('handle').click(); 16 | await expect(page.getByTestId('active-snap-index')).toHaveText('1'); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /test/tests/with-redirect.spec.ts: -------------------------------------------------------------------------------- 1 | import { test, expect } from '@playwright/test'; 2 | import { openDrawer } from './helpers'; 3 | 4 | test.beforeEach(async ({ page }) => { 5 | await page.goto('/with-redirect'); 6 | }); 7 | 8 | test.describe('With redirect', () => { 9 | test('should restore body position settings', async ({ page }) => { 10 | await openDrawer(page); 11 | await page.getByTestId('link').click(); 12 | 13 | await page.waitForURL('**/with-redirect/long-page'); 14 | 15 | const content = page.getByTestId('content'); 16 | 17 | // safe check 18 | await expect(content).toBeVisible(); 19 | 20 | content.scrollIntoViewIfNeeded(); 21 | 22 | await expect(page.getByTestId('content')).toBeInViewport(); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /test/tests/with-scaled-background.spec.ts: -------------------------------------------------------------------------------- 1 | import { test, expect } from '@playwright/test'; 2 | import { openDrawer } from './helpers'; 3 | 4 | test.beforeEach(async ({ page }) => { 5 | await page.goto('/with-scaled-background'); 6 | }); 7 | 8 | test.describe('With scaled background', () => { 9 | test('should scale background', async ({ page }) => { 10 | await expect(page.locator('[data-vaul-drawer-wrapper]')).not.toHaveCSS('transform', ''); 11 | 12 | await page.getByTestId('trigger').click(); 13 | 14 | await expect(page.locator('[data-vaul-drawer-wrapper]')).toHaveCSS('transform', /matrix/); 15 | }); 16 | 17 | test('should scale background when dragging', async ({ page }) => { 18 | await expect(page.locator('[data-vaul-drawer-wrapper]')).not.toHaveCSS('transform', ''); 19 | 20 | await openDrawer(page); 21 | 22 | await page.hover('[data-vaul-drawer]'); 23 | await page.mouse.down(); 24 | await page.mouse.move(0, 100); 25 | 26 | await expect(page.locator('[data-vaul-drawer-wrapper]')).toHaveCSS('transform', /matrix/); 27 | 28 | await page.mouse.up(); 29 | 30 | await expect(page.locator('[data-vaul-drawer-wrapper]')).not.toHaveCSS('transform', ''); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /test/tests/without-scaled-background.spec.ts: -------------------------------------------------------------------------------- 1 | import { test, expect } from '@playwright/test'; 2 | 3 | test.beforeEach(async ({ page }) => { 4 | await page.goto('/without-scaled-background'); 5 | }); 6 | 7 | test.describe('Without scaled background', () => { 8 | test('should not scale background', async ({ page }) => { 9 | await expect(page.locator('[data-vaul-drawer-wrapper]')).not.toHaveCSS('transform', ''); 10 | 11 | await page.getByTestId('trigger').click(); 12 | 13 | await expect(page.locator('[data-vaul-drawer-wrapper]')).not.toHaveCSS('transform', ''); 14 | }); 15 | }); 16 | -------------------------------------------------------------------------------- /test/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "baseUrl": ".", 5 | "lib": [ 6 | "dom", 7 | "dom.iterable", 8 | "esnext" 9 | ], 10 | "allowJs": true, 11 | "skipLibCheck": true, 12 | "strict": true, 13 | "noEmit": true, 14 | "esModuleInterop": true, 15 | "module": "esnext", 16 | "moduleResolution": "bundler", 17 | "resolveJsonModule": true, 18 | "isolatedModules": true, 19 | "jsx": "preserve", 20 | "incremental": true, 21 | "plugins": [ 22 | { 23 | "name": "next" 24 | } 25 | ], 26 | "paths": { 27 | "@/*": [ 28 | "./src/*" 29 | ] 30 | } 31 | }, 32 | "include": [ 33 | "next-env.d.ts", 34 | "**/*.ts", 35 | "**/*.tsx", 36 | ".next/types/**/*.ts" 37 | ], 38 | "exclude": [ 39 | "node_modules" 40 | ] 41 | } 42 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "jsx": "react", 4 | "target": "es2018", 5 | "moduleResolution": "node", 6 | "esModuleInterop": true, 7 | "lib": ["es2015", "dom"], 8 | "strict": true 9 | }, 10 | "include": ["src"], 11 | } 12 | -------------------------------------------------------------------------------- /turbo.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://turbo.build/schema.json", 3 | "pipeline": { 4 | "build": { 5 | "dependsOn": ["^build"], 6 | "outputs": ["dist/**", ".next/**"] 7 | }, 8 | "dev": { 9 | "cache": false 10 | } 11 | } 12 | } 13 | --------------------------------------------------------------------------------