Skip to content

Commit c494fa3

Browse files
authored
Ignore Escape when event got prevented in Dialog component (#1424)
* ignore `Escape` when event got prevented Some external libraries only use `event.preventDefault()` and not `event.stopPropagation()`. This means that the Dialog can still receive an `Escape` keydown event which closes the Dialog. We can also think about the `Escape` behaviour inside the modal as the "default behaviour" once the Dialog is open. Therefore, we can also check the `event.defaultPrevented` and ignore this event when this is the case. * update changelog
1 parent c4e35f3 commit c494fa3

File tree

3 files changed

+4
-0
lines changed

3 files changed

+4
-0
lines changed

CHANGELOG.md

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

1212
- Ensure `DialogPanel` exposes its ref ([#1404](https://github.com/tailwindlabs/headlessui/pull/1404))
13+
- Ignore `Escape` when event got prevented in `Dialog` component ([#1424](https://github.com/tailwindlabs/headlessui/pull/1424))
1314

1415
## [Unreleased - @headlessui/react]
1516

1617
### Fixed
1718

1819
- Fix closing of `Popover.Panel` in React 18 ([#1409](https://github.com/tailwindlabs/headlessui/pull/1409))
20+
- Ignore `Escape` when event got prevented in `Dialog` component ([#1424](https://github.com/tailwindlabs/headlessui/pull/1424))
1921

2022
## [@headlessui/react@1.6.1] - 2022-05-03
2123

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ let DialogRoot = forwardRefWithAs(function Dialog<
237237

238238
// Handle `Escape` to close
239239
useEventListener(ownerDocument?.defaultView, 'keydown', (event) => {
240+
if (event.defaultPrevented) return
240241
if (event.key !== Keys.Escape) return
241242
if (dialogState !== DialogStates.Open) return
242243
if (hasNestedDialogs) return

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ export let Dialog = defineComponent({
212212

213213
// Handle `Escape` to close
214214
useEventListener(ownerDocument.value?.defaultView, 'keydown', (event) => {
215+
if (event.defaultPrevented) return
215216
if (event.key !== Keys.Escape) return
216217
if (dialogState.value !== DialogStates.Open) return
217218
if (hasNestedDialogs.value) return

0 commit comments

Comments
 (0)