ナビゲーション

v2.1.0
オーバーレイ

ダイアログ

オーバーレイとして表示され、スクリプトなしで開閉できるネイティブのモーダルです。

このページの内容

ダイアログは表示階層の最上位にあるオーバーレイです。背後のページを暗くしてぼかす背景幕の上に表示されます。ネイティブの<dialog>に.dialogを指定します。commandforとcommand="show-modal"を指定したボタンで開くと、ブラウザーがダイアログをトップレイヤーに表示し、フォーカスをダイアログ内に保ち、Escで閉じる操作も処理するため、スクリプトは不要です。

ダイアログ

<button type="button" class="btn" commandfor="delete-project" command="show-modal">  Delete project</button><dialog id="delete-project" class="dialog" closedby="any" aria-labelledby="delete-project-title">  <header class="dialog-header">    <h2 class="dialog-title" id="delete-project-title">Delete this project?</h2>    <p class="dialog-description">      Atlas and its 14 deployments are removed, and its domains stop resolving at once.    </p>  </header>  <footer class="dialog-footer">    <button type="button" class="btn" commandfor="delete-project" command="close">Cancel</button>    <button type="button" class="btn btn-primary" commandfor="delete-project" command="close">      Delete project    </button>  </footer></dialog>
<>  <button type="button" className="btn" {...{ commandfor: 'delete-project', command: 'show-modal' }}>    Delete project  </button>  <dialog id="delete-project" className="dialog" closedby="any" aria-labelledby="delete-project-title">    <header className="dialog-header">      <h2 className="dialog-title" id="delete-project-title">Delete this project?</h2>      <p className="dialog-description">        Atlas and its 14 deployments are removed, and its domains stop resolving at once.      </p>    </header>    <footer className="dialog-footer">      <button type="button" className="btn" {...{ commandfor: 'delete-project', command: 'close' }}>        Cancel      </button>      <button type="button" className="btn btn-primary" {...{ commandfor: 'delete-project', command: 'close' }}>        Delete project      </button>    </footer>  </dialog></>
<button type="button" class="btn" commandfor="delete-project" command="show-modal">  Delete project</button><dialog id="delete-project" class="dialog" closedby="any" aria-labelledby="delete-project-title">  <header class="dialog-header">    <h2 class="dialog-title" id="delete-project-title">Delete this project?</h2>    <p class="dialog-description">      Atlas and its 14 deployments are removed, and its domains stop resolving at once.    </p>  </header>  <footer class="dialog-footer">    <button type="button" class="btn" commandfor="delete-project" command="close">Cancel</button>    <button type="button" class="btn btn-primary" commandfor="delete-project" command="close">      Delete project    </button>  </footer></dialog>
<template>  <button type="button" class="btn" commandfor="delete-project" command="show-modal">    Delete project  </button>  <dialog id="delete-project" class="dialog" closedby="any" aria-labelledby="delete-project-title">    <header class="dialog-header">      <h2 class="dialog-title" id="delete-project-title">Delete this project?</h2>      <p class="dialog-description">        Atlas and its 14 deployments are removed, and its domains stop resolving at once.      </p>    </header>    <footer class="dialog-footer">      <button type="button" class="btn" commandfor="delete-project" command="close">Cancel</button>      <button type="button" class="btn btn-primary" commandfor="delete-project" command="close">        Delete project      </button>    </footer>  </dialog></template>

#開閉

従来はスクリプトが必要だったモーダルの開閉を、3つの属性で設定できます。

  • ボタンにcommandfor="id"とcommand="show-modal"を指定すると、ダイアログがモーダルとして開きます。command="close"で閉じると、開いたボタンにフォーカスが戻ります。
  • ダイアログにclosedby="any"を指定すると、常に使えるEscに加えて、背景幕をクリックして閉じられます。誤クリックで作業内容を失ってはいけない場合は、この属性を省略します。属性に未対応のブラウザーでも、Escと閉じるボタンは使えます。
  • ダイアログ内の<form method="dialog">を送信するとダイアログが閉じ、どのボタンで送信したかをダイアログのreturnValueで取得できます。

モーダルダイアログが開いている間は、背後のページを操作したりスクロールしたりできません。

#フォーム付き

dialog-bodyはヘッダーとフッターの間にコンテンツを配置します。残りの高さを使い、コンテンツがビューポート内に収まりきらない場合は、ダイアログ内でスクロールします。dialog-closeは隅に控えめな正方形のボタンを配置します。このボタンにはアクセシブルな名前を付けます。

フォーム付き

<button type="button" class="btn btn-primary" commandfor="invite" command="show-modal">  Invite member</button><dialog id="invite" class="dialog" aria-labelledby="invite-title">  <header class="dialog-header">    <h2 class="dialog-title" id="invite-title">Invite a member</h2>    <p class="dialog-description">They receive an email with a link that expires in seven days.</p>  </header>  <form class="dialog-body flex flex-col gap-md" method="dialog" id="invite-form">    <label class="field">      Email <input class="input" type="email" placeholder="ada@example.com" required autofocus>    </label>    <label class="field">      Role <select class="select">        <option class="select-option">Member</option>        <option class="select-option">Admin</option>        <option class="select-option">Billing</option>      </select>    </label>  </form>  <footer class="dialog-footer">    <button type="button" class="btn" commandfor="invite" command="close">Cancel</button>    <button type="submit" class="btn btn-primary" form="invite-form">Send invite</button>  </footer>  <button type="button" class="btn btn-quiet btn-square btn-sm dialog-close" commandfor="invite" command="close" aria-label="Close">    <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">      <path d="M4 4L12 12" /><path d="M4 12L12 4" />    </svg>  </button></dialog>
import XIcon from '@master/icons-react/native/16/soft/x'<>  <button type="button" className="btn btn-primary" {...{ commandfor: 'invite', command: 'show-modal' }}>    Invite member  </button>  <dialog id="invite" className="dialog" aria-labelledby="invite-title">    <header className="dialog-header">      <h2 className="dialog-title" id="invite-title">Invite a member</h2>      <p className="dialog-description">        They receive an email with a link that expires in seven days.      </p>    </header>    <form className="dialog-body flex flex-col gap-md" method="dialog" id="invite-form">      <label className="field">        Email <input className="input" type="email" placeholder="ada@example.com" required autoFocus />      </label>      <label className="field">        Role <select className="select">          <option className="select-option">Member</option>          <option className="select-option">Admin</option>          <option className="select-option">Billing</option>        </select>      </label>    </form>    <footer className="dialog-footer">      <button type="button" className="btn" {...{ commandfor: 'invite', command: 'close' }}>        Cancel      </button>      <button type="submit" className="btn btn-primary" form="invite-form">Send invite</button>    </footer>    <button type="button" className="btn btn-quiet btn-square btn-sm dialog-close" {...{ commandfor: 'invite', command: 'close' }} aria-label="Close">      <XIcon className="icon" />    </button>  </dialog></>
<script>  import XIcon from '@master/icons-svelte/native/16/soft/x'</script><button type="button" class="btn btn-primary" commandfor="invite" command="show-modal">  Invite member</button><dialog id="invite" class="dialog" aria-labelledby="invite-title">  <header class="dialog-header">    <h2 class="dialog-title" id="invite-title">Invite a member</h2>    <p class="dialog-description">They receive an email with a link that expires in seven days.</p>  </header>  <form class="dialog-body flex flex-col gap-md" method="dialog" id="invite-form">    <label class="field">      Email <input class="input" type="email" placeholder="ada@example.com" required autofocus>    </label>    <label class="field">      Role <select class="select">        <option class="select-option">Member</option>        <option class="select-option">Admin</option>        <option class="select-option">Billing</option>      </select>    </label>  </form>  <footer class="dialog-footer">    <button type="button" class="btn" commandfor="invite" command="close">Cancel</button>    <button type="submit" class="btn btn-primary" form="invite-form">Send invite</button>  </footer>  <button type="button" class="btn btn-quiet btn-square btn-sm dialog-close" commandfor="invite" command="close" aria-label="Close">    <XIcon class="icon" />  </button></dialog>
<script setup>import XIcon from '@master/icons-vue/native/16/soft/x'</script><template>  <button type="button" class="btn btn-primary" commandfor="invite" command="show-modal">    Invite member  </button>  <dialog id="invite" class="dialog" aria-labelledby="invite-title">    <header class="dialog-header">      <h2 class="dialog-title" id="invite-title">Invite a member</h2>      <p class="dialog-description">        They receive an email with a link that expires in seven days.      </p>    </header>    <form class="dialog-body flex flex-col gap-md" method="dialog" id="invite-form">      <label class="field">        Email <input class="input" type="email" placeholder="ada@example.com" required autofocus>      </label>      <label class="field">        Role <select class="select">          <option class="select-option">Member</option>          <option class="select-option">Admin</option>          <option class="select-option">Billing</option>        </select>      </label>    </form>    <footer class="dialog-footer">      <button type="button" class="btn" commandfor="invite" command="close">Cancel</button>      <button type="submit" class="btn btn-primary" form="invite-form">Send invite</button>    </footer>    <button type="button" class="btn btn-quiet btn-square btn-sm dialog-close" commandfor="invite" command="close" aria-label="Close">      <XIcon class="icon" />    </button>  </dialog></template>

