# Modal

> Modal is an accessible dialog with focus trapping, scroll lock, and keyboard navigation. Renders its portal backdrop with role="dialog" and aria-modal="true".

Source: https://vaneui.com/docs/overlay-components/modal

Import: `import { Modal, Button, Checkbox, Input, Label, ModalBody, ModalCloseButton, ModalFooter, ModalHeader, Row, Stack, Text, ThemeProvider, Title, useOverlayRegistration } from "@vaneui/ui"`

Use Modal when the user has to finish or dismiss a task before returning to the page, such as a confirmation, a short form or a detail view. While it is open, the rest of the page gets `inert` and `aria-hidden`. A panel that leaves the page usable belongs in a [Popup](/docs/overlay-components/popup), and a message that needs no response in an [Alert](/docs/basic-components/alert).

## Basic usage

A controlled modal opened by a Button. The Modal portals to `document.body`, traps focus inside the dialog while open, locks body scroll, and closes on overlay click or `Escape`. This one has no header, so `aria-label` gives the dialog its accessible name.

Modal content defaults: `md`, `primary`, `outline`, `rounded`, `shadow`, `flex column`, `gap`, `wFull`, `overflowAuto`, `relative`, `noPadding` (sub-components own their own padding).

```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button onClick={() => setOpen(true)}>Open Modal</Button>
    <Modal open={open} onClose={() => setOpen(false)} aria-label="Confirm action">
      <Stack>
        <Text fontBold>Confirm Action</Text>
        <Text>Are you sure?</Text>
        <Row justifyEnd>
          <Button onClick={() => setOpen(false)}>Cancel</Button>
          <Button primary filled onClick={() => setOpen(false)}>Confirm</Button>
        </Row>
      </Stack>
    </Modal>
  </>
);
```

## Compound Modal

Use `ModalHeader`, `ModalBody`, `ModalFooter`, and `ModalCloseButton` for full control over layout. When a `ModalHeader`, `ModalBody`, or `ModalFooter` is a direct child, Modal renders the children as-is without auto-wrapping. Each sub-component carries its own layout defaults:

- `ModalHeader`: `flex row`, `itemsCenter`, `justifyBetween`, `gap`, `padding`
- `ModalBody`: `flex column`, `gap`, `padding`, `overflowAuto`
- `ModalFooter`: `flex row`, `itemsCenter`, `justifyEnd`, `gap`, `padding`
- `ModalCloseButton`: `sm`, `secondary`, `outline`, `transparent`, `noShadow`, `noInsetRing`

The parts meet one inset apart: `ModalBody` keeps its padding, `ModalHeader` drops its bottom padding, `ModalFooter` drops its top padding, and the Modal puts no gap between them. A header with `borderB`, a footer with `borderT` and a `filled` part keep their padding on that side.

```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button onClick={() => setOpen(true)}>Edit Profile</Button>
    <Modal open={open} onClose={() => setOpen(false)}>
      <ModalHeader>
        <Title>Edit Profile</Title>
        <ModalCloseButton />
      </ModalHeader>
      <ModalBody>
        <Stack>
          <Label>Name</Label>
          <Input placeholder="Enter your name" />
          <Label>Email</Label>
          <Input placeholder="Enter your email" />
        </Stack>
      </ModalBody>
      <ModalFooter>
        <Button sm onClick={() => setOpen(false)}>Cancel</Button>
        <Button sm primary filled onClick={() => setOpen(false)}>Save</Button>
      </ModalFooter>
    </Modal>
  </>
);
```

## Customizing the close button

`ModalCloseButton` takes the props of [Button](/docs/basic-components/button) except `href`, `tag` and `loading`. A prop you pass wins over its defaults, so `<ModalCloseButton lg />` or `<ModalCloseButton danger />` changes the × button. With children it renders a labelled button with Button's own look, such as a Cancel button in the footer. This needs `@vaneui/ui` 1.5.0 or later.

