Modal
A small window, also called a dialog, that asks one question or confirms an action before people carry on.
import { Modal } from '@halo-ds/react';
When to use it
A modal opens on top of the page, over an Overlay, and waits for an answer. Use it for decisions that can't wait, like confirming a delete or saving changes before leaving. It can also hold a short form in its Content slot. For messages that don't need an answer, use an Alert.
Do
- Write the title as a question, like "Delete project?".
- Name buttons after what they do: "Delete", not "OK".
- Put the main action last, or on top when stacked.
- Say what will happen, especially when it can't be undone.
Don't
- Open a modal nobody asked for, like a newsletter sign-up.
- Open a modal on top of another modal.
- Put a long form in a modal; give it its own page or a Drawer.
- Hide the way out: always offer Cancel or ×.
Modal or Alert?
Both show a message. Pick by whether people must answer before they carry on.
- Modal: One question or confirmation, in a sentence or two. People must answer before they carry on. Two buttons, three at most.
- Alert: A message in the page that does not block anything. Stays until the problem is fixed or it is dismissed. If people do not have to decide now, use an Alert, not a modal.
Props
Modal props
| Prop | Type | What it does | ||
|---|---|---|---|---|
open (required) | boolean | |||
onClose (required) | () => void | Called on Escape, the close button, or a click on the overlay. | ||
title (required) | ReactNode | Figma: Title. | ||
description | ReactNode | Figma: Description. | ||
type | `"destructive" \ | "default" \ | undefined` | destructive uses a red icon. Pair it with a destructive Button. Figma: Type. |
icon | ReactNode | Replaces the icon in the circle, or false to hide it. Figma: Icon. | ||
layout | `"horizontal" \ | "stacked" \ | undefined` | horizontal puts the buttons in a row on the right; stacked makes them full width. Figma: Layout. |
showClose | `boolean \ | undefined` | Figma: Show close. | |
actions | ReactNode | Buttons. Put the main action last in a row, first when stacked. Figma: Actions. | ||
closeOnOverlayClick | `boolean \ | undefined` | Closes when the overlay is clicked. Turn off when closing would lose work. | |
overlay | `"dim" \ | "blur" \ | undefined` | Overlay style. |
children | ReactNode | Extra content between the text and the buttons, like a form. Figma: Content slot. | ||
className | `string \ | undefined` | ||
classNames | `Partial<Record<ModalPart, string>> \ | undefined` | Extra class names for the parts inside the component, by part name, like { header: 'my-header' }. | |
styles | `Partial<Record<ModalPart, CSSProperties>> \ | undefined` | Inline styles for the parts inside the component, by part name, like { footer: { gap: 4 } }. |
It also takes the usual HTML attributes for its element.
Examples
Overview
import { useState } from 'react';
import { Button, Modal } from '@halo-ds/react';
export default function Example() {
const [open, setOpen] = useState(false);
const close = () => setOpen(false);
return (
<>
<Button onClick={() => setOpen(true)}>Delete project</Button>
<Modal
open={open}
onClose={close}
title="Delete this project?"
description="Its files and tasks are deleted too. This can't be undone."
actions={
<>
<Button variant="secondary" onClick={close}>Cancel</Button>
<Button variant="destructive" onClick={close}>Delete</Button>
</>
}
/>
</>
);
}
Destructive
A red icon and button, with the buttons stacked full width.
import { useState } from 'react';
import { Button, Modal } from '@halo-ds/react';
export default function Demo() {
const [open, setOpen] = useState(false);
const close = () => setOpen(false);
return (
<>
<Button variant="destructive" onClick={() => setOpen(true)}>Delete project</Button>
<Modal
open={open}
onClose={close}
type="destructive"
layout="stacked"
title="Delete project?"
description="This removes all files for everyone. You can't undo it."
actions={
<>
<Button variant="destructive" onClick={close}>Delete</Button>
<Button variant="secondary" onClick={close}>Cancel</Button>
</>
}
/>
</>
);
}
With a form
A field in the content area. data-autofocus gives it focus first.
import { useState } from 'react';
import { Button, InputField, Modal } from '@halo-ds/react';
export default function Demo() {
const [open, setOpen] = useState(false);
const close = () => setOpen(false);
return (
<>
<Button variant="secondary" onClick={() => setOpen(true)}>Rename</Button>
<Modal
open={open}
onClose={close}
title="Rename file"
actions={
<>
<Button variant="secondary" onClick={close}>Cancel</Button>
<Button onClick={close}>Rename</Button>
</>
}
>
<InputField label="Name" placeholder="Untitled report" defaultValue="Quarterly report" data-autofocus />
</Modal>
</>
);
}
More: https://halodesignsystem.dev/components/modal