webdevpuneet.com

HTML Invoker Commands: Open Dialog With No JavaScript

HTML Invoker Commands: Open Dialog With No JavaScript · Modals · Plain HTML, CSS & JS · Live preview

CategoryModals

About this UI Snippet

command and commandfor: Buttons That Do Things Without a Script

Screenshot of the HTML Invoker Commands: Open Dialog With No JavaScript snippet rendered live

For decades, opening a modal meant a click handler that found the dialog and called showModal(). Invoker commands move that wiring into HTML. A <button command="show-modal" commandfor="myDialog"> opens the dialog with that id, and the browser handles focus and the top layer. This snippet wires a dialog, a popover menu and one custom command using attributes alone.

Built-in commands

show-modal, close and request-close target a <dialog>. toggle-popover, show-popover and hide-popover target any element with the popover attribute. The button needs commandfor with the target's id and nothing else; the menu items use hide-popover so choosing one closes the menu.

Return values

A close button's value becomes the dialog's returnValue, so "Send invite" and "Cancel" can be told apart on the dialog's close event without a click listener on each.

Light dismiss with closedby

closedby="any" on the dialog lets a click outside it, or Escape, close it. closedby="closerequest" limits it to Escape, and none forces an explicit button.

Custom commands

A command starting with two dashes, here --copy-link, does nothing by itself but fires a command event on the target with event.command and event.source. That keeps small actions declarative: the button states what it does, and one listener carries it out.

Fallback

A support check on commandForElement decides whether to add a ten-line click handler that maps the same attributes to showModal, close, togglePopover and hidePopover. For an animated version see the animated dialog and popover.

Accessibility notes

The dialog is labelled with aria-labelledby, focus moves into it when it opens and returns to the button that opened it when it closes, and the status line is an aria-live region so the custom command's result is announced. Give every command button a visible label; an icon-only trigger needs an aria-label.

What's included

Features

Zero-script dialog
command="show-modal" opens a dialog with focus trapping and Escape.
Popover toggling
toggle-popover and hide-popover control menus from the markup.
returnValue from buttons
A close button value tells you which button was used.
closedby light dismiss
Outside click and Escape close the dialog without extra code.
Custom commands
--copy-link fires a command event on the target.
Anchored menu
The popover is positioned under its trigger with anchor positioning.
Animated open and close
@starting-style and allow-discrete handle enter and exit.
Click-shim fallback
Browsers without invokers get the same behaviour from a short script.

Step by step

How to Use

  1. 1
    Give the target an idAdd id="icDialog" to a dialog, or id="icMenu" plus the popover attribute to a div.
  2. 2
    Point a button at itWrite command="show-modal" commandfor="icDialog" on the opening button. No listener is required in supporting browsers.
  3. 3
    Close from insideUse command="close" commandfor="icDialog" on Cancel and Send; the button value becomes dialog.returnValue.
  4. 4
    Choose how it dismissesclosedby="any" allows outside click and Escape; closerequest limits it to Escape; none needs a button.
  5. 5
    Add a custom commandUse command="--your-name" and listen for the command event on the target, checking event.command.
  6. 6
    Keep the fallbackThe script checks commandForElement and only runs a click shim where invokers are missing.

Real-world uses

Common Use Cases

Server-rendered pages
Add confirmation dialogs and menus to pages built from templates, without shipping a framework or a bundle for a single modal.
CMS and email-style content
Let content editors place a button that opens a dialog by writing two attributes, where inline scripts are stripped or blocked.
Reducing handler code
Delete the open, close and toggle listeners that every project repeats and keep JavaScript for real behaviour.
Accessible defaults
Get correct top-layer, focus return and Escape behaviour from the platform instead of a half-copied modal snippet.

Got questions?

Frequently Asked Questions

Current Chrome, Edge and Firefox, with Safari following. For older versions the snippet adds a small click shim keyed on commandForElement, so behaviour stays the same while support spreads.

show-modal opens a dialog as a modal: the page behind becomes inert and focus is trapped. A popover is non-modal, dismisses on outside click by default and is for menus, tips and small panels.

Any command name that starts with two dashes is allowed. Pressing the button fires a command event on the element named by commandfor, with event.command set to your name, and your listener decides what to do.

Yes. closedby="any" lets a click outside the dialog or the Escape key close it. In browsers without it, the fallback listens for clicks on the dialog element itself, which is the backdrop area.

Copy & paste

Source Code

<main class="ic-page">
  <h1>Buttons that run commands</h1>
  <p class="ic-sub">A dialog, a popover and a custom command, all wired with HTML attributes. There is no click handler for opening or closing anything.</p>

  <div class="ic-row">
    <button class="ic-btn" type="button" command="show-modal" commandfor="icDialog">Invite teammate</button>
    <button class="ic-btn ic-ghost" type="button" command="toggle-popover" commandfor="icMenu">More &#9662;</button>
    <button class="ic-btn ic-ghost" type="button" command="--copy-link" commandfor="icStatus">Copy link</button>
  </div>
  <p class="ic-status" id="icStatus" aria-live="polite">Nothing copied yet.</p>

  <dialog class="ic-dialog" id="icDialog" closedby="any" aria-labelledby="icTitle">
    <h2 id="icTitle">Invite a teammate</h2>
    <p>They get an email with a link that works for 7 days. Click outside this box or press Escape to dismiss it.</p>
    <div class="ic-actions">
      <button class="ic-btn ic-ghost" type="button" command="close" commandfor="icDialog">Cancel</button>
      <button class="ic-btn" type="button" command="close" commandfor="icDialog" value="sent">Send invite</button>
    </div>
  </dialog>

  <div class="ic-menu" id="icMenu" popover>
    <button type="button" command="hide-popover" commandfor="icMenu">Rename</button>
    <button type="button" command="hide-popover" commandfor="icMenu">Duplicate</button>
    <button type="button" command="hide-popover" commandfor="icMenu">Archive</button>
  </div>
  <p class="ic-note" id="icNote"></p>
</main>

Extend with AI

Take This Snippet Further With AI

Paste this snippet into an AI coding assistant and ask it to explain how command and commandfor replace click handlers and what closedby changes. Useful follow-ups: add a delete-confirmation flow using returnValue, chain a dialog opened from a popover item, or log every custom command. Ask it to keep the feature-detected fallback.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a page that opens a dialog and a popover using HTML invoker commands, with a fallback, in plain HTML, CSS and JavaScript.

Requirements:
- A button with command="show-modal" commandfor="dialogId" that opens a <dialog> with closedby="any", a heading (aria-labelledby), a sentence, and Cancel and Send buttons that use command="close" commandfor="dialogId" (Send has value="sent").
- A button with command="toggle-popover" commandfor="menuId" that toggles a div[popover] menu with three items that each use command="hide-popover" commandfor="menuId". Anchor the menu under its button with anchor-name and position-anchor inside @supports.
- A third button with a custom command="--copy-link" commandfor="statusId"; listen for the command event on the status element, copy a URL to the clipboard and show it in an aria-live line.
- Animate the dialog, its ::backdrop and the popover with @starting-style and transitions that list overlay and display with allow-discrete.
- Fallback: if !('commandForElement' in HTMLButtonElement.prototype), add one click listener mapping show-modal, close, toggle-popover, hide-popover and custom commands, and close the dialog when its backdrop is clicked.
- Respect prefers-reduced-motion and work at phone width.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.