#サイズ

ダイアログの既定幅は28remです。1文で済む確認には幅22remのdialog-smを使えます。dialog-lgの幅は40remです。画面が狭い場合は、どのサイズでもビューポートの左右に1remずつ余白を確保します。フッターはコンテナークエリーでダイアログ自体の幅に対応し、22rem未満では、主操作を先頭にしてボタンを全幅で縦に並べます。

サイズ

<button type="button" class="btn" commandfor="size-sm" command="show-modal">Small</button><button type="button" class="btn" commandfor="size-lg" command="show-modal">Large</button><dialog id="size-sm" class="dialog dialog-sm" closedby="any" aria-labelledby="size-sm-title">  <header class="dialog-header">    <h2 class="dialog-title" id="size-sm-title">Sign out?</h2>    <p class="dialog-description">Unsaved drafts stay on this device.</p>  </header>  <footer class="dialog-footer">    <button type="button" class="btn" commandfor="size-sm" command="close">Stay</button>    <button type="button" class="btn btn-primary" commandfor="size-sm" command="close">      Sign out    </button>  </footer></dialog><dialog id="size-lg" class="dialog dialog-lg" closedby="any" aria-labelledby="size-lg-title">  <header class="dialog-header">    <h2 class="dialog-title" id="size-lg-title">Release notes</h2>    <p class="dialog-description">      Version 2.1 adds overlays that need no script: dialogs, popovers, menus and tooltips.    </p>  </header>  <footer class="dialog-footer">    <button type="button" class="btn btn-primary" commandfor="size-lg" command="close">Done</button>  </footer></dialog>
<>  <button type="button" className="btn" {...{ commandfor: 'size-sm', command: 'show-modal' }}>    Small  </button>  <button type="button" className="btn" {...{ commandfor: 'size-lg', command: 'show-modal' }}>    Large  </button>  <dialog id="size-sm" className="dialog dialog-sm" closedby="any" aria-labelledby="size-sm-title">    <header className="dialog-header">      <h2 className="dialog-title" id="size-sm-title">Sign out?</h2>      <p className="dialog-description">Unsaved drafts stay on this device.</p>    </header>    <footer className="dialog-footer">      <button type="button" className="btn" {...{ commandfor: 'size-sm', command: 'close' }}>        Stay      </button>      <button type="button" className="btn btn-primary" {...{ commandfor: 'size-sm', command: 'close' }}>        Sign out      </button>    </footer>  </dialog>  <dialog id="size-lg" className="dialog dialog-lg" closedby="any" aria-labelledby="size-lg-title">    <header className="dialog-header">      <h2 className="dialog-title" id="size-lg-title">Release notes</h2>      <p className="dialog-description">        Version 2.1 adds overlays that need no script: dialogs, popovers, menus and tooltips.      </p>    </header>    <footer className="dialog-footer">      <button type="button" className="btn btn-primary" {...{ commandfor: 'size-lg', command: 'close' }}>        Done      </button>    </footer>  </dialog></>
<button type="button" class="btn" commandfor="size-sm" command="show-modal">Small</button><button type="button" class="btn" commandfor="size-lg" command="show-modal">Large</button><dialog id="size-sm" class="dialog dialog-sm" closedby="any" aria-labelledby="size-sm-title">  <header class="dialog-header">    <h2 class="dialog-title" id="size-sm-title">Sign out?</h2>    <p class="dialog-description">Unsaved drafts stay on this device.</p>  </header>  <footer class="dialog-footer">    <button type="button" class="btn" commandfor="size-sm" command="close">Stay</button>    <button type="button" class="btn btn-primary" commandfor="size-sm" command="close">      Sign out    </button>  </footer></dialog><dialog id="size-lg" class="dialog dialog-lg" closedby="any" aria-labelledby="size-lg-title">  <header class="dialog-header">    <h2 class="dialog-title" id="size-lg-title">Release notes</h2>    <p class="dialog-description">      Version 2.1 adds overlays that need no script: dialogs, popovers, menus and tooltips.    </p>  </header>  <footer class="dialog-footer">    <button type="button" class="btn btn-primary" commandfor="size-lg" command="close">Done</button>  </footer></dialog>
<template>  <button type="button" class="btn" commandfor="size-sm" command="show-modal">Small</button>  <button type="button" class="btn" commandfor="size-lg" command="show-modal">Large</button>  <dialog id="size-sm" class="dialog dialog-sm" closedby="any" aria-labelledby="size-sm-title">    <header class="dialog-header">      <h2 class="dialog-title" id="size-sm-title">Sign out?</h2>      <p class="dialog-description">Unsaved drafts stay on this device.</p>    </header>    <footer class="dialog-footer">      <button type="button" class="btn" commandfor="size-sm" command="close">Stay</button>      <button type="button" class="btn btn-primary" commandfor="size-sm" command="close">        Sign out      </button>    </footer>  </dialog>  <dialog id="size-lg" class="dialog dialog-lg" closedby="any" aria-labelledby="size-lg-title">    <header class="dialog-header">      <h2 class="dialog-title" id="size-lg-title">Release notes</h2>      <p class="dialog-description">        Version 2.1 adds overlays that need no script: dialogs, popovers, menus and tooltips.      </p>    </header>    <footer class="dialog-footer">      <button type="button" class="btn btn-primary" commandfor="size-lg" command="close">        Done      </button>    </footer>  </dialog></template>

#ドロワー

dialog-drawerはインライン方向の終端に配置するパネルです。ページを表示したまま、フィルターや詳細、設定を開けます。角丸や影が切れないようにビューポートの端から8px内側に配置し、その端からスライドして現れます。

ドロワー

<button type="button" class="btn" commandfor="filters" command="show-modal">Filters</button><dialog id="filters" class="dialog dialog-drawer" closedby="any" aria-labelledby="filters-title">  <header class="dialog-header">    <h2 class="dialog-title" id="filters-title">Filters</h2>    <p class="dialog-description">Narrow the list of deployments.</p>  </header>  <div class="dialog-body flex flex-col gap-md">    <label class="field">Branch <input class="input" value="main"></label>    <label class="check"><input type="checkbox" class="checkbox" checked> Production only</label>  </div>  <footer class="dialog-footer">    <button type="button" class="btn" commandfor="filters" command="close">Reset</button>    <button type="button" class="btn btn-primary" commandfor="filters" command="close">      Apply    </button>  </footer></dialog>
<>  <button type="button" className="btn" {...{ commandfor: 'filters', command: 'show-modal' }}>    Filters  </button>  <dialog id="filters" className="dialog dialog-drawer" closedby="any" aria-labelledby="filters-title">    <header className="dialog-header">      <h2 className="dialog-title" id="filters-title">Filters</h2>      <p className="dialog-description">Narrow the list of deployments.</p>    </header>    <div className="dialog-body flex flex-col gap-md">      <label className="field">Branch <input className="input" defaultValue="main" /></label>      <label className="check">        <input type="checkbox" className="checkbox" defaultChecked /> Production only      </label>    </div>    <footer className="dialog-footer">      <button type="button" className="btn" {...{ commandfor: 'filters', command: 'close' }}>        Reset      </button>      <button type="button" className="btn btn-primary" {...{ commandfor: 'filters', command: 'close' }}>        Apply      </button>    </footer>  </dialog></>
<button type="button" class="btn" commandfor="filters" command="show-modal">Filters</button><dialog id="filters" class="dialog dialog-drawer" closedby="any" aria-labelledby="filters-title">  <header class="dialog-header">    <h2 class="dialog-title" id="filters-title">Filters</h2>    <p class="dialog-description">Narrow the list of deployments.</p>  </header>  <div class="dialog-body flex flex-col gap-md">    <label class="field">Branch <input class="input" value="main"></label>    <label class="check"><input type="checkbox" class="checkbox" checked> Production only</label>  </div>  <footer class="dialog-footer">    <button type="button" class="btn" commandfor="filters" command="close">Reset</button>    <button type="button" class="btn btn-primary" commandfor="filters" command="close">      Apply    </button>  </footer></dialog>
<template>  <button type="button" class="btn" commandfor="filters" command="show-modal">Filters</button>  <dialog id="filters" class="dialog dialog-drawer" closedby="any" aria-labelledby="filters-title">    <header class="dialog-header">      <h2 class="dialog-title" id="filters-title">Filters</h2>      <p class="dialog-description">Narrow the list of deployments.</p>    </header>    <div class="dialog-body flex flex-col gap-md">      <label class="field">Branch <input class="input" value="main"></label>      <label class="check"><input type="checkbox" class="checkbox" checked> Production only</label>    </div>    <footer class="dialog-footer">      <button type="button" class="btn" commandfor="filters" command="close">Reset</button>      <button type="button" class="btn btn-primary" commandfor="filters" command="close">        Apply      </button>    </footer>  </dialog></template>

