Skip to content

Commit

Permalink
fix: correctly set dialog's cancel event handler
Browse files Browse the repository at this point in the history
Fixes: #477
  • Loading branch information
targos committed Apr 28, 2023
1 parent 4488265 commit 05bdfc5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/components/modal/ConfirmModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function ConfirmModal(props: ConfirmModalProps) {
children,
} = props;

const { ref, onClick } = useDialog({
const dialogProps = useDialog({
isOpen,
requestCloseOnEsc,
requestCloseOnBackdrop,
Expand All @@ -85,7 +85,7 @@ export function ConfirmModal(props: ConfirmModalProps) {

return (
<Portal>
<ConfirmModalDialog ref={ref} onClick={onClick}>
<ConfirmModalDialog {...dialogProps}>
<ConfirmModalContents headerColor={headerColor} style={{ maxWidth }}>
<ConfirmModalChildrenRoot headerColor={headerColor}>
{children}
Expand Down
4 changes: 2 additions & 2 deletions src/components/modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function Modal(props: ModalProps) {
height,
} = props;

const { ref, onClick } = useDialog({
const dialogProps = useDialog({
isOpen,
requestCloseOnEsc,
requestCloseOnBackdrop,
Expand All @@ -82,7 +82,7 @@ export function Modal(props: ModalProps) {

return (
<Portal>
<DialogRoot ref={ref} onClick={onClick}>
<DialogRoot {...dialogProps}>
<DialogContents
style={{
maxWidth,
Expand Down
43 changes: 24 additions & 19 deletions src/components/modal/useDialog.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import React, { useCallback, useEffect, useRef } from 'react';
import {
MouseEventHandler,
ReactEventHandler,
useCallback,
useEffect,
useRef,
} from 'react';
import { useKbsDisableGlobal } from 'react-kbs';

export function useDialog({
isOpen,
Expand All @@ -11,32 +18,30 @@ export function useDialog({
requestCloseOnBackdrop: boolean;
onRequestClose?: () => void;
}) {
const ref = useRef<HTMLDialogElement>(null);
useKbsDisableGlobal(isOpen);

useEffect(() => {
function onEsc(event: Event) {
event.preventDefault();
if (requestCloseOnEsc && onRequestClose) {
onRequestClose();
}
}
const dialog = ref.current;
if (dialog) {
dialog.addEventListener('cancel', onEsc);
return () => dialog.removeEventListener('cancel', onEsc);
}
}, [onRequestClose, requestCloseOnEsc]);
const dialogRef = useRef<HTMLDialogElement>(null);

useEffect(() => {
const dialog = ref.current;
const dialog = dialogRef.current;
if (dialog && isOpen) {
dialog.showModal();
return () => dialog.close();
}
}, [isOpen]);

const onClick = useCallback(
(event: React.MouseEvent<HTMLDialogElement, MouseEvent>) => {
const onCancel = useCallback<ReactEventHandler<HTMLDialogElement>>(
(event) => {
event.preventDefault();
if (requestCloseOnEsc && onRequestClose) {
onRequestClose();
}
},
[onRequestClose, requestCloseOnEsc],
);

const onClick = useCallback<MouseEventHandler<HTMLDialogElement>>(
(event) => {
// Since the dialog has no size of itself, this condition is only
// `true` when we click on the backdrop.
if (event.target === event.currentTarget && requestCloseOnBackdrop) {
Expand All @@ -46,5 +51,5 @@ export function useDialog({
[requestCloseOnBackdrop, onRequestClose],
);

return { ref, onClick };
return { ref: dialogRef, onClick, onCancel };
}

0 comments on commit 05bdfc5

Please sign in to comment.