Bedform UIMessages

Modal

Present content in a modal dialog.

Modal

*
string
*
string
*
string

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:

MemberPurpose
openReflects whether the dialog is open
returnValueValue 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

ClassPurpose
.ore-modalNative dialog root
.ore-modal-contentDialog frame
.ore-modal-headerTitle bar
.ore-modal-titleAccessible dialog title
.ore-modal-descriptionAccessible supporting text
.ore-modal-header-buttonHeader action
.modal-form-bodyScrollable dark form section
.modal-confirmationConfirmation section
.modal-actionsDialog 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.

On this page