@human-kit/ui 1.0.0-beta.2 → 1.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { computePosition, flip, shift, offset as offsetMiddleware, size, autoUpdate } from '@floating-ui/dom';
1
+ import { computePosition, flip, shift, limitShift, offset as offsetMiddleware, size, autoUpdate } from '@floating-ui/dom';
2
2
  /** Viewport gutter applied when no `collisionPadding` is given. */
3
3
  const DEFAULT_COLLISION_PADDING = 8;
4
4
  /**
@@ -108,7 +108,25 @@ export function floating(floatingElement, options) {
108
108
  return [
109
109
  offsetMiddleware(offset),
110
110
  ...(shouldFlip ? [flip({ boundary: boundaryElement || undefined, padding })] : []),
111
- shift({ boundary: boundaryElement || undefined, padding }),
111
+ // `shift`'s main axis is the one running ALONG the placement edge, and its
112
+ // cross axis is off by default. For `top`/`bottom` that main axis is x, so a
113
+ // dropdown too wide for the viewport is pulled back into view. For
114
+ // `left`/`right` it is y — nothing pulls it back horizontally, so a panel
115
+ // that fits on neither side stays wherever the placement put it, which for
116
+ // `left-*` means off the left edge entirely.
117
+ //
118
+ // Submenus are exactly that case: they open `right-start` and flip to
119
+ // `left-start`, and on a narrow viewport neither side has room. Enabled only
120
+ // for the horizontal placements, because on `top`/`bottom` the cross axis is
121
+ // vertical and shifting there would slide a dropdown over its own trigger —
122
+ // which `flip` already handles better. `limitShift` keeps the panel from
123
+ // sliding so far that it detaches from its anchor.
124
+ shift((state) => ({
125
+ boundary: boundaryElement || undefined,
126
+ padding,
127
+ crossAxis: state.placement.startsWith('left') || state.placement.startsWith('right'),
128
+ limiter: limitShift()
129
+ })),
112
130
  size({
113
131
  padding,
114
132
  apply({ rects, availableWidth, availableHeight, elements, placement }) {
@@ -88,6 +88,7 @@ All public Table part prop types are exported from the table barrel, including `
88
88
  ## Usage guidelines
89
89
 
90
90
  - Use `Table.Root` as the stateful container for focus, selection, and sorting state.
91
+ - Use `keyboardNavigation` to choose how far the roving tab stop reaches into the body: `'grid'` (default) makes every body cell a focus target, `'row'` makes the row the only one, and `'none'` leaves the body inert. Cell navigation costs a focus registration plus focus-derived state per cell, which a virtualized table pays again on every row it mounts; drop to `'row'` when the body is a list rather than a working surface, and to `'none'` only when nothing in it is actionable. The header keeps its own navigation in every mode.
91
92
  - Use `selectionBehavior="toggle"` to allow deselecting an already selected row, or `selectionBehavior="replace"` to keep selected rows selected when pressed again.
92
93
  - `Table.Column` is a logical-only wrapper for column metadata; it does not render DOM by itself and should wrap a single `Table.ColumnHeaderCell`.
93
94
  - Wrap each header cell in `Table.Column` so the table can register stable column metadata.
@@ -132,6 +133,7 @@ All public Table part prop types are exported from the table barrel, including `
132
133
 
133
134
  - `Table.Root` renders an interactive `grid` over native table markup.
134
135
  - Keyboard navigation uses roving `tabindex` across header and body cells.
136
+ - Under `keyboardNavigation="row"` the body's roving tab stop sits on the rows: arrows walk rows, `Enter` presses one and `Space` toggles its selection. Focusable content inside body cells becomes a regular tab stop instead, since no roving focus reaches it there.
135
137
  - Body rows can also become the active focus target when horizontal navigation moves past the start or end of a row, and repeated left/right navigation loops back into the opposite edge cell.
136
138
  - `Table.Checkbox` can receive DOM focus directly while still participating in the table's roving-focus grid.
137
139
  - First-column body cells become `rowheader` when their associated column has `rowHeader`.
@@ -26,6 +26,10 @@
26
26
  void row.cellOrderEpoch;
27
27
  return row.getCellIndex(key);
28
28
  });
29
+ // Only `'grid'` navigation makes a body cell a focus target. In the other
30
+ // modes it skips the focus registry and the focus-derived state below, which
31
+ // a virtualizer would otherwise pay per cell on every scroll block.
32
+ const isFocusTarget = $derived(row.section === 'body' && table.keyboardNavigation === 'grid');
29
33
 
30
34
  function syncCellRegistration() {
31
35
  if (row.section !== 'body') return;
@@ -54,12 +58,15 @@
54
58
  });
55
59
 
56
60
  $effect(() => {
61
+ if (!isFocusTarget) return;
57
62
  syncCellRegistration();
58
63
  });
59
64
 
60
65
  onDestroy(() => {
61
66
  row.unregisterCellToken(key);
62
- if (row.section === 'body') {
67
+ // Mirrors `isFocusTarget` without reading it: a derived is not safe to
68
+ // read while the component tears down.
69
+ if (row.section === 'body' && table.keyboardNavigation === 'grid') {
63
70
  table.unregisterCell(key);
64
71
  }
65
72
  });
@@ -88,8 +95,8 @@
88
95
  });
89
96
  // Focus/selection/disabled state is fine-grained `$state` in the context —
90
97
  // the reads below track it directly, no version counters needed.
91
- const isFocused = $derived(row.section === 'body' ? table.isCellFocused(key) : false);
92
- const isFocusVisible = $derived(row.section === 'body' ? isFocused && table.focusVisible : false);
98
+ const isFocused = $derived(isFocusTarget ? table.isCellFocused(key) : false);
99
+ const isFocusVisible = $derived(isFocusTarget ? isFocused && table.focusVisible : false);
93
100
  const isRowSelected = $derived(row.section === 'body' ? row.rowState.isSelected : false);
94
101
  const isRowDisabled = $derived(
95
102
  row.section === 'body'
@@ -110,7 +117,7 @@
110
117
  );
111
118
  const isCellFocusable = $derived(row.section !== 'body' || !isRowDisabled);
112
119
  const cellTabIndex = $derived.by(() => {
113
- if (row.section !== 'body') return undefined;
120
+ if (!isFocusTarget) return undefined;
114
121
  if (isColumnHidden) return undefined;
115
122
  if (!isCellFocusable) return undefined;
116
123
  if (focusDelegate) return undefined;
@@ -124,10 +131,22 @@
124
131
  table.setFocusVisible(shouldShowFocusVisible(element ?? null));
125
132
  }
126
133
 
134
+ // A pointer press moves focus to whatever the current mode can focus, so the
135
+ // keyboard carries on from where the pointer left off.
136
+ function focusPressTarget() {
137
+ if (isFocusTarget) {
138
+ table.focusCellByKey(key);
139
+ return;
140
+ }
141
+ if (table.keyboardNavigation === 'row') {
142
+ table.focusRowByToken(row.rowToken, 'start');
143
+ }
144
+ }
145
+
127
146
  function handleClick(event: MouseEvent) {
128
147
  if (row.section !== 'body') return;
129
148
  if (isRowDisabled) return;
130
- table.focusCellByKey(key);
149
+ focusPressTarget();
131
150
  table.pressRow(
132
151
  row.rowId as TableSelectionKey | undefined,
133
152
  'pointer',
@@ -144,7 +163,7 @@
144
163
  function handleDoubleClick() {
145
164
  if (row.section !== 'body') return;
146
165
  if (isRowDisabled) return;
147
- table.focusCellByKey(key);
166
+ focusPressTarget();
148
167
  table.pressRow(
149
168
  row.rowId as TableSelectionKey | undefined,
150
169
  'pointer-double',
@@ -226,11 +245,11 @@
226
245
  style:inset-inline-end={pinState?.side === 'right' ? `${pinState.offset}px` : undefined}
227
246
  style:z-index={pinState ? 1 : undefined}
228
247
  style:display={isColumnHidden ? 'none' : 'table-cell'}
229
- onfocus={row.section === 'body' ? handleFocus : undefined}
248
+ onfocus={isFocusTarget ? handleFocus : undefined}
230
249
  onclick={row.section === 'body' ? handleClick : undefined}
231
250
  ondblclick={row.section === 'body' ? handleDoubleClick : undefined}
232
251
  onmousedown={row.section === 'body' ? handleMouseDown : undefined}
233
- onkeydown={row.section === 'body' ? handleKeyDown : undefined}
252
+ onkeydown={isFocusTarget ? handleKeyDown : undefined}
234
253
  {...restProps}
235
254
  >
236
255
  {#if children}
@@ -75,6 +75,9 @@
75
75
 
76
76
  const tabIndex = $derived.by(() => {
77
77
  if (!isVisible || isDisabled) return undefined;
78
+ // Without cell navigation no roving tab stop ever reaches a body cell, so
79
+ // the checkbox has to be a tab stop of its own to stay operable.
80
+ if (section.section === 'body' && table.keyboardNavigation !== 'grid') return 0;
78
81
  return table.isCellTabStop(cell.cellKey) ? 0 : -1;
79
82
  });
80
83
 
@@ -14,7 +14,7 @@ export { default as TableCheckbox } from './checkbox/table-checkbox.svelte';
14
14
  export { default as TableCheckboxIndicator } from './checkbox-indicator/table-checkbox-indicator.svelte';
15
15
  export { default as TableCell } from './cell/table-cell.svelte';
16
16
  export { default as TableInteractiveCell } from './interactive-cell/table-interactive-cell.svelte';
17
- export { createTableContext, getTableContext, setTableContext, useTableContext, getTableSectionContext, setTableSectionContext, useTableSectionContext, getTableRowContext, setTableRowContext, useTableRowContext, getTableColumnContext, setTableColumnContext, useTableColumnContext, getTableCellContext, setTableCellContext, useTableCellContext, type TableContext, type TableRowItem, type TableRowFocusEdge, type TableDisabledBehavior, type TableSelectionBehavior, type TableSelectionCheckboxState, type TableSelectionKey, type TableSelectionMode, type TableRowActionHandler, type TableSortDirection, type TableSortDescriptor, type TableColumnWidth, type TableColumnPin, type TableColumnPinState, type TableGridCoord, type TableSectionKind, type TableSectionContext, type TableRowContext, type TableColumnContext, type TableCellContext, type CreateTableContextOptions, DEFAULT_TABLE_COLUMN_MIN_WIDTH } from './root/context.svelte';
17
+ export { createTableContext, getTableContext, setTableContext, useTableContext, getTableSectionContext, setTableSectionContext, useTableSectionContext, getTableRowContext, setTableRowContext, useTableRowContext, getTableColumnContext, setTableColumnContext, useTableColumnContext, getTableCellContext, setTableCellContext, useTableCellContext, type TableContext, type TableRowItem, type TableRowFocusEdge, type TableDisabledBehavior, type TableKeyboardNavigation, type TableSelectionBehavior, type TableSelectionCheckboxState, type TableSelectionKey, type TableSelectionMode, type TableRowActionHandler, type TableSortDirection, type TableSortDescriptor, type TableColumnWidth, type TableColumnPin, type TableColumnPinState, type TableGridCoord, type TableSectionKind, type TableSectionContext, type TableRowContext, type TableColumnContext, type TableCellContext, type CreateTableContextOptions, DEFAULT_TABLE_COLUMN_MIN_WIDTH } from './root/context.svelte';
18
18
  export { getTableSsrMinWidth, type TableColumnWidthSpec, type GetTableSsrMinWidthOptions } from './utils/ssr-min-width.js';
19
19
  import * as TableParts from './index.parts.js';
20
20
  export default TableParts;
@@ -26,6 +26,9 @@
26
26
  void row.cellOrderEpoch;
27
27
  return row.getCellIndex(key);
28
28
  });
29
+ // See `Table.Cell`: outside `'grid'` navigation a body cell is not a focus
30
+ // target, so it skips the focus registry and the focus-derived state below.
31
+ const isFocusTarget = $derived(row.section === 'body' && table.keyboardNavigation === 'grid');
29
32
 
30
33
  function syncCellRegistration() {
31
34
  if (row.section !== 'body') return;
@@ -54,12 +57,15 @@
54
57
  });
55
58
 
56
59
  $effect(() => {
60
+ if (!isFocusTarget) return;
57
61
  syncCellRegistration();
58
62
  });
59
63
 
60
64
  onDestroy(() => {
61
65
  row.unregisterCellToken(key);
62
- if (row.section === 'body') {
66
+ // Mirrors `isFocusTarget` without reading it: a derived is not safe to
67
+ // read while the component tears down.
68
+ if (row.section === 'body' && table.keyboardNavigation === 'grid') {
63
69
  table.unregisterCell(key);
64
70
  }
65
71
  });
@@ -88,8 +94,8 @@
88
94
  });
89
95
  // Focus/selection/disabled state is fine-grained `$state` in the context —
90
96
  // the reads below track it directly, no version counters needed.
91
- const isFocused = $derived(row.section === 'body' ? table.isCellFocused(key) : false);
92
- const isFocusVisible = $derived(row.section === 'body' ? isFocused && table.focusVisible : false);
97
+ const isFocused = $derived(isFocusTarget ? table.isCellFocused(key) : false);
98
+ const isFocusVisible = $derived(isFocusTarget ? isFocused && table.focusVisible : false);
93
99
  const isRowSelected = $derived(row.section === 'body' ? row.rowState.isSelected : false);
94
100
  const isRowDisabled = $derived(
95
101
  row.section === 'body'
@@ -110,7 +116,7 @@
110
116
  );
111
117
  const isCellFocusable = $derived(row.section !== 'body' || !isRowDisabled);
112
118
  const cellTabIndex = $derived.by(() => {
113
- if (row.section !== 'body') return undefined;
119
+ if (!isFocusTarget) return undefined;
114
120
  if (isColumnHidden) return undefined;
115
121
  if (!isCellFocusable) return undefined;
116
122
  if (focusDelegate) return undefined;
@@ -128,11 +134,23 @@
128
134
  table.setFocusVisible(shouldShowFocusVisible(element ?? null));
129
135
  }
130
136
 
137
+ // A pointer press moves focus to whatever the current mode can focus, so the
138
+ // keyboard carries on from where the pointer left off.
139
+ function focusPressTarget() {
140
+ if (isFocusTarget) {
141
+ table.focusCellByKey(key);
142
+ return;
143
+ }
144
+ if (table.keyboardNavigation === 'row') {
145
+ table.focusRowByToken(row.rowToken, 'start');
146
+ }
147
+ }
148
+
131
149
  function handleClick(event: MouseEvent) {
132
150
  if (!isCellTarget(event)) return;
133
151
  if (row.section !== 'body') return;
134
152
  if (isRowDisabled) return;
135
- table.focusCellByKey(key);
153
+ focusPressTarget();
136
154
  table.pressRow(
137
155
  row.rowId as TableSelectionKey | undefined,
138
156
  'pointer',
@@ -150,7 +168,7 @@
150
168
  if (!isCellTarget(event)) return;
151
169
  if (row.section !== 'body') return;
152
170
  if (isRowDisabled) return;
153
- table.focusCellByKey(key);
171
+ focusPressTarget();
154
172
  table.pressRow(
155
173
  row.rowId as TableSelectionKey | undefined,
156
174
  'pointer-double',
@@ -235,11 +253,11 @@
235
253
  style:inset-inline-end={pinState?.side === 'right' ? `${pinState.offset}px` : undefined}
236
254
  style:z-index={pinState ? 1 : undefined}
237
255
  style:display={isColumnHidden ? 'none' : 'table-cell'}
238
- onfocus={row.section === 'body' ? handleFocus : undefined}
256
+ onfocus={isFocusTarget ? handleFocus : undefined}
239
257
  onclick={row.section === 'body' ? handleClick : undefined}
240
258
  ondblclick={row.section === 'body' ? handleDoubleClick : undefined}
241
259
  onmousedown={row.section === 'body' ? handleMouseDown : undefined}
242
- onkeydown={row.section === 'body' ? handleKeyDown : undefined}
260
+ onkeydown={isFocusTarget ? handleKeyDown : undefined}
243
261
  {...restProps}
244
262
  >
245
263
  {#if children}
@@ -3,6 +3,20 @@ export type TableRowItem = Record<string, unknown> & {
3
3
  id: TableSelectionKey;
4
4
  };
5
5
  export type TableSelectionMode = 'none' | 'single' | 'multiple';
6
+ /**
7
+ * How far the roving tab stop reaches into the body.
8
+ *
9
+ * `'grid'` is the full ARIA grid pattern: every body cell is a focus target,
10
+ * which costs a registration plus focus-derived state per cell — with a
11
+ * virtualizer that is paid again on every scroll block. `'row'` keeps the body
12
+ * reachable (arrows walk rows, Enter/Space press one) at one focus target per
13
+ * row instead of one per cell. `'none'` drops body focus entirely; use it only
14
+ * when nothing in the body is actionable.
15
+ *
16
+ * The header keeps its own cell navigation in every mode — it is a single row,
17
+ * so it costs nothing, and it is where sorting and resizing live.
18
+ */
19
+ export type TableKeyboardNavigation = 'grid' | 'row' | 'none';
6
20
  export type TableSelectionBehavior = 'toggle' | 'replace';
