-
Notifications
You must be signed in to change notification settings - Fork 4
Enhance Modal component to support controlled and uncontrolled states with improved open state management #1609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,10 @@ import { | |||||||||
| isValidElement, | ||||||||||
| type PropsWithChildren, | ||||||||||
| type ReactNode, | ||||||||||
| useCallback, | ||||||||||
| useEffect, | ||||||||||
| useRef, | ||||||||||
| useState, | ||||||||||
| } from "react"; | ||||||||||
| import "./Modal.css"; | ||||||||||
| import { NewButton, NewIcon } from "../.."; | ||||||||||
|
|
@@ -189,11 +192,63 @@ export function Modal(props: ModalProps) { | |||||||||
| titleContent, | ||||||||||
| footerContent, | ||||||||||
| ariaDescribedby, | ||||||||||
| contentClasses, | ||||||||||
| open: controlledOpen, | ||||||||||
| onOpenChange, | ||||||||||
| defaultOpen, | ||||||||||
| } = props; | ||||||||||
|
|
||||||||||
| const filteredChildren: ReactNode[] = []; | ||||||||||
| const contentRef = useRef<HTMLDivElement | null>(null); | ||||||||||
|
|
||||||||||
| const isControlled = controlledOpen !== undefined; | ||||||||||
| const [uncontrolledOpen, setUncontrolledOpen] = useState<boolean>( | ||||||||||
| defaultOpen ?? false | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| let effectiveOpen = uncontrolledOpen; | ||||||||||
| if (isControlled) { | ||||||||||
| effectiveOpen = controlledOpen!; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Radix Select/DropdownMenu can leave body pointer-events disabled if the modal closes first. | ||||||||||
| const restoreBodyPointerEvents = useCallback(() => { | ||||||||||
| if (typeof document === "undefined") return; | ||||||||||
| const bodyStyle = document.body.style; | ||||||||||
| if (bodyStyle.pointerEvents === "none") { | ||||||||||
| bodyStyle.pointerEvents = ""; | ||||||||||
| if (typeof CustomEvent === "function") { | ||||||||||
| document.dispatchEvent(new CustomEvent("dismissableLayer.update")); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| }, []); | ||||||||||
|
|
||||||||||
| const handleOpenChange = useCallback( | ||||||||||
| (next: boolean) => { | ||||||||||
| onOpenChange?.(next); | ||||||||||
| if (!isControlled) { | ||||||||||
| setUncontrolledOpen(next); | ||||||||||
| } | ||||||||||
| if (!next) { | ||||||||||
| if (typeof window !== "undefined") { | ||||||||||
| window.requestAnimationFrame(restoreBodyPointerEvents); | ||||||||||
| } else { | ||||||||||
| restoreBodyPointerEvents(); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| }, | ||||||||||
| [onOpenChange, isControlled, restoreBodyPointerEvents] | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| useEffect(() => { | ||||||||||
| if (!effectiveOpen) { | ||||||||||
| restoreBodyPointerEvents(); | ||||||||||
| } | ||||||||||
| return () => { | ||||||||||
| restoreBodyPointerEvents(); | ||||||||||
| }; | ||||||||||
| }, [effectiveOpen, restoreBodyPointerEvents]); | ||||||||||
|
|
||||||||||
| let exit = <Modal.Exit />; | ||||||||||
|
|
||||||||||
| let title = titleContent ? <Modal.Title>{titleContent}</Modal.Title> : null; | ||||||||||
|
|
@@ -238,9 +293,9 @@ export function Modal(props: ModalProps) { | |||||||||
|
|
||||||||||
| return ( | ||||||||||
| <Dialog.Root | ||||||||||
| open={props.open} | ||||||||||
| onOpenChange={props.onOpenChange} | ||||||||||
| defaultOpen={props.defaultOpen} | ||||||||||
| {...(isControlled ? { open: controlledOpen } : {})} | ||||||||||
| {...(defaultOpen !== undefined ? { defaultOpen } : {})} | ||||||||||
|
Comment on lines
+296
to
+297
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Passing both The component passes If a user provides both Fix: {...(isControlled ? { open: controlledOpen } : {})}
{...(!isControlled && defaultOpen !== undefined ? { defaultOpen } : {})}This ensures
Suggested change
Spotted by Graphite Agent |
||||||||||
| onOpenChange={handleOpenChange} | ||||||||||
| > | ||||||||||
| {trigger ? <Dialog.Trigger asChild>{trigger}</Dialog.Trigger> : null} | ||||||||||
| <Dialog.Portal> | ||||||||||
|
|
@@ -250,7 +305,7 @@ export function Modal(props: ModalProps) { | |||||||||
| "modal", | ||||||||||
| "modal__content", | ||||||||||
| ...componentClasses("modal", csVariant, csSize, undefined), | ||||||||||
| props.contentClasses | ||||||||||
| contentClasses | ||||||||||
| )} | ||||||||||
| aria-describedby={ariaDescribedby} | ||||||||||
| onOpenAutoFocus={(e) => { | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Simplify after fixing state management.
Once internal state management is removed (per previous comment), lines 225-227 become unnecessary:
const handleOpenChange = useCallback( (next: boolean) => { onOpenChange?.(next); - if (!isControlled) { - setUncontrolledOpen(next); - } if (!next) { if (typeof window !== "undefined") { window.requestAnimationFrame(restoreBodyPointerEvents); } else { restoreBodyPointerEvents(); } } }, - [onOpenChange, isControlled, restoreBodyPointerEvents] + [onOpenChange, restoreBodyPointerEvents] );📝 Committable suggestion
🤖 Prompt for AI Agents