本文へ移動

k8o

お問い合わせ

255文字以内で入力してください

GitHub Issueからのお問い合わせもお待ちしております。

Blog

公開: 2026年1月13日(火)更新: 2026年11月9日(月)約8分で読めます

Invoker Commands APIでモーダルをJSなしで開く

Invoker Commands APIは、commandfor属性とcommand属性だけで、ボタンからdialogを開いたり閉じたりする操作を書けるAPIです。JavaScriptは要りません。popoverの開閉と独自のコマンドも説明します。

Newly availableInvoker commands2025年〜
  • Chrome135
  • Edge135
  • Firefox144
  • Safari26.2

はじめに

Invoker Commands APIは、<button>にcommandfor属性とcommand属性を書くだけで、ほかの要素を操作できるAPIです。Dialogをモーダルで開いたり閉じたり、Popoverを開閉したりする操作を、JavaScriptなしで書けます。Safari 26.2が対応して、Baseline 2025に入りました。

Popoverの開閉は、popovertarget属性で書けます。一方で、Dialogをモーダルで開くには、JavaScriptで<dialog>要素のshowModal()メソッドを呼び出す必要がありました。

js
openButton.addEventListener('click', () => {
  dialog.showModal();
});

Invoker Commands APIを使えば、これをHTMLの属性だけで書けます。

html
<button commandfor="my-dialog" command="show-modal">開く</button>

<dialog id="my-dialog">Dialogの内容</dialog>

開くを押すと、showModal()を呼んだときと同じく、Dialogがモーダルで開きます。Popover APIのpopovertarget属性とpopovertargetaction属性の考え方を、Dialogなどにも広げたものです。

基本的な使い方

commandforには操作する要素のidを、commandには実行するコマンドを書きます。組み込みのコマンドは次の6つで、押すとそれぞれのメソッドを呼んだのと同じ動作をします。

  • show-modal:DialogのshowModal()
  • close:Dialogのclose()
  • request-close:DialogのrequestClose()
  • show-popover:PopoverのshowPopover()
  • hide-popover:PopoverのhidePopover()
  • toggle-popover:PopoverのtogglePopover()

Dialogの操作

html
<button commandfor="dialog" command="show-modal">Dialogを開く</button>

<dialog id="dialog">
  <p>Dialogの内容</p>
  <button commandfor="dialog" command="close">閉じる</button>
</dialog>
結果

組み込みのコマンドでも、操作の前にcommandforで指定した要素へcommandイベントが届きます。event.commandはshow-modalのようなコマンドの名前、event.sourceは押したボタンで、preventDefault()を呼ぶとDialogは開きません。closeのボタンにvalue属性を書くと、その値が閉じたあとのreturnValueになります。

<form>の中に置くボタンにはtype="button"を書きます。typeを書かずにcommandforとcommandを付けたボタンは、押してもフォームを送信せず、コマンドも実行しません。type="submit"ではフォームを送信するだけで、コマンドは実行されません。<form method="dialog">の中のキャンセルボタンも同じです。

html
<dialog id="edit-dialog">
  <form method="dialog">
    <input name="name" />
    <button type="button" commandfor="edit-dialog" command="close">キャンセル</button>
    <button>保存</button>
  </form>
</dialog>

Popoverの操作

html
<button commandfor="popover" command="toggle-popover">メニュー</button>

<div id="popover" popover>
  ポップオーバーの内容
</div>
結果

Popoverなら、これまでのpopovertarget属性とpopovertargetaction属性でも同じことができます。

html
<button popovertarget="popover" popovertargetaction="toggle">メニュー</button>

<div id="popover" popover>
  ポップオーバーの内容
</div>
結果

カスタムコマンド

組み込みのコマンドのほかに、--で始まる名前で独自のコマンドを作れます。動作はJavaScriptで書きます。ボタンを押すとcommandforの要素にcommandイベントが届くので、event.commandで分けて処理します。

html
<button commandfor="my-image" command="--zoom-in">拡大</button>
<button commandfor="my-image" command="--zoom-out">縮小</button>
<button commandfor="my-image" command="--reset">リセット</button>
<img id="my-image" src="/demos/invoker-commands/k8o.avif" alt="k8o" width="96" height="96" />
結果

--で始まらない名前は、組み込みのコマンドでなければ無視されます。command="zoom-in"のボタンを押してもcommandイベントは届かず、ボタンのcommandプロパティは空文字列を返します。

おわりに

commandforとcommandを使うと、JavaScriptを書かずにDialogをモーダルで開いたり、Popoverを開閉したりできます。--で始まるカスタムコマンドとcommandイベントを組み合わせれば、組み込みのコマンドに無い操作も、同じボタンの書き方で呼べます。