- Authors and Last Updated: Keith Cirkel (https://github.com/keithamus), Luke Warlow. Last updated on 20 February 2025.
- Specification and MDN Documentation: Specification, MDN Documentation
Pitch in Code Example:
<button command="show-modal" commandfor="my-dialog">This opens a dialog</button> <dialog id="my-dialog">This is the dialog</dialog>- Introduction: Adding
commandforandcommandattributes to<button>and elements enables more accessible and declarative button behavior, reducing bugs and JavaScript. Buttons withcommanddispatchCommandEventoncommandforelement with default behaviors. - Background: Inline JavaScript for DOM interactions has fallen out of favor due to security and maintainability. Many frameworks attempt to reintroduce inline handler experiences. Handling clicking is only half the problem. There has been a rise in customizing controls. The
popoverattribute provided a declarative way for popovers. This proposal aims to address similar needs for other interactive elements. Terms:
- Invoke/Invoked/Invoking: Refers to a complete press action using a HID.
- Invoker: A
<button type="button">withcommandforandcommandattributes. - Invoker Target: An element referenced by
commandfor.
- Proposed Plan: Add
commandForElementandcommandas attributes to<button>.commandforis an IDREF to an element.commandis a freeform hint. Custom commands start with double dash. Valid commands dispatchCommandEvents. If bothpopovertargetandcommandfor/commandare set,commandtakes precedence.commandis ignored for form participants ortype=submitbuttons. Example Code:
- Popovers:
buttonwithcommandforandcommand="toggle-popover"toggles popover. - Dialogs:
buttonwithcommandforandcommand="show-modal"shows dialog.buttonwithcommand="close"closes dialog. - Custom behavior: Custom JavaScript can be triggered using custom commands.
- Popovers:
- Defaults: Depending on
commandforelement, invoking button triggers additional behaviors. Table shows built-in invocations on specific element types. - Accessibility: For built-in behaviors,
commandmaps to specific accessibility mappings. Buttons getaria-details,aria-expanded, and focus restored. Other accessibility considerations are discussed. - Invokers and Custom Elements: Custom Elements can use
CommandEventfor their behaviors. - Security: Care is taken to ensure no new significant security implications. Invokers require user interaction and
commandforreferences an ID in the same document. PAQ (Potentially Asked Questions):
- Name:
commandis chosen as it notifies related element of action request, not specific to user gesture likeclick. - Name change:
invokewas originally used but changed tocommandfor simplicity. - Implicit attributes: Initially, auto
commandand implicitcommandforare out of scope. <form>and<a>defaults: Omitted as proposal doesn't aim to replace them.- Limited to buttons: To avoid problems with non-interactive elements.
button[type=submit]andbutton[type=reset]: Not included as they have default actions.input[type=button]: Not supported as it's a legacy element.popovertarget: Coexists withcommand/commandforbut intended to replace it.- Passing arguments: Custom
commands can be used in freeform ways with various techniques like usingdata-*attributes.
- Name:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用。你还可以使用@来通知其他用户。