Modal
Modal dialog built on the native dialog element.
- Browsers
- Chrome 123+Firefox 121+Safari 17.5+
- Tier
- 0
- Bundle compressed
- 944 B
- Forced layout budget
- 0 ms
- Axe budget
- 0
- SSR compatible
- Yes
- Built on
- elements/dialog
- Published contract
- Test harness
Playground
Modal
Closed
Controlled
Changes: , state:
Examples
Sizes
size writes data-size: sm 24rem, md (default) 28rem, lg 40rem, full fills the viewport. On a phone every size except full is a bottom sheet.
import { Modal } from "@gunkin/uiify";
export default function Example() {
return (
<>
<Modal id="size-sm" size="sm" title="Small" trigger="Small">
<p>24rem wide.</p>
</Modal>
<Modal id="size-lg" size="lg" title="Large" trigger="Large">
<p>40rem wide.</p>
</Modal>
</>
);
}Long content
The title, content and footer share one scroll area. Compose the parts inside ModalBody for a document-style dialog.
import { ModalTrigger, ModalContent, ModalBody, ModalHeader, ModalTitle, ModalClose, ModalFooter } from "@gunkin/uiify";
export default function Example() {
return <>
<ModalTrigger target="terms">Read terms</ModalTrigger>
<ModalContent id="terms" aria-labelledby="terms-title">
<ModalBody>
<ModalHeader><ModalTitle id="terms-title">Terms of service</ModalTitle><ModalClose aria-label="Close" target="terms">×</ModalClose></ModalHeader>
{Array.from({ length: 30 }, (_, index) => <p key={index}>Clause {index + 1}: text that makes the document taller than the screen.</p>)}
<ModalFooter><ModalClose target="terms">Done</ModalClose></ModalFooter>
</ModalBody>
</ModalContent>
</>;
}Opening a modal from a modal
Render the child Modal inside the parent. It opens on top, Escape closes only the child, focus returns to the button that opened it, and the page is dimmed once.
import { Modal, ModalClose } from "@gunkin/uiify";
export default function Example() {
return (
<Modal
id="editor"
trigger="Edit profile"
title="Edit profile"
description="Unsaved changes are lost when you close."
>
<Modal
id="discard"
trigger="Discard changes"
title="Discard changes?"
description="This cannot be undone."
footer={<ModalClose target="discard">Keep editing</ModalClose>}
/>
</Modal>
);
}API
Modal
| Prop | Type | Default | Description |
|---|---|---|---|
id* | string | — | Unique dialog id that the trigger and close button target, so the dialog opens and closes before hydration. |
trigger* | ReactNode | — | Content of the button that opens the dialog. |
title* | ReactNode | — | Heading rendered inside the dialog and used as its accessible name. |
description | ReactNode | — | Supporting text rendered under the title and referenced by aria-describedby. |
children | ReactNode | — | Body content, rendered in the scrolling ModalBody. |
closeLabel | string | "Close" | Accessible name of the close icon in the header. |
footer | ReactNode | — | Actions row pinned under the body. Put a ModalClose here for a bottom Close button. |
size | "sm" | "md" | "lg" | "full" | "md" | Width step (24, 28, 40rem). full fills the viewport. Written to data-size; override --uiify-modal-inline-size for a custom width. |
align | "center" | "start" | "center" | Vertical position on tablet and desktop. Phones get a bottom sheet unless size is full. |
dismiss | "any" | "closerequest" | "none" | "any" | closedby: any adds backdrop taps, closerequest allows only Escape, none only your controls. Safari lacks closedby, so Escape always closes; UIEnhance handles backdrop taps (any) and sheet swipe (not none). |
backdrop | "opaque" | "blur" | "none" | "opaque" | Backdrop treatment. Written to data-backdrop. |
className | string | — | Class applied to the dialog element. |
ModalTrigger
| Prop | Type | Default | Description |
|---|---|---|---|
target* | string | — | Id of the ModalContent this button opens; the browser resolves it at click time, so no JavaScript of ours runs. |
children | ReactNode | — | Content of the trigger button. |
as | TAs & NativeButtonAs<TAs> | "button" | A component that forwards props and ref to a native button; other tags are rejected, because invoker commands need a real button. |
ModalContent
| Prop | Type | Default | Description |
|---|---|---|---|
id* | string | — | Unique id supplied by the consumer; referenced by the trigger and close target. |
size | "sm" | "md" | "lg" | "full" | "md" | Width step (24, 28, 40rem). full fills the viewport. Written to data-size; override --uiify-modal-inline-size for a custom width. |
align | "center" | "start" | "center" | Vertical position on tablet and desktop. Phones get a bottom sheet unless size is full. |
dismiss | "any" | "closerequest" | "none" | "any" | closedby: any adds backdrop taps, closerequest allows only Escape, none only your controls. Safari lacks closedby, so Escape always closes; UIEnhance handles backdrop taps (any) and sheet swipe (not none). |
backdrop | "opaque" | "blur" | "none" | "opaque" | Backdrop treatment. Written to data-backdrop. |
children | ReactNode | — | Content of the dialog. |
ModalClose
| Prop | Type | Default | Description |
|---|---|---|---|
target* | string | — | Id of the ModalContent this button closes. |
children | ReactNode | — | Content of the close button. |
as | TAs & NativeButtonAs<TAs> | "button" | A component that forwards props and ref to a native button; other tags are rejected, because invoker commands need a real button. |
ModalHeader
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | — | Pinned top row holding the title, description and close control. |
ModalTitle
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | — | The dialog's accessible name; reference its id from aria-labelledby. |
ModalDescription
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | — | Supporting text; reference its id from aria-describedby. |
ModalBody
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | — | The scrolling region between header and footer. |
ModalFooter
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | — | Actions row pinned under the body. |
ModalGrabber
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | — | Decorative drag handle on the mobile sheet; the swipe starts here. |
ModalClientContent (@gunkin/uiify/components/modal/client)
| Prop | Type | Default | Description |
|---|---|---|---|
id* | string | — | Unique id supplied by the consumer; referenced by the trigger and close. |
open | boolean | — | Controlled open state, authoritative after hydration. |
defaultOpen | boolean | — | Uncontrolled initial state, applied with showModal() after hydration; the server markup stays closed. |
onOpenChange | (open: boolean) => void | — | Fires once per native or programmatic change with the new state, including once at mount if the browser opened it before hydration. |
onOpenChangeComplete | (open: boolean) => void | — | Fires once per open or close transition after its CSS transitions finish, or immediately without any, including the defaultOpen transition after hydration; never for an unchanged dialog. |
size | "sm" | "md" | "lg" | "full" | "md" | Width step (24, 28, 40rem). full fills the viewport. Written to data-size; override --uiify-modal-inline-size for a custom width. |
align | "center" | "start" | "center" | Vertical position on tablet and desktop. Phones get a bottom sheet unless size is full. |
dismiss | "any" | "closerequest" | "none" | "any" | closedby: any adds backdrop taps, closerequest allows only Escape, none only your controls. Safari lacks closedby, so Escape always closes; UIEnhance handles backdrop taps (any) and sheet swipe (not none). |
backdrop | "opaque" | "blur" | "none" | "opaque" | Backdrop treatment. Written to data-backdrop. |
children | ReactNode | — | Content of the dialog. |
Keyboard
| Key | Action |
|---|---|
| Escape | Close (only the topmost modal when nested) |
| Tab | Cycle focus inside modal |
| Shift+Tab | Reverse tab order |
Accessibility
Native <dialog> opened by a command="show-modal" invoker before any JavaScript runs. The browser owns modality, focus containment, Escape and focus return; closing uses request-close so onCancel can veto. The header close icon is always present. On phones the modal is a bottom sheet; swiping it down and tapping the backdrop are shortcuts, never the only way out.
Styling hooks
data-uiify-modaldata-part="trigger|content|header|title|description|body|footer|grabber|close"data-size="sm|lg|full"data-align="start"data-backdrop="blur|none"--uiify-trigger-open (inside [data-uiify-modal][data-part="root"])--uiify-modal-inline-size (dialog width)