dialog要素のcommand属性とclosedby属性

JSを使わないダイアログ


概要

- `` 要素の開閉は `command` 属性を使用 - 開く `button` 要素に `command` 属性を指定 - ボタンの `commandfor` 属性に、操作したい `dialog` 要素の `id` 値を指定 - `command` 属性で指定できる値 - `command="show-modal"` : モーダルUIの展開 - `command="close"` : モーダルUIを閉じる - `command="toggle-popover"` : ポップオーバーUIの開閉 - `command="show-popover"` : ポップオーバーUIの展開 - `command="hide-popover"` : ポップオーバーUIを閉じる - `command="request-close"` : requestClose()の機能を提供(Chrome 139でリリース予定) - `` 要素の外側をクリックした際に閉じる `Light dismiss` 機能は、 `closedby` 属性を使用 - `closedby` 属性に指定できる値 - `closedby="any"` - ダイアログの外側をクリックするか ESC キーを押すと `dialog` 要素が閉じる - `popover="auto"` の動作に似たもの - `closedby="closerequest"` - `ESC` キーを押すと `dialog` 要素が閉じる - `closedby` 属性が指定されていない場合の挙動に似ている - `closedby="none"` - `ESC` キーを押しても `dialog` 要素は閉じない - 2025/06/04 現在、 `closedby` に対応しているブラウザはChromeとEdge、Firefoxはプレビュー版で対応、そしてSafariは未対応 - `command` 属性と `closedby` 属性の未対応ブラウザは、Polyfill を使用すれば対応可能 - `` 要素にはアクセシブルな名前を持つことが求められるため、 `` 要素の`aria-labelledby` 属性に、ダイアログ内 `h1` 要素の `id` 値を指定 - ダイアログ内は、[WHATWGの仕様書](https://html.spec.whatwg.org/multipage/interactive-elements.html#the-dialog-element) に従うと、 `h1` 要素を使って問題なし - モーダルが開いている際は背面が `inert` (不活性化) されるため、背面コンテンツの見出しはツリーに存在しないことになるため - 開閉時のアニメーション、背面のスクロール抑制、スクロールバーの有無によるガタツキの防止はCSSで指定

デモ

Test 1: closedby="any" (default behavior)

Can be closed with ESC key, backdrop click, and close button.

Status: Closed

Dialog with closedby="any"

This dialog can be closed by:

  • Pressing ESC key
  • Clicking the backdrop
  • Clicking the close button

Test 2: closedby="closerequest"

Can be closed with ESC key and close button only. Backdrop clicks are ignored.

Status: Closed

Dialog with closedby="closerequest"

This dialog can be closed by:

  • Pressing ESC key ✓
  • Clicking the backdrop ✗
  • Clicking the close button ✓

Test 3: closedby="none"

Can only be closed with the close button. ESC key and backdrop clicks are ignored.

Status: Closed

Dialog with closedby="none"

This dialog can be closed by:

  • Pressing ESC key ✗
  • Clicking the backdrop ✗
  • Clicking the close button ✓

Test 4: Dynamic Attribute Changes

You can change the closedBy attribute while the dialog is open.

Status: Closed

Dialog with Dynamic closedBy

Current setting: closedby="any"


コード

HTML

<button type="button" commandfor="my-dialog" command="show-modal">
  モーダルを開く
</button>

<dialog
  id="my-dialog"
  closedby="any"
  aria-labelledby="my-dialog-heading"
  autofocus
>
  <h1 id="my-dialog-heading">Heading</h1>
  <p>Content</p>
  <button type="button" commandfor="my-dialog" command="close">閉じる</button>
</dialog>

CSS

/** ダイアログ開閉時のアニメーション */
dialog {
  position: fixed; /* Safariで表示に不具合があるので明示 */
  inset: 0; /* Safariで表示に不具合があるので明示 */
  overscroll-behavior-block: contain;
  transition-duration: 300ms;
  transition-property: display, overlay, opacity;
  transition-timing-function: ease-out;
  transition-behavior: allow-discrete;

  &::backdrop {
    background-color: oklch(from black l c h / 50%);
    backdrop-filter: blur(4px);
    transition-duration: inherit;
    transition-property: opacity;
    transition-timing-function: inherit;
  }

  &:modal,
  &:modal::backdrop {
    @starting-style {
      opacity: 0;
    }
  }

  &:not(:modal),
  &:not(:modal)::backdrop {
    opacity: 0;
  }
}

参照