Skip to content

Commit 39210df

Browse files
committed
Prevent default behaviour when clicking outside of a Dialog.Panel (#2919)
* use `event.preventDefault()` in the `useOutsideClick` on Dialog's When using a `Dialog`, we should prevent the default behaviour of the event that triggered the "close" in the `useOutsideClick` call. We recently made improvements to improve outside click behaviour on touch devices (#2572) but due to the `touchend` event, the touch is still forwarded and therefore a potential button _behind_ the "backdrop" will also be clicked. This is not what we want. Added the `event.preventDefault()` for the Dialog specifically because there are other places where we use `useOutsideClick` and where we _do_ want the behaviour where the click just continues. A concrete example of this is 2 `Menu`'s next to eachother where you open the first one, and then click on the second one. This should close first one (outside click) and open the second one (by not preventing the event) * update changelog
1 parent 369be39 commit 39210df

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

packages/@headlessui-react/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
- Make sure panels re-register when IDs are calculated in React < 18 ([#2883](https://github.com/tailwindlabs/headlessui/pull/2883))
1818
- Expose `disabled` state on `<Tab />` component ([#2918](https://github.com/tailwindlabs/headlessui/pull/2918))
19+
- Prevent default behaviour when clicking outside of a `Dialog.Panel` ([#2919](https://github.com/tailwindlabs/headlessui/pull/2919))
1920

2021
## [1.7.18] - 2024-01-08
2122

packages/@headlessui-react/src/components/dialog/dialog.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,14 @@ function DialogFn<TTag extends ElementType = typeof DEFAULT_DIALOG_TAG>(
299299
if (hasNestedDialogs) return false
300300
return true
301301
})()
302-
useOutsideClick(resolveRootContainers, close, outsideClickEnabled)
302+
useOutsideClick(
303+
resolveRootContainers,
304+
(event) => {
305+
event.preventDefault()
306+
close()
307+
},
308+
outsideClickEnabled
309+
)
303310

304311
// Handle `Escape` to close
305312
let escapeToCloseEnabled = (() => {

packages/@headlessui-vue/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

1212
- Expose `disabled` state on `<Tab />` component ([#2918](https://github.com/tailwindlabs/headlessui/pull/2918))
13+
- Prevent default behaviour when clicking outside of a `DialogPanel` ([#2919](https://github.com/tailwindlabs/headlessui/pull/2919))
1314

1415
## [1.7.19] - 2024-02-07
1516

packages/@headlessui-vue/src/components/dialog/dialog.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ export let Dialog = defineComponent({
238238
})
239239
useOutsideClick(
240240
resolveRootContainers,
241-
(_event, target) => {
241+
(event, target) => {
242+
event.preventDefault()
242243
api.close()
243244
nextTick(() => target?.focus())
244245
},

0 commit comments

Comments
 (0)