Modal
Present content in a modal dialog.
Modal
Usage
import { Button } from "@bedform/ui/button";
import { Checkbox } from "@bedform/ui/checkbox";
import { Container } from "@bedform/ui/container";
import { Modal } from "@bedform/ui/modal";
import { Textfield } from "@bedform/ui/textfield";
import { useRef } from "react";
export function FormModal() {
const modal = useRef<HTMLDialogElement>(null);
return (
<>
<Button type="button" onClick={() => modal.current?.showModal()}>
Open modal
</Button>
<Modal
ref={modal}
aria-labelledby="form-modal-title"
aria-describedby="form-modal-description"
>
<form className="ore-modal-content" method="dialog">
<header className="ore-modal-header">
<button
className="ore-modal-header-button"
type="submit"
value="back"
aria-label="Back"
>
<svg aria-hidden="true" viewBox="0 0 8 8">…</svg>
</button>
<h2 className="ore-modal-title" id="form-modal-title">
Header
</h2>
<button
className="ore-modal-header-button"
type="submit"
value="close"
aria-label="Close"
>
<svg aria-hidden="true" viewBox="0 0 8 8">…</svg>
</button>
</header>
<Container className="modal-form-body ore-scrollbar" variant="dark">
<p className="ore-modal-description" id="form-modal-description">
Add the information for this form.
</p>
<Textfield label="Label" description="Description" />
</Container>
<Container className="modal-confirmation" variant="light">
<Checkbox>Checkbox example for confirmation</Checkbox>
</Container>
<Container className="modal-actions" variant="light">
<Button type="submit" value="confirm">
Confirm
</Button>
<Button type="submit" value="cancel" color="secondary">
Cancel
</Button>
</Container>
</form>
</Modal>
</>
);
}Modal renders a native dialog, accepts all of its attributes, and forwards its ref to it. The frame, header and sections have no components of their own; give them the public classes below.
Open and close
Call the native showModal() method through the ref to open a modal dialog. A form with method="dialog" closes it without submitting data; the activated submit button's value becomes dialog.returnValue.
The modal controller also opens a dialog when a clicked element's data-modal-target matches the dialog's id, and closes it from an element with data-modal-close="value". Use data-modal-close only for a close control that cannot be placed in the dialog's form.
modal.current?.showModal();
modal.current?.close("cancel");Events
Native close fires after the dialog closes, and native cancel fires when the user requests dismissal with Escape. Use onClose and onCancel, and read the result from returnValue; the modal controller writes "backdrop" or "escape" for those dismissal paths.
<Modal ref={modal} onClose={() => console.log(modal.current?.returnValue)}>
…
</Modal>Native API
The ref exposes the HTMLDialogElement API:
| Member | Purpose |
|---|---|
open | Reflects whether the dialog is open |
returnValue | Value supplied by close() or the dialog form |
showModal() | Opens in the top layer as a modal |
close(value?) | Closes with an optional return value |
Public classes
| Class | Purpose |
|---|---|
.ore-modal | Native dialog root |
.ore-modal-content | Dialog frame |
.ore-modal-header | Title bar |
.ore-modal-title | Accessible dialog title |
.ore-modal-description | Accessible supporting text |
.ore-modal-header-button | Header action |
.modal-form-body | Scrollable dark form section |
.modal-confirmation | Confirmation section |
.modal-actions | Dialog action section |
Accessibility
The native dialog provides top-layer rendering, focus containment, inert background content, Escape handling, and focus restoration. Supply aria-labelledby and, when useful, aria-describedby.