> ## Content Index
> Fetch the complete content index at: https://atomic-noggin-bloggin.ghost.io/llms.txt
> Use this file to discover other available public pages before exploring further.

# Taking control of command
- URL: https://atomic-noggin-bloggin.ghost.io/taking-control-of-command/
- Published: 2026-11-19T11:49:33.000Z
- Updated: 2026-11-19T12:03:23.000Z
- Description: Browsers have a new API that creates an HTML alternative to event listeners for triggering interactivity; but it's too limited in scope to be very useful. I fixed that.
- Author: Patrick Denny
- Tags: JavaScript

The [Invoker Command API](https://developer.mozilla.org/en-US/docs/Web/API/Invoker%5FCommands%5FAPI?ref=atomic-noggin-bloggin.ghost.io) just recently became baseline for major browsers in of 2025 (first landing in Chrome on April 1st of that year and finally hitting Safari December 12th). If you're unaware, it is essentially a convenience API for invoking button click actions with html attributes instead of JS event listeners. 

It introduces two new attributes to the **`<button>`** element:

- **`commandfor`** \- Turns the button into a command invoker, targeting a given interactive element. It takes the ID of the element to target as its value.
- **`command`** \- Specifies the action to be performed on the target element. It takes in a predetermined kabob-case representation of the target element's method to invoke.  
  - May also take in a custom command name, that needs to be prefixed with two hyphen characters ( `--`)
- **`commandForElement`** and **`command`** properties are also available on the **`HTMLButtonElement`** interface to allow programable access to the API.

In theory, this should allow developers to wire up intra-element interactivity directly in the DOM without needing to write custom event listeners for each control element; only declare the possible actions to take on the target elements themselves. However, in this initial iteration, the API has been constricted to a limited number of actions on a tiny subset of potential targets.

## Limited out of box functionality

The API as it stands currently supports six(6) predetermined actions that can be invoked for dialogs and popovers exclusively.

- "show-model", "close", and "request-close" which only work if the target element is a **`<dialog>`.**
- "show-popover", "close-popover", and "toggle-popover" which only work if the target element has the global [**popover**](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global%5Fattributes/popover?ref=atomic-noggin-bloggin.ghost.io) attribute set.

Future command actions have[ been proposed](https://open-ui.org/components/future-invokers.explainer/?ref=atomic-noggin-bloggin.ghost.io), but there is no clear indication when – or even if – they will be implemented.

While there is the ability to fire custom commands as a work-around, this only dispatches a [**commandEvent**](https://developer.mozilla.org/en-US/docs/Web/API/CommandEvent?ref=atomic-noggin-bloggin.ghost.io) to the target element. This means you're trading `clickEvent` listeners assigned to the buttons, which would fire a specific action, with a catch-all **`commandEvent`** listener on the target which now needs to handle multiple potential command actions. Adding and removing these custom commands over time would become complex and difficult to maintain.

To further muddy the waters, the **`popover`** command actions are duplicated by ([possibly redundant](https://open-ui.org/components/invokers.explainer/?ref=atomic-noggin-bloggin.ghost.io#what-does-this-mean-for-popovertarget)) parallel [Popover API](https://developer.mozilla.org/en-US/docs/Web/API/Popover%5FAPI?ref=atomic-noggin-bloggin.ghost.io). In a fun piece of circular logic, a **`<dialog>`** "show" command action is intentionally not included because it was [recommended for deprecation](https://github.com/whatwg/html/issues/9376?ref=atomic-noggin-bloggin.ghost.io) since non-modal dialogs can now be [declaratively opened](https://atomic-noggin-bloggin.ghost.io/the-secret-third-empty-value-in-js/) via the Popover API that the Invoker Command API is meant to replace. 

## Extending functionality with a custom behavior

Using the Custom Behavior Registry package [I wrote about earlier](https://atomic-noggin-bloggin.ghost.io/look-ma-my-first-npm-package/), I created a [custom-commands behavior](https://github.com/AtomicNoggin/custom-behavior-registry/tree/main/examples/custom-commands?ref=atomic-noggin-bloggin.ghost.io) to extend the scope of this API to be more universal and future proof by improving it in three specific ways

### 1) Allow Invokers to call any element method

The first thing this **custom-commands** behavior does is expand action availability.

If the given command action doesn't start with two hyphens and doesn't match one of the API's pre-set actions (using the original command property getter, in case new values are added in the future) it will opportunistically check the target element for a method with a *camelCase* equivalent to the *kebab-case* action value before firing it. 

This means that after loading **custom-commands**, the following HTML will work as is. No extra coding needed.

```html
<input id="foreground" type="color"> 
<button command="show-picker" commandfor="foreground">Open</button>
```

It also means that any custom or polyfilled methods dynamically added to an element will also work as expected, as with the following example code:

```javascript
// adding missing methods to <details>
HTMLDetailsElement.prototype.show = 
  HTMLDetailsElement.prototype.show || 
  function show () { 
    this.open = true;
    this.scrollIntoViewIfNeeded 
      ? this.scrollIntoViewIfNeeded()
      : this.scrollIntoView();
  };
HTMLDetailsElement.prototype.close = 
  HTMLDetailsElement.prototype.close || 
  function  close () { 
    this.open = false;
  };
HTMLDetailsElement.prototype.toggle = 
  HTMLDetailsElement.prototype.toggle || 
  function toggle () { 
  this.open
    ? this.close()
    : this.close();
  };
```

```html
<details id="cool-facts">
  <summary hidden><!-- removed for design reasons --></summary>
  ...
  <button command="close" commandfor="cool-facts">Close Me</button>
</details>
<button command="show" commandfor="cool-facts">Show me cool facts!</button>
```

Of course, if we can open up the possible actions we can invoke, why not open up the type of elements that do the invoking.

### 2) Let other interactive elements become command invokers

While a **`<button>`** is the most logical command invoker, there's no need for this API to be just a fancy click event replacement for a single tag. Especially when used with custom command names.

To this end, the **custom-commands** behavior extends the **Command Invoker API** to be available for **`<input>`**, **`<select>`**, & **`<textarea>`** tags and any other *focusable* element with a valid `**tabindex**` attribute set. By default, the form elements (except those of type *button*, *submit*, or *reset*) will invoke using a `change` event; all others will invoke on `click`. Since these events may not always be ideal for every use case, it also adds an optional third **commandtrigger** attribute to specify which event to use instead. 

To demonstrate how this would work, assume we had a data table for displaying search results. We could add a **`CommandEvent`** listener to handle multiple commands, like so:

```javascript
const results = document.getElementById("results-list")
results.addEventListener("command",(event) => {
  switch (event.command) {
    case "--filter":
      filterRowsByType(event.source.value)
      break;
    case "--find-matching":
      filterRowsContaining(event.source.value);
      break;
    case "--sort-rows":
      sortByColumn(event.source);
  }
}
```

Which could then be wired up declaratively with the following HTML:

```html
<!-- invokes command anytime the selection changes -->
<select id="filterby" command="--filter" commandfor="results-list">
  ....
</select>
<!-- switch commandtrigger to input so it invokes as the user types -->
<input 
  id="typeahead-find" type="text" command="--find-matching" 
  commandfor="results-list" commandtrigger="input"
>
...
<table id="result-list">
  <tr>
    <!-- setting tabindex allows any element to be a command invoker -->
    <th 
      aria-sort="none" tabindex="0"
      command="--sort-rows" commandfor="result-list" 
    >Title</th>
    <th  
      aria-sort="none" tabindex="0" 
      command="--sort-rows" commandfor="result-list"
    >Status</th>
    ...
  </tr>
  ...
</table>
```

Now that we have a more robust way of invoking custom commands, lets make a robust way of maintaining those commands.

### 3) Introduce a global `CustomCommandRegistry`

If the Command Invoker API is intended to be a declarative alternative to event listeners for built-in commands, it stands to reason that there should be a way also declare custom commands without needing event listeners as well. 

For that reason, the **custom-commands** behavior also introduces a **`CustomCommandRegistry`** to the **`Element`** interface. This way, developers can define one or more custom commands on any element without having to create catch-all command event listeners. They can easily add, remove, test for, and retrieve individual command methods as needed. It also allows for arguments to be passed in, such as the command name, source and/or target element, or even properties of those elements (like `**input.value**`). By default the Registry will attempt to bind the command method to it's owning element so calls to `this` work as expected. Binding can optionally overridden with another element or object as needed.

Under the hood, the **`CustomCommandRegistry`** automatically handles adding a CommandEvent listener as soon as the first command is defined, and removing it once the last command is undefined. To keep memory usage low, each element's registry, available as **`{element}.customCommands`**, only instantiates itself the first time it is referenced in code, and will be garbage collected along with the element once it's no longer referenced. 

If we were to convert the previous **`CommandEvent`** listener to defined commands, it would look something like this:

```javascript
const results = document.getElementById("results-list")
results.customCommands.define(
  "--filter",
  filterRowsByType,
  { arguments:["source.value"] }
);
results.customCommands.define(
  "--find-matching",
  filterRowsContaining,
  { arguments:["source.value"] }
);
results.customCommands.define(
  "--sort-rows",
  filterRowsContaining,
  { arguments:["source"] }
);

```

If you want to se this in action, check it out [on Codepen](https://codepen.io/editor/AtomicNoggin/pen/01a0d0d8-c79d-70b9-ad91-15726ea2a19e?ref=atomic-noggin-bloggin.ghost.io)

See the Pen [Extending Invoker Command API](https://codepen.io/editor/AtomicNoggin/pen/01a0d0d8-c79d-70b9-ad91-15726ea2a19e?ref=atomic-noggin-bloggin.ghost.io) by Patrick Denny ([@AtomicNoggin](https://codepen.io/AtomicNoggin?ref=atomic-noggin-bloggin.ghost.io)) on [CodePen](https://codepen.io/?ref=atomic-noggin-bloggin.ghost.io). 

#### CustomCommandRegistry interface

`**{element}.customCommands.define(name, method[, options])**` 

registers a new command

**Parameters**

`name`

A string representing the name of the command. must start with two hyphens (`--`) 

`method`

A function that will be called when the command is invoked. By default it will be bound to the element, unless another element is specified in the options. 

`options` *optional*

an object that specifies characteristics about the command method. The available options are: 

`arguments`

An array of values representing the arguments to pass into the method. No methods are passed if missing. Acceptable values are 
- `"event"`: the command event
- `"command"`: the name of the command
- `"source"`: the source element that invoked the command
- `"source.*<property>*"`: the value of a source element's property, where *<property>* is the name of a valid property (e.g. `"source.value"`)
- `"target"`: the target element whose command was invoked  
Useful if the method is bound to an Object other than the command owner.
- `"target.*<property>*"`: the value of a target element's property, where *<property>* is the name of a valid property (e.g. `"target.id"`)
- *any other value*: a static value. Passed in as is

`boundTo`

Element to bind method to, if different than the command owner. It can be the `**HTMLElement**` or `**Object**` to bound to, or a string representing the id of an element in the DOM 

**Return value**

None (`undefined`).

**Throws**

A `**TypeError**` if 
- the command name is not a string, or doesn't begin with two hyphens, or
- the method is not a function

`**{element}.customCommands.undefined(name)**` 

remove a command from the registy if it exists

**Parameters**

`name`

A string representing the name of the command.

**Return value**

`true` if the named command was removed, `false` if it wasn't defined. 

`**{element}.customCommands.has(name)**` 

determine if a command is defined

**Parameters**

`name`

A string representing the name of the command.

**Return value**

`true` if the named command is defined, `false` if not. 

`**{element}.customCommands.get(name)**` 

retrieve a defined command

**Parameters**

`name`

A string representing the name of the command.

**Return value**

The `function` associated to the provied name if the command is defined, `null` if not. 

`**{element}.customCommands.fire(name[, ...args])**` 

invoke a defined command, optionally passing in one or mar arguments

**Parameters**

`name`

A string representing the name of the command.

`...args` optional

One or more arguments to pass into the command during invocation. 

**Return value**

The return value, if any from the command method.

**Throws**

A `**TypeError**` if the command is not defined.