本文へ移動
日本語

仕組み

The panel is a vertical flex: header and footer stay put and only the body scrolls, with dividers that fade in by scroll position and a 16px fade at the clipped edge. On opening, the component measures the element that had focus (the button that opened it) and sets the panel's transform-origin there, so the dialog grows out of that button; without a visible opener it rises from below. Closing is controlled from outside. While open, focus is trapped, siblings are inert, page scroll is locked (unless contained) and Esc does not propagate. With busy set, nothing can close it. The panel unmounts after the exit animation. The CSS includes 40px pill buttons (ds-modal__btn: --primary ink, --secondary grey, --danger).

向いている場面

  • A small task on desktop that should not leave the page: edit a record, fill a few fields, read some detail.
  • Content the user must handle before continuing (modal): the page behind is temporarily inactive.
  • Content bigger than a popover but not worth a separate page.

向かない場面

  • Mobile (viewport under 768px): use a bottom sheet; the open and close props are the same.
  • Confirming a dangerous action such as delete: use a confirm dialog that focuses Cancel and handles submitting and failure.
  • Just saying an action succeeded: use a toast.
  • Dialogs inside dialogs: for steps, swap the content inside one dialog.
  • Long details to compare with the page, or large forms: use a drawer or a separate page.

使い方

import { useState } from 'react'
import { Modal } from './Modal.jsx'

export function EditPrice() {
  const [open, setOpen] = useState(false)
  const [saving, setSaving] = useState(false)

  const save = async () => {
    setSaving(true) // while saving, the close button, mask and Esc cannot close it
    await savePrice()
    setSaving(false)
    setOpen(false)
  }

  return (
    <>
      <button className="ds-modal__btn ds-modal__btn--primary" onClick={() => setOpen(true)}>Edit price</button>
      <Modal
        open={open}
        onClose={() => setOpen(false)}
        title="Edit price"
        size="md"
        busy={saving}
        footer={
          <>
            <button className="ds-modal__btn ds-modal__btn--secondary" disabled={saving} onClick={() => setOpen(false)}>
              Cancel
            </button>
            <button className="ds-modal__btn ds-modal__btn--primary" aria-busy={saving || undefined} onClick={save}>
              Save
            </button>
          </>
        }
      >
        <PriceForm />
      </Modal>
    </>
  )
}

プロパティ

プロパティ型既定値説明
openboolean—Whether the modal is open.
onClose() => void—Asks to close (close button, mask click, Esc); the caller sets open to false.
titleReactNode—Title, also the accessible name of the dialog (aria-labelledby). Pass aria-label yourself when there is no title.
childrenReactNode—Body. When it overflows, only the body area scrolls while title and footer stay.
footerReactNode—Footer, pill buttons right-aligned with Cancel on the left and Confirm on the right. Omit for no footer.
size'sm' | 'md' | 'lg''sm'Width tier: 400 (confirmations), 520 (forms), 720 (complex content); 16px of margin when the viewport is narrower.
closablebooleantrueShows the close button at the top right.
maskClosablebooleantrueWhether clicking the mask closes the modal.
closeOnEscbooleantrueWhether Esc closes the modal.
busybooleanfalseSubmitting: the close button, mask and Esc do not close it, and the panel gets aria-busy.
containedbooleanfalseLimits the modal to the nearest positioned ancestor (absolute). By default it is portaled to body over the whole viewport.
initialFocusRefObject<HTMLElement | null>—Element to focus first after opening, such as the first form field. Defaults to the dialog itself.
labelsobject—Replaces built-in text (close). Shallow-merged over the English defaults.
...restHTMLAttributes<HTMLDivElement>—className and other attributes go to the panel (the role=dialog element), such as aria-describedby and style.

アクセシビリティ

  • The panel is role="dialog" with aria-modal="true" and its title linked by aria-labelledby; pass aria-describedby for extra description.
  • On open, focus moves into the dialog (or initialFocus) and Tab cycles inside; on close, focus returns to the opener.
  • Siblings are inert while open; outside contained mode, page scrolling is locked too.
  • Esc and mask clicks close it (except while busy); Esc does not propagate, and Esc during IME composition does not close it.
  • The close button is a real button with an accessible name, a 32px circle with a 44px hit area; footer pill buttons are 40px with a 44px hit area.
  • A scrollable body is keyboard-focusable so arrow keys scroll it; the focus ring is drawn inside.
  • With reduced motion the panel only fades; button spinners slow down but keep going.

モーション

Enter: the mask (dimmed, 6px blur) fades in over 240ms; the panel grows from the opener, scale 0.9 to 1 with a fade, 240ms ease-out (without an opener it rises 12px and scales from 0.96). Exit is faster: 160ms ease-in, shrinking back toward the opener, then unmount. Dividers fade in 160ms; pill buttons press to 0.97. Reduced motion: fades only.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

Modal.jsxmodal.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

オーバーレイ
「ボトムシート」は Pro に含まれます。解除するとプレビューが表示されます。