#始端への配置

dialog-drawer-startを追加すると、幅18remのドロワーをインライン方向の始端に配置でき、ナビゲーションに使えます。幅の狭いレイアウトでサイドバーを格納した場合は、このドロワーで表示できます。アプリシェルを参照してください。

始端に配置したドロワー

<button type="button" class="btn" commandfor="nav-drawer" command="show-modal">  Open navigation</button><dialog id="nav-drawer" class="dialog dialog-drawer dialog-drawer-start" closedby="any" aria-label="Navigation">  <div class="dialog-body">    <nav class="nav" aria-label="Workspace">      <a class="nav-item" href="#" aria-current="page">Overview</a>      <a class="nav-item" href="#">Projects</a>      <a class="nav-item" href="#">Domains</a>      <a class="nav-item" href="#">Settings</a>    </nav>  </div></dialog>
<>  <button type="button" className="btn" {...{ commandfor: 'nav-drawer', command: 'show-modal' }}>    Open navigation  </button>  <dialog id="nav-drawer" className="dialog dialog-drawer dialog-drawer-start" closedby="any" aria-label="Navigation">    <div className="dialog-body">      <nav className="nav" aria-label="Workspace">        <a className="nav-item" href="#" aria-current="page">Overview</a>        <a className="nav-item" href="#">Projects</a>        <a className="nav-item" href="#">Domains</a>        <a className="nav-item" href="#">Settings</a>      </nav>    </div>  </dialog></>
<button type="button" class="btn" commandfor="nav-drawer" command="show-modal">  Open navigation</button><dialog id="nav-drawer" class="dialog dialog-drawer dialog-drawer-start" closedby="any" aria-label="Navigation">  <div class="dialog-body">    <nav class="nav" aria-label="Workspace">      <a class="nav-item" href="#" aria-current="page">Overview</a>      <a class="nav-item" href="#">Projects</a>      <a class="nav-item" href="#">Domains</a>      <a class="nav-item" href="#">Settings</a>    </nav>  </div></dialog>
<template>  <button type="button" class="btn" commandfor="nav-drawer" command="show-modal">    Open navigation  </button>  <dialog id="nav-drawer" class="dialog dialog-drawer dialog-drawer-start" closedby="any" aria-label="Navigation">    <div class="dialog-body">      <nav class="nav" aria-label="Workspace">        <a class="nav-item" href="#" aria-current="page">Overview</a>        <a class="nav-item" href="#">Projects</a>        <a class="nav-item" href="#">Domains</a>        <a class="nav-item" href="#">Settings</a>      </nav>    </div>  </dialog></template>

#シート

dialog-sheetは画面の下端からせり上がります。小さな画面で、親指が届きやすい位置に短い操作や選択肢の一覧を表示できます。--dialog-widthを上限として横幅いっぱいに広がり、画面の下端に接する左右の角は直角になります。env(safe-area-inset-bottom)でホームインジケーターとの間に余白を確保します。

シート

<button type="button" class="btn" commandfor="share-sheet" command="show-modal">Share</button><dialog id="share-sheet" class="dialog dialog-sheet" closedby="any" aria-labelledby="share-sheet-title">  <header class="dialog-header">    <h2 class="dialog-title" id="share-sheet-title">Share this project</h2>    <p class="dialog-description">Anyone with the link can view the latest deployment.</p>  </header>  <div class="dialog-body">    <div class="input-group">      <input class="input-group-field" value="https://atlas.dev/p/9f2c" readonly aria-label="Link">      <button type="button" class="btn">Copy</button>    </div>  </div>  <footer class="dialog-footer">    <button type="button" class="btn btn-primary" commandfor="share-sheet" command="close">      Done    </button>  </footer></dialog>
<>  <button type="button" className="btn" {...{ commandfor: 'share-sheet', command: 'show-modal' }}>    Share  </button>  <dialog id="share-sheet" className="dialog dialog-sheet" closedby="any" aria-labelledby="share-sheet-title">    <header className="dialog-header">      <h2 className="dialog-title" id="share-sheet-title">Share this project</h2>      <p className="dialog-description">Anyone with the link can view the latest deployment.</p>    </header>    <div className="dialog-body">      <div className="input-group">        <input className="input-group-field" defaultValue="https://atlas.dev/p/9f2c" readOnly aria-label="Link" />        <button type="button" className="btn">Copy</button>      </div>    </div>    <footer className="dialog-footer">      <button type="button" className="btn btn-primary" {...{ commandfor: 'share-sheet', command: 'close' }}>        Done      </button>    </footer>  </dialog></>
<button type="button" class="btn" commandfor="share-sheet" command="show-modal">Share</button><dialog id="share-sheet" class="dialog dialog-sheet" closedby="any" aria-labelledby="share-sheet-title">  <header class="dialog-header">    <h2 class="dialog-title" id="share-sheet-title">Share this project</h2>    <p class="dialog-description">Anyone with the link can view the latest deployment.</p>  </header>  <div class="dialog-body">    <div class="input-group">      <input class="input-group-field" value="https://atlas.dev/p/9f2c" readonly aria-label="Link">      <button type="button" class="btn">Copy</button>    </div>  </div>  <footer class="dialog-footer">    <button type="button" class="btn btn-primary" commandfor="share-sheet" command="close">      Done    </button>  </footer></dialog>
<template>  <button type="button" class="btn" commandfor="share-sheet" command="show-modal">Share</button>  <dialog id="share-sheet" class="dialog dialog-sheet" closedby="any" aria-labelledby="share-sheet-title">    <header class="dialog-header">      <h2 class="dialog-title" id="share-sheet-title">Share this project</h2>      <p class="dialog-description">Anyone with the link can view the latest deployment.</p>    </header>    <div class="dialog-body">      <div class="input-group">        <input class="input-group-field" value="https://atlas.dev/p/9f2c" readonly aria-label="Link">        <button type="button" class="btn">Copy</button>      </div>    </div>    <footer class="dialog-footer">      <button type="button" class="btn btn-primary" commandfor="share-sheet" command="close">        Done      </button>    </footer>  </dialog></template>

#スワイプ

ドロワーやシートにdialog-swipeを追加すると、指、トラックパッド、マウスの動きに追従し、手を離すと閉じるようになります。ダイアログはパネルが移動するトラックになり、透明でビューポートと同じ大きさになります。ダイアログの内容はすべて、質感をまとうdialog-panelに入れます。シートには、つかむ位置を示す上端の短い溝dialog-handleを付けます。drawerビヘイビアを適用してください。attach()はすべてのdialog-swipeを見つけます。

スワイプで閉じるシート

