- 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
commandfor
andcommand
attributes to<button>
and elements enables more accessible and declarative button behavior, reducing bugs and JavaScript. Buttons withcommand
dispatchCommandEvent
oncommandfor
element 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
popover
attribute 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">
withcommandfor
andcommand
attributes. - Invoker Target: An element referenced by
commandfor
.
- Proposed Plan: Add
commandForElement
andcommand
as attributes to<button>
.commandfor
is an IDREF to an element.command
is a freeform hint. Custom commands start with double dash. Valid commands dispatchCommandEvents
. If bothpopovertarget
andcommandfor
/command
are set,command
takes precedence.command
is ignored for form participants ortype=submit
buttons. Example Code:
- Popovers:
button
withcommandfor
andcommand="toggle-popover"
toggles popover. - Dialogs:
button
withcommandfor
andcommand="show-modal"
shows dialog.button
withcommand="close"
closes dialog. - Custom behavior: Custom JavaScript can be triggered using custom commands.
- Popovers:
- Defaults: Depending on
commandfor
element, invoking button triggers additional behaviors. Table shows built-in invocations on specific element types. - Accessibility: For built-in behaviors,
command
maps 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
CommandEvent
for their behaviors. - Security: Care is taken to ensure no new significant security implications. Invokers require user interaction and
commandfor
references an ID in the same document. PAQ (Potentially Asked Questions):
- Name:
command
is chosen as it notifies related element of action request, not specific to user gesture likeclick
. - Name change:
invoke
was originally used but changed tocommand
for simplicity. - Implicit attributes: Initially, auto
command
and implicitcommandfor
are 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
/commandfor
but intended to replace it.- Passing arguments: Custom
command
s can be used in freeform ways with various techniques like usingdata-*
attributes.
- Name:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。