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.
commandTextorcommandIconmust be present.commandTooltipis required withcommandIcon(an icon-only button needs an accessible name).- A text command needs an explicit
width: the grid can't auto-fit a fixed column, and a translated label's length varies. - The icon markup must parse to an
<svg>root (§ 4.1). type,frozen,hidden,sortable: true,filterable: true,resizable: true,renderCell,valueGetter,sortFieldsandfilterFieldshave no meaning on a command column and are reported rather than silently ignored.
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):
isNonDataColumn(column):column._isCommand === trueor the selection column.isNonDataColumnKey(key): the same, by normalized key, O(1) through_commandColumnKeys.isCommandColumnKey(key): command columns only; the grid exposes it asisCommandColumn(columnKey).
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 ]
- A command declared before every data column is leading; any other is trailing. Within a block, the declaration order holds.
_pinNonDataColumns(columns)produces that order from any input order.applySavedColumnOrder()ends with it, and_reorderColumnsForFreeze()builds the frozen / unfrozen split inside it. With command columns present,_reorderColumnsForFreeze()always runs (it otherwise exits early when nothing is frozen), sosetColumns()andreorderColumn()re-pin through it.- The saved column order stores no command key, and a stored key naming one is ignored, so a saved layout can never move a command. Commands the saved order doesn't know are pinned, never appended last.
- Drag and drop: a command header is never a drag source (its
dragstartis cancelled, so it can't be dragged to the group bar either) and never a drop target (_wouldReorderColumn()); a drop-before on a trailing command normalizes to "after the last data column".reorderColumn()refuses (false) when either end is a command column. - Freezing: the leading commands join the sticky run exactly when the selection column does, that is while a data column is frozen (
_isStickyWithFrozen()incolumns-freeze.feature.js), and scroll otherwise. Trailing commands always scroll with the data.
4. Rendering
4.1 Icon parsing
_parseCommandIcon(markup) parses the markup once per distinct string (cached
in _commandIconCache) with DOMParser as image/svg+xml:
- An
<svg>withoutxmlns(the usual hand-written form, and what a<template>'sinnerHTMLserializes to) gets the SVG namespace added first, since it would otherwise parse into no namespace and not render. - A parse error, or a root that isn't an SVG
<svg>, is a validation violation. <script>and<foreignObject>elements,on*attributes andjavascript:URLs inhref/xlink:hrefare removed, with one console warning. The markup is host-authored, but a copied icon set is the realistic risk.- The root gets
aria-hidden="true"andfocusable="false", and is adopted into the document; each pooled cell gets a clone.
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:
- Once per pooled cell, a
<button type="button" class="vn-grid-command">holding a clone of the parsed icon or the resolved text, withtitleandaria-labelset to the tooltip anddata-vn-grid-command-columnto the column's normalized key. The cell getsvn-grid-command-celland losesvn-grid-numeric/vn-grid-truncate-ellipsis; search highlighting never runs on it. - Every pass, only
button.dataset.vnGridRowIndex = dataIndex. The click resolves the live row from it, never from a closure. - Caption rows, aggregate footer rows and skeleton rows render an empty cell (their own populate paths clear every cell); a command column never hosts the caption toggle.
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:
_activateCommandButton(button)reads the column from the cell'scellIndexand the row fromdisplayRows[button.dataset.vnGridRowIndex], then callsonCommand({ command, columnKey, row, rowIndex, rowKey }).rowKeyis the selection key (_resolveRowKey).- 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). - 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
tests/node/command-column.test.js: validation rules (strict and not), stamping, formatting callbacks, placement throughsetColumns(), a saved order and the freeze regrouping, the persisted order, the hide / freeze / reorder / auto-fit / grouping refusals, and_activateCommandButton()'s detail.tests/playwright/command-column.spec.js(fixturetests/playwright/fixtures/command-column-grid.html): rendering and header semantics, the event after a sort, Enter/Space with focus kept, Tab order, no selection or double-click side effects, the header menu and drag/drop refusals, freezing with a horizontal scroll, grouping, Excel export, icon parsing and stripping, and persistence across reloads.tests/playwright/northwind-customers-js-master-detail.spec.js: the lens command opens the product panel and Escape returns focus to it; a double-click on the lens leaves the panel open.