<button type="button" class="btn" commandfor="share-swipe" command="show-modal">Share</button><dialog id="share-swipe" class="dialog dialog-sheet dialog-swipe" closedby="any" aria-labelledby="share-swipe-title">  <div class="dialog-panel">    <div class="dialog-handle" aria-hidden="true"></div>    <header class="dialog-header">      <h2 class="dialog-title" id="share-swipe-title">Share this project</h2>      <p class="dialog-description">Anyone with the link can view the latest deployment.</p>    </header>    <div class="dialog-body">      <div class="input-group">        <input class="input-group-field" value="https://atlas.dev/p/9f2c" readonly aria-label="Link">        <button type="button" class="btn">Copy</button>      </div>    </div>    <footer class="dialog-footer">      <button type="button" class="btn btn-primary" commandfor="share-swipe" command="close">        Done      </button>    </footer>  </div></dialog><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => attach(root.current!), [])  return (    <div ref={root}>      <button type="button" className="btn" {...{ commandfor: 'share-swipe', command: 'show-modal' }}>        Share      </button>      <dialog id="share-swipe" className="dialog dialog-sheet dialog-swipe" closedby="any" aria-labelledby="share-swipe-title">        <div className="dialog-panel">          <div className="dialog-handle" aria-hidden="true"></div>          <header className="dialog-header">            <h2 className="dialog-title" id="share-swipe-title">Share this project</h2>            <p className="dialog-description">Anyone with the link can view the latest deployment.</p>          </header>          <div className="dialog-body">            <div className="input-group">              <input className="input-group-field" defaultValue="https://atlas.dev/p/9f2c" readOnly aria-label="Link" />              <button type="button" className="btn">Copy</button>            </div>          </div>          <footer className="dialog-footer">            <button type="button" className="btn btn-primary" {...{ commandfor: 'share-swipe', command: 'close' }}>              Done            </button>          </footer>        </div>      </dialog>    </div>  )}
<script>  import { attach } from '@master/ui/behaviors'</script><div {@attach attach}>  <button type="button" class="btn" commandfor="share-swipe" command="show-modal">Share</button>  <dialog id="share-swipe" class="dialog dialog-sheet dialog-swipe" closedby="any" aria-labelledby="share-swipe-title">    <div class="dialog-panel">      <div class="dialog-handle" aria-hidden="true"></div>      <header class="dialog-header">        <h2 class="dialog-title" id="share-swipe-title">Share this project</h2>        <p class="dialog-description">Anyone with the link can view the latest deployment.</p>      </header>      <div class="dialog-body">        <div class="input-group">          <input class="input-group-field" value="https://atlas.dev/p/9f2c" readonly aria-label="Link">          <button type="button" class="btn">Copy</button>        </div>      </div>      <footer class="dialog-footer">        <button type="button" class="btn btn-primary" commandfor="share-swipe" command="close">          Done        </button>      </footer>    </div>  </dialog></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <div ref="root">    <button type="button" class="btn" commandfor="share-swipe" command="show-modal">Share</button>    <dialog id="share-swipe" class="dialog dialog-sheet dialog-swipe" closedby="any" aria-labelledby="share-swipe-title">      <div class="dialog-panel">        <div class="dialog-handle" aria-hidden="true"></div>        <header class="dialog-header">          <h2 class="dialog-title" id="share-swipe-title">Share this project</h2>          <p class="dialog-description">Anyone with the link can view the latest deployment.</p>        </header>        <div class="dialog-body">          <div class="input-group">            <input class="input-group-field" value="https://atlas.dev/p/9f2c" readonly aria-label="Link">            <button type="button" class="btn">Copy</button>          </div>        </div>        <footer class="dialog-footer">          <button type="button" class="btn btn-primary" commandfor="share-swipe" command="close">            Done          </button>        </footer>      </div>    </dialog>  </div></template>
開いたら、溝かタイトルをつかんでシートを下にドラッグするか、はじいて閉じます。半分まで動かさずに離すと元の位置に戻ります。スマートフォンではスワイプします。

ドロワーも同じように、配置された端に向かってスワイプします。ハンドルは不要で、パネル全体をつかめます。

スワイプで閉じるドロワー

<button type="button" class="btn" commandfor="filters-swipe" command="show-modal">Filters</button><dialog id="filters-swipe" class="dialog dialog-drawer dialog-swipe" closedby="any" aria-labelledby="filters-swipe-title">  <div class="dialog-panel">    <header class="dialog-header">      <h2 class="dialog-title" id="filters-swipe-title">Filters</h2>      <p class="dialog-description">Narrow the list of deployments.</p>    </header>    <div class="dialog-body flex flex-col gap-md">      <label class="field">Branch <input class="input" value="main"></label>      <label class="check"><input type="checkbox" class="checkbox" checked> Production only</label>    </div>    <footer class="dialog-footer">      <button type="button" class="btn" commandfor="filters-swipe" command="close">Reset</button>      <button type="button" class="btn btn-primary" commandfor="filters-swipe" command="close">        Apply      </button>    </footer>  </div></dialog><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => attach(root.current!), [])  return (    <div ref={root}>      <button type="button" className="btn" {...{ commandfor: 'filters-swipe', command: 'show-modal' }}>        Filters      </button>      <dialog id="filters-swipe" className="dialog dialog-drawer dialog-swipe" closedby="any" aria-labelledby="filters-swipe-title">        <div className="dialog-panel">          <header className="dialog-header">            <h2 className="dialog-title" id="filters-swipe-title">Filters</h2>            <p className="dialog-description">Narrow the list of deployments.</p>          </header>          <div className="dialog-body flex flex-col gap-md">            <label className="field">Branch <input className="input" defaultValue="main" /></label>            <label className="check">              <input type="checkbox" className="checkbox" defaultChecked /> Production only            </label>          </div>          <footer className="dialog-footer">            <button type="button" className="btn" {...{ commandfor: 'filters-swipe', command: 'close' }}>              Reset            </button>            <button type="button" className="btn btn-primary" {...{ commandfor: 'filters-swipe', command: 'close' }}>              Apply            </button>          </footer>        </div>      </dialog>    </div>  )}
<script>  import { attach } from '@master/ui/behaviors'</script><div {@attach attach}>  <button type="button" class="btn" commandfor="filters-swipe" command="show-modal">Filters</button>  <dialog id="filters-swipe" class="dialog dialog-drawer dialog-swipe" closedby="any" aria-labelledby="filters-swipe-title">    <div class="dialog-panel">      <header class="dialog-header">        <h2 class="dialog-title" id="filters-swipe-title">Filters</h2>        <p class="dialog-description">Narrow the list of deployments.</p>      </header>      <div class="dialog-body flex flex-col gap-md">        <label class="field">Branch <input class="input" value="main"></label>        <label class="check">          <input type="checkbox" class="checkbox" checked> Production only        </label>      </div>      <footer class="dialog-footer">        <button type="button" class="btn" commandfor="filters-swipe" command="close">Reset</button>        <button type="button" class="btn btn-primary" commandfor="filters-swipe" command="close">          Apply        </button>      </footer>    </div>  </dialog></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <div ref="root">    <button type="button" class="btn" commandfor="filters-swipe" command="show-modal">      Filters    </button>    <dialog id="filters-swipe" class="dialog dialog-drawer dialog-swipe" closedby="any" aria-labelledby="filters-swipe-title">      <div class="dialog-panel">        <header class="dialog-header">          <h2 class="dialog-title" id="filters-swipe-title">Filters</h2>          <p class="dialog-description">Narrow the list of deployments.</p>        </header>        <div class="dialog-body flex flex-col gap-md">          <label class="field">Branch <input class="input" value="main"></label>          <label class="check">            <input type="checkbox" class="checkbox" checked> Production only          </label>        </div>        <footer class="dialog-footer">          <button type="button" class="btn" commandfor="filters-swipe" command="close">            Reset          </button>          <button type="button" class="btn btn-primary" commandfor="filters-swipe" command="close">            Apply          </button>        </footer>      </div>    </dialog>  </div></template>
パネルを端に向かってドラッグするか、横の背景幕を押します。

スワイプの仕組みは次のとおりです。

  • パネルはブラウザーが動かします。トラックはスナップポイントを持つスクロールコンテナーです。スナップポイントは、開いた状態で止まる位置と、手を離したパネルが端の外側で止まる位置です。端の外側には、ビューポートと同じ大きさのスクロール領域があります。指、トラックパッド、ホイールでスクロールするため、手の動きへの追従、はじいたときの慣性、止まる位置の選択はブラウザー自身が行います。トラックは逆向きのフレックスラインで、パネルはその末尾にあるため、スクリプトなしで開いた状態の位置から始まります。
  • スクロールでできないことはビヘイビアが行います。パネルがどのような方法でトラックから出た場合でも、ダイアログを閉じます。closedby="any"の動作も取り戻します。トラックはブラウザーが反応する背景幕を覆ってしまうため、パネルの横を押したときにビヘイビアがダイアログを閉じます。ドラッグでスクロールできないマウスでも、ハンドル、またはコントロールや本文以外の部分でパネルをつかめるようにし、はじいた方向の位置、または近いほうの位置で離せるようにします。精密なポインターがパネルをつかんでいる間、トラックはスナップしません。残っている背景幕の濃さを--dialog-scrimに書き込み、パネルが離れるにつれて背景幕を薄くします。
  • ダイアログはダイアログのままです。 Esc、閉じるボタン、フォーカストラップ、トリガーへのフォーカスの復帰、スライドイン・スライドアウトは、通常のドロワーやシートと同じです。
  • スクロールする本文は独自のスクロールを保ちます。dialog-body内では、マウスのドラッグやホイール操作は、本文のスクロールやテキストの選択に使われます。タッチスクリーンでは、本文の先頭まで達したドラッグはそのままシートを動かします。

スワイプは近道です。閉じるボタンかダイアログを閉じるアクションを残し、Escも使えるようにしてください。

#デテント

