Command Columns Implementation

A command column (kind: 'command') renders one button per data row. The button shows either a localized text or an SVG icon with a localized tooltip, and activating it (a click, or Enter/Space on the focused button) fires vn-grid-command with the command identifier and the row. The column is structure, not data: like the selection checkbox column it has no header text and no header menu, and it can't be hidden, moved, grouped, frozen, sorted, filtered, resized or auto-fitted, from the UI or the public API.

<vn-grid-column kind="command" column-key="details" command="openDetails"
                command-tooltip="Show product details" width="40">
  <template>
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
      <circle cx="11" cy="11" r="7"/><path d="M21 21l-4.3-4.3"/>
    </svg>
  </template>
</vn-grid-column>

<vn-grid-column kind="command" column-key="remove" command-text="Remove" width="90"></vn-grid-column>
grid.addEventListener('vn-grid-command', (e) => {
    const { command, columnKey, row, rowIndex, rowKey } = e.detail;
    if (command === 'openDetails') openDetails(row);
});

1. Column definition

Property Attribute Meaning
kind: 'command' kind="command" Makes the column a command column. It has no type (a data type is meaningless on it).
key column-key Unique column identifier; the default command identifier.
command command The identifier reported in the event. Defaults to key.
commandText command-text Visible button text. Ignored when commandIcon is set.
commandIcon command-icon, or a <template> child SVG markup for an icon-only button (root element <svg>).
commandTooltip command-tooltip Tooltip and accessible name. Required with an icon; defaults to the text.
width width Required for a text command; optional for an icon command (default 40 px).

Declaratively, a <template> child holding the <svg> is the icon source (vanilla-grid-element.js _parseDeclarativeColumns() reads its innerHTML when there is no command-icon attribute). A template is inert, so the icon never renders inside <vn-grid-column> itself, and the element's text content never becomes a header for a command column.

1.1 Validation

_validateColumns() requires every column's kind to be 'data' or 'command', and runs _validateCommandColumn() on each command column. Each rule is a violation: an Error in strict mode (the default), a logger.warn with strict: false.

1.2 Stamping in setColumns()

After validation, setColumns() stamps each command column once (_stampCommandColumn()), so neither the render path nor the click path recomputes anything per row:

Stamp Value
_isCommand true (reset to false on a column that is no longer a command)
_commandPlacement 'leading' or 'trailing' (§ 3)
_commandWidth width, or 40 for an icon command; both the min and the max width
_commandId command, or key
_commandText / _commandTooltip resolved through formatting.getCommandText / getCommandTooltip
_commandIconEl the sanitized, parsed <svg> (cloned per pooled cell)
sortable, filterable, resizable false, so every existing sort / filter / resize gate refuses the column, as it does the synthetic selection column

It also rebuilds _commandColumnKeys, the normalized key set behind the key-based predicates, and drops any command key from the frozen set (a stored frozen set naming one is ignored; the hidden set is pruned the same way by pruneHiddenColumnKeys()).


2. One predicate for "not a data column"

Every site that used to exempt only __vgSelection__ falls in one of two groups:

Group Meaning Sites
Selection-only Renders or toggles the checkbox itself. The header and body checkbox, _onBodyCheckboxChange, selection key resolution, getColumn() (which returns a command column but not the selection column), the declaration re-applied by clearPersistedSettings() and on element re-insertion.
Non-data "This column is structure, not data." Header menu, filter panel, drag/drop, touch reorder, header title, resizer, sort click, hide/show, freeze, persistence, export, auto-fit, stretch-to-fit, resize, the grouping guards, caption toggle placement.

The non-data sites go through the columns feature's predicates (columns.feature.js):

The grid binds them as _isNonDataColumn, _isNonDataColumnKey and isCommandColumn (_bindFeatureDelegations()). The grouping feature and the Excel export, which have no columns-feature reference, test _isCommand directly next to their own internal key sets.


3. Placement

The selection column stays at index 0 in every path that orders columns, and the command columns sit in two fixed blocks around the data columns:

[ __vgSelection__ ][ leading commands ][ frozen data ][ unfrozen data ][ trailing commands ]

4. Rendering

4.1 Icon parsing

