Skip to content

Commit

Permalink
🎨 adds higher level closeable control prop
Browse files Browse the repository at this point in the history
  • Loading branch information
cottiera committed Oct 9, 2024
1 parent 1a5504d commit 5a1ec96
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/components/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ interface ModalProps {
/* 32x32 Phosphor icon for the title */
icon?: React.ReactNode;

/* Determines if the user can close the modal in any way */
closeable?: boolean;

/* Hides the close button in the top-right corner */
hideCloseButton?: boolean;

Expand All @@ -39,6 +42,7 @@ function Modal({
size = "lg",
title,
icon = null,
closeable = true,
hideCloseButton = false,
disableCloseOnEscape = false,
disableCloseOnClickOutside = false,
Expand Down Expand Up @@ -77,14 +81,15 @@ function Modal({
const handleClickOutside = useCallback(
(event: MouseEvent) => {
if (
closeable &&
!disableCloseOnClickOutside &&
modalRef.current &&
!modalRef.current.contains(event.target as Node)
) {
onClose();
}
},
[disableCloseOnClickOutside, onClose],
[closeable, disableCloseOnClickOutside, onClose],
);

// Close modal on click outside
Expand All @@ -101,7 +106,7 @@ function Modal({
// Close modal on escape keypress
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (!disableCloseOnEscape && event.key === "Escape") {
if (closeable && !disableCloseOnEscape && event.key === "Escape") {
onClose();
}
};
Expand All @@ -111,7 +116,7 @@ function Modal({
}

return () => document.removeEventListener("keydown", handleKeyDown);
}, [isOpen, disableCloseOnEscape, onClose]);
}, [isOpen, closeable, disableCloseOnEscape, onClose]);

// Encapsulates the visibility of the modal in a prop
if (!isOpen) return null;
Expand All @@ -126,7 +131,7 @@ function Modal({
<i className={clsx(headerTitleIcon)}>{icon}</i>
<h3>{title}</h3>
</div>
{!hideCloseButton && (
{closeable && !hideCloseButton && (
<div
className={clsx(closeButton)}
onClick={onClose}
Expand Down

0 comments on commit 5a1ec96

Please sign in to comment.