スワイプできるシートにdialog-detentsを追加すると、シートは2つの高さで止まります。開いたときのビューポートの半分の高さと、全体の高さです。ドラッグやはじく操作で2つの高さの間を移動します。全体の高さから下にはじくと半分の高さで止まり、半分の高さから下にはじくと閉じます。ハンドルはアクセシブルな名前を持つ<button>にします。押す、Enter、Spaceでシートをもう一方の高さに移し、↑で全体の高さに広げ、↓で半分の高さに縮めます。ハンドルのaria-expandedは、シートが全体の高さにあるかどうかを示します。

デテント付きのシート

<button type="button" class="btn" commandfor="comments-sheet" command="show-modal">Comments</button><dialog id="comments-sheet" class="dialog dialog-sheet dialog-swipe dialog-detents" closedby="any" aria-labelledby="comments-sheet-title">  <div class="dialog-panel">    <button type="button" class="dialog-handle" aria-label="Sheet height"></button>    <header class="dialog-header">      <h2 class="dialog-title" id="comments-sheet-title">Comments</h2>      <p class="dialog-description">Five people reviewed this deployment.</p>    </header>    <div class="dialog-body flex flex-col gap-md">      <p class="m(0)">Ada: the build on main is green again.</p>      <p class="m(0)">Grace: the preview for the pricing page loads in 1.2s.</p>      <p class="m(0)">Linus: the cache headers on the fonts are right now.</p>      <p class="m(0)">Margaret: the rollback plan is in the runbook.</p>      <p class="m(0)">Alan: ship it after the 3pm check.</p>    </div>    <button type="button" class="btn btn-quiet btn-square btn-sm dialog-close" commandfor="comments-sheet" command="close" aria-label="Close">      <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">        <path d="M4 4L12 12" /><path d="M4 12L12 4" />      </svg>    </button>  </div></dialog><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import XIcon from '@master/icons-react/native/16/soft/x'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => attach(root.current!), [])  return (    <div ref={root}>      <button type="button" className="btn" {...{ commandfor: 'comments-sheet', command: 'show-modal' }}>        Comments      </button>      <dialog id="comments-sheet" className="dialog dialog-sheet dialog-swipe dialog-detents" closedby="any" aria-labelledby="comments-sheet-title">        <div className="dialog-panel">          <button type="button" className="dialog-handle" aria-label="Sheet height"></button>          <header className="dialog-header">            <h2 className="dialog-title" id="comments-sheet-title">Comments</h2>            <p className="dialog-description">Five people reviewed this deployment.</p>          </header>          <div className="dialog-body flex flex-col gap-md">            <p className="m(0)">Ada: the build on main is green again.</p>            <p className="m(0)">Grace: the preview for the pricing page loads in 1.2s.</p>            <p className="m(0)">Linus: the cache headers on the fonts are right now.</p>            <p className="m(0)">Margaret: the rollback plan is in the runbook.</p>            <p className="m(0)">Alan: ship it after the 3pm check.</p>          </div>          <button type="button" className="btn btn-quiet btn-square btn-sm dialog-close" {...{ commandfor: 'comments-sheet', command: 'close' }} aria-label="Close">            <XIcon className="icon" />          </button>        </div>      </dialog>    </div>  )}
<script>  import { attach } from '@master/ui/behaviors'  import XIcon from '@master/icons-svelte/native/16/soft/x'</script><div {@attach attach}>  <button type="button" class="btn" commandfor="comments-sheet" command="show-modal">    Comments  </button>  <dialog id="comments-sheet" class="dialog dialog-sheet dialog-swipe dialog-detents" closedby="any" aria-labelledby="comments-sheet-title">    <div class="dialog-panel">      <button type="button" class="dialog-handle" aria-label="Sheet height"></button>      <header class="dialog-header">        <h2 class="dialog-title" id="comments-sheet-title">Comments</h2>        <p class="dialog-description">Five people reviewed this deployment.</p>      </header>      <div class="dialog-body flex flex-col gap-md">        <p class="m(0)">Ada: the build on main is green again.</p>        <p class="m(0)">Grace: the preview for the pricing page loads in 1.2s.</p>        <p class="m(0)">Linus: the cache headers on the fonts are right now.</p>        <p class="m(0)">Margaret: the rollback plan is in the runbook.</p>        <p class="m(0)">Alan: ship it after the 3pm check.</p>      </div>      <button type="button" class="btn btn-quiet btn-square btn-sm dialog-close" commandfor="comments-sheet" command="close" aria-label="Close">        <XIcon class="icon" />      </button>    </div>  </dialog></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import XIcon from '@master/icons-vue/native/16/soft/x'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <div ref="root">    <button type="button" class="btn" commandfor="comments-sheet" command="show-modal">      Comments    </button>    <dialog id="comments-sheet" class="dialog dialog-sheet dialog-swipe dialog-detents" closedby="any" aria-labelledby="comments-sheet-title">      <div class="dialog-panel">        <button type="button" class="dialog-handle" aria-label="Sheet height"></button>        <header class="dialog-header">          <h2 class="dialog-title" id="comments-sheet-title">Comments</h2>          <p class="dialog-description">Five people reviewed this deployment.</p>        </header>        <div class="dialog-body flex flex-col gap-md">          <p class="m(0)">Ada: the build on main is green again.</p>          <p class="m(0)">Grace: the preview for the pricing page loads in 1.2s.</p>          <p class="m(0)">Linus: the cache headers on the fonts are right now.</p>          <p class="m(0)">Margaret: the rollback plan is in the runbook.</p>          <p class="m(0)">Alan: ship it after the 3pm check.</p>        </div>        <button type="button" class="btn btn-quiet btn-square btn-sm dialog-close" commandfor="comments-sheet" command="close" aria-label="Close">          <XIcon class="icon" />        </button>      </div>    </dialog>  </div></template>
半分の高さで開きます。上にドラッグするか、ハンドルを押すか、Tab キーでハンドルに移動して ↑ と ↓ を押します。

半分の高さのとき、シートの下半分はフッターも含めて画面の端の外で待機します。そのためデテントは、リスト、コメント、地図など、アクションが上部にある閲覧用のシートに向いています。高さがビューポートの半分に満たないシートは、シート全体が見える位置でのみ止まり、ハンドルもその状態を示します。

#機能がない場合

  • スクリプトが無効な場合、端の外側の領域は停止位置になりません(@media (scripting: enabled))。手を離したパネルは元の位置に戻り、Escと閉じるボタンでダイアログを閉じられます。スクリプトが動作する環境では必ずビヘイビアを適用してください。適用しないと、手を離したパネルが画面外で止まり、ページは操作できないままになります。
  • ビヘイビアがない場合、背景幕はダイアログが閉じるまで薄くなりません。これはスクロール駆動アニメーションではありません。逆向きのスクロールコンテナーでは、ChromiumとWebKitでスクロールタイムラインとビュータイムラインの向きが逆になるためです。
  • scroll-initial-targetに対応したブラウザー(Chromium)では、デテント付きのシートはスクリプトの実行前から半分の高さで開きます。それ以外のブラウザーでは、ビヘイビアが動かすまで全体の高さで開きます。

スワイプは、ChromiumとWebKitでマウスとホイールを使って、またChromiumではタッチ操作でも検証しています。スクロールスナップ、逆向きのフレックスのスクロール、scrollendは、現行のすべてのブラウザーで使えます。

#アニメーション

ダイアログは、元の大きさの97%で0.5rem下にずれた状態から、280msかけてフェードインします。閉じるときは160msかけて消えます。開閉の両方をスタイルシートのCSSで定義します。@starting-styleで描画の初期状態を指定し、displayとoverlayをallow-discreteでトランジションさせることで、閉じるアニメーションが終わってからダイアログがトップレイヤーを離れます。動きを減らす設定では、フェードだけが残ります。

#アクセシビリティ

  • aria-labelledbyでダイアログのdialog-titleを参照し、ダイアログに名前を付けます。
  • show-modalで開くと、フォーカスはダイアログ内に移り、閉じると開いたボタンに戻ります。最初にフォーカスを当てるフィールドにautofocusを指定します。削除などの破壊的な操作を確認する場合は、安全な選択肢に指定します。
  • commandとcommandforは、現行のChrome、Edge、Firefox、Safariでサポートされています。古いブラウザーではクリックハンドラーからdialog.showModal()を呼び出します。スタイルシートはそのまま使えます。
  • スワイプだけを閉じる手段にしないでください。閉じるボタンか閉じるアクションを残します。Escでも引き続き閉じられます。デテント付きのシートでは、ハンドルは「シートの高さ」のような名前を持つボタンで、キー操作や押す操作でドラッグと同じように高さを変えます。デテントがない場合、ハンドルは支援技術から隠された溝です。操作領域は64×24pxで、タッチ画面では高さが44pxになります。

