Layer
A modal layer over the page, built on the native dialog element. What goes inside is up to you.
Usage
Layer opens a native dialog as a modal: the browser handles the top layer, the focus trap and Escape. For a regular modal panel, put a Dialog or a Sheet inside.
tsx
const { close, isActive, open } = useGraftDialog<void>();
<Button onClick={() => open()}>
<Button.Label>open</Button.Label>
</Button>
<Layer open={isActive} onClose={close} aria-label="Preview">
<YourOwnPanel />
</Layer>Closing
onClose is called on Escape and on a click on the backdrop. With dismissible={false} both are ignored, for steps the user has to complete.
tsx
const { close, isActive, open } = useGraftDialog<void>();
<Button onClick={() => open()}>
<Button.Label>open</Button.Label>
</Button>
<Layer open={isActive} onClose={close} aria-label="Preview">
<YourOwnPanel />
</Layer>Backdrop
Style it with the backdrop: variant.
tsx
<Layer open={isActive} onClose={close} className="backdrop:bg-black/80 backdrop:backdrop-blur-none">
<YourOwnPanel />
</Layer>Placement
The content is centred. place-items-stretch lets it fill the screen.
tsx
<Layer open={isActive} onClose={close}>
<Dialog className="max-w-lg">…</Dialog>
</Layer>
<Layer open={isActive} onClose={close} className="place-items-stretch">
<FullScreenTakeover />
</Layer>Timing
The fade takes 450ms on a soft ease-out, and with reduced motion it is instant. To change it, set the duration on the layer, its content and its backdrop:
css
.margo-layer,
.margo-layer > *,
.margo-layer::backdrop {
transition-duration: 320ms;
}Browser support
Where showModal doesn't exist the layer can't open. isMargoLayerSupported tells you in advance.
tsx
import { isMargoLayerSupported } from "margo-ui";
if (!isMargoLayerSupported()) renderFallback();Reference
| prop | type | default | |
|---|---|---|---|
open? | boolean | false | shows it; the content stays mounted until it has faded out |
onClose? | () => void | — | called on Escape and on a click on the backdrop |
dismissible? | boolean | true | false ignores Escape and backdrop clicks |
className? | string | — | added after the component's own class: what you set wins |
…rest | props of dialog | — | aria-label, aria-labelledby and the rest of the dialog's props |