This component uses the native html dialog element for modal heavy lifting.
The ModalDialog
manages the display of the modal content and its transitions
within the DOM. The actual stying of the modal content is delegated to the consuming component.
open?: boolean
close: () => void
children: ReactNode
The open state is managed with the consuming component, useModalState
provides sugar to simpify this.
(initialState?: boolean) => [state:boolean, setOpen:()=>void, setClosed:()=>void]
Support for the dialog element is good. The transitions on open/close are currently not supported by Firefox but degrades gracefully.
import ModalDialog, { useModalState } from '../ModalDialog';
const Foo = () => {
const [modalOpen, setModalOpen, setModalClosed] = useModalState();
return (
<button onClick={setModalOpen}>Open</button>
<ModalDialog open={modalOpen} close={setModalClosed}>
<>
Some card-like content...
<button onClick={setModalClosed}>Close</button>
</>
</ModalDialog>
);
}