Skip to content

Commit

Permalink
Fix broken escape key behaviour (#754)
Browse files Browse the repository at this point in the history
* fix broken `escape` key behaviour

We've "fixed" an issue when we had nested Dialogs ([#430](tailwindlabs/headlessui#430)).
The `escape` would not close the correct Dialog. The issue here was with
the logic to know whether we were the last Dialog or not. The issue was
_not_ how we implemented the `close` functionality.

To make things easier, we moved the global window event to a scoped div
(the Dialog itself). While that fixed the nested Dialog issue, it
introduced this bug where `escape` would not close if you click on a
non-focusable element like a span in the Dialog.

Since that PR we did a bunch of improvements on how the underlying
"stacking" system worked.
This PR reverts to the "global" window event listener so that we can
still catch all of the `escape` keydown events.

Fixes: #524
Fixes: #693

* update changelog
  • Loading branch information
RobinMalfait authored Aug 24, 2021
1 parent 271c236 commit a8419d8
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 12 deletions.
84 changes: 84 additions & 0 deletions src/components/dialog/dialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,90 @@ describe('Keyboard interactions', () => {
assertDialog({ state: DialogState.InvisibleUnmounted })
})
)

it(
'should be possible to close the dialog with Escape, when a field is focused',
suppressConsoleLogs(async () => {
function Example() {
let [isOpen, setIsOpen] = useState(false)
return (
<>
<button id="trigger" onClick={() => setIsOpen(v => !v)}>
Trigger
</button>
<Dialog open={isOpen} onClose={setIsOpen}>
Contents
<input id="name" />
<TabSentinel />
</Dialog>
</>
)
}
render(<Example />)

assertDialog({ state: DialogState.InvisibleUnmounted })

// Open dialog
await click(document.getElementById('trigger'))

// Verify it is open
assertDialog({
state: DialogState.Visible,
attributes: { id: 'headlessui-dialog-1' },
})

// Close dialog
await press(Keys.Escape)

// Verify it is close
assertDialog({ state: DialogState.InvisibleUnmounted })
})
)

it(
'should not be possible to close the dialog with Escape, when a field is focused but cancels the event',
suppressConsoleLogs(async () => {
function Example() {
let [isOpen, setIsOpen] = useState(false)
return (
<>
<button id="trigger" onClick={() => setIsOpen(v => !v)}>
Trigger
</button>
<Dialog open={isOpen} onClose={setIsOpen}>
Contents
<input
id="name"
onKeyDown={event => {
event.preventDefault()
event.stopPropagation()
}}
/>
<TabSentinel />
</Dialog>
</>
)
}
render(<Example />)

assertDialog({ state: DialogState.InvisibleUnmounted })

// Open dialog
await click(document.getElementById('trigger'))

// Verify it is open
assertDialog({
state: DialogState.Visible,
attributes: { id: 'headlessui-dialog-1' },
})

// Try to close the dialog
await press(Keys.Escape)

// Verify it is still open
assertDialog({ state: DialogState.Visible })
})
)
})
})

Expand Down
23 changes: 11 additions & 12 deletions src/components/dialog/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import React, {
useMemo,
useReducer,
useRef,
useState,

// Types
ContextType,
ElementType,
MouseEvent as ReactMouseEvent,
KeyboardEvent as ReactKeyboardEvent,
MutableRefObject,
Ref,
useState,
} from 'react'

import { Props } from '../../types'
Expand Down Expand Up @@ -217,6 +216,16 @@ let DialogRoot = forwardRefWithAs(function Dialog<
close()
})

// Handle `Escape` to close
useWindowEvent('keydown', event => {
if (event.key !== Keys.Escape) return
if (dialogState !== DialogStates.Open) return
if (hasNestedDialogs) return
event.preventDefault()
event.stopPropagation()
close()
})

// Scroll lock
useEffect(() => {
if (dialogState !== DialogStates.Open) return
Expand Down Expand Up @@ -282,16 +291,6 @@ let DialogRoot = forwardRefWithAs(function Dialog<
onClick(event: ReactMouseEvent) {
event.stopPropagation()
},

// Handle `Escape` to close
onKeyDown(event: ReactKeyboardEvent) {
if (event.key !== Keys.Escape) return
if (dialogState !== DialogStates.Open) return
if (hasNestedDialogs) return
event.preventDefault()
event.stopPropagation()
close()
},
}
let passthroughProps = rest

Expand Down

0 comments on commit a8419d8

Please sign in to comment.