Skip to content

Commit aff438e

Browse files
authored
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 2b7a57e commit aff438e

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

packages/@headlessui-react/CHANGELOG.md

+1
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 `Dialog.Panel` ([#2919](https://github.com/tailwindlabs/headlessui/pull/2919))
1314

1415
## [2.0.0-alpha.4] - 2024-01-03
1516

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,14 @@ function DialogFn<TTag extends ElementType = typeof DEFAULT_DIALOG_TAG>(
305305
if (hasNestedDialogs) return false
306306
return true
307307
})()
308-
useOutsideClick(resolveRootContainers, close, outsideClickEnabled)
308+
useOutsideClick(
309+
resolveRootContainers,
310+
(event) => {
311+
event.preventDefault()
312+
close()
313+
},
314+
outsideClickEnabled
315+
)
309316

310317
// Handle `Escape` to close
311318
let escapeToCloseEnabled = (() => {

packages/@headlessui-vue/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
### Fixed
1616

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

1920
## [1.7.17] - 2024-01-08
2021

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ export let Dialog = defineComponent({
237237
})
238238
useOutsideClick(
239239
resolveRootContainers,
240-
(_event, target) => {
240+
(event, target) => {
241+
event.preventDefault()
241242
api.close()
242243
nextTick(() => target?.focus())
243244
},

0 commit comments

Comments
 (0)