Modal
"use client";
import { useState } from "react";
import { Button, Modal, ModalFooter } from "@cherry-design-system/react";
export function DeleteProject({ onDelete }: { onDelete: () => void }) {
const [isOpen, setIsOpen] = useState(false);
return (
<>
<Button variant="outline" onClick={() => setIsOpen(true)}>Delete project</Button>
<Modal
open={isOpen}
onClose={() => setIsOpen(false)}
title="Delete project?"
description="This cannot be undone."
>
<ModalFooter>
<Button variant="outline" onClick={() => setIsOpen(false)}>Cancel</Button>
<Button variant="accent" onClick={onDelete}>Delete</Button>
</ModalFooter>
</Modal>
</>
);
}Modal is a dialog that blocks the page until the user responds, built on the native <dialog> element (showModal). The focus trap, top layer, inert page and Escape come from the platform. Compose the content with ModalBody and ModalFooter. For a panel that slides in from an edge use Sheet; for a non-blocking panel use Popover.
Examples
Body content and size
ModalBody is the scrolling content area. size sets the width: sm (400px), md (480px) or lg (640px).
"use client";
import { useState } from "react";
import { Button, Field, Input, Modal, ModalBody, ModalFooter, Stack } from "@cherry-design-system/react";
export function InviteMember() {
const [isOpen, setIsOpen] = useState(false);
return (
<>
<Button onClick={() => setIsOpen(true)}>Invite</Button>
<Modal open={isOpen} onClose={() => setIsOpen(false)} title="Invite a member" size="lg">
<ModalBody>
<Stack>
<Field label="Email">
<Input name="email" type="email" />
</Field>
</Stack>
</ModalBody>
<ModalFooter>
<Button variant="outline" onClick={() => setIsOpen(false)}>Cancel</Button>
<Button onClick={() => setIsOpen(false)}>Send invite</Button>
</ModalFooter>
</Modal>
</>
);
}Required decision
dismissible={false} stops Escape and backdrop clicks from closing the dialog, and hideClose removes the header close button. Make sure the footer offers a way out.
"use client";
import { Button, Modal, ModalBody, ModalFooter } from "@cherry-design-system/react";
export function Terms({ open, onAccept }: { open: boolean; onAccept: () => void }) {
return (
<Modal open={open} onClose={onAccept} title="Updated terms" dismissible={false} hideClose>
<ModalBody>Please review and accept the updated terms to continue.</ModalBody>
<ModalFooter>
<Button onClick={onAccept}>Accept</Button>
</ModalFooter>
</Modal>
);
}Properties
Modal
Controlled open state.
Called on Escape, a backdrop click or the close button. Set open to false here.
Heading; also the dialog's accessible name.
Line under the title; wired as the accessible description.
400, 480 or 640px wide. Defaults to "md".
Hide the close button in the header.
Accessible name of the close button. Defaults to "Close".
Escape and backdrop clicks close it. Defaults to true.
Modal renders a <dialog>, forwards its ref to it and passes other <dialog> attributes through. It is a Client Component.
ModalBody
ModalBody has no props of its own. It renders a scrolling <div>, forwards its ref and passes <div> attributes through.
ModalFooter
ModalFooter has no props of its own. It renders the action row as a <div> aligned to the end, forwards its ref and passes <div> attributes through.
Styling
| Hook | Values |
|---|---|
| Class | cherry-modal |
data-size | sm, md, lg |
| States | [open], ::backdrop |
| Parts | cherry-modal__header, cherry-modal__title, cherry-modal__description, cherry-modal__close, cherry-modal__body, cherry-modal__footer |
The dialog reads --cherry-bg-elevated, --cherry-radius-2xl and --cherry-elevation-xl; the backdrop uses --cherry-bg-overlay. The title uses the h5 text tokens.
Accessibility
Modal is a native modal <dialog>: the page behind is inert, focus moves in and stays in, Escape closes it and focus returns to the trigger. title names the dialog and description describes it; give it a title, or an aria-label when there is no visible heading.