Invoker Commands API

Create declarative HTML-based interactions for popovers, dialogs, and custom controls without JavaScript event handlers

What Is the Invoker Commands API?

The Invoker Commands API represents a fundamental shift in how we create interactive web experiences. Rather than relying on JavaScript event listeners to connect buttons to their actions, this declarative API allows you to define interactions directly in HTML markup.

A button with commandfor and command attributes automatically knows what element it controls and what action to perform--no JavaScript required. The result is faster initial interactivity, cleaner code, and HTML that describes both structure and behavior.

This API builds on the foundation laid by similar HTML attributes like popovertarget and popovertargetaction, extending the concept to support a broader range of actions and custom behaviors. The result is a more flexible, powerful system that reduces the need for JavaScript interaction libraries while maintaining the ability to create sophisticated custom behaviors when needed. For teams focused on modern web development practices, this declarative approach aligns perfectly with component-based architecture patterns.

Why Use Invoker Commands?

Key benefits for modern web development

Instant Interactivity

Controls become interactive immediately, without waiting for JavaScript to download and execute. Users can interact with your interface before any script execution.

Declarative Simplicity

The relationship between triggers and targets is visible directly in HTML. No hunting through JavaScript files to understand how your interface works.

Reduced JavaScript

Built-in commands for popovers and dialogs require zero JavaScript. Custom commands need only minimal event handling, reducing your codebase.

Better Performance

Smaller bundle sizes and faster time-to-interactive, especially on slow connections or devices with limited processing power.

Built-In Commands Reference

The Invoker Commands API provides several built-in commands that cover common interaction patterns. These commands work without any JavaScript, making them ideal for production use.

Popover Commands

CommandTargetEffect
show-popoverElements with popover attributeShows a hidden popover
hide-popoverElements with popover attributeHides a visible popover
toggle-popoverElements with popover attributeToggles popover visibility

Dialog Commands

CommandTargetEffect
show-modal<dialog> elementsShows dialog as a modal
close<dialog> elementsCloses the dialog
request-close<dialog> elementsRequests closure with cancellation support

These commands mirror the functionality of popovertarget and popovertargetaction while offering a more consistent and extensible approach to declarative interactions. When implementing these patterns, consider how they integrate with your overall frontend optimization strategy for maximum performance gains.

Example: Creating Popovers

Popovers represent one of the most common use cases for invoker commands. The implementation pattern is straightforward: your popover element needs the popover attribute, and your trigger buttons use commandfor and command to control it.

<button type="button" commandfor="mypopover" command="toggle-popover">
 Toggle Popover
</button>

<div id="mypopover" popover>
 <p>This is popover content</p>
 <button type="button" commandfor="mypopover" command="hide-popover">
 Close
 </button>
</div>

This pattern requires no JavaScript. The browser handles all the interaction logic, making your popover controls work immediately--even if JavaScript fails to load.

For more complex behaviors, you can combine invoker commands with JavaScript event listeners on the popover element itself, creating a progressive enhancement pattern that works everywhere. This approach is particularly valuable when building accessible web interfaces that need to work across all devices and connection speeds.

Example: Creating Dialogs

Modal dialogs receive similar treatment through the Invoker Commands API. Use the <dialog> element as your target, and buttons control its visibility with the appropriate commands.

<button type="button" commandfor="mydialog" command="show-modal">
 Show Dialog
</button>

<dialog id="mydialog">
 <p>Dialog content goes here</p>
 <button type="button" commandfor="mydialog" command="close">
 Close
 </button>
</dialog>

The request-close command enables confirmation dialogs. When triggered, it calls requestClose() on the dialog, which fires a cancel event. By listening for this event and calling preventDefault(), you can implement confirmation flows for unsaved changes or other scenarios where accidental closure could result in data loss. This pattern integrates seamlessly with React development services and other modern JavaScript frameworks.

Custom Commands

For actions beyond built-in commands, you can create custom commands by prefixing your command name with --. Custom commands require JavaScript event handling but maintain the declarative trigger-target pattern.

<button commandfor="myimg" command="--rotate-left">Rotate Left</button>
<button commandfor="myimg" command="--rotate-right">Rotate Right</button>

<img id="myimg" src="photo.jpg" alt="[photo]" />
const myImg = document.getElementById("myimg");

myImg.addEventListener("command", (event) => {
 if (event.command === "--rotate-left") {
 myImg.style.rotate = "-90deg";
 } else if (event.command === "--rotate-right") {
 myImg.style.rotate = "90deg";
 }
});

The CommandEvent provides source (the button element) and command (the command string) properties for handling custom behaviors. This pattern scales beautifully--adding a new control requires only a new button with the appropriate command attribute. When combining custom commands with advanced interactions, our JavaScript development expertise can help you build sophisticated interfaces that remain maintainable and performant.

Browser Support

5

Major Browsers Supported

2025-12

Baseline Status Achieved

2028

Expected Widely Available

Browser Support Timeline
BrowserVersionRelease Date
Chrome135April 2025
Edge135April 2025
Firefox144October 2025
Safari26.2December 2025
Opera1202025

Conclusion

The Invoker Commands API brings declarative, HTML-based interactions to the modern web. By defining trigger-target relationships directly in markup, you create interfaces that are faster, more maintainable, and more accessible.

The feature is available today in Chrome, Edge, Firefox, Safari, and their mobile counterparts. Start with built-in commands for popovers and dialogs, then explore custom commands for your specific needs. As web development continues evolving toward declarative patterns, invoker commands point the way forward.

Ready to build faster, more interactive web experiences? Our team specializes in modern web development techniques that leverage the latest browser APIs for optimal performance and user experience. From JavaScript development to frontend optimization, we help you deliver exceptional digital products.

Build Modern Web Experiences

Our team specializes in leveraging the latest web APIs to create fast, interactive websites.

Frequently Asked Questions

Sources

  1. MDN Web Docs - Invoker Commands API - Complete API reference with all attributes, events, and examples
  2. Web Platform DX - Invoker Commands Feature Explorer - Baseline status and browser support data
  3. WHATWG HTML Specification - Button Command Attribute - Official specification
  4. Chrome for Developers - Introducing command and commandfor - Google developer documentation
  5. Webinista - Command and Commandfor: Invoker Commands API - Practical tutorial with code patterns