_parseCommandIcon(markup) parses the markup once per distinct string (cached in _commandIconCache) with DOMParser as image/svg+xml:

Without a DOMParser (a non-browser host such as the Node unit suite) the parse is skipped: no violation, and the button renders no icon.

4.2 Header cell

The same treatment as the selection header: no text, draggable = false, default cursor, no title, no resizer, no filter or sort icon. The <th> has class vn-grid-command-header-cell and an aria-label set to the command's tooltip, so a screen reader walking the table doesn't announce a blank header. The delegated header handlers (click / sort, pointerdown / touch reorder, dragstart, contextmenu) return early for it.

4.3 Body cell

In populateRow(), next to the selection branch and like it outside the rowChanged gate:

The per-pass cost is one dataset write per command cell, the same as the selection checkbox, and nothing scales with the row count, so scaled mode (1M rows) takes the same path. rendering.feature.js is already on the BUNDLE_FAST_PATH build profile.

4.4 Styling

See Themes § 4.10: the button paints in currentColor with a translucent hover mix and three tokens (--vn-grid-command-size, --vn-grid-command-radius, --vn-grid-command-hover-bg).


5. Activation and the event

handleClick() (interaction-pointer.feature.js) resolves .vn-grid-command by delegation on the viewport, before the selection branch, the same pattern as the caption toggle:

  1. _activateCommandButton(button) reads the column from the cell's cellIndex and the row from displayRows[button.dataset.vnGridRowIndex], then calls onCommand({ command, columnKey, row, rowIndex, rowKey }). rowKey is the selection key (_resolveRowKey).
  2. It returns before the viewport.focus() every other click ends with, so focus stays on the button, or goes wherever the host's handler moved it (a dialog's close button, say).
  3. It schedules no selection toggle: a command click is an action only.

handlePointerDown() returns early on a command button and resets the double-click tracking, so a press on a command never counts toward a row double-click, nor completes one started on the row.

<vn-grid> passes an onCommand callback to the grid that dispatches vn-grid-command (VanillaGridEvents.COMMAND) on the element with bubbles: true, composed: true, exactly as onRowDblClick becomes vn-grid-row-dblclick.

Keyboard: the button is a native <button>, so Enter and Space dispatch a click: the grid's key handler leaves Space to a focused <button>, and Enter is never a grid key; the arrow, Page, Home and End keys still scroll the grid (Keyboard Navigation § 4). Every button is a Tab stop, like the caption toggle. When a scroll repopulates the pool row holding the focused button with a different row, the grid hands focus to the viewport, as it does for any focused in-row control.


6. Localization

The grid owns no command strings. The host gives them in its language through formatting.getCommandText(column) (default column.commandText) and formatting.getCommandTooltip(column) (default column.commandTooltip, else the text), resolved once per setColumns(). See Localization § 7.1.


7. Interaction with other features

Area Behavior
Sorting / filtering Never sortable or filterable: stamped sortable: false / filterable: false, no header affordance, and the delegated click returns early. A persisted sort naming one is skipped with the existing "not sortable" warning.
Search Not a search field and never highlighted.
Grouping Can't be grouped ('column-unsupported', _findGroupableColumnIndex()), never hosts the caption toggle, and doesn't count as a column left visible (_isInternalColumn()). See Grouping.
Aggregates A command column has no type, and the reducer type gate in aggregate-registry.js admits no function for an untyped column.
Excel export Skipped, even when options.columns names it. See Export.
Persistence Not in the saved order, widths, hidden or frozen sets; a stored value naming one is ignored. No storage-format change. See Settings Persistence.
clearPersistedSettings() Re-applies the declaration, which re-pins the commands.
Hide / freeze Refused by hideColumn(), showColumn(), freezeColumn(), unfreezeColumn(); canHideColumn() is false, and _countVisibleDataColumns() excludes commands, so a grid can't be left showing only commands.
Resize / auto-fit / stretch-to-fit Fixed width (_getMinColumnWidth = _getMaxColumnWidth = _commandWidth), no resizer, skipped by autoFitColumn(), autoFitAllColumns(), the first-data auto-fit and stretch-to-fit.
Toolbar No change; it never lists columns.

8. Tests