```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button onClick={() => setOpen(true)}>Rename project</Button>
    <Modal open={open} onClose={() => setOpen(false)}>
      <ModalHeader>
        <Title>Rename project</Title>
        <ModalCloseButton lg />
      </ModalHeader>
      <ModalBody>
        <Input placeholder="Project name" />
      </ModalBody>
      <ModalFooter>
        <ModalCloseButton secondary>Cancel</ModalCloseButton>
        <Button filled onClick={() => setOpen(false)}>Save</Button>
      </ModalFooter>
    </Modal>
  </>
);
```

A passed `onClick` runs before the modal closes, and `event.preventDefault()` in it keeps the modal open:

```tsx
<ModalCloseButton
  onClick={(event) => {
    if (hasUnsavedChanges) event.preventDefault();
  }}
/>
```

## Convenience props

Use the `title`, `footer`, and `withCloseButton` shorthand props to compose a structured modal without writing sub-components. A close button is shown by default regardless of `title` (toggle with `withCloseButton`): with a title it sits in the header, without one it floats top-right. A plain-text `title` renders as a `Title`. Children become the body.

```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button onClick={() => setOpen(true)}>Open Confirmation</Button>
    <Modal
      open={open}
      onClose={() => setOpen(false)}
      title="Quick Confirmation"
      footer={
        <Row justifyEnd>
          <Button sm onClick={() => setOpen(false)}>Cancel</Button>
          <Button sm primary filled onClick={() => setOpen(false)}>Confirm</Button>
        </Row>
      }
    >
      <Text>Are you sure you want to proceed?</Text>
    </Modal>
  </>
);
```

## Confirmation dialog

A common pattern is a destructive action that requires explicit confirmation. Disable `closeOnOverlayClick` so users can't dismiss accidentally by clicking outside.

```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button danger filled onClick={() => setOpen(true)}>Delete Account</Button>
    <Modal
      open={open}
      onClose={() => setOpen(false)}
      closeOnOverlayClick={false}
      title="Delete account?"
      footer={
        <Row justifyEnd>
          <Button sm onClick={() => setOpen(false)}>Cancel</Button>
          <Button sm danger filled onClick={() => setOpen(false)}>Delete</Button>
        </Row>
      }
    >
      <Text>This action is permanent and cannot be undone.</Text>
    </Modal>
  </>
);
```

## Form Modal

Modals can host forms. Focus trapping keeps `Tab` / `Shift+Tab` navigation inside the modal, and pressing `Escape` closes it (unless disabled). Use `initialFocus` to direct keyboard focus to a specific field on open.

```tsx demo
const [open, setOpen] = useState(false);
const nameRef = useRef(null);

return (
  <>
    <Button onClick={() => setOpen(true)}>Edit Profile</Button>
    <Modal
      open={open}
      onClose={() => setOpen(false)}
      initialFocus={nameRef}
      title="Edit Profile"
      footer={
        <Row justifyEnd>
          <Button sm onClick={() => setOpen(false)}>Cancel</Button>
          <Button sm primary filled onClick={() => setOpen(false)}>Save</Button>
        </Row>
      }
    >
      <Stack>
        <Label>Name</Label>
        <Input ref={nameRef} placeholder="Enter your name" />
        <Label>Email</Label>
        <Input type="email" placeholder="you@example.com" />
        <Label row><Checkbox />Send me email updates</Label>
      </Stack>
    </Modal>
  </>
);
```

## Shape, variant and appearance

These apply to the modal panel, not the backdrop. Modal is `rounded`, `outline` and `primary` by default.

