Skip to content

Commit

Permalink
[DevOverlay] close popover/overlay on Esc
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Jan 14, 2025
1 parent 6616e20 commit edc28ca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ const DevToolsPopover = ({
const buttonRef = useRef<HTMLDivElement>(null)
const [isPopoverOpen, setIsPopoverOpen] = useState(false)

// Close popover when clicking outside of it or its button
useEffect(() => {
// Close popover when clicking outside of it or its button
const handleClickOutside = (event: MouseEvent) => {
if (
!(popoverRef.current?.getBoundingClientRect()
Expand All @@ -77,8 +77,19 @@ const DevToolsPopover = ({
}
}

// Close popover when pressing escape
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
setIsPopoverOpen(false)
}
}

document.addEventListener('mousedown', handleClickOutside)
return () => document.removeEventListener('mousedown', handleClickOutside)
document.addEventListener('keydown', handleKeyDown)
return () => {
document.removeEventListener('mousedown', handleClickOutside)
document.removeEventListener('keydown', handleKeyDown)
}
}, [])

const togglePopover = () => setIsPopoverOpen((prev) => !prev)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ export function Errors({
}
}, [errors.length, minimize])

useEffect(() => {
function handleKeyDown(event: KeyboardEvent) {
if (event.key === 'Escape') {
setDisplayState('minimized')
}
}

document.addEventListener('keydown', handleKeyDown)
return () => document.removeEventListener('keydown', handleKeyDown)
}, [])

const hide = useCallback(() => setDisplayState('hidden'), [])
const fullscreen = useCallback(() => setDisplayState('fullscreen'), [])

Expand Down

0 comments on commit edc28ca

Please sign in to comment.