Skip to content

armgit / vaul

Public Imported from GitHub
0

A drawer component for React.

fix: controlled state (#408)

Emil Kowalski0d3939180790
diff --git a/README.md b/README.md
index 4635b29..550a5bc 100644
--- a/README.md
+++ b/README.md
@@ -21,7 +21,6 @@ function MyComponent() {
<Drawer.Trigger>Open</Drawer.Trigger>
<Drawer.Portal>
<Drawer.Content>
- <Drawer.Handle />
<p>Content</p>
</Drawer.Content>
<Drawer.Overlay />
@@ -63,20 +62,32 @@ Additional props:
`modal`: When `false` it allows to interact with elements outside of the drawer without closing it. Defaults to `true`.
-`handleOnly`: When `true` only allows the drawer to be dragged by the `<Drawer.Handle />` component. Defaults to `false`.
-
`direction`: Direction of the drawer. Can be `top` or `bottom`, `left`, `right`. Defaults to `bottom`.
-`preventScrollRestoration`: When `true` it prevents scroll restoration when the drawer is closed after a navigation happens inside of it. Defaults to `true`.
-
`disablePreventScroll`: When `true` scroll prevention mechanism will be disabled. Scroll prevention ensures that page will not scroll on mobile when opening drawer. However this mechanism gets confused when drawer has an input with autofocus and therefore opens simulataneosly with touch keyboard. Defaults to `true`. `modal` set to `false` also disables it.
`noBodyStyles`: When `true` the `body` doesn't get any styles assigned from Vaul.
`setBackgroundColorOnScale`: When `false` we don't change body's background color when the drawer is open. `true` by default.
+`defaultOpen`: Opened by default, still reacts to `open` state changes.
+
`[data-vaul-no-drag]`: When interacting with an element with this data attribute, the drawer won't be dragged.
+### Controlled Drawer
+
+Drawer can be controlled programmatically by providing the `open` prop. If you want to react to open state changes from within the Drawer use the `onOpenChange` prop, this will allow you to provide your own open state while still closing the drawer when the escape key is pressed for example.
+
+```
+const [open, setOpen] = React.useState(false);
+
+// ...
+
+<Drawer.Root open={open} onOpenChange={setOpen}>
+ // ...
+</Drawer.Root>
+```
+
### Trigger
The button that opens the dialog. [Props](https://www.radix-ui.com/docs/primitives/components/dialog#trigger).
@@ -101,10 +112,6 @@ An optional accessible description to be announced when the dialog is opened. [P
The button that closes the dialog. [Props](https://www.radix-ui.com/docs/primitives/components/dialog#close).
-### Handle
-
-A drag hint (also known as grabber). Shows people that they can drag the drawer to resize it; they can also tap it to cycle through the snap points, and double tap quickly to close the drawer. Set `preventCycle={true}` to stop this behavior. If you want to change the handle's hit area you can do so by styling the `[data-vaul-handle-hitarea]` selector (Defaults to 44x44 on mobile devices).
-
### Portal
Portals your drawer into the body.
diff --git a/package.json b/package.json
index e1c9c92..4b2dac5 100644
--- a/package.json
+++ b/package.json
@@ -63,6 +63,6 @@
},
"packageManager": "[email protected]",
"dependencies": {
- "@radix-ui/react-dialog": "^1.0.4"
+ "@radix-ui/react-dialog": "^1.1.1"
}
}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index d0985d2..3403bec 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -9,8 +9,8 @@ importers:
.:
dependencies:
'@radix-ui/react-dialog':
- specifier: ^1.0.4
+ specifier: ^1.1.1
devDependencies:
'@playwright/test':
specifier: ^1.37.1
@@ -182,6 +182,7 @@ packages:
engines: {node: '>=6.9.0'}
dependencies:
regenerator-runtime: 0.14.1
+ dev: true
resolution: {integrity: sha512-1/sA4dwrzBAyeUoQ6oxahHKmrZvsnLCg4RfxW3ZFGGmQkSNQPFNLV9CUEFQP1x9EYXHTo5p6xdhZM1Ne9p/AfA==}
@@ -462,284 +463,268 @@ packages:
playwright: 1.41.2
dev: true
- /@radix-ui/[email protected]:
- resolution: {integrity: sha512-yQ8oGX2GVsEYMWGxcovu1uGWPCxV5BFfeeYxqPmuAzUyLT9qmaMXSAhXpb0WrspIeqYzdJpkh2vHModJPgRIaw==}
- dependencies:
- '@babel/runtime': 7.23.9
+ /@radix-ui/[email protected]:
+ resolution: {integrity: sha512-4Z8dn6Upk0qk4P74xBhZ6Hd/w0mPEzOOLxy4xiPXOXqjF7jZS0VAKk7/x/H6FyY2zCkYJqePf1G5KmkmNJ4RBA==}
dev: false
- resolution: {integrity: sha512-fDSBgd44FKHa1FRMU59qBMPFcl2PZE+2nmqunj+BWFyYYjnhIDWL2ItDs3rrbJDQOtzt5nIebLCQc4QRfz6LJw==}
+ resolution: {integrity: sha512-b4inOtiaOnYf9KWyO3jAeeCG6FeyfY6ldiEPanbUjWd+xIk5wZeHa8yVwmrJ2vderhu/BQvzCrJI0lHd+wIiqw==}
peerDependencies:
'@types/react': '*'
- react: ^16.8 || ^17.0 || ^18.0
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
dependencies:
- '@babel/runtime': 7.23.9
'@types/react': 18.2.55
react: 18.2.0
dev: false
- resolution: {integrity: sha512-ebbrdFoYTcuZ0v4wG5tedGnp9tzcV8awzsxYph7gXUyvnNLuTIcCk1q17JEbnVhXAKG9oX3KtchwiMIAYp9NLg==}
+ resolution: {integrity: sha512-OKrckBy+sMEgYM/sMmqmErVn0kZqrHPJze+Ql3DzYsDDp0hl0L62nx/2122/Bvps1qz645jlcu2tD9lrRSdf8A==}
peerDependencies:
'@types/react': '*'
- react: ^16.8 || ^17.0 || ^18.0
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
dependencies:
- '@babel/runtime': 7.23.9
'@types/react': 18.2.55
react: 18.2.0
dev: false
- resolution: {integrity: sha512-GjWJX/AUpB703eEBanuBnIWdIXg6NvJFCXcNlSZk4xdszCdhrJgBoUd1cGk67vFO+WdA2pfI/plOpqz/5GUP6Q==}
+ resolution: {integrity: sha512-zysS+iU4YP3STKNS6USvFVqI4qqx8EpiwmT5TuCApVEBca+eRCbONi4EgzfNSuVnOXvC5UPHHMjs8RXO6DH9Bg==}
peerDependencies:
'@types/react': '*'
'@types/react-dom': '*'
- react: ^16.8 || ^17.0 || ^18.0
- react-dom: ^16.8 || ^17.0 || ^18.0
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
+ react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
'@types/react-dom':
optional: true
dependencies:
- '@babel/runtime': 7.23.9
- '@radix-ui/primitive': 1.0.1
- '@radix-ui/react-compose-refs': 1.0.1(@types/[email protected])([email protected])
- '@radix-ui/react-context': 1.0.1(@types/[email protected])([email protected])
- '@radix-ui/react-dismissable-layer': 1.0.5(@types/[email protected])(@types/[email protected])([email protected])([email protected])
- '@radix-ui/react-focus-guards': 1.0.1(@types/[email protected])([email protected])
- '@radix-ui/react-focus-scope': 1.0.4(@types/[email protected])(@types/[email protected])([email protected])([email protected])
- '@radix-ui/react-id': 1.0.1(@types/[email protected])([email protected])
- '@radix-ui/react-portal': 1.0.4(@types/[email protected])(@types/[email protected])([email protected])([email protected])
- '@radix-ui/react-presence': 1.0.1(@types/[email protected])(@types/[email protected])([email protected])([email protected])
- '@radix-ui/react-primitive': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])
- '@radix-ui/react-slot': 1.0.2(@types/[email protected])([email protected])
- '@radix-ui/react-use-controllable-state': 1.0.1(@types/[email protected])([email protected])
+ '@radix-ui/primitive': 1.1.0
+ '@radix-ui/react-compose-refs': 1.1.0(@types/[email protected])([email protected])
+ '@radix-ui/react-context': 1.1.0(@types/[email protected])([email protected])
+ '@radix-ui/react-dismissable-layer': 1.1.0(@types/[email protected])(@types/[email protected])([email protected])([email protected])
+ '@radix-ui/react-focus-guards': 1.1.0(@types/[email protected])([email protected])
+ '@radix-ui/react-focus-scope': 1.1.0(@types/[email protected])(@types/[email protected])([email protected])([email protected])
+ '@radix-ui/react-id': 1.1.0(@types/[email protected])([email protected])
+ '@radix-ui/react-portal': 1.1.1(@types/[email protected])(@types/[email protected])([email protected])([email protected])
+ '@radix-ui/react-presence': 1.1.0(@types/[email protected])(@types/[email protected])([email protected])([email protected])
+ '@radix-ui/react-primitive': 2.0.0(@types/[email protected])(@types/[email protected])([email protected])([email protected])
+ '@radix-ui/react-slot': 1.1.0(@types/[email protected])([email protected])
+ '@radix-ui/react-use-controllable-state': 1.1.0(@types/[email protected])([email protected])
'@types/react': 18.2.55
'@types/react-dom': 18.2.18
aria-hidden: 1.2.3
react: 18.2.0
react-dom: 18.2.0([email protected])
- react-remove-scroll: 2.5.5(@types/[email protected])([email protected])
+ react-remove-scroll: 2.5.7(@types/[email protected])([email protected])
dev: false
- resolution: {integrity: sha512-aJeDjQhywg9LBu2t/At58hCvr7pEm0o2Ke1x33B+MhjNmmZ17sy4KImo0KPLgsnc/zN7GPdce8Cnn0SWvwZO7g==}
+ resolution: {integrity: sha512-/UovfmmXGptwGcBQawLzvn2jOfM0t4z3/uKffoBlj724+n3FvBbZ7M0aaBOmkp6pqFYpO4yx8tSVJjx3Fl2jig==}
peerDependencies:
'@types/react': '*'
'@types/react-dom': '*'
- react: ^16.8 || ^17.0 || ^18.0
- react-dom: ^16.8 || ^17.0 || ^18.0
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
+ react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
'@types/react-dom':
optional: true
dependencies:
- '@babel/runtime': 7.23.9
- '@radix-ui/primitive': 1.0.1
- '@radix-ui/react-compose-refs': 1.0.1(@types/[email protected])([email protected])
- '@radix-ui/react-primitive': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])
- '@radix-ui/react-use-callback-ref': 1.0.1(@types/[email protected])([email protected])
- '@radix-ui/react-use-escape-keydown': 1.0.3(@types/[email protected])([email protected])
+ '@radix-ui/primitive': 1.1.0
+ '@radix-ui/react-compose-refs': 1.1.0(@types/[email protected])([email protected])
+ '@radix-ui/react-primitive': 2.0.0(@types/[email protected])(@types/[email protected])([email protected])([email protected])
+ '@radix-ui/react-use-callback-ref': 1.1.0(@types/[email protected])([email protected])
+ '@radix-ui/react-use-escape-keydown': 1.1.0(@types/[email protected])([email protected])
'@types/react': 18.2.55
'@types/react-dom': 18.2.18
react: 18.2.0
react-dom: 18.2.0([email protected])
dev: false
- resolution: {integrity: sha512-Rect2dWbQ8waGzhMavsIbmSVCgYxkXLxxR3ZvCX79JOglzdEy4JXMb98lq4hPxUbLr77nP0UOGf4rcMU+s1pUA==}
+ resolution: {integrity: sha512-w6XZNUPVv6xCpZUqb/yN9DL6auvpGX3C/ee6Hdi16v2UUy25HV2Q5bcflsiDyT/g5RwbPQ/GIT1vLkeRb+ITBw==}
peerDependencies:
'@types/react': '*'
- react: ^16.8 || ^17.0 || ^18.0
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
dependencies:
- '@babel/runtime': 7.23.9
'@types/react': 18.2.55
react: 18.2.0
dev: false
- resolution: {integrity: sha512-sL04Mgvf+FmyvZeYfNu1EPAaaxD+aw7cYeIB9L9Fvq8+urhltTRaEo5ysKOpHuKPclsZcSUMKlN05x4u+CINpA==}
+ resolution: {integrity: sha512-200UD8zylvEyL8Bx+z76RJnASR2gRMuxlgFCPAe/Q/679a/r0eK3MBVYMb7vZODZcffZBdob1EGnky78xmVvcA==}
peerDependencies:
'@types/react': '*'
'@types/react-dom': '*'
- react: ^16.8 || ^17.0 || ^18.0
- react-dom: ^16.8 || ^17.0 || ^18.0
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
+ react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
'@types/react-dom':
optional: true
dependencies:
- '@babel/runtime': 7.23.9
- '@radix-ui/react-compose-refs': 1.0.1(@types/[email protected])([email protected])
- '@radix-ui/react-primitive': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])
- '@radix-ui/react-use-callback-ref': 1.0.1(@types/[email protected])([email protected])
+ '@radix-ui/react-compose-refs': 1.1.0(@types/[email protected])([email protected])
+ '@radix-ui/react-primitive': 2.0.0(@types/[email protected])(@types/[email protected])([email protected])([email protected])
+ '@radix-ui/react-use-callback-ref': 1.1.0(@types/[email protected])([email protected])
'@types/react': 18.2.55
'@types/react-dom': 18.2.18
react: 18.2.0
react-dom: 18.2.0([email protected])
dev: false
- resolution: {integrity: sha512-tI7sT/kqYp8p96yGWY1OAnLHrqDgzHefRBKQ2YAkBS5ja7QLcZ9Z/uY7bEjPUatf8RomoXM8/1sMj1IJaE5UzQ==}
+ resolution: {integrity: sha512-EJUrI8yYh7WOjNOqpoJaf1jlFIH2LvtgAl+YcFqNCa+4hj64ZXmPkAKOFs/ukjz3byN6bdb/AVUqHkI8/uWWMA==}
peerDependencies:
'@types/react': '*'
- react: ^16.8 || ^17.0 || ^18.0
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
dependencies:
- '@babel/runtime': 7.23.9
- '@radix-ui/react-use-layout-effect': 1.0.1(@types/[email protected])([email protected])
+ '@radix-ui/react-use-layout-effect': 1.1.0(@types/[email protected])([email protected])
'@types/react': 18.2.55
react: 18.2.0
dev: false
- resolution: {integrity: sha512-Qki+C/EuGUVCQTOTD5vzJzJuMUlewbzuKyUy+/iHM2uwGiru9gZeBJtHAPKAEkB5KWGi9mP/CHKcY0wt1aW45Q==}
+ resolution: {integrity: sha512-A3UtLk85UtqhzFqtoC8Q0KvR2GbXF3mtPgACSazajqq6A41mEQgo53iPzY4i6BwDxlIFqWIhiQ2G729n+2aw/g==}
peerDependencies:
'@types/react': '*'
'@types/react-dom': '*'
- react: ^16.8 || ^17.0 || ^18.0
- react-dom: ^16.8 || ^17.0 || ^18.0
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
+ react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
'@types/react-dom':
optional: true
dependencies:
- '@babel/runtime': 7.23.9
- '@radix-ui/react-primitive': 1.0.3(@types/[email protected])(@types/[email protected])([email protected])([email protected])
+ '@radix-ui/react-primitive': 2.0.0(@types/[email protected])(@types/[email protected])([email protected])([email protected])
+ '@radix-ui/react-use-layout-effect': 1.1.0(@types/[email protected])([email protected])
'@types/react': 18.2.55
'@types/react-dom': 18.2.18
react: 18.2.0
react-dom: 18.2.0([email protected])
dev: false
- resolution: {integrity: sha512-UXLW4UAbIY5ZjcvzjfRFo5gxva8QirC9hF7wRE4U5gz+TP0DbRk+//qyuAQ1McDxBt1xNMBTaciFGvEmJvAZCg==}
+ resolution: {integrity: sha512-Gq6wuRN/asf9H/E/VzdKoUtT8GC9PQc9z40/vEr0VCJ4u5XvvhWIrSsCB6vD2/cH7ugTdSfYq9fLJCcM00acrQ==}
peerDependencies:
'@types/react': '*'
'@types/react-dom': '*'
- react: ^16.8 || ^17.0 || ^18.0
- react-dom: ^16.8 || ^17.0 || ^18.0
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
+ react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
'@types/react-dom':
optional: true
dependencies:
- '@babel/runtime': 7.23.9
- '@radix-ui/react-compose-refs': 1.0.1(@types/[email protected])([email protected])
- '@radix-ui/react-use-layout-effect': 1.0.1(@types/[email protected])([email protected])
+ '@radix-ui/react-compose-refs': 1.1.0(@types/[email protected])([email protected])
+ '@radix-ui/react-use-layout-effect': 1.1.0(@types/[email protected])([email protected])
'@types/react': 18.2.55
'@types/react-dom': 18.2.18
react: 18.2.0
react-dom: 18.2.0([email protected])
dev: false
- resolution: {integrity: sha512-yi58uVyoAcK/Nq1inRY56ZSjKypBNKTa/1mcL8qdl6oJeEaDbOldlzrGn7P6Q3Id5d+SYNGc5AJgc4vGhjs5+g==}
+ resolution: {integrity: sha512-ZSpFm0/uHa8zTvKBDjLFWLo8dkr4MBsiDLz0g3gMUwqgLHz9rTaRRGYDgvZPtBJgYCBKXkS9fzmoySgr8CO6Cw==}
peerDependencies:
'@types/react': '*'
'@types/react-dom': '*'
- react: ^16.8 || ^17.0 || ^18.0
- react-dom: ^16.8 || ^17.0 || ^18.0
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
+ react-dom: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
'@types/react-dom':
optional: true
dependencies:
- '@babel/runtime': 7.23.9
- '@radix-ui/react-slot': 1.0.2(@types/[email protected])([email protected])
+ '@radix-ui/react-slot': 1.1.0(@types/[email protected])([email protected])
'@types/react': 18.2.55
'@types/react-dom': 18.2.18
react: 18.2.0
react-dom: 18.2.0([email protected])
dev: false
- resolution: {integrity: sha512-YeTpuq4deV+6DusvVUW4ivBgnkHwECUu0BiN43L5UCDFgdhsRUWAghhTF5MbvNTPzmiFOx90asDSUjWuCNapwg==}
+ resolution: {integrity: sha512-FUCf5XMfmW4dtYl69pdS4DbxKy8nj4M7SafBgPllysxmdachynNflAdp/gCsnYWNDnge6tI9onzMp5ARYc1KNw==}
peerDependencies:
'@types/react': '*'
- react: ^16.8 || ^17.0 || ^18.0
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
dependencies:
- '@babel/runtime': 7.23.9
- '@radix-ui/react-compose-refs': 1.0.1(@types/[email protected])([email protected])
+ '@radix-ui/react-compose-refs': 1.1.0(@types/[email protected])([email protected])
'@types/react': 18.2.55
react: 18.2.0
dev: false
- resolution: {integrity: sha512-D94LjX4Sp0xJFVaoQOd3OO9k7tpBYNOXdVhkltUbGv2Qb9OXdrg/CpsjlZv7ia14Sylv398LswWBVVu5nqKzAQ==}
+ resolution: {integrity: sha512-CasTfvsy+frcFkbXtSJ2Zu9JHpN8TYKxkgJGWbjiZhFivxaeW7rMeZt7QELGVLaYVfFMsKHjb7Ak0nMEe+2Vfw==}
peerDependencies:
'@types/react': '*'
- react: ^16.8 || ^17.0 || ^18.0
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
dependencies:
- '@babel/runtime': 7.23.9
'@types/react': 18.2.55
react: 18.2.0
dev: false
- resolution: {integrity: sha512-Svl5GY5FQeN758fWKrjM6Qb7asvXeiZltlT4U2gVfl8Gx5UAv2sMR0LWo8yhsIZh2oQ0eFdZ59aoOOMV7b47VA==}
+ resolution: {integrity: sha512-MtfMVJiSr2NjzS0Aa90NPTnvTSg6C/JLCV7ma0W6+OMV78vd8OyRpID+Ng9LxzsPbLeuBnWBA1Nq30AtBIDChw==}
peerDependencies:
'@types/react': '*'
- react: ^16.8 || ^17.0 || ^18.0
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
dependencies:
- '@babel/runtime': 7.23.9
- '@radix-ui/react-use-callback-ref': 1.0.1(@types/[email protected])([email protected])
+ '@radix-ui/react-use-callback-ref': 1.1.0(@types/[email protected])([email protected])
'@types/react': 18.2.55
react: 18.2.0
dev: false
- resolution: {integrity: sha512-vyL82j40hcFicA+M4Ex7hVkB9vHgSse1ZWomAqV2Je3RleKGO5iM8KMOEtfoSB0PnIelMd2lATjTGMYqN5ylTg==}
+ resolution: {integrity: sha512-L7vwWlR1kTTQ3oh7g1O0CBF3YCyyTj8NmhLR+phShpyA50HCfBFKVJTpshm9PzLiKmehsrQzTYTpX9HvmC9rhw==}
peerDependencies:
'@types/react': '*'
- react: ^16.8 || ^17.0 || ^18.0
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
dependencies:
- '@babel/runtime': 7.23.9
- '@radix-ui/react-use-callback-ref': 1.0.1(@types/[email protected])([email protected])
+ '@radix-ui/react-use-callback-ref': 1.1.0(@types/[email protected])([email protected])
'@types/react': 18.2.55
react: 18.2.0
dev: false
- resolution: {integrity: sha512-v/5RegiJWYdoCvMnITBkNNx6bCj20fiaJnWtRkU18yITptraXjffz5Qbn05uOiQnOvi+dbkznkoaMltz1GnszQ==}
+ resolution: {integrity: sha512-+FPE0rOdziWSrH9athwI1R0HDVbWlEhd+FR+aSDk4uWGmSJ9Z54sdZVDQPZAinJhJXwfT+qnj969mCsT2gfm5w==}
peerDependencies:
'@types/react': '*'
- react: ^16.8 || ^17.0 || ^18.0
+ react: ^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc
peerDependenciesMeta:
'@types/react':
optional: true
dependencies:
- '@babel/runtime': 7.23.9
'@types/react': 18.2.55
react: 18.2.0
dev: false
@@ -3694,8 +3679,8 @@ packages:
tslib: 2.6.2
dev: false
- resolution: {integrity: sha512-ImKhrzJJsyXJfBZ4bzu8Bwpka14c/fQt0k+cyFp/PBhTfyDnU5hjOtM4AG/0AMyy8oKzOTR0lDgJIM7pYXI0kw==}
+ resolution: {integrity: sha512-FnrTWO4L7/Bhhf3CYBNArEG/yROV0tKmTv7/3h9QCFvH6sndeFf1wPqOcbFVu5VAulS5dV1wGT3GZZ/1GawqiA==}
engines: {node: '>=10'}
peerDependencies:
'@types/react': ^16.8.0 || ^17.0.0 || ^18.0.0
@@ -3764,6 +3749,7 @@ packages:
resolution: {integrity: sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==}
+ dev: true
resolution: {integrity: sha512-sy6TXMN+hnP/wMy+ISxg3krXx7BAtWVO4UouuCN/ziM9UEne0euamVNafDfvC83bRNr95y0V5iijeDQFUNpvrg==}
diff --git a/src/constants.ts b/src/constants.ts
index fd6a10c..69e2f89 100644
--- a/src/constants.ts
+++ b/src/constants.ts
@@ -4,3 +4,15 @@ export const TRANSITIONS = {
};
export const VELOCITY_THRESHOLD = 0.4;
+
+export const CLOSE_THRESHOLD = 0.25;
+
+export const SCROLL_LOCK_TIMEOUT = 100;
+
+export const BORDER_RADIUS = 8;
+
+export const NESTED_DISPLACEMENT = 16;
+
+export const WINDOW_TOP_OFFSET = 26;
+
+export const DRAG_CLASS = 'vaul-dragging';
diff --git a/src/context.ts b/src/context.ts
index 2b4e89f..a2b1396 100644
--- a/src/context.ts
+++ b/src/context.ts
@@ -12,7 +12,6 @@ interface DrawerContextValue {
onNestedOpenChange: (o: boolean) => void;
onNestedRelease: (event: React.PointerEvent<HTMLDivElement>, open: boolean) => void;
dismissible: boolean;
- handleOnly: boolean;
isOpen: boolean;
isDragging: boolean;
keyboardIsOpen: React.MutableRefObject<boolean>;
@@ -22,9 +21,7 @@ interface DrawerContextValue {
shouldFade: boolean;
activeSnapPoint?: number | string | null;
setActiveSnapPoint: (o: number | string | null) => void;
- visible: boolean;
closeDrawer: () => void;
- setVisible: (o: boolean) => void;
openProp?: boolean;
onOpenChange?: (o: boolean) => void;
direction?: DrawerDirection;
@@ -42,7 +39,6 @@ export const DrawerContext = React.createContext<DrawerContextValue>({
onNestedRelease: () => {},
openProp: undefined,
dismissible: false,
- handleOnly: false,
isOpen: false,
isDragging: false,
keyboardIsOpen: { current: false },
@@ -53,9 +49,7 @@ export const DrawerContext = React.createContext<DrawerContextValue>({
activeSnapPoint: null,
onOpenChange: () => {},
setActiveSnapPoint: () => {},
- visible: false,
closeDrawer: () => {},
- setVisible: () => {},
direction: 'bottom',
});
diff --git a/src/index.tsx b/src/index.tsx
index 4c01345..070b248 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -1,27 +1,25 @@
'use client';
import * as DialogPrimitive from '@radix-ui/react-dialog';
-import React from 'react';
+import React, { useEffect } from 'react';
import { DrawerContext, useDrawerContext } from './context';
import './style.css';
-import { usePreventScroll, isInput, isIOS, useIsomorphicLayoutEffect } from './use-prevent-scroll';
+import { usePreventScroll, isInput, isIOS } from './use-prevent-scroll';
import { useComposedRefs } from './use-composed-refs';
import { useSnapPoints } from './use-snap-points';
import { set, reset, getTranslate, dampenValue, isVertical } from './helpers';
-import { TRANSITIONS, VELOCITY_THRESHOLD } from './constants';
+import {
+ TRANSITIONS,
+ VELOCITY_THRESHOLD,
+ CLOSE_THRESHOLD,
+ SCROLL_LOCK_TIMEOUT,
+ BORDER_RADIUS,
+ NESTED_DISPLACEMENT,
+ WINDOW_TOP_OFFSET,
+ DRAG_CLASS,
+} from './constants';
import { DrawerDirection } from './types';
-
-const CLOSE_THRESHOLD = 0.25;
-
-const SCROLL_LOCK_TIMEOUT = 100;
-
-const BORDER_RADIUS = 8;
-
-const NESTED_DISPLACEMENT = 16;
-
-const WINDOW_TOP_OFFSET = 26;
-
-const DRAG_CLASS = 'vaul-dragging';
+import { useControllableState } from './use-controllable-state';
export interface WithFadeFromProps {
snapPoints: (number | string)[];
@@ -46,31 +44,28 @@ export type DialogProps = {
scrollLockTimeout?: number;
fixed?: boolean;
dismissible?: boolean;
- handleOnly?: boolean;
onDrag?: (event: React.PointerEvent<HTMLDivElement>, percentageDragged: number) => void;
onRelease?: (event: React.PointerEvent<HTMLDivElement>, open: boolean) => void;
modal?: boolean;
nested?: boolean;
onClose?: () => void;
direction?: 'top' | 'bottom' | 'left' | 'right';
- preventScrollRestoration?: boolean;
disablePreventScroll?: boolean;
+ defaultOpen?: boolean;
} & (WithFadeFromProps | WithoutFadeFromProps);
export function Root({
open: openProp,
onOpenChange,
children,
- shouldScaleBackground,
onDrag: onDragProp,
onRelease: onReleaseProp,
snapPoints,
- nested = false,
+ shouldScaleBackground = false,
setBackgroundColorOnScale = true,
closeThreshold = CLOSE_THRESHOLD,
scrollLockTimeout = SCROLL_LOCK_TIMEOUT,
dismissible = true,
- handleOnly = false,
fadeFromIndex = snapPoints && snapPoints.length - 1,
activeSnapPoint: activeSnapPointProp,
setActiveSnapPoint: setActiveSnapPointProp,
@@ -79,14 +74,15 @@ export function Root({
onClose,
noBodyStyles,
direction = 'bottom',
- preventScrollRestoration = true,
+ defaultOpen = false,
disablePreventScroll = false,
}: DialogProps) {
- const [isOpen = false, setIsOpen] = React.useState<boolean>(false);
+ const [isOpen = false, setIsOpen] = useControllableState({
+ defaultProp: defaultOpen,
+ prop: openProp,
+ onChange: onOpenChange,
+ });
const [hasBeenOpened, setHasBeenOpened] = React.useState<boolean>(false);
- // Not visible = translateY(100%)
- const [visible, setVisible] = React.useState<boolean>(false);
- const [mounted, setMounted] = React.useState<boolean>(false);
const [isDragging, setIsDragging] = React.useState<boolean>(false);
const [justReleased, setJustReleased] = React.useState<boolean>(false);
const overlayRef = React.useRef<HTMLDivElement>(null);
@@ -398,32 +394,13 @@ export function Root({
}, [activeSnapPointIndex, snapPoints, snapPointsOffset]);
function closeDrawer() {
- if (!drawerRef.current) return;
-
cancelDrag();
-
onClose?.();
- set(drawerRef.current, {
- transform: isVertical(direction)
- ? `translate3d(0, ${direction === 'bottom' ? '100%' : '-100%'}, 0)`
- : `translate3d(${direction === 'right' ? '100%' : '-100%'}, 0, 0)`,
- transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
- });
-
- set(overlayRef.current, {
- opacity: '0',
- transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
- });
scaleBackground(false);
+ setIsOpen(false);
setTimeout(() => {
- setVisible(false);
- setIsOpen(false);
- }, 300);
-
- setTimeout(() => {
- // reset(document.documentElement, 'scrollBehavior');
if (snapPoints) {
setActiveSnapPoint(snapPoints[0]);
}
@@ -441,27 +418,6 @@ export function Root({
}
}, [isOpen, shouldScaleBackground]);
- // LayoutEffect to prevent extra render where openProp and isOpen are not synced yet
- useIsomorphicLayoutEffect(() => {
- if (openProp) {
- setIsOpen(true);
- setHasBeenOpened(true);
- } else {
- closeDrawer();
- }
- }, [openProp]);
-
- // This can be done much better
- React.useEffect(() => {
- if (mounted) {
- onOpenChange?.(isOpen);
- }
- }, [isOpen]);
-
- React.useEffect(() => {
- setMounted(true);
- }, []);
-
function resetDrawer() {
if (!drawerRef.current) return;
const wrapper = document.querySelector('[data-vaul-drawer-wrapper]');
@@ -587,7 +543,7 @@ export function Root({
}, [isOpen]);
React.useEffect(() => {
- if (drawerRef.current && visible) {
+ if (drawerRef.current) {
// Find all scrollable elements inside our drawer and assign a class to it so that we can disable overflow when dragging to prevent pointermove not being captured
const children = drawerRef?.current?.querySelectorAll('*');
children?.forEach((child: Element) => {
@@ -597,7 +553,7 @@ export function Root({
}
});
}
- }, [visible]);
+ }, [isOpen]);
function scaleBackground(open: boolean) {
const wrapper = document.querySelector('[data-vaul-drawer-wrapper]');
@@ -677,7 +633,7 @@ export function Root({
}
}
- function onNestedDrag(event: React.PointerEvent<HTMLDivElement>, percentageDragged: number) {
+ function onNestedDrag(_event: React.PointerEvent<HTMLDivElement>, percentageDragged: number) {
if (percentageDragged < 0) return;
const initialDim = isVertical(direction) ? window.innerHeight : window.innerWidth;
@@ -693,7 +649,7 @@ export function Root({
});
}
- function onNestedRelease(event: React.PointerEvent<HTMLDivElement>, o: boolean) {
+ function onNestedRelease(_event: React.PointerEvent<HTMLDivElement>, o: boolean) {
const dim = isVertical(direction) ? window.innerHeight : window.innerWidth;
const scale = o ? (dim - NESTED_DISPLACEMENT) / dim : 1;
const translate = o ? -NESTED_DISPLACEMENT : 0;
@@ -710,25 +666,18 @@ export function Root({
return (
<DialogPrimitive.Root
+ defaultOpen={defaultOpen}
modal={modal}
- onOpenChange={(o: boolean) => {
- if (openProp !== undefined) {
- onOpenChange?.(o);
- return;
- }
-
- if (!o) {
- closeDrawer();
- } else {
+ onOpenChange={(open) => {
+ if (open) {
setHasBeenOpened(true);
- setIsOpen(o);
}
+ setIsOpen(open);
}}
open={isOpen}
>
<DrawerContext.Provider
value={{
- visible,
activeSnapPoint,
snapPoints,
setActiveSnapPoint,
@@ -737,11 +686,9 @@ export function Root({
scaleBackground,
onOpenChange,
onPress,
- setVisible,
onRelease,
onDrag,
dismissible,
- handleOnly,
isOpen,
isDragging,
shouldFade,
@@ -750,7 +697,6 @@ export function Root({
onNestedOpenChange,
onNestedRelease,
keyboardIsOpen,
- openProp,
modal,
snapPointsOffset,
direction,
@@ -762,117 +708,9 @@ export function Root({
);
}
-export type HandleProps = React.ComponentPropsWithoutRef<'div'> & {
- preventCycle?: boolean;
-};
-
-const LONG_HANDLE_PRESS_TIMEOUT = 250;
-const DOUBLE_TAP_TIMEOUT = 120;
-
-export const Handle = React.forwardRef<HTMLDivElement, HandleProps>(function (
- { preventCycle = false, children, ...rest },
- ref,
-) {
- const {
- visible,
- closeDrawer,
- isDragging,
- snapPoints,
- activeSnapPoint,
- setActiveSnapPoint,
- dismissible,
- handleOnly,
- onPress,
- onDrag,
- } = useDrawerContext();
-
- const closeTimeoutIdRef = React.useRef<number | null>(null);
- const shouldCancelInteractionRef = React.useRef(false);
-
- function handleStartCycle() {
- // Stop if this is the second click of a double click
- if (shouldCancelInteractionRef.current) {
- handleCancelInteraction();
- return;
- }
- window.setTimeout(() => {
- handleCycleSnapPoints();
- }, DOUBLE_TAP_TIMEOUT);
- }
-
- function handleCycleSnapPoints() {
- // Prevent accidental taps while resizing drawer
- if (isDragging || preventCycle || shouldCancelInteractionRef.current) {
- handleCancelInteraction();
- return;
- }
- // Make sure to clear the timeout id if the user releases the handle before the cancel timeout
- handleCancelInteraction();
-
- if ((!snapPoints || snapPoints.length === 0) && dismissible) {
- closeDrawer();
- return;
- }
-
- const isLastSnapPoint = activeSnapPoint === snapPoints[snapPoints.length - 1];
- if (isLastSnapPoint && dismissible) {
- closeDrawer();
- return;
- }
-
- const currentSnapIndex = snapPoints.findIndex((point) => point === activeSnapPoint);
- if (currentSnapIndex === -1) return; // activeSnapPoint not found in snapPoints
- const nextSnapPoint = snapPoints[currentSnapIndex + 1];
- setActiveSnapPoint(nextSnapPoint);
- }
-
- function handleStartInteraction() {
- closeTimeoutIdRef.current = window.setTimeout(() => {
- // Cancel click interaction on a long press
- shouldCancelInteractionRef.current = true;
- }, LONG_HANDLE_PRESS_TIMEOUT);
- }
-
- function handleCancelInteraction() {
- window.clearTimeout(closeTimeoutIdRef.current);
- shouldCancelInteractionRef.current = false;
- }
-
- return (
- <div
- onClick={handleStartCycle}
- onDoubleClick={() => {
- shouldCancelInteractionRef.current = true;
- closeDrawer();
- }}
- onPointerCancel={handleCancelInteraction}
- onPointerDown={(e) => {
- if (handleOnly) onPress(e);
- handleStartInteraction();
- }}
- onPointerMove={(e) => {
- if (handleOnly) onDrag(e);
- }}
- // onPointerUp is already handled by the content component
- ref={ref}
- data-vaul-drawer-visible={visible ? 'true' : 'false'}
- data-vaul-handle=""
- aria-hidden="true"
- {...rest}
- >
- {/* Expand handle's hit area beyond what's visible to ensure a 44x44 tap target for touch devices */}
- <span data-vaul-handle-hitarea="" aria-hidden="true">
- {children}
- </span>
- </div>
- );
-});
-
-Handle.displayName = 'Drawer.Handle';
-
export const Overlay = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>>(
function ({ children, ...rest }, ref) {
- const { overlayRef, snapPoints, onRelease, shouldFade, isOpen, visible } = useDrawerContext();
+ const { overlayRef, snapPoints, onRelease, shouldFade, isOpen } = useDrawerContext();
const composedRef = useComposedRefs(ref, overlayRef);
const hasSnapPoints = snapPoints && snapPoints.length > 0;
@@ -880,7 +718,6 @@ export const Overlay = React.forwardRef<HTMLDivElement, React.ComponentPropsWith
<DialogPrimitive.Overlay
onMouseUp={onRelease}
ref={composedRef}
- data-vaul-drawer-visible={visible ? 'true' : 'false'}
data-vaul-overlay=""
data-vaul-snap-points={isOpen && hasSnapPoints ? 'true' : 'false'}
data-vaul-snap-points-overlay={isOpen && shouldFade ? 'true' : 'false'}
@@ -905,21 +742,19 @@ export const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
onPress,
onRelease,
onDrag,
- dismissible,
keyboardIsOpen,
snapPointsOffset,
- visible,
- closeDrawer,
modal,
- openProp,
- onOpenChange,
- setVisible,
- handleOnly,
+ isOpen,
direction,
+ snapPoints,
} = useDrawerContext();
+ // Needed to use transition instead of animations
+ const [delayedSnapPoints, setDelayedSnapPoints] = React.useState(false);
const composedRef = useComposedRefs(ref, drawerRef);
const pointerStartRef = React.useRef<{ x: number; y: number } | null>(null);
const wasBeyondThePointRef = React.useRef(false);
+ const hasSnapPoints = snapPoints && snapPoints.length > 0;
const isDeltaInDirection = (delta: { x: number; y: number }, direction: DrawerDirection, threshold = 0) => {
if (wasBeyondThePointRef.current) return true;
@@ -945,16 +780,20 @@ export const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
return true;
};
- React.useEffect(() => {
- // Trigger enter animation without using CSS animation
- setVisible(true);
+ useEffect(() => {
+ if (hasSnapPoints) {
+ window.requestAnimationFrame(() => {
+ setDelayedSnapPoints(true);
+ });
+ }
}, []);
return (
<DialogPrimitive.Content
data-vaul-drawer-direction={direction}
data-vaul-drawer=""
- data-vaul-drawer-visible={visible ? 'true' : 'false'}
+ data-vaul-delayed-snap-points={delayedSnapPoints ? 'true' : 'false'}
+ data-vaul-snap-points={isOpen && hasSnapPoints ? 'true' : 'false'}
{...rest}
ref={composedRef}
style={
@@ -974,27 +813,21 @@ export const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
}
}}
onPointerDown={(event) => {
- if (handleOnly) return;
rest.onPointerDown?.(event);
pointerStartRef.current = { x: event.clientX, y: event.clientY };
onPress(event);
}}
onPointerDownOutside={(e) => {
onPointerDownOutside?.(e);
+
if (!modal || e.defaultPrevented) {
e.preventDefault();
return;
}
+
if (keyboardIsOpen.current) {
keyboardIsOpen.current = false;
}
- e.preventDefault();
- onOpenChange?.(false);
- if (!dismissible || openProp !== undefined) {
- return;
- }
-
- closeDrawer();
}}
onFocusOutside={(e) => {
if (!modal) {
@@ -1009,7 +842,6 @@ export const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
}
}}
onPointerMove={(event) => {
- if (handleOnly) return;
rest.onPointerMove?.(event);
if (!pointerStartRef.current) return;
const yPosition = event.clientY - pointerStartRef.current.y;
@@ -1069,7 +901,6 @@ export const Drawer = {
Root,
NestedRoot,
Content,
- Handle,
Overlay,
Trigger: DialogPrimitive.Trigger,
Portal: DialogPrimitive.Portal,
diff --git a/src/style.css b/src/style.css
index befc73d..dbae8a0 100644
--- a/src/style.css
+++ b/src/style.css
@@ -2,61 +2,102 @@
touch-action: none;
will-change: transform;
transition: transform 0.5s cubic-bezier(0.32, 0.72, 0, 1);
+ animation-duration: 0.5s;
+ animation-timing-function: cubic-bezier(0.32, 0.72, 0, 1);
}
-[data-vaul-drawer][data-vaul-drawer-direction='bottom'] {
+[data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='bottom'][data-state='open'] {
+ animation-name: slideFromBottom;
+}
+[data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='bottom'][data-state='closed'] {
+ animation-name: slideToBottom;
+}
+
+[data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='top'][data-state='open'] {
+ animation-name: slideFromTop;
+}
+[data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='top'][data-state='closed'] {
+ animation-name: slideToTop;
+}
+
+[data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='left'][data-state='open'] {
+ animation-name: slideFromLeft;
+}
+[data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='left'][data-state='closed'] {
+ animation-name: slideToLeft;
+}
+
+[data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='right'][data-state='open'] {
+ animation-name: slideFromRight;
+}
+[data-vaul-drawer][data-vaul-snap-points='false'][data-vaul-drawer-direction='right'][data-state='closed'] {
+ animation-name: slideToRight;
+}
+
+[data-vaul-drawer][data-vaul-snap-points='true'][data-vaul-drawer-direction='bottom'] {
transform: translate3d(0, 100%, 0);
}
-[data-vaul-drawer][data-vaul-drawer-direction='top'] {
+[data-vaul-drawer][data-vaul-snap-points='true'][data-vaul-drawer-direction='top'] {
transform: translate3d(0, -100%, 0);
}
-[data-vaul-drawer][data-vaul-drawer-direction='left'] {
+[data-vaul-drawer][data-vaul-snap-points='true'][data-vaul-drawer-direction='left'] {
transform: translate3d(-100%, 0, 0);
}
-[data-vaul-drawer][data-vaul-drawer-direction='right'] {
+[data-vaul-drawer][data-vaul-snap-points='true'][data-vaul-drawer-direction='right'] {
transform: translate3d(100%, 0, 0);
}
-.vaul-dragging .vaul-scrollable [data-vaul-drawer-direction='top'] {
+.data-vaul-dragging .data-vaul-scrollable [vault-drawer-direction='top'] {
overflow-y: hidden !important;
}
-.vaul-dragging .vaul-scrollable [data-vaul-drawer-direction='bottom'] {
+.data-vaul-dragging .data-vaul-scrollable [vault-drawer-direction='bottom'] {
overflow-y: hidden !important;
}
-.vaul-dragging .vaul-scrollable [data-vaul-drawer-direction='left'] {
+.data-vaul-dragging .data-vaul-scrollable [vault-drawer-direction='left'] {
overflow-x: hidden !important;
}
-.vaul-dragging .vaul-scrollable [data-vaul-drawer-direction='right'] {
+.data-vaul-dragging .data-vaul-scrollable [vault-drawer-direction='right'] {
overflow-x: hidden !important;
}
-[data-vaul-drawer][data-vaul-drawer-visible='true'][data-vaul-drawer-direction='top'] {
+[data-vaul-drawer][data-vaul-delayed-snap-points='true'][data-vaul-drawer-direction='top'] {
transform: translate3d(0, var(--snap-point-height, 0), 0);
}
-[data-vaul-drawer][data-vaul-drawer-visible='true'][data-vaul-drawer-direction='bottom'] {
+[data-vaul-drawer][data-vaul-delayed-snap-points='true'][data-vaul-drawer-direction='bottom'] {
transform: translate3d(0, var(--snap-point-height, 0), 0);
}
-[data-vaul-drawer][data-vaul-drawer-visible='true'][data-vaul-drawer-direction='left'] {
+[data-vaul-drawer][data-vaul-delayed-snap-points='true'][data-vaul-drawer-direction='left'] {
transform: translate3d(var(--snap-point-height, 0), 0, 0);
}
-[data-vaul-drawer][data-vaul-drawer-visible='true'][data-vaul-drawer-direction='right'] {
+[data-vaul-drawer][data-vaul-delayed-snap-points='true'][data-vaul-drawer-direction='right'] {
transform: translate3d(var(--snap-point-height, 0), 0, 0);
}
-[data-vaul-overlay] {
+[data-vaul-overlay][data-vaul-snap-points='false'] {
+ animation-duration: 0.5s;
+ animation-timing-function: cubic-bezier(0.32, 0.72, 0, 1);
+}
+[data-vaul-overlay][data-vaul-snap-points='false'][data-state='open'] {
+ animation-name: fadeIn;
+}
+[data-vaul-overlay][data-vaul-snap-points='false'][data-state='closed'] {
+ animation-name: fadeOut;
+}
+
+[data-vaul-overlay][data-vaul-snap-points='true'] {
opacity: 0;
transition: opacity 0.5s cubic-bezier(0.32, 0.72, 0, 1);
}
-[data-vaul-overlay][data-vaul-drawer-visible='true'] {
+[data-vaul-overlay][data-vaul-snap-points='true'] {
opacity: 1;
}
@@ -99,63 +140,95 @@
width: 200%;
}
-[data-vaul-handle] {
- display: block;
- position: relative;
- opacity: 0.8;
- margin-left: auto;
- margin-right: auto;
- height: 5px;
- width: 56px;
- border-radius: 1rem;
- touch-action: pan-y;
- cursor: grab;
+[data-vaul-overlay][data-vaul-snap-points='true']:not([data-vaul-snap-points-overlay='true']):not(
+ [data-state='closed']
+ ) {
+ opacity: 0;
}
-[data-vaul-handle]:hover,
-[data-vaul-handle]:active {
+[data-vaul-overlay][data-vaul-snap-points-overlay='true'] {
opacity: 1;
}
-[data-vaul-handle]:active {
- cursor: grabbing;
+/* This will allow us to not animate via animation, but still benefit from delaying unmount via Radix. */
+@keyframes fake-animation {
+ from {
+ }
+ to {
+ }
}
-[data-vaul-handle-hitarea] {
- position: absolute;
- left: 50%;
- top: 50%;
- transform: translate(-50%, -50%);
- width: max(100%, 2.75rem); /* 44px */
- height: max(100%, 2.75rem); /* 44px */
- touch-action: inherit;
+@keyframes fadeIn {
+ from {
+ opacity: 0;
+ }
+ to {
+ opacity: 1;
+ }
}
-[data-vaul-overlay][data-vaul-snap-points='true']:not([data-vaul-snap-points-overlay='true']):not([data-state='closed']) {
- opacity: 0;
+@keyframes fadeOut {
+ to {
+ opacity: 0;
+ }
}
-[data-vaul-overlay][data-vaul-snap-points-overlay='true']:not([data-vaul-drawer-visible='false']) {
- opacity: 1;
+@keyframes slideFromBottom {
+ from {
+ transform: translate3d(0, 100%, 0);
+ }
+ to {
+ transform: translate3d(0, 0, 0);
+ }
}
-/* This will allow us to not animate via animation, but still benefit from delaying unmount via Radix. */
-@keyframes fake-animation {
+@keyframes slideToBottom {
+ to {
+ transform: translate3d(0, 100%, 0);
+ }
+}
+
+@keyframes slideFromTop {
+ from {
+ transform: translate3d(0, -100%, 0);
+ }
+ to {
+ transform: translate3d(0, 0, 0);
+ }
+}
+
+@keyframes slideToTop {
+ to {
+ transform: translate3d(0, -100%, 0);
+ }
+}
+
+@keyframes slideFromLeft {
from {
+ transform: translate3d(-100%, 0, 0);
}
to {
+ transform: translate3d(0, 0, 0);
}
}
-@media (hover: hover) and (pointer: fine) {
- [data-vaul-drawer] {
- user-select: none;
+@keyframes slideToLeft {
+ to {
+ transform: translate3d(-100%, 0, 0);
}
}
-@media (pointer: fine) {
- [data-vaul-handle-hitarea]: {
- width: 100%;
- height: 100%;
+@keyframes slideFromRight {
+ from {
+ transform: translate3d(100%, 0, 0);
+ }
+ to {
+ transform: translate3d(0, 0, 0);
+ }
+}
+
+@keyframes slideToRight {
+ to {
+ transform: translate3d(100%, 0, 0);
}
}
diff --git a/src/use-snap-points.ts b/src/use-snap-points.ts
index 6a1e872..dbaab31 100644
--- a/src/use-snap-points.ts
+++ b/src/use-snap-points.ts
@@ -109,6 +109,7 @@ export function useSnapPoints({
(dimension: number) => {
const newSnapPointIndex = snapPointsOffset?.findIndex((snapPointDim) => snapPointDim === dimension) ?? null;
onSnapPointChange(newSnapPointIndex);
+
set(drawerRef.current, {
transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
transform: isVertical(direction) ? `translate3d(0, ${dimension}px, 0)` : `translate3d(${dimension}px, 0, 0)`,
@@ -138,8 +139,7 @@ export function useSnapPoints({
React.useEffect(() => {
if (activeSnapPoint || activeSnapPointProp) {
const newIndex =
- snapPoints?.findIndex((snapPoint) => snapPoint === activeSnapPointProp || snapPoint === activeSnapPoint) ??
- -1;
+ snapPoints?.findIndex((snapPoint) => snapPoint === activeSnapPointProp || snapPoint === activeSnapPoint) ?? -1;
if (snapPointsOffset && newIndex !== -1 && typeof snapPointsOffset[newIndex] === 'number') {
snapToPoint(snapPointsOffset[newIndex] as number);
}
diff --git a/test/src/app/default-open/page.tsx b/test/src/app/default-open/page.tsx
new file mode 100644
index 0000000..b217963
--- /dev/null
+++ b/test/src/app/default-open/page.tsx
@@ -0,0 +1,203 @@
+'use client';
+
+import { useState } from 'react';
+import { Drawer } from 'vaul';
+
+export default function Page() {
+ return (
+ <div className="w-screen h-screen bg-white p-8 flex justify-center items-center" data-vaul-drawer-wrapper="">
+ <Drawer.Root defaultOpen>
+ <Drawer.Trigger asChild>
+ <button data-testid="trigger" className="text-2xl">
+ Open Drawer
+ </button>
+ </Drawer.Trigger>
+ <Drawer.Portal>
+ <Drawer.Overlay data-testid="overlay" className="fixed inset-0 bg-black/40" />
+ <Drawer.Content
+ data-testid="content"
+ className="bg-zinc-100 flex flex-col rounded-t-[10px] h-[96%] mt-24 fixed bottom-0 left-0 right-0"
+ >
+ <Drawer.Close data-testid="drawer-close">Close</Drawer.Close>
+ <button data-testid="controlled-close" className="text-2xl">
+ Close
+ </button>
+ <div className="p-4 bg-white rounded-t-[10px] flex-1">
+ <div className="mx-auto w-12 h-1.5 flex-shrink-0 rounded-full bg-zinc-300 mb-8" />
+ <div className="max-w-md mx-auto">
+ <Drawer.Title className="font-medium mb-4">Unstyled drawer for React.</Drawer.Title>
+ <p className="text-zinc-600 mb-2">
+ This component can be used as a replacement for a Dialog on mobile and tablet devices.
+ </p>
+ <p className="text-zinc-600 mb-8">
+ It uses{' '}
+ <a
+ href="https://www.radix-ui.com/docs/primitives/components/dialog"
+ className="underline"
+ target="_blank"
+ >
+ Radix&apos;s Dialog primitive
+ </a>{' '}
+ under the hood and is inspired by{' '}
+ <a
+ href="https://twitter.com/devongovett/status/1674470185783402496"
+ className="underline"
+ target="_blank"
+ >
+ this tweet.
+ </a>
+ </p>
+ </div>
+ </div>
+ <div className="p-4 bg-zinc-100 border-t border-zinc-200 mt-auto">
+ <div className="flex gap-6 justify-end max-w-md mx-auto">
+ <a
+ className="text-xs text-zinc-600 flex items-center gap-0.25"
+ href="https://github.com/emilkowalski/vaul"
+ target="_blank"
+ >
+ GitHub
+ <svg
+ fill="none"
+ height="16"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="2"
+ viewBox="0 0 24 24"
+ width="16"
+ aria-hidden="true"
+ className="w-3 h-3 ml-1"
+ >
+ <path d="M18 13v6a2 2 0 01-2 2H5a2 2 0 01-2-2V8a2 2 0 012-2h6"></path>
+ <path d="M15 3h6v6"></path>
+ <path d="M10 14L21 3"></path>
+ </svg>
+ </a>
+ <a
+ className="text-xs text-zinc-600 flex items-center gap-0.25"
+ href="https://twitter.com/emilkowalski_"
+ target="_blank"
+ >
+ Twitter
+ <svg
+ fill="none"
+ height="16"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="2"
+ viewBox="0 0 24 24"
+ width="16"
+ aria-hidden="true"
+ className="w-3 h-3 ml-1"
+ >
+ <path d="M18 13v6a2 2 0 01-2 2H5a2 2 0 01-2-2V8a2 2 0 012-2h6"></path>
+ <path d="M15 3h6v6"></path>
+ <path d="M10 14L21 3"></path>
+ </svg>
+ </a>
+ </div>
+ </div>
+ </Drawer.Content>
+ </Drawer.Portal>
+ </Drawer.Root>
+ <Drawer.Root>
+ <Drawer.Trigger asChild>
+ <button data-testid="fully-controlled-trigger" className="text-2xl">
+ Open Drawer
+ </button>
+ </Drawer.Trigger>
+ <Drawer.Portal>
+ <Drawer.Overlay data-testid="overlay" className="fixed inset-0 bg-black/40" />
+ <Drawer.Content
+ data-testid="fully-controlled-content"
+ className="bg-zinc-100 flex flex-col rounded-t-[10px] h-[96%] mt-24 fixed bottom-0 left-0 right-0"
+ >
+ <Drawer.Close data-testid="drawer-close">Close</Drawer.Close>
+ <button data-testid="controlled-close" className="text-2xl">
+ Close
+ </button>
+ <div className="p-4 bg-white rounded-t-[10px] flex-1">
+ <div className="mx-auto w-12 h-1.5 flex-shrink-0 rounded-full bg-zinc-300 mb-8" />
+ <div className="max-w-md mx-auto">
+ <Drawer.Title className="font-medium mb-4">Unstyled drawer for React.</Drawer.Title>
+ <p className="text-zinc-600 mb-2">
+ This component can be used as a replacement for a Dialog on mobile and tablet devices.
+ </p>
+ <p className="text-zinc-600 mb-8">
+ It uses{' '}
+ <a
+ href="https://www.radix-ui.com/docs/primitives/components/dialog"
+ className="underline"
+ target="_blank"
+ >
+ Radix&apos;s Dialog primitive
+ </a>{' '}
+ under the hood and is inspired by{' '}
+ <a
+ href="https://twitter.com/devongovett/status/1674470185783402496"
+ className="underline"
+ target="_blank"
+ >
+ this tweet.
+ </a>
+ </p>
+ </div>
+ </div>
+ <div className="p-4 bg-zinc-100 border-t border-zinc-200 mt-auto">
+ <div className="flex gap-6 justify-end max-w-md mx-auto">
+ <a
+ className="text-xs text-zinc-600 flex items-center gap-0.25"
+ href="https://github.com/emilkowalski/vaul"
+ target="_blank"
+ >
+ GitHub
+ <svg
+ fill="none"
+ height="16"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="2"
+ viewBox="0 0 24 24"
+ width="16"
+ aria-hidden="true"
+ className="w-3 h-3 ml-1"
+ >
+ <path d="M18 13v6a2 2 0 01-2 2H5a2 2 0 01-2-2V8a2 2 0 012-2h6"></path>
+ <path d="M15 3h6v6"></path>
+ <path d="M10 14L21 3"></path>
+ </svg>
+ </a>
+ <a
+ className="text-xs text-zinc-600 flex items-center gap-0.25"
+ href="https://twitter.com/emilkowalski_"
+ target="_blank"
+ >
+ Twitter
+ <svg
+ fill="none"
+ height="16"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="2"
+ viewBox="0 0 24 24"
+ width="16"
+ aria-hidden="true"
+ className="w-3 h-3 ml-1"
+ >
+ <path d="M18 13v6a2 2 0 01-2 2H5a2 2 0 01-2-2V8a2 2 0 012-2h6"></path>
+ <path d="M15 3h6v6"></path>
+ <path d="M10 14L21 3"></path>
+ </svg>
+ </a>
+ </div>
+ </div>
+ </Drawer.Content>
+ </Drawer.Portal>
+ </Drawer.Root>
+ </div>
+ );
+}
diff --git a/test/src/app/with-handle/page.tsx b/test/src/app/with-handle/page.tsx
deleted file mode 100644
index 09b4979..0000000
--- a/test/src/app/with-handle/page.tsx
+++ /dev/null
@@ -1,156 +0,0 @@
-'use client';
-
-import { clsx } from 'clsx';
-import { useState } from 'react';
-import { Drawer } from 'vaul';
-
-const snapPoints = ['148px', '355px'];
-
-export default function Page() {
- const [snap, setSnap] = useState<number | string | null>(snapPoints[0]);
-
- const activeSnapPointIndex = snapPoints.indexOf(snap as string);
-
- return (
- <div className="w-screen h-screen bg-white p-8 flex justify-center items-center">
- <div data-testid="active-snap-index">{activeSnapPointIndex}</div>
- <Drawer.Root open snapPoints={snapPoints} activeSnapPoint={snap} setActiveSnapPoint={setSnap}>
- <Drawer.Trigger asChild>
- <button data-testid="trigger">Open Drawer</button>
- </Drawer.Trigger>
- <Drawer.Overlay className="fixed inset-0 bg-black/40" />
- <Drawer.Portal>
- <Drawer.Content
- data-testid="content"
- className="fixed flex flex-col bg-white border border-gray-200 border-b-none rounded-t-[10px] bottom-0 left-0 right-0 h-full max-h-[97%] mx-[-1px]"
- >
- <Drawer.Handle data-testid="handle" className="bg-gray-300 mb-8" />
- <div
- className={clsx('flex flex-col max-w-md mx-auto w-full p-4 pt-5', {
- 'overflow-y-auto': snap === 1,
- 'overflow-hidden': snap !== 1,
- })}
- >
- <div className="flex items-center">
- <svg
- className="text-yellow-400 h-5 w-5 flex-shrink-0"
- viewBox="0 0 20 20"
- fill="currentColor"
- aria-hidden="true"
- >
- <path
- fill-rule="evenodd"
- d="M10.868 2.884c-.321-.772-1.415-.772-1.736 0l-1.83 4.401-4.753.381c-.833.067-1.171 1.107-.536 1.651l3.62 3.102-1.106 4.637c-.194.813.691 1.456 1.405 1.02L10 15.591l4.069 2.485c.713.436 1.598-.207 1.404-1.02l-1.106-4.637 3.62-3.102c.635-.544.297-1.584-.536-1.65l-4.752-.382-1.831-4.401z"
- clip-rule="evenodd"
- ></path>
- </svg>
- <svg
- className="text-yellow-400 h-5 w-5 flex-shrink-0"
- viewBox="0 0 20 20"
- fill="currentColor"
- aria-hidden="true"
- >
- <path
- fill-rule="evenodd"
- d="M10.868 2.884c-.321-.772-1.415-.772-1.736 0l-1.83 4.401-4.753.381c-.833.067-1.171 1.107-.536 1.651l3.62 3.102-1.106 4.637c-.194.813.691 1.456 1.405 1.02L10 15.591l4.069 2.485c.713.436 1.598-.207 1.404-1.02l-1.106-4.637 3.62-3.102c.635-.544.297-1.584-.536-1.65l-4.752-.382-1.831-4.401z"
- clip-rule="evenodd"
- ></path>
- </svg>
- <svg
- className="text-yellow-400 h-5 w-5 flex-shrink-0"
- viewBox="0 0 20 20"
- fill="currentColor"
- aria-hidden="true"
- >
- <path
- fill-rule="evenodd"
- d="M10.868 2.884c-.321-.772-1.415-.772-1.736 0l-1.83 4.401-4.753.381c-.833.067-1.171 1.107-.536 1.651l3.62 3.102-1.106 4.637c-.194.813.691 1.456 1.405 1.02L10 15.591l4.069 2.485c.713.436 1.598-.207 1.404-1.02l-1.106-4.637 3.62-3.102c.635-.544.297-1.584-.536-1.65l-4.752-.382-1.831-4.401z"
- clip-rule="evenodd"
- ></path>
- </svg>
- <svg
- className="text-yellow-400 h-5 w-5 flex-shrink-0"
- viewBox="0 0 20 20"
- fill="currentColor"
- aria-hidden="true"
- >
- <path
- fill-rule="evenodd"
- d="M10.868 2.884c-.321-.772-1.415-.772-1.736 0l-1.83 4.401-4.753.381c-.833.067-1.171 1.107-.536 1.651l3.62 3.102-1.106 4.637c-.194.813.691 1.456 1.405 1.02L10 15.591l4.069 2.485c.713.436 1.598-.207 1.404-1.02l-1.106-4.637 3.62-3.102c.635-.544.297-1.584-.536-1.65l-4.752-.382-1.831-4.401z"
- clip-rule="evenodd"
- ></path>
- </svg>
- <svg
- className="text-gray-300 h-5 w-5 flex-shrink-0"
- viewBox="0 0 20 20"
- fill="currentColor"
- aria-hidden="true"
- >
- <path
- fill-rule="evenodd"
- d="M10.868 2.884c-.321-.772-1.415-.772-1.736 0l-1.83 4.401-4.753.381c-.833.067-1.171 1.107-.536 1.651l3.62 3.102-1.106 4.637c-.194.813.691 1.456 1.405 1.02L10 15.591l4.069 2.485c.713.436 1.598-.207 1.404-1.02l-1.106-4.637 3.62-3.102c.635-.544.297-1.584-.536-1.65l-4.752-.382-1.831-4.401z"
- clip-rule="evenodd"
- ></path>
- </svg>
- </div>{' '}
- <h1 className="text-2xl mt-2 font-medium">The Hidden Details</h1>
- <p className="text-sm mt-1 text-gray-600 mb-6">2 modules, 27 hours of video</p>
- <p className="text-gray-600">
- The world of user interface design is an intricate landscape filled with hidden details and nuance. In
- this course, you will learn something cool. To the untrained eye, a beautifully designed UI.
- </p>
- <button className="bg-black text-gray-50 mt-8 rounded-md h-[48px] flex-shrink-0 font-medium">
- Buy for $199
- </button>
- <div className="mt-12">
- <h2 className="text-xl font-medium">Module 01. The Details</h2>
- <div className="space-y-4 mt-4">
- <div>
- <span className="block">Layers of UI</span>
- <span className="text-gray-600">A basic introduction to Layers of Design.</span>
- </div>
- <div>
- <span className="block">Typography</span>
- <span className="text-gray-600">The fundamentals of type.</span>
- </div>
- <div>
- <span className="block">UI Animations</span>
- <span className="text-gray-600">Going through the right easings and durations.</span>
- </div>
- </div>
- </div>
- <div className="mt-12">
- <figure>
- <blockquote className="font-serif">
- “I especially loved the hidden details video. That was so useful, learned a lot by just reading it.
- Can&rsquo;t wait for more course content!”
- </blockquote>
- <figcaption>
- <span className="text-sm text-gray-600 mt-2 block">Yvonne Ray, Frontend Developer</span>
- </figcaption>
- </figure>
- </div>
- <div className="mt-12">
- <h2 className="text-xl font-medium">Module 02. The Process</h2>
- <div className="space-y-4 mt-4">
- <div>
- <span className="block">Build</span>
- <span className="text-gray-600">Create cool components to practice.</span>
- </div>
- <div>
- <span className="block">User Insight</span>
- <span className="text-gray-600">Find out what users think and fine-tune.</span>
- </div>
- <div>
- <span className="block">Putting it all together</span>
- <span className="text-gray-600">Let&apos;s build an app together and apply everything.</span>
- </div>
- </div>
- </div>
- </div>
- </Drawer.Content>
- </Drawer.Portal>
- </Drawer.Root>
- </div>
- );
-}
diff --git a/test/tests/base.spec.ts b/test/tests/base.spec.ts
index 22d3e8b..6c02a94 100644
--- a/test/tests/base.spec.ts
+++ b/test/tests/base.spec.ts
@@ -40,6 +40,12 @@ test.describe('Base tests', () => {
await expect(page.getByTestId('content')).not.toBeVisible();
});
+ test('should be open by defafult when `defaultOpen` is true', async ({ page }) => {
+ await page.goto('/default-open');
+
+ await expect(page.getByTestId('content')).toBeVisible();
+ });
+
test('should close when dragged down', async ({ page }) => {
await openDrawer(page);
await page.hover('[data-vaul-drawer]');
diff --git a/test/tests/with-handle.spec.ts b/test/tests/with-handle.spec.ts
deleted file mode 100644
index 1104ada..0000000
--- a/test/tests/with-handle.spec.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import { test, expect } from '@playwright/test';
-import { ANIMATION_DURATION } from './constants';
-
-test.beforeEach(async ({ page }) => {
- await page.goto('/with-handle');
-});
-
-test.describe('With handle', () => {
- test('click should cycle to the next snap point', async ({ page }) => {
- await page.waitForTimeout(ANIMATION_DURATION);
-
- await expect(page.getByTestId('content')).toBeVisible();
- await expect(page.getByTestId('active-snap-index')).toHaveText('0');
-
- await page.getByTestId('handle').click();
- await expect(page.getByTestId('active-snap-index')).toHaveText('1');
-
- await page.getByTestId('handle').click();
- await expect(page.getByTestId('content')).not.toBeVisible();
- });
-
- test('double click should close the drawer', async ({ page }) => {
- await page.waitForTimeout(ANIMATION_DURATION);
-
- await expect(page.getByTestId('content')).toBeVisible();
- await expect(page.getByTestId('active-snap-index')).toHaveText('0');
- await page.getByTestId('handle').dispatchEvent("dblclick");
-
- await page.waitForTimeout(ANIMATION_DURATION);
- await expect(page.getByTestId('content')).not.toBeVisible();
- });
-});
diff --git a/website/src/app/components/hero.tsx b/website/src/app/components/hero.tsx
index c2dc262..239b63e 100644
--- a/website/src/app/components/hero.tsx
+++ b/website/src/app/components/hero.tsx
@@ -1,6 +1,7 @@
'use client';
import { Drawer } from 'vaul';
+import React from 'react';
export function Hero() {
return (
@@ -53,7 +54,7 @@ export function Hero() {
<Drawer.Overlay className="fixed inset-0 bg-black/40" />
<Drawer.Content className="bg-gray-100 flex flex-col rounded-t-[10px] h-full mt-24 max-h-[96%] fixed bottom-0 left-0 right-0">
<div className="p-4 bg-white rounded-t-[10px] flex-1">
- <Drawer.Handle className="bg-gray-300 mb-8" />
+ <div className="mx-auto w-12 h-1.5 flex-shrink-0 rounded-full bg-gray-300 mb-8" />
<div className="max-w-md mx-auto">
<Drawer.Title className="font-medium mb-4">Drawer for React.</Drawer.Title>
<p className="text-gray-600 mb-2">