Invoker Commands APIでモーダルをJSなしで開く
Invoker Commands APIは、commandfor属性とcommand属性だけで、ボタンからdialogを開いたり閉じたりする操作を書けるAPIです。JavaScriptは要りません。popoverの開閉と独自のコマンドも説明します。
- 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()メソッドを呼び出す必要がありました。
openButton.addEventListener('click', () => {
dialog.showModal();
});Invoker Commands APIを使えば、これを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の操作
<button commandfor="dialog" command="show-modal">Dialogを開く</button>
<dialog id="dialog">
<p>Dialogの内容</p>
<button commandfor="dialog" command="close">閉じる</button>
</dialog>/* 開いたDialogが収まる高さを取る */
body {
min-height: 10rem;
}組み込みのコマンドでも、操作の前にcommandforで指定した要素へcommandイベントが届きます。event.commandはshow-modalのようなコマンドの名前、event.sourceは押したボタンで、preventDefault()を呼ぶとDialogは開きません。closeのボタンにvalue属性を書くと、その値が閉じたあとのreturnValueになります。
<form>の中に置くボタンにはtype="button"を書きます。typeを書かずにcommandforとcommandを付けたボタンは、押してもフォームを送信せず、コマンドも実行しません。type="submit"ではフォームを送信するだけで、コマンドは実行されません。<form method="dialog">の中のキャンセルボタンも同じです。
<dialog id="edit-dialog">
<form method="dialog">
<input name="name" />
<button type="button" commandfor="edit-dialog" command="close">キャンセル</button>
<button>保存</button>
</form>
</dialog>Popoverの操作
<button commandfor="popover" command="toggle-popover">メニュー</button>
<div id="popover" popover>
ポップオーバーの内容
</div>/* 開いたポップオーバーが収まる高さを取る */
body {
min-height: 8rem;
}Popoverなら、これまでのpopovertarget属性とpopovertargetaction属性でも同じことができます。
<button popovertarget="popover" popovertargetaction="toggle">メニュー</button>
<div id="popover" popover>
ポップオーバーの内容
</div>/* 開いたポップオーバーが収まる高さを取る */
body {
min-height: 8rem;
}カスタムコマンド
組み込みのコマンドのほかに、--で始まる名前で独自のコマンドを作れます。動作はJavaScriptで書きます。ボタンを押すとcommandforの要素にcommandイベントが届くので、event.commandで分けて処理します。
<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" />img {
display: block;
/* 2倍に拡大しても収まる余白を取る */
margin: 4rem auto;
border-radius: 50%;
transition: scale 0.3s;
}const image = document.getElementById('my-image');
let scale = 1;
image.addEventListener('command', (event) => {
if (event.command === '--zoom-in') {
scale = Math.min(scale + 0.25, 2);
} else if (event.command === '--zoom-out') {
scale = Math.max(scale - 0.25, 0.5);
} else if (event.command === '--reset') {
scale = 1;
}
image.style.scale = `${scale}`;
});--で始まらない名前は、組み込みのコマンドでなければ無視されます。command="zoom-in"のボタンを押してもcommandイベントは届かず、ボタンのcommandプロパティは空文字列を返します。
おわりに
commandforとcommandを使うと、JavaScriptを書かずにDialogをモーダルで開いたり、Popoverを開閉したりできます。--で始まるカスタムコマンドとcommandイベントを組み合わせれば、組み込みのコマンドに無い操作も、同じボタンの書き方で呼べます。