> For the complete documentation index, see [llms.txt](https://docs.rierino.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rierino.com/sdk-core-ui/components/modal.md). Agents can also search and read this documentation through its [MCP server](https://docs.rierino.com/~gitbook/mcp).

# Modal

Shows a dialog above the page for a task or decision that needs full attention.

{% code title="modal/demo.tsx" %}

```tsx
import { Button, Modal } from "@rierino-open/core-ui";

export default function ModalDemo() {
  return (
    <Modal>
      <Button intent="g-button">Archive product</Button>
      <Modal.Content aria-label="Archive product">
        <Modal.Header className="px-6"
          title="Archive this product?"
          description="It will be hidden from the store until you restore it."
        />
        <Modal.Footer className="px-6">
          <Modal.Close>Cancel</Modal.Close>
          <Modal.Close intent="danger" appearance="solid">
            Archive
          </Modal.Close>
        </Modal.Footer>
      </Modal.Content>
    </Modal>
  );
}
```

{% endcode %}

## Usage

{% tabs %}
{% tab title="React app" %}

```tsx
import { Button, Modal } from '@rierino-open/core-ui';

<Modal>
  <Button intent="g-button">Archive product</Button>
  <Modal.Content aria-label="Archive product">
    <Modal.Header className="px-6" title="Archive this product?" />
    <Modal.Footer className="px-6">
      <Modal.Close>Cancel</Modal.Close>
    </Modal.Footer>
  </Modal.Content>
</Modal>
```

{% endtab %}

{% tab title="Hosted editor" %}

```jsx
import CoreUI from '@rierino-open/core-ui';
const { Button, Modal } = CoreUI;

<Modal>
  <Button intent="g-button">Archive product</Button>
  <Modal.Content aria-label="Archive product">
    <Modal.Header className="px-6" title="Archive this product?" />
    <Modal.Footer className="px-6">
      <Modal.Close>Cancel</Modal.Close>
    </Modal.Footer>
  </Modal.Content>
</Modal>
```

{% endtab %}
{% endtabs %}

`Modal` takes two children: the trigger, and `Modal.Content`. The trigger can be any Core UI `Button`; pressing it opens the modal.

To open and close the modal from your own state, use **`isOpen` and `onOpenChange`**, on `Modal` or directly on `Modal.Content`. Props named `open` or `onClose` are not supported.

{% hint style="info" %}
**Add horizontal padding to the parts**

`Modal.Header`, `Modal.Body` and `Modal.Footer` have vertical spacing but no horizontal padding, so their content would touch the dialog's edges. Add your own, for example `className="px-6"` on each part, as in these examples. `px-6` is included in Core UI's stylesheet, so it also works in hosted editors.
{% endhint %}

### Parts

| Part                | Purpose                                                                            |
| ------------------- | ---------------------------------------------------------------------------------- |
| `Modal`             | Connects the trigger to the dialog and holds the open state                        |
| `Modal.Content`     | The backdrop and the dialog box, with a × button                                   |
| `Modal.Header`      | The title and description. Pass `title` and `description`, or children             |
| `Modal.Title`       | A heading, for when you build the header yourself                                  |
| `Modal.Description` | A line of text under the title                                                     |
| `Modal.Body`        | The main content. It scrolls when it is too tall for the screen                    |
| `Modal.Footer`      | Buttons at the bottom                                                              |
| `Modal.Close`       | An outline button that closes the dialog. It takes `Button` props such as `intent` |

## Examples

### Saving before closing

Control the modal with `isOpen` and `onOpenChange`, and set `isOpen` to `false` once your action is done. `onOpenChange` is called with `false` when the modal is dismissed with `Esc`, the × button, `Modal.Close` or a click outside.

{% code title="modal/controlled.tsx" %}

```tsx
import { useState } from "react";
import { Button, Modal, TextField } from "@rierino-open/core-ui";

export default function ModalControlled() {
  const [open, setOpen] = useState(false);
  const [name, setName] = useState("Summer collection");
  const [saved, setSaved] = useState(name);

  const save = () => {
    setSaved(name);
    setOpen(false);
  };

  return (
    <div style={{ display: "grid", gap: 12, justifyItems: "center", fontSize: 12 }}>
      <Modal isOpen={open} onOpenChange={setOpen}>
        <Button appearance="outline">Rename collection</Button>
        <Modal.Content aria-label="Rename collection" size="sm">
          <Modal.Header className="px-6" title="Rename collection" />
          <Modal.Body className="px-6">
            <TextField aria-label="Name" value={name} onChange={(e) => setName(e.target.value)} autoFocus />
          </Modal.Body>
          <Modal.Footer className="px-6">
            <Modal.Close>Cancel</Modal.Close>
            <Button intent="g-button" onPress={save}>
              Save
            </Button>
          </Modal.Footer>
        </Modal.Content>
      </Modal>
      <span>Name: {saved}</span>
    </div>
  );
}
```

{% endcode %}

{% hint style="info" %}
**Modal.Close only closes**

`Modal.Close` runs its own `onPress` to close the dialog. Passing `onPress` replaces that, so the dialog stays open. For an action that should also close the dialog, use a normal `Button` and set `isOpen` to `false`, as above.
{% endhint %}

### Opening without a trigger

`Modal.Content` also works on its own: pass `isOpen` and `onOpenChange` to it directly and open it from anywhere, such as after a row is clicked or a request fails.

{% code title="modal/without-trigger.tsx" %}

```tsx
import { useState } from "react";
import { Button, Modal } from "@rierino-open/core-ui";

export default function ModalWithoutTrigger() {
  const [open, setOpen] = useState(false);

  return (
    <>
      {/* The button below isn't a Modal trigger: it just sets state. */}
      <Button appearance="outline" onPress={() => setOpen(true)}>
        Open from anywhere
      </Button>

      <Modal.Content aria-label="Opened from state" isOpen={open} onOpenChange={setOpen} size="sm">
        <Modal.Header className="px-6" title="Opened from state" description="No trigger element is needed." />
        <Modal.Footer className="px-6">
          <Button appearance="outline" onPress={() => setOpen(false)}>
            Close
          </Button>
        </Modal.Footer>
      </Modal.Content>
    </>
  );
}
```

{% endcode %}

When it closes, focus goes back to the element that had focus before it opened.

### Sizes

`size` sets the dialog's maximum width: `xs` (20rem), `sm`, `md`, `lg` (the default, 32rem), `xl`, `2xl`, `3xl`, `4xl` or `5xl` (64rem). The dialog is never wider than the screen.

{% code title="modal/sizes.tsx" %}

```tsx
import { Button, Modal } from "@rierino-open/core-ui";

const sizes = ["xs", "md", "2xl"] as const;

export default function ModalSizes() {
  return (
    <div style={{ display: "flex", gap: 12 }}>
      {sizes.map((size) => (
        <Modal key={size}>
          <Button appearance="outline">size="{size}"</Button>
          <Modal.Content aria-label={`Size ${size}`} size={size}>
            <Modal.Header className="px-6" title={`size="${size}"`} description="The dialog's maximum width." />
            <Modal.Footer className="px-6">
              <Modal.Close>Close</Modal.Close>
            </Modal.Footer>
          </Modal.Content>
        </Modal>
      ))}
    </div>
  );
}
```

{% endcode %}

### Alert dialogs

`role="alertdialog"` is for messages that need a decision, such as confirming a deletion. Clicking outside doesn't close it and there's no × button, so people have to choose one of your buttons.

{% code title="modal/alert.tsx" %}

```tsx
import { Button, Modal } from "@rierino-open/core-ui";

export default function ModalAlert() {
  return (
    <Modal>
      <Button intent="danger">Delete catalogue</Button>
      <Modal.Content role="alertdialog" aria-label="Delete catalogue" size="md">
        <Modal.Header className="px-6"
          title="Delete the whole catalogue?"
          description="All 1,284 products will be deleted. This can't be undone."
        />
        <Modal.Footer className="px-6">
          <Modal.Close>Keep catalogue</Modal.Close>
          <Modal.Close intent="danger" appearance="solid">
            Delete
          </Modal.Close>
        </Modal.Footer>
      </Modal.Content>
    </Modal>
  );
}
```

{% endcode %}

`Esc` still closes an alert dialog. Add `isKeyboardDismissDisabled` to require a button press.

### Long content

`Modal.Body` scrolls when the content is taller than the screen, while the header and footer stay in place.

{% code title="modal/scrolling.tsx" %}

```tsx
import { Button, Modal } from "@rierino-open/core-ui";

const changes = Array.from({ length: 30 }, (_, i) => `Version ${30 - i}: updated prices and stock`);

export default function ModalScrolling() {
  return (
    <Modal>
      <Button appearance="outline">View history</Button>
      <Modal.Content aria-label="History">
        <Modal.Header className="px-6" title="History" description="Every change to this product." />
        <Modal.Body className="px-6">
          {changes.map((change) => (
            <p key={change} style={{ fontSize: 13, margin: 0 }}>
              {change}
            </p>
          ))}
        </Modal.Body>
        <Modal.Footer className="px-6">
          <Modal.Close>Close</Modal.Close>
        </Modal.Footer>
      </Modal.Content>
    </Modal>
  );
}
```

{% endcode %}

### Backdrop and dismissing

* `isBlurred` blurs the page behind the modal.
* `isDismissable={false}` stops a click outside from closing it, and hides the × button.
* `closeButton={false}` hides the × button while keeping the other ways to close.

{% code title="modal/options.tsx" %}

```tsx
import { Button, Modal } from "@rierino-open/core-ui";

export default function ModalOptions() {
  return (
    <div style={{ display: "flex", gap: 12 }}>
      <Modal>
        <Button appearance="outline">Blurred backdrop</Button>
        <Modal.Content aria-label="Blurred backdrop" isBlurred size="sm">
          <Modal.Header className="px-6" title="Blurred backdrop" description="The page behind is blurred." />
          <Modal.Footer className="px-6">
            <Modal.Close>Close</Modal.Close>
          </Modal.Footer>
        </Modal.Content>
      </Modal>

      <Modal>
        <Button appearance="outline">Must use a button</Button>
        <Modal.Content aria-label="Not dismissable" isDismissable={false} closeButton={false} size="sm">
          <Modal.Header className="px-6"
            title="Not dismissable"
            description="Clicking outside doesn't close it, and there's no × button."
          />
          <Modal.Footer className="px-6">
            <Modal.Close>Close</Modal.Close>
          </Modal.Footer>
        </Modal.Content>
      </Modal>
    </div>
  );
}
```

{% endcode %}

## On small screens

Below the `sm` breakpoint (640px), the dialog is a full-width sheet at the bottom of the screen with rounded top corners. From `sm` up, it is centred.

## Accessibility

* **Name every modal** with `aria-label` on `Modal.Content`, or `aria-labelledby`. Without one, it is announced as "Dialog": the title in `Modal.Header` doesn't name it automatically.
* When the modal opens, focus moves into it and stays inside until it closes. When it closes, focus returns to the trigger.
* The page behind can't be scrolled or reached with the keyboard while the modal is open.
* The × button is labelled "Close".

## Styling

| Target               | How                      |
| -------------------- | ------------------------ |
| The backdrop         | `classNames.overlay`     |
| The dialog box       | `classNames.content`     |
| Header, body, footer | `className` on each part |

The footer stacks its buttons on small screens, last button on top, and lines them up in a row with space between from the `sm` breakpoint up. The backdrop sets `data-entering` and `data-exiting` while it animates.

## API reference

### Modal

| Prop           | Type                          | Default | Description                                                   |
| -------------- | ----------------------------- | ------- | ------------------------------------------------------------- |
| `isOpen`       | `boolean`                     |         | Whether the overlay is open by default (controlled).          |
| `defaultOpen`  | `boolean`                     |         | Whether the overlay is open by default (uncontrolled).        |
| `onOpenChange` | `((isOpen: boolean) => void)` |         | Handler that is called when the overlay's open state changes. |

### Modal.Content

| Prop                        | Type                                                                                      | Default    | Description                                                                                                                                                           |
| --------------------------- | ----------------------------------------------------------------------------------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `isOpen`                    | `boolean`                                                                                 |            | Whether the overlay is open by default (controlled).                                                                                                                  |
| `onOpenChange`              | `((isOpen: boolean) => void)`                                                             |            | Handler that is called when the overlay's open state changes.                                                                                                         |
| `isDismissable`             | `boolean`                                                                                 | `false`    | Whether to close the modal when the user interacts outside it.                                                                                                        |
| `isKeyboardDismissDisabled` | `boolean`                                                                                 | `false`    | Whether pressing the escape key to close the modal should be disabled.                                                                                                |
| `size`                      | `"2xl" \| "3xl" \| "4xl" \| "5xl" \| "lg" \| "md" \| "sm" \| "xl" \| "xs"`                | `"lg"`     | The dialog's maximum width from the `sm` breakpoint up, from `xs` (20rem) to `5xl` (64rem). Below `sm`, the dialog is a full-width sheet at the bottom of the screen. |
| `aria-label`                | `string`                                                                                  |            | Names the dialog for screen readers. Use it, or `aria-labelledby`, on every modal.                                                                                    |
| `aria-labelledby`           | `string`                                                                                  |            | The id of the element that names the dialog.                                                                                                                          |
| `role`                      | `"alertdialog" \| "dialog"`                                                               | `"dialog"` | `alertdialog` for a message that needs a decision: clicking outside doesn't close it and it has no × button.                                                          |
| `closeButton`               | `boolean`                                                                                 | `true`     | Shows a × button in the top right corner. Only shown while the modal is dismissable.                                                                                  |
| `isBlurred`                 | `boolean`                                                                                 | `false`    | Blurs the page behind the modal.                                                                                                                                      |
| `dialogProps`               | `DialogProps`                                                                             |            | Props passed to the inner React Aria `Dialog`.                                                                                                                        |
| `classNames`                | `{ overlay?: ModalOverlayProps["className"]; content?: ModalOverlayProps["className"]; }` |            | Classes for the backdrop (`overlay`) and the dialog box (`content`).                                                                                                  |

### Modal.Header

| Prop          | Type              | Default | Description |
| ------------- | ----------------- | ------- | ----------- |
| `children`    | `React.ReactNode` |         |             |
| `className`   | `string`          |         |             |
| `title`       | `string`          |         |             |
| `description` | `string`          |         |             |

These also accept the other props of React Aria's [Modal](https://react-spectrum.adobe.com/react-aria/Modal.html#props) and [DialogTrigger](https://react-spectrum.adobe.com/react-aria/Dialog.html#dialogtrigger).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.rierino.com/sdk-core-ui/components/modal.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
