Skip to content

Commit

Permalink
Fix modal's hideOnEscape functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
gnapse committed Feb 7, 2024
1 parent 3215fbb commit 3320f7b
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export function Modal({
hideOnInteractOutside = true,
children,
portalElement,
onKeyDown,
...props
}: ModalProps) {
const setOpen = React.useCallback(
Expand Down Expand Up @@ -194,6 +195,22 @@ export function Modal({
[isOpen],
)

const handleKeyDown = React.useCallback(
function handleKeyDown(event: React.KeyboardEvent<HTMLDivElement>) {
if (
hideOnEscape &&
onDismiss != null &&
event.key === 'Escape' &&
!event.defaultPrevented
) {
event.stopPropagation()
onDismiss()
}
onKeyDown?.(event)
},
[onDismiss, hideOnEscape, onKeyDown],
)

if (!isOpen) {
return null
}
Expand Down Expand Up @@ -223,7 +240,6 @@ export function Modal({
ref={dialogRef}
as={Box}
store={store}
hideOnEscape={hideOnEscape}
preventBodyScroll
borderRadius="full"
background="default"
Expand All @@ -242,6 +258,8 @@ export function Modal({
portal={false}
backdrop={false}
hideOnInteractOutside={false}
hideOnEscape={false}
onKeyDown={handleKeyDown}
>
<ModalContext.Provider value={contextValue}>
{children}
Expand Down

0 comments on commit 3320f7b

Please sign in to comment.