#React、Svelte、Vue

dialogコンポーネントは、アプリケーションの状態に応じてダイアログを開閉し、ブラウザーで実際に開閉されたことを通知します。開くための要素とダイアログ内の各部には、ページ側のマークアップをそのまま使います。すべてのダイアログにdrawerビヘイビアを適用するため、dialog-swipeとdialog-panelを持つドロワーやシートは、手を離すと閉じ、ほかの閉じ方と同じように通知されます。dialog-swipeのないダイアログでは、ビヘイビアは何もしません。npx master-ui add dialogでプロジェクトにコピーできます。構成と使い方はReact、Svelte、Vueを参照してください。

ui/dialog.tsx
'use client'import { useEffect, useId, useReducer, useRef, type ComponentProps, type ReactNode } from 'react'import { drawer } from '@master/ui/behaviors/drawer.js'export interface DialogProps extends Omit<ComponentProps<'dialog'>, 'open' | 'ref'> {  /** Whether the dialog is open, when this component is told. Leave it out and the trigger opens it. */  open?: boolean  onOpenChange?: (open: boolean) => void  /** The button that opens the dialog: spread what it is given on a button of your own. */  trigger?: (attributes: { commandfor: string, command: 'show-modal' }) => ReactNode}/** * A modal dialog. The browser opens it from the trigger, traps focus and closes it on Esc; this component * only opens and closes it from `open`, and says when the browser did. Its children are the contract's * parts: `dialog-header`, `dialog-body`, `dialog-footer`. A button inside closes it with * `commandfor={id}` and `command="close"`, or from a form with `method="dialog"`. * * A drawer or a sheet with `dialog-swipe` in its `className` holds a `dialog-panel` and is let go of to * close; the drawer behavior, attached to every dialog, closes it and says so here like any other close. */export function Dialog({ open, onOpenChange, trigger, className, id, children, ...dialog }: DialogProps) {  const generated = useId()  const name = id ?? generated  const element = useRef<HTMLDialogElement>(null)  const heard = useRef(onOpenChange)  /** What the owner knows: the last state it set or was told. */  const known = useRef(false)  const [tick, again] = useReducer((count: number) => count + 1, 0)  useEffect(() => { heard.current = onOpenChange })  useEffect(() => {    const root = element.current    if (!root || open === undefined) return    known.current = open    if (open && !root.open) root.showModal()    else if (!open && root.open) root.close()  }, [open, tick])  useEffect(() => {    const root = element.current    if (!root) return    const controller = new AbortController()    const report = () => {      if (root.open !== known.current) heard.current?.(known.current = root.open)      // An `open` this component is told wins: if its owner did not take the change, the dialog goes back.      again()    }    for (const type of ['toggle', 'close']) root.addEventListener(type, report, { signal: controller.signal })    const detach = drawer(root)    return () => {      controller.abort()      detach()    }  }, [])  return (    <>      {trigger?.({ commandfor: name, command: 'show-modal' })}      <dialog id={name} className={className ? `dialog ${className}` : 'dialog'} ref={element} {...{ closedby: 'any' }} {...dialog}>{children}</dialog>    </>  )}
ui/Dialog.svelte
<script lang="ts" module>  import type { Snippet } from 'svelte'  import type { HTMLDialogAttributes } from 'svelte/elements'  export interface DialogProps extends Omit<HTMLDialogAttributes, 'open'> {    /** Whether the dialog is open. Bind it, or leave it out and the trigger opens it. */    open?: boolean    onOpenChange?: (open: boolean) => void    /** The button that opens the dialog: spread what it is given on a button of your own. */    trigger?: Snippet<[{ commandfor: string, command: 'show-modal' }]>  }</script><script lang="ts">  import type { Attachment } from 'svelte/attachments'  import { drawer } from '@master/ui/behaviors/drawer.js'  let { open = $bindable(), onOpenChange, trigger, class: className, id, children, ...dialog }: DialogProps = $props()  const generated = $props.id()  const name = $derived(id ?? generated)  /** What the owner knows: the last state it set or was told. */  let known = false  const presence: Attachment<HTMLDialogElement> = (root) => {    const controller = new AbortController()    const report = () => {      if (root.open === known) return      open = known = root.open      onOpenChange?.(known)    }    for (const type of ['toggle', 'close']) root.addEventListener(type, report, { signal: controller.signal })    const detach = drawer(root)    $effect(() => {      if (open === undefined) return      known = open      if (open && !root.open) root.showModal()      else if (!open && root.open) root.close()    })    return () => {      controller.abort()      detach()    }  }</script><!--  @component  A modal dialog. The browser opens it from the trigger, traps focus and closes it on Esc; this component  only opens and closes it from `open`, and says when the browser did. Its children are the contract's  parts: `dialog-header`, `dialog-body`, `dialog-footer`. A button inside closes it with  `commandfor={id}` and `command="close"`, or from a form with `method="dialog"`.  A drawer or a sheet with `dialog-swipe` in its `class` holds a `dialog-panel` and is let go of to close;  the drawer behavior, attached to every dialog, closes it and says so here like any other close.-->{@render trigger?.({ commandfor: name, command: 'show-modal' })}<dialog id={name} class={['dialog', className]} closedby="any" {@attach presence} {...dialog}>{@render children?.()}</dialog>
ui/Dialog.vue
<script setup lang="ts">import { computed, nextTick, onBeforeUnmount, onMounted, useId, useTemplateRef, watch } from 'vue'import { drawer } from '@master/ui/behaviors/drawer.js'defineOptions({ inheritAttrs: false })const { id } = defineProps<{  id?: string}>()/** Whether the dialog is open: `v-model:open`. Leave it out and the trigger opens it. */const open = defineModel<boolean>('open')defineSlots<{  /** The button that opens the dialog: bind what it is given on a button of your own. */  trigger?: (attributes: { commandfor: string, command: 'show-modal' }) => unknown  default?: () => unknown}>()/* * A modal dialog. The browser opens it from the trigger, traps focus and closes it on Esc; this component * only opens and closes it from `open`, and says when the browser did. Its default slot is the contract's * parts: `dialog-header`, `dialog-body`, `dialog-footer`. A button inside closes it with * `commandfor` and `command="close"`, or from a form with `method="dialog"`. * * A drawer or a sheet with `dialog-swipe` in its `class` holds a `dialog-panel` and is let go of to close; * the drawer behavior, attached to every dialog, closes it and says so here like any other close. */const generated = useId()const name = computed(() => id ?? generated)const root = useTemplateRef<HTMLDialogElement>('root')/** What the owner knows: the last state it set or was told. */let known = falseconst sync = () => {  const dialog = root.value  if (!dialog || open.value === undefined) return  known = open.value  if (open.value && !dialog.open) dialog.showModal()  else if (!open.value && dialog.open) dialog.close()}const report = () => {  const dialog = root.value  if (!dialog) return  if (dialog.open !== known) open.value = known = dialog.open  // An `open` this component is told wins: if its owner did not take the change, the dialog goes back.  void nextTick(sync)}let detach: (() => void) | undefinedonMounted(() => {  if (root.value) detach = drawer(root.value)  sync()})onBeforeUnmount(() => detach?.())watch(open, sync, { flush: 'post' })</script><template>  <slot name="trigger" v-bind="{ commandfor: name, command: 'show-modal' as const }" />  <dialog :id="name" class="dialog" closedby="any" ref="root" v-bind="$attrs" @toggle="report" @close="report"><slot /></dialog></template>

#クラス

クラス説明
dialogaria-labelledbyまたはaria-labelで名前を付けた dialog 要素に適用するオーバーレイシート。幅は28remで、ビューポートの端から1remの余白を確保します。
dialog-sm幅は 22rem で、1 文の確認メッセージに適しています。既定は 28rem です。dialog と併用します
dialog-lg幅は 40rem です。既定は 28rem です。dialog と併用します
dialog-drawerページを表示したまま作業できる、inline-end 側の幅 24rem のパネルです。ビューポートの端から 8px 内側に浮かび、配置される側の端からスライドインします。dialog と併用します
dialog-drawer-startdialog-drawer と併用すると、ナビゲーション用の幅 18rem のパネルが inline-start 側の端に配置されます。dialog と併用します
dialog-sheet下端からせり上がるパネルです。下側の角は直角で、ホームインジケーターに重なりません。dialog と併用します
dialog-swipedialog-drawer または dialog-sheet と併用します。ダイアログはビューポートと同じ大きさの透明なトラックになり、その中の dialog-panel が指、トラックパッド、ホイール、マウスに追従し、離すと閉じます。drawer ビヘイビアを適用します。dialog と併用します
dialog-detentsdialog-sheet と dialog-swipe と併用します。シートは可能な限りの高さになり、ビューポートの半分が見える状態で開き、その位置か全高で止まります。dialog と併用します
dialog-paneldialog-swipe 内のドロワーまたはシート本体です。質感をまとい、ヘッダー、本文、フッター、閉じるボタンを収めます。
dialog-handleつまみ部分です。シート上端の短い溝で、dialog-panel の先頭に置きます。dialog-detents を使うシートでは、動作を名前にしたボタンにします。ビヘイビアがボタンに合わせてシートを動かし、aria-expanded を設定します。それ以外では aria-hidden="true" を付けた div にします。
dialog-headerシート上部にタイトルと説明を配置する領域
dialog-titleタイトルです。ダイアログのaria-labelledbyでこのタイトルを参照します。
dialog-descriptionタイトルの下で、その意味を補足する控えめな一文
dialog-bodyヘッダーとフッターの間のコンテンツ。残りの高さを使い、長い場合はシート内でスクロール
dialog-footer主要アクションを最後に並べます。ダイアログの幅が 22rem 未満では、各アクションを全幅で縦に並べ、主要アクションを先頭に置きます。
dialog-closeインライン方向の末尾側の上隅に控えめな正方形ボタンを配置。アクセシブルな名前が必要

#カスタムプロパティ

プロパティ既定値説明
--dialog-width28rem優先幅
--dialog-padding1.5rem各パーツの周囲のパディング
--dialog-radius1.25rem角の半径
--dialog-scrimdialog-swipe で、背景幕がどれだけ残っているかを示します。未設定時の 1(すべて)から、パネルが離れるにつれて 0 まで下がります。drawer ビヘイビアが書き込みます。

#ソース

@master/ui/components/dialog.css
/* * Dialog: a native <dialog>, opened as a modal by a button with commandfor and command="show-modal". * The browser supplies the top layer, the focus trap, Escape and, with closedby="any", light dismiss. * The stylesheet supplies the overlay sheet, the scrim, and the motion in and out through @starting-style * and discrete transitions. Geometry: a 20px corner, 24px of padding, 10px controls. * * Swipe: with dialog-swipe a drawer or a sheet follows a finger, a trackpad or a wheel, and is let go of to * close. The dialog becomes the track the panel moves on: as large as the viewport, transparent, scrolled * and snapped, holding the dialog-panel, which wears the material, and an empty stretch past the edge the * panel rests on. Scroll snapping makes the panel rest open, at half height in a sheet with dialog-detents, * or gone past the edge; following the hand, the momentum and the fling are the browser's own. The track is * a reversed flex line where the panel rests at the end, so it starts scrolled to the open rest with no * script. The drawer behavior closes the dialog once the panel has gone, gives closedby="any" back on the * track, lets a mouse drag the panel (a mouse cannot scroll by dragging; the track stops snapping while a * fine pointer holds the panel), writes how much scrim is left to --dialog-scrim and gives the handle its * keys. The scrim is not a scroll-driven animation: in a reversed scroller Chromium and WebKit run scroll * and view timelines in opposite directions. Fallbacks: without scripting the gone rest is left out, so a * panel let go of springs back and Escape and the close buttons close it; without the behavior the scrim * stays whole until the dialog closes; without scroll-initial-target (outside Chromium) a sheet with * detents opens at full height until the behavior puts it at half. */@layer components.ui {  .dialog {    --dialog-width: 28rem;    --dialog-padding: 1.5rem;    --dialog-radius: 1.25rem;    --surface-color: var(--color-surface-floating);    /* The parts respond to the dialog's own width, not the viewport's. */    container: dialog / inline-size;    flex-direction: column;    gap: 1.25rem;    width: min(var(--dialog-width), calc(100% - 2rem));    max-width: none;    max-height: min(calc(100dvh - 2rem), 44rem);    margin: auto;    padding: var(--dialog-padding) 0;    border: 0;    border-radius: calc(var(--dialog-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    overflow: hidden;    background: var(--color-surface-floating);    color: var(--color-text-body);    box-shadow: var(--shadow-overlay);    font-size: .875rem;    line-height: 1.55;    /* Closed is where it leaves to: it fades and settles back half as far as it came, faster than it arrived. */    opacity: 0;    scale: .985;    translate: 0 .25rem;    transition-property: opacity, scale, translate, overlay, display;    transition-duration: var(--duration-quick);    transition-timing-function: var(--easing-glide);    transition-behavior: allow-discrete;    &[open] {      display: flex;      opacity: 1;      scale: 1;      translate: 0 0;      transition-duration: var(--duration-base);      /* Where it arrives from, on the first frame it is rendered. */      @starting-style {        opacity: 0;        scale: .97;        translate: 0 .5rem;      }    }    /* The scrim dims and softens the page; it fades with the dialog. */    &::backdrop {      background: var(--color-surface-scrim);      backdrop-filter: blur(3px);      opacity: 0;      transition-property: opacity, overlay, display;      transition-duration: var(--duration-quick);      transition-timing-function: var(--easing-glide);      transition-behavior: allow-discrete;    }    &[open]::backdrop {      opacity: 1;      transition-duration: var(--duration-base);    }    @starting-style {      &[open]::backdrop {        opacity: 0;      }    }    &:focus-visible {      outline: none;    }    &.dialog-sm {      --dialog-width: 22rem;    }    &.dialog-lg {      --dialog-width: 40rem;    }    /*     * Drawer: a panel on the inline-end edge for work that keeps the page in view. It floats 8px inside the     * viewport, so its corner and its shadow stay whole, and it slides in from the edge it rests on. Like the     * sheet it keeps clear of what the device lays over the page: the status bar and the home indicator above     * and below it, and in landscape the notch at either side, which the device reports on both.     */    &.dialog-drawer {      --dialog-width: 24rem;      --dialog-edge-top: max(.5rem, env(safe-area-inset-top));      --dialog-edge-bottom: max(.5rem, env(safe-area-inset-bottom));      --dialog-edge-inline: max(.5rem, env(safe-area-inset-left), env(safe-area-inset-right));      --dialog-from: calc(100% + var(--dialog-edge-inline));      width: min(var(--dialog-width), calc(100% - 2 * var(--dialog-edge-inline)));      height: calc(100dvh - var(--dialog-edge-top) - var(--dialog-edge-bottom));      max-height: none;      margin: var(--dialog-edge-top) var(--dialog-edge-inline) var(--dialog-edge-bottom);      margin-inline-start: auto;      scale: 1;      translate: var(--dialog-from) 0;      &:dir(rtl) {        --dialog-from: calc(-100% - var(--dialog-edge-inline));      }      &[open] {        translate: 0 0;        @starting-style {          scale: 1;          translate: var(--dialog-from) 0;        }      }    }    /*     * Start: the same panel on the inline-start edge, where navigation lives. A sidebar that has left a narrow     * layout comes back as this drawer. Use it together with dialog-drawer.     */    &.dialog-drawer.dialog-drawer-start {      --dialog-width: 18rem;      --dialog-from: calc(-100% - var(--dialog-edge-inline));      margin-inline: var(--dialog-edge-inline) auto;      &:dir(rtl) {        --dialog-from: calc(100% + var(--dialog-edge-inline));      }    }    /*     * Sheet: a panel that rises from the bottom edge, for a task on a small screen or a short list of     * choices. It spans the width up to --dialog-width, squares its lower corners and clears the home indicator.     */    &.dialog-sheet {      width: min(var(--dialog-width), 100%);      max-height: calc(100dvh - 3rem);      margin: auto auto 0;      padding-bottom: max(var(--dialog-padding), env(safe-area-inset-bottom));      border-end-start-radius: 0;      border-end-end-radius: 0;      scale: 1;      translate: 0 100%;      &[open] {        translate: 0 0;        @starting-style {          scale: 1;          translate: 0 100%;        }      }    }    /*     * Swipe: the dialog is the track, the whole viewport, and the panel inside it is the drawer or the sheet.     * Only the panel takes the pointer, so a press beside it lands on the scrim, and a wheel there moves nothing.     * It enters and leaves as the drawer or the sheet does: the whole track slides.     */    &.dialog-swipe {      inset: 0;      width: auto;      height: auto;      max-width: none;      max-height: none;      margin: 0;      padding: 0;      gap: 0;      border-radius: 0;      overflow: auto;      overscroll-behavior: contain;      scrollbar-width: none;      background: transparent;      box-shadow: none;      pointer-events: none;      &::-webkit-scrollbar {        display: none;      }      /* The stretch past the edge, as large as the track: where the panel goes when it is let go of. */      &::after {        content: '';        flex: none;      }      /* The gone rest exists only where a script can close the dialog that rests there. */      @media (scripting: enabled) {        &::after {          scroll-snap-align: start;        }      }      @media (prefers-reduced-motion: no-preference) {        scroll-behavior: smooth;      }      /* A mouse drags the panel by scrolling the track from the behavior; the track does not snap meanwhile. */      @media (pointer: fine) {        &:has(.dialog-panel:active) {          scroll-snap-type: none;        }      }    }    /* The scrim thins as the panel leaves: the behavior writes how much of it is left to --dialog-scrim. */    &.dialog-swipe::backdrop {      background: color-mix(in oklab, var(--color-surface-scrim) calc(var(--dialog-scrim, 1) * 100%), transparent);      backdrop-filter: blur(calc(var(--dialog-scrim, 1) * 3px));      pointer-events: auto;    }    /* A reader who asks for less transparency gets the scrim without the blur behind it, swiped or not. */    @media (prefers-reduced-transparency: reduce) {      &::backdrop, &.dialog-swipe::backdrop {        backdrop-filter: none;      }    }    /* A swiped drawer moves across: the panel rests at the inline end, the stretch lies before it. */    &.dialog-drawer.dialog-swipe {      width: auto;      height: auto;      margin: 0;      flex-direction: row-reverse;      scroll-snap-type: x mandatory;      &::after {        inline-size: 100%;      }    }    &.dialog-drawer.dialog-drawer-start.dialog-swipe {      margin: 0;      flex-direction: row;    }    /* A swiped sheet moves down: the panel rests on the bottom edge, the stretch lies above it. */    &.dialog-sheet.dialog-swipe {      width: auto;      max-height: none;      margin: 0;      padding: 0;      flex-direction: column-reverse;      scroll-snap-type: y mandatory;      &::after {        block-size: 100%;      }    }    @media (prefers-reduced-motion: reduce) {      scale: none;      translate: none;      &[open], &.dialog-drawer, &.dialog-drawer[open], &.dialog-sheet, &.dialog-sheet[open] {        scale: none;        translate: none;        @starting-style {          scale: none;          translate: none;        }      }    }    /*     * The edge in forced colors, also while the dialog itself has focus, which takes away its outline above.     * A swiped dialog is only the track: its panel draws the edge.     */    @media (forced-colors: active) {      &, &:focus-visible {        outline: 1px solid CanvasText;        outline-offset: -1px;      }      &.dialog-swipe, &.dialog-swipe:focus-visible {        outline: none;      }    }  }  /* The title and what it means, at the top of the sheet. */  .dialog-header {    display: grid;    flex: none;    gap: .375rem;    padding: 0 var(--dialog-padding);  }  /* Leave the title clear of the close button. */  .dialog:has(.dialog-close) .dialog-header {    padding-inline-end: calc(var(--dialog-padding) + 2rem);  }  .dialog-title {    margin: 0;    color: var(--color-text-strong);    font: var(--font-weight-control) 1.0625rem/1.35 var(--font-family-sans);    font-feature-settings: inherit;    letter-spacing: -.01em;    text-wrap: balance;    overflow-wrap: anywhere;  }  .dialog-description {    margin: 0;    color: var(--color-text-muted);    text-wrap: pretty;    overflow-wrap: anywhere;  }  /* The body takes the remaining height and scrolls inside the sheet; a little block padding keeps focus rings whole. */  .dialog-body {    flex: 1 1 auto;    min-height: 0;    margin-block: -.25rem;    padding: .25rem var(--dialog-padding);    overflow-y: auto;    overscroll-behavior: contain;    scrollbar-width: thin;    scrollbar-color: var(--color-line-strong) transparent;  }  /* Actions end the sheet, the main one last. In a narrow dialog they stack at full width, the main one first. */  .dialog-footer {    display: flex;    flex: none;    flex-wrap: wrap;    justify-content: flex-end;    gap: .5rem;    padding: .25rem var(--dialog-padding) 0;    @container dialog (inline-size < 22rem) {      flex-direction: column-reverse;      align-items: stretch;    }  }  /* A quiet square button in the corner; give it an accessible name. */  .dialog .dialog-close {    position: absolute;    inset-block-start: calc(var(--dialog-padding) - .4375rem);    inset-inline-end: calc(var(--dialog-padding) - .4375rem);  }  /*   * The panel: in a dialog-swipe, the drawer or the sheet itself. It wears the dialog's material and lays out   * its parts as the dialog does, and it is the container their queries measure. It clips its corners   * without becoming a scroller, so what it holds snaps on the track.   */  .dialog-swipe .dialog-panel {    --surface-color: var(--color-surface-floating);    position: relative;    container: dialog / inline-size;    display: flex;    flex: none;    flex-direction: column;    gap: 1.25rem;    box-sizing: border-box;    padding: var(--dialog-padding) 0;    border-radius: calc(var(--dialog-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    overflow: clip;    background: var(--color-surface-floating);    box-shadow: var(--shadow-overlay);    pointer-events: auto;    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;    }  }  /*   * In a drawer it floats 8px inside the viewport, at the inline end or, with dialog-drawer-start, the start,   * and clear of the safe areas as the drawer itself is.   */  .dialog-drawer .dialog-panel {    width: min(var(--dialog-width), calc(100% - 2 * var(--dialog-edge-inline)));    margin: var(--dialog-edge-top) var(--dialog-edge-inline) var(--dialog-edge-bottom);    scroll-margin: var(--dialog-edge-top) var(--dialog-edge-inline) var(--dialog-edge-bottom);    scroll-snap-align: end;  }  .dialog-drawer-start .dialog-panel {    scroll-snap-align: start;  }  /* In a sheet it stands on the bottom edge with square lower corners, clear of the home indicator. */  .dialog-sheet .dialog-panel {    width: min(var(--dialog-width), 100%);    max-height: calc(100% - 3rem);    margin-inline: auto;    padding-bottom: max(var(--dialog-padding), env(safe-area-inset-bottom));    border-end-start-radius: 0;    border-end-end-radius: 0;    scroll-snap-align: end;  }  /*   * Detents: the sheet is as tall as it may be and also rests showing half the track, where it opens. The   * half rest is an empty box from the panel's top edge down half the track, snapped to the track's bottom.   */  .dialog-detents .dialog-panel {    height: calc(100% - 3rem);    &::before {      content: '';      position: absolute;      inset-block-start: 0;      inset-inline: 0;      block-size: calc((100% + 3rem) / 2);      pointer-events: none;      scroll-snap-align: end;      /* A fling from full height stops here; only one from half height lets the sheet go. */      scroll-snap-stop: always;      scroll-initial-target: nearest;    }  }  /* On a touch screen a drag that reaches the top of a scrolled body goes on to move the sheet. */  @media (pointer: coarse) {    .dialog-sheet.dialog-swipe .dialog-body {      overscroll-behavior-y: auto;    }  }  /*   * The grab handle: a short groove in the sheet's top edge, centered over the title. Its target is 64 by   * 24px, 88 by 44px on a touch screen; the groove keeps its size. It takes ink while it is held. In a sheet   * with dialog-detents it is a button the behavior gives keys: it moves the sheet between half and full height   * and says which with aria-expanded. Elsewhere it only shows where to take hold, and is hidden from assistive technology.   */  .dialog-handle {    position: absolute;    inset-block-start: 0;    inset-inline: 0;    z-index: 1;    width: 4rem;    height: var(--dialog-padding);    margin: 0 auto;    padding: 0;    border: 0;    outline: none;    background: none;    color: inherit;    cursor: grab;    appearance: none;    /* A pan still moves the track; a double tap does not zoom the page. */    touch-action: manipulation;    -webkit-tap-highlight-color: transparent;    &::before {      content: '';      position: absolute;      inset-block-start: .5rem;      inset-inline: 0;      width: 2.25rem;      height: .3125rem;      margin-inline: auto;      border-radius: 999px;      corner-shape: round;      background: var(--color-line-strong);      transition: background-color var(--duration-quick) var(--easing-glide);    }    @media (hover: hover) {      &:hover::before {        background: var(--color-line-mark);      }    }    &:active {      cursor: grabbing;    }    &:active::before {      background: var(--color-text-muted);    }    &:focus-visible::before {      @apply --focus-ring;    }    @media (pointer: coarse) {      width: 5.5rem;      height: 2.75rem;    }    @media (forced-colors: active) {      &::before {        background: CanvasText;      }      &:focus-visible::before {        outline-color: Highlight;      }    }  }  /* While a modal dialog is open the page behind it does not scroll. */  :root:has(.dialog:modal) {    overflow: hidden;  }}