(props: DialogContainerProps)
| 28 | * or when the trigger unmounts while the dialog is open. |
| 29 | */ |
| 30 | export function DialogContainer(props: DialogContainerProps): ReactNode { |
| 31 | let {children, onDismiss} = props; |
| 32 | |
| 33 | let childArray = React.Children.toArray(children); |
| 34 | if (childArray.length > 1) { |
| 35 | throw new Error('Only a single child can be passed to DialogContainer.'); |
| 36 | } |
| 37 | |
| 38 | let [lastChild, setLastChild] = useState<ReactElement | null>(null); |
| 39 | |
| 40 | // React.Children.toArray mutates the children, and we need them to be stable |
| 41 | // between renders so that the lastChild comparison works. |
| 42 | let child: ReactElement | undefined = undefined; |
| 43 | if (Array.isArray(children)) { |
| 44 | child = children.find(React.isValidElement); |
| 45 | } else if (React.isValidElement(children)) { |
| 46 | child = children; |
| 47 | } |
| 48 | |
| 49 | if (child && child !== lastChild) { |
| 50 | setLastChild(child); |
| 51 | } |
| 52 | |
| 53 | let onOpenChange = (isOpen: boolean) => { |
| 54 | if (!isOpen) { |
| 55 | onDismiss(); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | return ( |
| 60 | <ModalContext.Provider value={{isOpen: !!child, onOpenChange}}> |
| 61 | {lastChild} |
| 62 | </ModalContext.Provider> |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | export interface DialogContainerValue { |
| 67 | /** |
nothing calls this directly
no outgoing calls
no test coverage detected