Skip to content

Commit

Permalink
handle disableNativeEvents, hideCloseButton and onClose optional in D…
Browse files Browse the repository at this point in the history
…ialog component
  • Loading branch information
Sharqiewicz committed Aug 16, 2024
1 parent 3b5be5b commit 078b831
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions src/components/Dialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CloseButton } from '../buttons/CloseButton';

interface DialogProps {
visible: boolean;
onClose: () => void;
onClose?: () => void;
headerText?: string;
content: JSX.Element;
actions?: JSX.Element;
Expand All @@ -14,10 +14,23 @@ interface DialogProps {
className?: string;
};
id?: string;
disableNativeEvents?: boolean;
hideCloseButton?: boolean;
}

export const Dialog: FC<DialogProps> = ({ visible, onClose, headerText, content, actions, id, form }) => {
export const Dialog: FC<DialogProps> = ({
visible,
onClose,
headerText,
content,
actions,
id,
form,
disableNativeEvents = false,
hideCloseButton = false,
}) => {
const ref = useRef<HTMLDialogElement>(null);
const dialog = ref.current;

// If it was the form submission we want to only close the dialog without calling onClose
const [isSubmitting, setIsSubmitting] = useState(false);
Expand All @@ -31,7 +44,7 @@ export const Dialog: FC<DialogProps> = ({ visible, onClose, headerText, content,
}

dialog.close();
onClose();
onClose && onClose();
},
[isSubmitting, onClose],
);
Expand All @@ -45,7 +58,6 @@ export const Dialog: FC<DialogProps> = ({ visible, onClose, headerText, content,

// Manage native <dialog> events and visibility
useEffect(() => {
const dialog = ref.current;
if (dialog) {
dialog.addEventListener('close', closeListener);
if (visible && !dialog.open) {
Expand All @@ -58,7 +70,25 @@ export const Dialog: FC<DialogProps> = ({ visible, onClose, headerText, content,
dialog.removeEventListener('close', closeListener);
};
}
}, [visible, closeListener, headerText]);
}, [visible, closeListener, headerText, dialog]);

// This useEffect handles disableNativeEvents ( prevents the dialog from closing on Escape key press )
useEffect(() => {
if (!disableNativeEvents || !dialog) return;

const handleKeyDown = (e: KeyboardEvent) => {
if (disableNativeEvents && e.key === 'Escape') {
e.preventDefault();
return;
}
};

dialog.addEventListener('keydown', handleKeyDown);

return () => {
dialog.removeEventListener('keydown', handleKeyDown);
};
}, [disableNativeEvents, headerText, dialog]);

const handleFormSubmit = (event: Event) => {
if (form) {
Expand All @@ -79,9 +109,14 @@ export const Dialog: FC<DialogProps> = ({ visible, onClose, headerText, content,
);

return createPortal(
<Modal className={`bg-base-200 border border-[--modal-border]`} id={id} ref={ref}>
<Modal
className={`bg-base-200 border border-[--modal-border]`}
id={id}
ref={ref}
aria-labelledby={headerText ? `${id}-header` : undefined}
>
<Modal.Header className={`text-2xl claim-title flex mb-5 ${headerText ? 'justify-between' : 'justify-end'}`}>
{headerText} <CloseButton onClick={onClose} />
{headerText} {hideCloseButton ? <></> : <CloseButton onClick={onClose} />}
</Modal.Header>
{form ? (
<form onSubmit={handleFormSubmit} className={form.className} formMethod="dialog">
Expand Down

0 comments on commit 078b831

Please sign in to comment.