
Zero-Script Interactive Controls with HTML Invoker Commands
Interactive web components such as modal dialogs, disclosure drawers, and popovers traditionally force developers to write repetitive JavaScript event listeners just to wire a trigger button to its target element. Even simple open-and-close controls often end up duplicating querySelector lookups, calling showModal() or close(), managing aria-expanded attributes, and remembering to restore keyboard focus back to the triggering control when the overlay dismisses.
With the HTML Invoker Commands API—part of the HTML Living Standard and supported natively in Chromium 135+ during 2025—buttons can declaratively control other elements through commandfor and command attributes without a single line of wiring script. Built-in commands such as show-modal, close, request-close, show-popover, hide-popover, and toggle-popover handle browser-native focus management and accessibility semantics automatically, while custom commands prefixed with -- dispatch a standardized command event on the target element for clean, decoupled component logic.
Example
<button type="button" commandfor="settings-dialog" command="show-modal">
Open Preferences
</button>
<dialog id="settings-dialog" aria-labelledby="settings-title">
<h2 id="settings-title">Display Preferences</h2>
<p>Changes apply immediately across your workspace.</p>
<button type="button" commandfor="settings-dialog" command="close">
Done
</button>
</dialog>
Implementation
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Declarative Invoker Controls</title>
<style>
.toolbar {
display: flex;
flex-wrap: wrap;
gap: 0.75rem;
align-items: center;
font-family: system-ui, sans-serif;
}
.btn {
padding: 0.625rem 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.5rem;
background: #ffffff;
color: #0f172a;
font: inherit;
font-weight: 600;
cursor: pointer;
}
.btn--primary {
border-color: #0f172a;
background: #0f172a;
color: #ffffff;
}
.btn:focus-visible {
outline: 2px solid #2563eb;
outline-offset: 2px;
}
dialog.modal {
max-width: 28rem;
padding: 1.5rem;
border: 1px solid #cbd5e1;
border-radius: 0.75rem;
box-shadow: 0 20px 45px rgba(15, 23, 42, 0.2);
}
dialog.modal::backdrop {
background: rgba(15, 23, 42, 0.5);
}
.modal__actions {
display: flex;
justify-content: flex-end;
gap: 0.5rem;
margin-top: 1.25rem;
}
[popover].help-tip {
max-width: 18rem;
padding: 0.75rem 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.5rem;
box-shadow: 0 10px 25px rgba(15, 23, 42, 0.12);
}
</style>
</head>
<body>
<div class="toolbar">
<button
type="button"
class="btn btn--primary"
commandfor="delete-dialog"
command="show-modal"
>
Archive Project
</button>
<button
type="button"
class="btn"
commandfor="archive-help"
command="toggle-popover"
>
What happens when archived?
</button>
<!-- Custom invoker command handled on the target element -->
<button
type="button"
class="btn"
commandfor="counter-badge"
command="--increment"
>
Bump Counter
</button>
<output id="counter-badge" aria-live="polite" data-count="0">Count: 0</output>
</div>
<div id="archive-help" class="help-tip" popover>
Archived projects remain read-only and can be restored within 30 days.
</div>
<dialog id="delete-dialog" class="modal" aria-labelledby="archive-heading">
<h2 id="archive-heading">Archive this project?</h2>
<p>Team members will still be able to view historical logs and artifacts.</p>
<div class="modal__actions">
<button
type="button"
class="btn"
commandfor="delete-dialog"
command="request-close"
>
Cancel
</button>
<button
type="button"
class="btn btn--primary"
commandfor="delete-dialog"
command="close"
value="confirm"
>
Confirm Archive
</button>
</div>
</dialog>
<script>
// Handle custom --increment command directly on the target element
const counterBadge = document.getElementById('counter-badge');
counterBadge.addEventListener('command', (event) => {
if (event.command === '--increment') {
const next = Number(counterBadge.dataset.count || 0) + 1;
counterBadge.dataset.count = String(next);
counterBadge.textContent = `Count: ${next}`;
}
});
// Progressive enhancement fallback for browsers without native commandfor support
if (!('commandForElement' in HTMLButtonElement.prototype)) {
document.addEventListener('click', (event) => {
const trigger = event.target.closest('button[commandfor][command]');
if (!trigger) return;
const target = document.getElementById(trigger.getAttribute('commandfor'));
const command = trigger.getAttribute('command');
if (!target || !command) return;
if (command === 'show-modal' && typeof target.showModal === 'function') {
target.showModal();
} else if (command === 'close' && typeof target.close === 'function') {
target.close(trigger.value || undefined);
} else if (command === 'request-close' && typeof target.requestClose === 'function') {
target.requestClose(trigger.value || undefined);
} else if (command === 'toggle-popover' && typeof target.togglePopover === 'function') {
target.togglePopover({ source: trigger });
} else if (command.startsWith('--')) {
const customCmd = new Event('command', { cancelable: true });
customCmd.command = command;
customCmd.source = trigger;
target.dispatchEvent(customCmd);
}
});
}
</script>
</body>
</html>
Main Point: HTML Invoker Commands replace boilerplate trigger scripts with declarative command and commandfor attributes that preserve native accessibility and focus behavior.
Credits
Photo by Joakim Nådell on Unsplash