7
21
  export type TableDisabledBehavior = 'selection' | 'all';
8
22
  export type TableSortDirection = 'ascending' | 'descending';
@@ -84,6 +98,7 @@ type TableCellRegistration = {
84
98
  };
85
99
  export type CreateTableContextOptions = {
86
100
  selectionMode?: TableSelectionMode;
101
+ keyboardNavigation?: TableKeyboardNavigation;
87
102
  selectionBehavior?: TableSelectionBehavior;
88
103
  disabledBehavior?: TableDisabledBehavior;
89
104
  disallowEmptySelection?: boolean;
@@ -126,6 +141,7 @@ export type TableContext = {
126
141
  widthEpoch: number;
127
142
  createInstanceToken: (prefix: string) => string;
128
143
  selectionMode: TableSelectionMode;
144
+ keyboardNavigation: TableKeyboardNavigation;
129
145
  selectionBehavior: TableSelectionBehavior;
130
146
  disabledBehavior: TableDisabledBehavior;
131
147
  disallowEmptySelection: boolean;
@@ -213,6 +229,7 @@ export type TableContext = {
213
229
  deselectAllRows: () => void;
214
230
  setSelection: (keys: Iterable<TableSelectionKey>) => void;
215
231
  setSelectionMode: (mode: TableSelectionMode) => void;
232
+ setKeyboardNavigation: (navigation: TableKeyboardNavigation) => void;
216
233
  setSelectionBehavior: (behavior: TableSelectionBehavior) => void;
217
234
  setDisabledBehavior: (behavior: TableDisabledBehavior) => void;
218
235
  setDisallowEmptySelection: (disallow: boolean) => void;
@@ -53,6 +53,7 @@ export function createTableContext(options = {}) {
53
53
  // mutated in place), so a plain `Set` inside `$state` is enough; the
54
54
  // in-place-mutated collections use SvelteSet/SvelteMap instead.
55
55
  let selectionMode = $state(options.selectionMode ?? 'none');
56
+ let keyboardNavigation = $state(options.keyboardNavigation ?? 'grid');
56
57
  let selectionBehavior = $state(options.selectionBehavior ?? 'toggle');
57
58
  let disabledBehavior = $state(options.disabledBehavior ?? 'all');
58
59
  let disallowEmptySelection = $state(options.disallowEmptySelection ?? false);
@@ -1579,7 +1580,16 @@ export function createTableContext(options = {}) {
1579
1580
  return getDefaultFocusKey() === key;
1580
1581
  }
1581
1582
  function isRowTabStop(token) {
1582
- return focusedRowTarget?.rowToken === token;
1583
+ if (focusedRowTarget)
1584
+ return focusedRowTarget.rowToken === token;
1585
+ if (keyboardNavigation !== 'row')
1586
+ return false;
1587
+ // Row navigation with nothing focused yet: the body needs its own entry
1588
+ // point, but only when the header does not already provide the grid's
1589
+ // single tab stop.
1590
+ if (focusedCellKey || getDefaultFocusKey())
1591
+ return false;
1592
+ return getFocusableBodyRowToken('start') === token;
1583
1593
  }
1584
1594
  function getGlobalRowIndex(rowToken) {
1585
1595
  const orderedHeaderRows = getOrderedRowTokens('header');
@@ -1917,12 +1927,81 @@ export function createTableContext(options = {}) {
1917
1927
  const target = cellElement?.closest('table') ?? cellElement;
1918
1928
  return isRtl(target);
1919
1929
  }
1930
+ function syncSelectionAfterFocusMove(direction, interaction, previousFocusedRowId) {
1931
+ if (direction !== 'up' && direction !== 'down')
1932
+ return;
1933
+ if (selectionMode === 'none')
1934
+ return;
1935
+ if (interaction.ctrlKey || interaction.metaKey || interaction.altKey)
1936
+ return;
1937
+ const targetRowId = getFocusedRowId();
1938
+ if (targetRowId === null)
1939
+ return;
1940
+ if (interaction.shiftKey) {
1941
+ extendSelectionToRow(targetRowId, selectionAnchorKey ?? previousFocusedRowId);
1942
+ return;
1943
+ }
1944
+ if (selectionBehavior !== 'replace')
1945
+ return;
1946
+ replaceSelectionWithRow(targetRowId);
1947
+ }
1948
+ function getOrderedFocusableBodyRowTokens() {
1949
+ return getOrderedRowTokens('body').filter((token) => {
1950
+ const row = rows.get(token);
1951
+ return Boolean(row) && !isRowDisabled(row?.id, row?.disabled);
1952
+ });
1953
+ }
1954
+ /**
1955
+ * Focus movement for `keyboardNavigation: 'row'`, where body cells never
1956
+ * register and the row itself is the only focus target in the body. Returns
1957
+ * `false` when the move belongs to the header, which keeps cell navigation.
1958
+ */
1959
+ function moveRowFocus(direction, interaction) {
1960
+ const tokens = getOrderedFocusableBodyRowTokens();
1961
+ if (tokens.length === 0)
1962
+ return false;
1963
+ if (!focusedRowTarget) {
1964
+ // Coming from the header (or from nothing at all): only downwards
1965
+ // enters the body, so anything else stays with the cell path.
1966
+ if (direction !== 'down')
1967
+ return false;
1968
+ const previousFocusedRowId = getFocusedRowId();
1969
+ focusRowByToken(tokens[0], 'start');
1970
+ syncSelectionAfterFocusMove(direction, interaction, previousFocusedRowId);
1971
+ return true;
1972
+ }
1973
+ // A row spans every column, so there is nothing to move to sideways.
1974
+ if (direction === 'left' || direction === 'right')
1975
+ return true;
1976
+ const previousFocusedRowId = getFocusedRowId();
1977
+ const currentIndex = tokens.indexOf(focusedRowTarget.rowToken);
1978
+ const targetToken = tokens[direction === 'up' ? currentIndex - 1 : currentIndex + 1];
1979
+ if (targetToken) {
1980
+ focusRowByToken(targetToken, focusedRowTarget.edge);
1981
+ syncSelectionAfterFocusMove(direction, interaction, previousFocusedRowId);
1982
+ return true;
1983
+ }
1984
+ // Past the first row, focus leaves the body upwards into the header.
1985
+ if (direction === 'up' && currentIndex === 0) {
1986
+ const headerRowIndexes = getOrderedRowTokens('header')
1987
+ .map((token) => getGlobalRowIndex(token))
1988
+ .filter((index) => index >= 0)
1989
+ .sort((left, right) => left - right);
1990
+ const lastHeaderRowIndex = headerRowIndexes.at(-1);
1991
+ if (lastHeaderRowIndex !== undefined) {
1992
+ focusCellByKey(getClosestCellKey(lastHeaderRowIndex, 0));
1993
+ }
1994
+ }
1995
+ return true;
1996
+ }
1920
1997
  function moveFocus(direction, interaction = {}) {
1921
1998
  // ArrowLeft/ArrowRight are physical: in RTL layouts the visually next
1922
1999
  // cell is the previous one in DOM order, so invert before navigating.
1923
2000
  if ((direction === 'left' || direction === 'right') && isRtlLayout()) {
1924
2001
  direction = direction === 'left' ? 'right' : 'left';
1925
2002
  }
2003
+ if (keyboardNavigation === 'row' && moveRowFocus(direction, interaction))
2004
+ return;
1926
2005
  const rowMap = getRowsWithCells();
1927
2006
  const currentCoord = getFocusedCoord();
1928
2007
  const rowIndexes = Array.from(rowMap.keys()).sort((a, b) => a - b);
@@ -1930,22 +2009,7 @@ export function createTableContext(options = {}) {
1930
2009
  return;
1931
2010
  const previousFocusedRowId = getFocusedRowId();
1932
2011
  function maybeSyncSelectionAfterFocus() {
1933
- if (direction !== 'up' && direction !== 'down')
1934
- return;
1935
- if (selectionMode === 'none')
1936
- return;
1937
- if (interaction.ctrlKey || interaction.metaKey || interaction.altKey)
1938
- return;
1939
- const targetRowId = getFocusedRowId();
1940
- if (targetRowId === null)
1941
- return;
1942
- if (interaction.shiftKey) {
1943
- extendSelectionToRow(targetRowId, selectionAnchorKey ?? previousFocusedRowId);
1944
- return;
1945
- }
1946
- if (selectionBehavior !== 'replace')
1947
- return;
1948
- replaceSelectionWithRow(targetRowId);
2012
+ syncSelectionAfterFocusMove(direction, interaction, previousFocusedRowId);
1949
2013
  }
1950
2014
  if (!currentCoord) {
1951
2015
  if (focusedRowTarget) {
@@ -2052,6 +2116,12 @@ export function createTableContext(options = {}) {
2052
2116
  focusCellByKey(rowCells[rowCells.length - 1]?.key ?? null);
2053
2117
  }
2054
2118
  function moveToGridStart() {
2119
+ // With row navigation the grid's edges are its first and last body rows;
2120
+ // body cells are not focus targets.
2121
+ if (keyboardNavigation === 'row' && focusedRowTarget) {
2122
+ moveToBodyRowStart();
2123
+ return;
2124
+ }
2055
2125
  const rowIndexes = Array.from(getRowsWithCells().keys()).sort((a, b) => a - b);
2056
2126
  if (rowIndexes.length === 0)
2057
2127
  return;
@@ -2070,6 +2140,10 @@ export function createTableContext(options = {}) {
2070
2140
  focusRowByToken(targetToken, focusedRowTarget?.edge ?? 'end');
2071
2141
  }
2072
2142
  function moveToGridEnd() {
2143
+ if (keyboardNavigation === 'row' && focusedRowTarget) {
2144
+ moveToBodyRowEnd();
2145
+ return;
2146
+ }
2073
2147
  const rowMap = getRowsWithCells();
2074
2148
  const bodyGlobalRows = getOrderedRowTokens('body')
2075
2149
  .map((token) => getGlobalRowIndex(token))
@@ -2142,6 +2216,24 @@ export function createTableContext(options = {}) {
2142
2216
  }
2143
2217
  notifySelection();
2144
2218
  }
2219
+ function setKeyboardNavigation(navigation) {
2220
+ if (keyboardNavigation === navigation)
2221
+ return;
2222
+ keyboardNavigation = navigation;
2223
+ if (navigation === 'grid') {
2224
+ // Body cells re-register on their own: their registration effect
2225
+ // reads this state, so it re-runs for every mounted cell.
2226
+ return;
2227
+ }
2228
+ for (const cell of Array.from(cells.values())) {
2229
+ if (cell.section === 'body') {
2230
+ unregisterCell(cell.key);
2231
+ }
2232
+ }
2233
+ if (navigation === 'none') {
2234
+ focusedRowTarget = null;
2235
+ }
2236
+ }
2145
2237
  function setSelectionBehavior(behavior) {
2146
2238
  selectionBehavior = behavior;
2147
2239
  notifySelection();
@@ -2238,6 +2330,9 @@ export function createTableContext(options = {}) {
2238
2330
  get selectionMode() {
2239
2331
  return selectionMode;
2240
2332
  },
2333
+ get keyboardNavigation() {
2334
+ return keyboardNavigation;
2335
+ },
2241
2336
  get selectionBehavior() {
2242
2337
  return selectionBehavior;
2243
2338
  },
@@ -2341,6 +2436,7 @@ export function createTableContext(options = {}) {
2341
2436
  deselectAllRows: asCommand(deselectAllRows),
2342
2437
  setSelection: asCommand(setSelection),
2343
2438
  setSelectionMode: asCommand(setSelectionMode),
2439
+ setKeyboardNavigation: asCommand(setKeyboardNavigation),
2344
2440
  setSelectionBehavior: asCommand(setSelectionBehavior),
2345
2441
  setDisabledBehavior: asCommand(setDisabledBehavior),
2346
2442
  setDisallowEmptySelection: asCommand(setDisallowEmptySelection),
@@ -33,6 +33,7 @@
33
33
 
34
34
  let {
35
35
  selectionMode = 'none',
36
+ keyboardNavigation = 'grid',
36
37
  selectionBehavior = 'toggle',
37
38
  disabledBehavior = 'all',
38
39
  disallowEmptySelection = false,
@@ -83,6 +84,7 @@
83
84
  const ctx = setTableContext(
84
85
  createTableContext({
85
86
  selectionMode: (() => selectionMode)(),
87
+ keyboardNavigation: (() => keyboardNavigation)(),
86
88
  selectionBehavior: (() => selectionBehavior)(),
87
89
  disabledBehavior: (() => disabledBehavior)(),
88
90
  disallowEmptySelection: (() => disallowEmptySelection)(),
@@ -374,6 +376,10 @@
374
376
  ctx.setSelectionMode(selectionMode);
375
377
  });
376
378
 
379
+ $effect(() => {
380
+ ctx.setKeyboardNavigation(keyboardNavigation);
381
+ });
382
+
377
383
  $effect(() => {
378
384
  if (hiddenColumns !== undefined) {
379
385
  const nextHiddenColumns = parseHiddenColumns(hiddenColumns);
@@ -547,6 +553,7 @@
547
553
  aria-rowcount={ariaRowCount}
548
554
  aria-multiselectable={selectionMode === 'multiple' ? true : undefined}
549
555
  data-selection-mode={selectionMode}
556
+ data-keyboard-navigation={keyboardNavigation}
550
557
  data-selection-behavior={selectionBehavior}
551
558
  data-disabled-behavior={disabledBehavior}
552
559
  data-focus-within={focusWithin || undefined}
@@ -51,6 +51,7 @@
51
51
 
52
52
  function handleKeyDown(event: KeyboardEvent) {
53
53
  if (section.section !== 'body') return;
54
+ if (table.keyboardNavigation === 'none') return;
54
55
  if (event.target !== rowElement) return;
55
56
  handleTableBodyKeydown({
56
57
  event,
@@ -275,6 +276,11 @@
275
276
  const isAriaDisabled = $derived(
276
277
  section.section === 'body' ? table.isRowDisabled(id, disabled) : disabled
277
278
  );
279
+ // The row is a focus target under `'grid'` (as the row-edge stop) and under
280
+ // `'row'` (as the only stop in the body); `'none'` leaves the body inert.
281
+ const isRowFocusTarget = $derived(
282
+ section.section === 'body' && !isAriaDisabled && table.keyboardNavigation !== 'none'
283
+ );
278
284
  const isSelectionDisabled = $derived(
279
285
  section.section === 'body' ? table.isRowSelectionDisabled(id, disabled) : disabled
280
286
  );
@@ -286,13 +292,7 @@
286
292
  <tr
287
293
  bind:this={rowElement}
288
294
  class={className}
289
- tabindex={section.section === 'body'
290
- ? !isAriaDisabled
291
- ? table.isRowTabStop(rowToken)
292
- ? 0
293
- : -1
294
- : undefined
295
- : undefined}
295
+ tabindex={isRowFocusTarget ? (table.isRowTabStop(rowToken) ? 0 : -1) : undefined}
296
296
  data-focused={isFocused ? 'true' : undefined}
297
297
  data-focus-visible={isFocusVisible ? 'true' : undefined}
298
298
  data-focus-within={isFocusWithin ? 'true' : undefined}
@@ -1,6 +1,6 @@
1
1
  import type { Component, Snippet } from 'svelte';
2
2
  import type { HTMLAttributes, HTMLButtonAttributes } from 'svelte/elements';
3
- import type { TableColumnPin, TableColumnWidth, TableContext, TableDisabledBehavior, TableRowActionHandler, TableRowItem, TableSelectionBehavior, TableSelectionKey, TableSelectionMode, TableSortDirection, TableSortDescriptor } from './root/context.svelte';
3
+ import type { TableColumnPin, TableColumnWidth, TableContext, TableDisabledBehavior, TableKeyboardNavigation, TableRowActionHandler, TableRowItem, TableSelectionBehavior, TableSelectionKey, TableSelectionMode, TableSortDirection, TableSortDescriptor } from './root/context.svelte';
4
4
  export type { TableColumnPin } from './root/context.svelte';
5
5
  export type TableColumnProps = {
6
6
  id: string;
@@ -89,6 +89,17 @@ export type TableFooterProps = Omit<HTMLAttributes<HTMLTableSectionElement>, 'ch
89
89
  };
90
90
  export type TableRootProps = Omit<HTMLAttributes<HTMLTableElement>, 'children'> & {
91
91
  selectionMode?: TableSelectionMode;
92
+ /**
93
+ * How far the roving tab stop reaches into the body, defaulting to `'grid'`.
94
+ *
95
+ * Cell navigation is not free: every body cell registers itself and derives
96
+ * its own focus state, and a virtualized table pays that again on every
97
+ * scroll block. `'row'` keeps the body keyboard-reachable at one focus
98
+ * target per row; `'none'` drops body focus altogether and should only be
99
+ * used when nothing in the body is actionable. The header keeps its own
100
+ * navigation — and with it sorting and resizing — in every mode.
101
+ */
102
+ keyboardNavigation?: TableKeyboardNavigation;
92
103
  selectionBehavior?: TableSelectionBehavior;
93
104
  disabledBehavior?: TableDisabledBehavior;
94
105
  disallowEmptySelection?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@human-kit/ui",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.3",
4
4
  "description": "Accessible, typed UI components for Svelte 5 — shipped as native ESM with per-component subpath exports so bundlers only include what you import.",
5
5
  "license": "MIT",
6
6
  "author": "Agustin Delgado <agustindariodelgado@gmail.com>",