Modal
An overlay opened in a <dialog>. Dialog and Drawer build on it; use it directly for content of your own.
Import
Copy code
import { Modal } from '@k8ordo/ui';Usage
Basic usage
Copy code
const [isOpen, setIsOpen] = useState(false);
<Button onClick={() => setIsOpen(true)}>
Open Modal
</Button>
<Modal
isOpen={isOpen}
onClose={() => setIsOpen(false)}
side="center"
>
<Dialog.Root>
<Dialog.Header
onClose={() => setIsOpen(false)}
title="Confirmation"
/>
<Dialog.Content>
<p>Are you sure you want to proceed?</p>
</Dialog.Content>
</Dialog.Root>
</Modal>Side
Copy code
<Button onClick={() => setCenterOpen(true)}>Center</Button>
<Button onClick={() => setBottomOpen(true)}>Bottom</Button>
<Button onClick={() => setRightOpen(true)}>Right</Button>
<Modal isOpen={centerOpen} onClose={() => setCenterOpen(false)} side="center">
<Dialog.Root>
<Dialog.Header onClose={() => setCenterOpen(false)} title="Center Modal" />
<Dialog.Content>Centered on screen</Dialog.Content>
</Dialog.Root>
</Modal>
<Modal isOpen={bottomOpen} onClose={() => setBottomOpen(false)} side="bottom">
<Dialog.Root>
<Dialog.Header onClose={() => setBottomOpen(false)} title="Bottom Modal" />
<Dialog.Content>Slides up from bottom</Dialog.Content>
</Dialog.Root>
</Modal>
<Modal isOpen={rightOpen} onClose={() => setRightOpen(false)} side="right">
<Dialog.Root>
<Dialog.Header onClose={() => setRightOpen(false)} title="Right Modal" />
<Dialog.Content>Slides in from right</Dialog.Content>
</Dialog.Root>
</Modal>Open by default
Copy code
<Modal defaultOpen side="center">
<Dialog.Root>
<Dialog.Header
onClose={() => {}}
title="Default Open Modal"
/>
<Dialog.Content>
<p>This modal is open by default.</p>
</Dialog.Content>
</Dialog.Root>
</Modal>Top layer and portals
Modal renders in a <dialog> in the browser’s top layer, so anything portaled to document.body ends up behind it. Modal provides its own <dialog> as the portal root through context. Read it with usePortalRoot and portal there, and floating UI stays on top inside the Modal. useToast inside a Modal already uses this, so only portals of your own need it.
Copy code
import { usePortalRoot } from '@k8ordo/ui';
import { createPortal } from 'react-dom';
function FloatingLayer({ children }) {
// Inside a Modal this is the top-layer <dialog>; outside it is undefined.
const portalRoot = usePortalRoot();
return createPortal(children, portalRoot?.current ?? document.body);
}Props
aria-describedby?- Type:
string - Default: -
aria-label?- Type:
string - Default: -
aria-labelledby?- Type:
string - Default: -
children?- Type:
ReactNode - Default: -
defaultOpen?- Type:
boolean - Default: -
isOpen?- Type:
boolean - Default: -
onClose?- Type:
() => void - Default: -
ref?- Type:
Ref<HTMLDialogElement> - Default: -
side?- Type:
ModalSide - Default:
'center'
| Prop | Type | Default |
|---|---|---|
aria-describedby? | string | - |
aria-label? | string | - |
aria-labelledby? | string | - |
children? | ReactNode | - |
defaultOpen? | boolean | - |
isOpen? | boolean | - |
onClose? | () => void | - |
ref? | Ref<HTMLDialogElement> | - |
side? | ModalSide | 'center' |