@dax-ui/modal
v0.8.0
Published
dax-ui Modal: dialogs with a gradient hero or plain header, any body, an aside column, skeleton loading and Save/Delete/custom footer actions
Maintainers
Readme
@dax-ui/modal
A dialog for viewing or editing a record. Pick a dark gradient hero header (eyebrow title,
icon in a glassy tile, large heading, chips) or a white plain one. The body can hold anything
and may have a side column next to it. While loading, it shows skeletons. The footer has
ready-made Save (with an icon), Delete (with an optional confirmation step) and Cancel buttons,
plus as many other buttons as you need. Built on MUI's Dialog, so focus trapping, focus
return, Escape and scroll locking work out of the box.
Installation
pnpm add @dax-ui/modalMUI and Emotion are included; the app only needs React 18 or 19.
One-time setup per app (skip if the app already uses dax-ui): install
@dax-ui/theme and wrap the app in its UIProvider.
pnpm add @dax-ui/themeAny icon component works; the examples use lucide-react. The body
examples use @dax-ui/detail-field and
@dax-ui/chip.
Usage
Details (hero header)
import { Modal } from '@dax-ui/modal';
import { DetailField, DetailGrid, DetailSection } from '@dax-ui/detail-field';
import { CalendarPlus, MapPin } from 'lucide-react';
<Modal
open={open}
onClose={() => setOpen(false)}
title="Event Details"
heading="Quarterly site visit"
icon={<CalendarPlus />}
badges={['Event']}
onDelete={() => deleteEvent(id)} // a returned promise shows a spinner until it settles
>
<DetailSection title="General Information">
<DetailGrid>
<DetailField label="Site" value="PK-nineToFive" icon={<MapPin />} />
</DetailGrid>
</DetailSection>
</Modal>Side column, header action, more buttons
import { Modal, ModalHeaderButton } from '@dax-ui/modal';
import { CircleCheck, ClipboardList, ExternalLink } from 'lucide-react';
<Modal
open={open}
onClose={close}
size="xl"
title="Follow-Up Details"
heading={followUp.title}
icon={<ClipboardList />}
badges={['Follow Up', { label: 'Overdue', color: 'danger' }]}
headerActions={
<ModalHeaderButton icon={<ExternalLink />} href={`/follow-ups/${id}`}>
Go to follow-up
</ModalHeaderButton>
}
aside={<Comments />}
asideLabel="Comments"
actions={[
{ label: 'Mark as Done', variant: 'text', icon: <CircleCheck />, onClick: markDone },
{ label: 'Edit', onClick: edit },
]}
onDelete={remove}
confirmDelete="Delete this follow-up? This can't be undone."
>
…
</Modal>Form (plain header)
<Modal
open={open}
onClose={close}
variant="plain"
title="Edit event"
formId="edit-event" // Save submits this form; Enter in a field does too
saving={isSaving}
saveDisabled={!isValid}
onCancel={close}
>
<form id="edit-event" onSubmit={handleSubmit}>…</form>
</Modal>Loading
<Modal open title="Event Details" icon={<CalendarPlus />} loading={isLoading} onDelete={remove}>
{event && <EventBody event={event} />}
</Modal>While loading, skeletons replace the body (and a missing heading), the body is marked
aria-busy, "Loading…" is announced, and Save and Delete are disabled. loadingContent
replaces the skeleton layout, e.g. with SkeletonFields from @dax-ui/skeleton.
Footer buttons
The built-in buttons appear when their handler is set, in this order: actions with
align: 'start' on the left; then on the right the other actions, Cancel (onCancel),
Delete (onDelete, red) and Save (onSave or formId, dark teal with a floppy-disk
icon). Each takes a label (saveLabel, deleteLabel, cancelLabel), a loading flag (saving,
deleting) and a disabled flag (saveDisabled, deleteDisabled). If onSave, onDelete or
an action's onClick returns a promise, its button shows a spinner until the promise settles.
confirmDelete (true or the question to ask) turns the first Delete click into an inline
confirmation in the footer. Focus moves to its Cancel button, and Escape backs out without
closing the modal. footer replaces the whole footer; hideFooter removes it.
Customizing
- Colors:
colorsoverrides any ofDEFAULT_MODAL_COLORS(header gradient, icon tile, title, buttons, dividers, backdrop, and more). They're CSS variables (--dax-modal-<key>), so setting one on an ancestor works too. Defaults follow light and dark mode (DEFAULT_MODAL_DARK_COLORS); the hero header is dark in both modes. - Header:
variant,icon,badges(strings and chip props become@dax-ui/chipchips; any element works),description,headerActions,headerBackgroundImagewithheaderOverlayOpacity,headerDecorations,hideCloseButton,closeLabel, or replace it withheader. - Size:
size(sm440,md640,lg800,xl1040,fullscreen) ormaxWidth. BelowfullScreenBelow(smby default) the modal fills the screen. - Behavior:
disableBackdropClose,disableEscapeClose,keepMounted.onClosereceives the reason:'closeButton','escapeKeyDown'or'backdropClick'. - Anything else:
headerSx,bodySx,asideSx,footerSx,sxon the root,slotPropsfor MUI Dialog's parts (slotProps.paper.sxstyles the modal box), and any other MUIDialogprop.
Accessibility
The dialog is named by its title and heading and described by its description. Without
them (e.g. with a custom header), pass aria-label or aria-labelledby. The icon and
decorations are hidden from screen readers. A body or aside that scrolls can be focused and
scrolled with the keyboard. Set asideLabel to make the aside a named region.
Props
Modal
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| open (required) | boolean | | Whether the modal is shown. |
| actions | ModalAction[] | | More footer buttons, in order, before Cancel, Delete and Save, e.g. [{ label: 'Mark as Done', variant: 'text', icon: <CheckIcon /> }, { label: 'Edit', onClick: edit }]. |
| aria-describedby | string | | Id of the element describing the dialog. Defaults to the description. |
| aria-label | string | | Accessible name of the dialog, when it has no title or heading (e.g. with a custom header). |
| aria-labelledby | string | | Id of the element naming the dialog. Defaults to the title and heading. |
| aside | ReactNode | | A right-hand column beside the body, with a divider between (e.g. comments). Stacks under the body below 900px. |
| asideLabel | string | | Accessible name of the aside; makes it a named region. |
| asideSx | SxProps<Theme> | | Style overrides for the aside. |
| asideWidth | string \| number | 380 | Width of the aside, in px or any CSS length. |
| badges | ReactNode \| ModalBadge[] | | Chips under the heading (hero) or beside the title (plain). Strings and chip props are shown as @dax-ui/chip chips that suit the header; any other node is shown as is. |
| bodySx | SxProps<Theme> | | Style overrides for the scrolling body. |
| cancelLabel | ReactNode | Cancel | Text of the Cancel button (also used in the delete confirmation). |
| children | ReactNode | | The body: anything. Scrolls when long, while the header and footer stay in place. |
| className | string | | Class name on the root. |
| closeLabel | string | Close | Accessible name of the close button. |
| colors | Partial<ModalColors> | | Overrides any of the modal's colors, e.g. { headerBackground: 'linear-gradient(120deg, #312e81, #6d28d9)', primary: '#6d28d9' }. Unset keys keep the defaults (which follow light and dark mode, except the header). |
| confirmDelete | ReactNode | false | Asks before deleting: the first click turns the footer into a confirmation with Cancel and Confirm. true uses a default question; a string or node is the question, e.g. "Delete this event?". |
| confirmDeleteLabel | ReactNode | Confirm | Text of the button that confirms the delete. |
| deleteDisabled | boolean | | Disables Delete. |
| deleteIcon | ReactNode | | Icon of the Delete button, e.g. a trash can. None by default. |
| deleteLabel | ReactNode | Delete | Text of the Delete button. |
| deletePosition | "start" \| "end" | end | Which end of the footer Delete sits at. |
| deleting | boolean | | Shows a spinner on Delete and disables it. |
| description | ReactNode | | Muted text under the heading (hero) or title (plain). Read as the dialog's description. |
| disableBackdropClose | boolean | false | Ignores clicks on the backdrop. |
| disableEscapeClose | boolean | false | Ignores the Escape key. |
| dividers | boolean | true for hero, false for plain`` | Draws a line above the footer. |
| footer | ReactNode | | Replaces the whole footer. |
| footerSx | SxProps<Theme> | | Style overrides for the footer. |
| formId | string | | Id of a <form> in the body. Save becomes its submit button (type="submit" form={formId}), so Enter in a field and the Save button both submit it. Shows Save even without onSave. |
| fullScreenBelow | false \| Breakpoint | sm | Below this breakpoint the modal fills the screen. false never does. |
| header | ReactNode | | Replaces the whole header. null removes it (name the dialog with aria-label then). |
| headerActions | ReactNode | | Buttons at the top right, before the close button, e.g. <ModalHeaderButton>. |
| headerBackgroundImage | string | | Image URL behind the hero header. colors.headerBackground is laid over it at headerOverlayOpacity. |
| headerDecorations | boolean | true | Draws the soft translucent circles over the hero header. |
| headerOverlayOpacity | number | 0.85 | How strongly the header background covers headerBackgroundImage: 0 shows the image as is, 1 hides it. |
| headerSx | SxProps<Theme> | | Style overrides for the header. |
| heading | ReactNode | | With hero: the large heading, e.g. the record's name. |
| hideCloseButton | boolean | false | Hides the close button (Escape and the backdrop still close the modal). |
| hideFooter | boolean | false | Removes the footer. |
| icon | ReactNode | | Icon in the header: in a glassy tile (hero) or a tinted square (plain). |
| keepMounted | boolean | false | Keeps the modal's content in the DOM while closed. |
| loading | boolean | false | Shows skeletons in the body instead of children, marks it busy, and disables Save and Delete. |
| loadingContent | ReactNode | | Replaces the default skeleton layout shown while loading. |
| loadingLabel | string | Loading… | Announced to screen readers while loading. |
| maxWidth | string \| number | | Exact maximum width, in px or any CSS length; overrides size. |
| onCancel | ((event: MouseEvent<HTMLButtonElement, MouseEvent>) => void) | | Adds an outlined Cancel button to the footer. |
| onClose | ((event: Event \| SyntheticEvent<Element, Event>, reason: ModalCloseReason) => void) | | Called when the modal asks to close: the close button, Escape, or a click on the backdrop (unless disabled with disableEscapeClose / disableBackdropClose). Without it there's no close button. |
| onDelete | ((event: MouseEvent<HTMLButtonElement, MouseEvent>) => void \| Promise<unknown>) | | Adds a red Delete button to the footer. If it returns a promise, the button shows a spinner until it settles. |
| onSave | ((event: MouseEvent<HTMLButtonElement, MouseEvent>) => void \| Promise<unknown>) | | Adds a primary Save button (with an icon) to the footer. If it returns a promise, the button shows a spinner until it settles. |
| saveDisabled | boolean | | Disables Save, e.g. while the form is invalid. |
| saveIcon | ReactNode | | Icon of the Save button; null for none. Defaults to a floppy disk. |
| saveLabel | ReactNode | Save | Text of the Save button. |
| saving | boolean | | Shows a spinner on Save and disables it. |
| size | "sm" \| "md" \| "lg" \| "xl" \| "fullscreen" | md | Width preset. fullscreen fills the screen. |
| slotProps | { root?: SlotProps<ElementType<ModalProps, keyof IntrinsicElements>, DialogRootSlotPropsOverrides, DialogOwnerState>; backdrop?: SlotProps<...>; container?: SlotProps<...> \| undefined; transition?: SlotComponentProps<...> \| undefined; paper?: SlotProps<...> \| undefined; } \| undefined | | Props for MUI Dialog's parts: paper, backdrop, container, transition, root. |
| style | CSSProperties | | Inline styles on the root. |
| sx | SxProps<Theme> | | Style overrides for the root (the layer holding the backdrop and the modal). Use slotProps.paper.sx for the modal box. |
| title | ReactNode | | With hero: the small line at the top, e.g. "Event Details". With plain: the title, e.g. "Edit event". |
| variant | "hero" \| "plain" | hero | hero: dark gradient header with an icon tile, a large heading and badges (detail views). plain: white header with a dark teal title (forms). |
ModalHeaderButton
An outlined button for headerActions, e.g. "Go to follow-up". It's glass on the hero
header and teal on the plain one. Pass href to render it as a link.
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| aria-label | string | | Accessible name, needed when there's only an icon. |
| children | ReactNode | | Button text. |
| className | string | | Class name on the button. |
| disabled | boolean | false | Disables the button. |
| href | string | | Makes the button a link to this URL. |
| icon | ReactNode | | Icon before the text, e.g. an external-link icon. |
| onClick | ((event: MouseEvent<HTMLElement, MouseEvent>) => void) | | Called on click. |
| sx | SxProps<Theme> | | Style overrides for the button. |
| target | string | | With href: where to open the link, e.g. '_blank'. |
