3.2 · Popovers & positioning
Buttons that act: invoker commands
Buttons that open dialogs and popovers without click handlers.
commandfor and command generalize
popovertarget. A button can open a modal
<dialog> (show-modal), close it
(close) or toggle a popover, with no script.
Commands that start with -- are your own. They fire a
command event on the target, so behavior stays
declarative in HTML and the script only listens. The demo also uses
closedby="any", which makes a dialog close on backdrop
clicks.
openButton.addEventListener('click', () => {
dialog.showModal()
})
cancelButton.addEventListener('click', () => {
dialog.close()
})
<button commandfor="confirm"
command="show-modal">Delete…</button>
<dialog id="confirm" closedby="any">
<button commandfor="confirm"
command="close">Cancel</button>
</dialog>