```tsx demo
const [shape, setShape] = useState('');
const close = () => setShape('');

return (
  <>
    <Row flexWrap>
      <Button onClick={() => setShape('sharp')}>sharp</Button>
      <Button onClick={() => setShape('pill')}>pill</Button>
      <Button onClick={() => setShape('filled')}>filled secondary</Button>
      <Button onClick={() => setShape('danger')}>danger border</Button>
    </Row>
    <Modal sharp aria-label="Sharp modal" open={shape === 'sharp'} onClose={close}>
      <ModalBody><Text>Sharp corners</Text></ModalBody>
    </Modal>
    <Modal pill aria-label="Pill modal" open={shape === 'pill'} onClose={close}>
      <ModalBody><Text>Pill corners</Text></ModalBody>
    </Modal>
    <Modal
      filled
      secondary
      aria-label="Filled secondary modal"
      open={shape === 'filled'}
      onClose={close}
    >
      <ModalBody><Text>Filled secondary panel</Text></ModalBody>
    </Modal>
    <Modal
      danger
      border
      aria-label="Danger border modal"
      open={shape === 'danger'}
      onClose={close}
    >
      <ModalBody><Text>Danger border</Text></ModalBody>
    </Modal>
  </>
);
```

## Sizes

Size props control modal content width via `--modal-width`. Padding, gap, and border-radius scale with it. The text inside follows the [type sizes inside a Card](/docs/layout-components/card#type-sizes-inside-a-card): a `Title` in the `ModalHeader` is 18px and an unsized `Text` in the `ModalBody` 14px, at every Modal size.

```tsx demo
const [size, setSize] = useState('');
const close = () => setSize('');
const body = (
  <ModalBody>
    <Text>Width, padding, gap and border-radius all scale together.</Text>
  </ModalBody>
);

return (
  <>
    <Row flexWrap>
      <Button xs onClick={() => setSize('xs')}>xs</Button>
      <Button sm onClick={() => setSize('sm')}>sm</Button>
      <Button onClick={() => setSize('md')}>md</Button>
      <Button lg onClick={() => setSize('lg')}>lg</Button>
      <Button xl onClick={() => setSize('xl')}>xl</Button>
    </Row>
    <Modal xs open={size === 'xs'} onClose={close}>
      <ModalHeader><Title>Extra small</Title><ModalCloseButton/></ModalHeader>
      {body}
    </Modal>
    <Modal sm open={size === 'sm'} onClose={close}>
      <ModalHeader><Title>Small</Title><ModalCloseButton/></ModalHeader>
      {body}
    </Modal>
    <Modal open={size === 'md'} onClose={close}>
      <ModalHeader><Title>Medium, the default</Title><ModalCloseButton/></ModalHeader>
      {body}
    </Modal>
    <Modal lg open={size === 'lg'} onClose={close}>
      <ModalHeader><Title>Large</Title><ModalCloseButton/></ModalHeader>
      {body}
    </Modal>
    <Modal xl open={size === 'xl'} onClose={close}>
      <ModalHeader><Title>Extra large</Title><ModalCloseButton/></ModalHeader>
      {body}
    </Modal>
  </>
);
```

## Appearances

Apply appearance and variant props to style the content surface (border, text, background).

```tsx demo
const [openPrimary, setOpenPrimary] = useState(false);
const [openDanger, setOpenDanger] = useState(false);

return (
  <>
    <Row>
      <Button primary filled onClick={() => setOpenPrimary(true)}>Primary modal</Button>
      <Button danger filled onClick={() => setOpenDanger(true)}>Danger modal</Button>
    </Row>
    <Modal
      open={openPrimary}
      onClose={() => setOpenPrimary(false)}
      aria-label="Primary appearance"
      primary
      filled
    >
      Primary modal
    </Modal>
    <Modal
      open={openDanger}
      onClose={() => setOpenDanger(false)}
      aria-label="Danger appearance"
      danger
      filled
    >
      Danger modal
    </Modal>
  </>
);
```

## Blur overlay

Pass `overlayProps={{ backdropBlur: true }}` to add a backdrop-filter blur behind the modal.

```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button onClick={() => setOpen(true)}>Open Modal</Button>
    <Modal
      open={open}
      onClose={() => setOpen(false)}
      aria-label="Blur overlay example"
      overlayProps={{ backdropBlur: true }}
    >
      <Text>Blurred background</Text>
    </Modal>
  </>
);
```

## Non-dismissible Modal

Disable `closeOnOverlayClick` and `closeOnEscape` to force the user to take an explicit action (typically a button in the footer) before the modal can close.

```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button onClick={() => setOpen(true)}>Open Modal</Button>
    <Modal
      open={open}
      onClose={() => setOpen(false)}
      aria-label="Action required"
      closeOnOverlayClick={false}
      closeOnEscape={false}
    >
      <Text>Must click a button to close</Text>
      <Button onClick={() => setOpen(false)}>Close</Button>
    </Modal>
  </>
);
```

## Full-screen Modal

Set `fullScreen` to make the modal fill the entire viewport. Full-screen modals have no border-radius (`sharp` is applied automatically) and use a transparent overlay, useful for immersive experiences or mobile-optimized views.

```tsx demo
const [open, setOpen] = useState(false);

return (
  <>
    <Button onClick={() => setOpen(true)}>Open Full Screen</Button>
    <Modal open={open} onClose={() => setOpen(false)} fullScreen>
      <ModalHeader>
        <Title>Full Screen View</Title>
        <ModalCloseButton />
      </ModalHeader>
      <ModalBody>
        <Text>Content fills the entire viewport.</Text>
      </ModalBody>
    </Modal>
  </>
);
```

## Accessibility and advanced props

Modal ships accessibility features enabled by default. The dialog renders with `role="dialog"` and `aria-modal="true"`, and is automatically wired with `aria-labelledby` (when `ModalHeader` is used) and `aria-describedby` (when `ModalBody` is used).

A dialog needs an accessible name. A `ModalHeader` with title content provides it, and so does the `title` prop on a Modal without sub-components. A compound Modal does not render `title`, so put the title in its `ModalHeader`. A Modal with no visible title, like the bare examples on this page, takes `aria-label` or `aria-labelledby`. A development build logs a console warning when a Modal opens with none of these.

| Prop | Default | Description |
|------|---------|-------------|
| `scrollLock` | `true` | Lock body scroll when modal is open |
| `focusTrap` | `true` | Trap `Tab` / `Shift+Tab` focus inside the modal |
| `inertBackground` | `true` | Make the rest of the page inert while the modal is open |
| `returnFocus` | `true` | Return focus to the trigger element on close |
| `initialFocus` |   | Ref to the element that should receive focus on open |
| `portal` | `true` | Render via portal into `document.body` |
| `keepMounted` | `false` | Keep DOM node mounted when closed |
| `noAnimation` | `false` | Disable enter/exit transitions |
| `transitionDuration` | `200` | Animation duration in ms |

```tsx
{/* Custom focus target */}
<Modal open={open} onClose={onClose} aria-label="Search" initialFocus={inputRef}>
  <Input ref={inputRef} placeholder="Auto-focused" />
</Modal>

{/* Keep mounted for animation or state preservation */}
<Modal open={open} onClose={onClose} aria-label="Details" keepMounted transitionDuration={300}>
  <Text>Stays in DOM when closed</Text>
</Modal>
```

## Content outside the dialog that stays usable

An open Modal makes everything else under `<body>` inert. VaneUI's own overlays stay usable: a `Popup`, `Menu`, `Tooltip` or `Overlay` opened while the dialog is open, and a second `Modal` on top. An element VaneUI did not render, such as a toast region or a container another library portals to `<body>`, becomes inert with the rest of the page. Three things keep it usable:

- `data-vane-overlay` on a child of `<body>` spares that element.
- The ref from `useOverlayRegistration()` registers an element as an overlay while it is mounted, and the Modal spares the child of `<body>` that holds it.
- `inertBackground={false}` turns the inert pass off for one Modal.

```tsx
import { createPortal } from 'react-dom';
import { useOverlayRegistration } from '@vaneui/ui';

function Toasts({ message }: { message: string }) {
  const overlayRef = useOverlayRegistration();
  return createPortal(
    <div ref={overlayRef} role="status">{message}</div>,
    document.body,
  );
}
```

All three need `@vaneui/ui` 1.6.0 or later.

## Customizing

Set app-wide Modal defaults with `ThemeProvider`'s `themeDefaults`:

```tsx demo
const [open, setOpen] = useState(false);

return (
  <ThemeProvider themeDefaults={{
    modal: { content: { lg: true, border: true } },
  }}>
    <Button onClick={() => setOpen(true)}>Open Modal</Button>
    <Modal open={open} onClose={() => setOpen(false)} aria-label="Themed modal">
      Content
    </Modal>
  </ThemeProvider>
);
```

## Frequently asked questions

### Why does my Modal ignore the `data-theme` of a wrapper?

Modal renders through a portal at the end of `<body>`, outside the wrapper, so it resolves the theme set on `<html>` or `<body>` instead. Put `data-theme="dark"` on `<html>` for a dark page, as described under [Overlays follow the page theme](/docs/customization/dark-mode#overlays-follow-the-page-theme). The same holds for `Popup`, `Menu` and `Tooltip`, which render through a portal too.

### How do I make a Modal wider than `xl`?

Re-declare the width token of a size in your own stylesheet, for example `:root { --modal-width-xl: 48rem; }`, and use `<Modal xl>`. Every `xl` Modal then takes the new width. The size still caps the dialog at the viewport width minus 2rem, so it keeps a margin on a phone. For a dialog that fills the whole viewport, use `fullScreen` instead.

### Why does the Cancel button in my form Modal submit the form?

Inside a `<form>`, a Button without a `type` is a submit button, Cancel included. Give Cancel `type="button"` and the main action `type="submit"`; `ModalCloseButton` already renders `type="button"`. When the form sits in the `ModalBody` and the buttons in the `ModalFooter`, link the submit button to the form with its `form` attribute, as described in the [Forms FAQ](/docs/form-components/forms#how-do-i-submit-a-form-from-a-button-outside-it).

## Props

| Prop | Category | Default | Description |
| --- | --- | --- | --- |
| `primary` | Appearance | yes | Primary color appearance (gray) |
| `accent` | Appearance |  | Accent color appearance (rose) |
| `secondary` | Appearance |  | Secondary color appearance (gray) |
| `tertiary` | Appearance |  | Tertiary color appearance |
| `success` | Appearance |  | Success color appearance (green) |
| `danger` | Appearance |  | Danger color appearance (red) |
| `warning` | Appearance |  | Warning color appearance (amber) |
| `info` | Appearance |  | Info color appearance (cyan) |
| `inheritAppearance` | Appearance |  | Inherit appearance from parent — suppresses own data-appearance/data-variant, uses parent's CSS variables |
| `flex1` | Flex |  | Take up remaining space (= `flex-1`, i.e. `flex: 1 1 0%`) |
| `flexAuto` | Flex |  | Grow but respect intrinsic size (= `flex-auto`, i.e. `flex: 1 1 auto`) |
| `flexNone` | Flex |  | Don't grow and don't shrink (= `flex-none`, i.e. `flex: none`) |
| `fontSans` | Font Family |  | Sans-serif font family (default) — emits font-sans |
| `fontSerif` | Font Family |  | Serif font family — emits font-serif |
| `fontMono` | Font Family |  | Monospace font family — emits font-mono |
| `fontHeading` | Font Family |  | Heading font family (defaults to sans, customizable via --font-heading) — emits font-heading |
| `italic` | Font Style |  | Italic font style |
| `notItalic` | Font Style |  | Not italic (normal) font style |
| `fontThin` | Font Weight |  | Thin font weight (100) — emits font-thin |
| `fontExtralight` | Font Weight |  | Extra light font weight (200) — emits font-extralight |
| `fontLight` | Font Weight |  | Light font weight (300) — emits font-light |
| `fontNormal` | Font Weight |  | Normal font weight (400) — emits font-normal |
| `fontMedium` | Font Weight |  | Medium font weight (500) — emits font-medium |
| `fontSemibold` | Font Weight |  | Semibold font weight (600) — emits font-semibold |
| `fontBold` | Font Weight |  | Bold font weight (700) — emits font-bold |
| `fontExtrabold` | Font Weight |  | Extra bold font weight (800) — emits font-extrabold |
| `fontBlack` | Font Weight |  | Black font weight (900) — emits font-black |
| `padding` | Padding |  | Enable internal padding |
| `paddingX` | Padding |  | Enable only horizontal padding |
| `paddingY` | Padding |  | Enable only vertical padding |
| `noPadding` | Padding | yes | Disable internal padding |
| `pill` | Shape |  | Fully rounded corners (circular) |
| `sharp` | Shape |  | No rounded corners (square) |
| `rounded` | Shape | yes | Medium rounded corners (default) |
| `noShrink` | Shrink |  | Prevent the flex item from shrinking below its content size (= `shrink-0`) |
| `xs` | Size |  | Extra small size |
| `sm` | Size |  | Small size |
| `md` | Size | yes | Medium size (default) |
| `lg` | Size |  | Large size |
| `xl` | Size |  | Extra large size |
| `skeleton` | Skeleton |  | Show the component as a loading placeholder in its own size and shape. On a container, every component inside it becomes a placeholder too |
| `textLeft` | Text Align |  | Align text to left (physical side, does not flip under RTL) |
| `textCenter` | Text Align |  | Align text to center |
| `textRight` | Text Align |  | Align text to right (physical side, does not flip under RTL) |
| `textJustify` | Text Align |  | Justify text |
| `textStart` | Text Align |  | Align text to the reading-direction start (left in LTR, right in RTL) |
| `textEnd` | Text Align |  | Align text to the reading-direction end (right in LTR, left in RTL) |
| `underline` | Text Decoration |  | Add underline decoration below text |
| `lineThrough` | Text Decoration |  | Add strikethrough/line-through decoration across text |
| `noUnderline` | Text Decoration |  | Remove ALL text-decoration — underline, line-through, and overline (text-decoration: none) |
| `overline` | Text Decoration |  | Add overline decoration above text |
| `uppercase` | Text Transform |  | Transform text to uppercase |
| `lowercase` | Text Transform |  | Transform text to lowercase |
| `capitalize` | Text Transform |  | Capitalize first letter of each word |
| `normalCase` | Text Transform |  | Normal text case (no transformation) |
| `truncate` | Truncate |  | Single line truncation with ellipsis |
| `lineClamp2` | Truncate |  | Truncate at 2 lines with ellipsis |
| `lineClamp3` | Truncate |  | Truncate at 3 lines with ellipsis |
| `lineClamp4` | Truncate |  | Truncate at 4 lines with ellipsis |
| `lineClamp5` | Truncate |  | Truncate at 5 lines with ellipsis |
| `noTruncate` | Truncate |  | Remove truncation |
| `filled` | Variant |  | Filled variant - solid background with contrasting text color |
| `outline` | Variant | yes | Outline variant - transparent background with border and colored text (default) |
| `ghost` | Variant |  | Ghost variant - transparent background, no border, appearance-colored text, tinted hover background |

Layout and utility props (Border, Display, Flex Direction, Gap, Height, Hide, Items, Justify, Overflow, Position, Responsive Sizing, Responsive Width, Ring, Shadow, Show, Transparent, Width, Wrap) are listed on [Common Props](https://vaneui.com/docs/reference/common-props.md).
