[ Overlays ]

Modal

A dialog that appears above the page and blocks interaction with the content behind it. It traps focus, locks page scroll, closes on Escape or overlay click, and returns focus to the trigger when closed.

4 examplesDark modeAccessible

Import#

import { Modal } from 'reusify-ui';

When to use#

  • Confirming destructive or important actions ("Delete project?").
  • Short focused tasks that shouldn't send the user to a new page, like inviting a teammate.
  • For long forms or side content, a Drawer often works better. For simple messages, use an Alert or Toast.

Examples#

Confirmation dialog#

Modal is controlled: you own the open state. The useDisclosure hook keeps that to one line.

import { Modal, Button, useDisclosure } from 'reusify-ui';

function DeleteProject() {
  const { isOpen, open, close } = useDisclosure();

  return (
    <>
      <Button variant="danger" onClick={open}>Delete project</Button>
      <Modal
        open={isOpen}
        onClose={close}
        size="sm"
        title="Delete project?"
        description="This permanently deletes Project Alpha. This can't be undone."
        footer={
          <>
            <Button variant="secondary" onClick={close}>Cancel</Button>
            <Button variant="danger" onClick={deleteProject}>Delete</Button>
          </>
        }
      />
    </>
  );
}

Form inside a modal#

Put the submit button in the footer and connect it with form="…". Add data-autofocus to the field that should receive focus on open.

<Modal
  open={isOpen}
  onClose={close}
  title="Invite a teammate"
  footer={
    <>
      <Button variant="secondary" onClick={close}>Cancel</Button>
      <Button type="submit" form="invite-form">Send invite</Button>
    </>
  }
>
  <form id="invite-form" onSubmit={sendInvite}>
    <Input label="Email" type="email" required data-autofocus />
    <Select label="Role" options={roles} />
  </form>
</Modal>

Sizes#

<Modal size="sm" … />   // 24rem
<Modal size="md" … />   // 32rem (default)
<Modal size="lg" … />   // 48rem
<Modal size="xl" … />   // 64rem
<Modal size="full" … /> // full screen

Controlling close behaviour#

For critical flows you can turn off overlay-click and Escape closing, and hide the close button.

See the code: the user can only close this modal with the button.

<Modal
  open={isOpen}
  onClose={close}
  closeOnOverlayClick={false}
  closeOnEsc={false}
  showCloseButton={false}
  title="Accept the new terms"
  footer={<Button onClick={accept}>I agree</Button>}
/>

API reference#

<Modal />

open*-
boolean

Whether the modal is visible.

onClose*-
() => void

Called on Escape, overlay click or the close button.

title-
ReactNode

Dialog heading (also its accessible name).

description-
ReactNode

Muted text under the title (its accessible description).

children-
ReactNode

Body content.

footer-
ReactNode

Right-aligned action row.

size'md'
'sm' | 'md' | 'lg' | 'xl' | 'full'

Maximum width.

closeOnOverlayClicktrue
boolean

Close when the backdrop is clicked.

closeOnEsctrue
boolean

Close when Escape is pressed.

showCloseButtontrue
boolean

Show the × button in the header.

className-
string

Class for the dialog panel.

Accessibility#

  • role="dialog" with aria-modal="true", labelled by the title and described by the description.
  • Focus moves into the dialog on open (or to the [data-autofocus] element) and is trapped inside while open.
  • Focus returns to the element that opened it when it closes.
  • Rendered in a portal on document.body and safe to use with server rendering.