@cloudscape-design/components 3.0.390 → 3.0.391

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
1
  export var PACKAGE_SOURCE = "components";
2
- export var PACKAGE_VERSION = "3.0.0 (0c7d0313)";
2
+ export var PACKAGE_VERSION = "3.0.0 (6e510169)";
3
3
  export var THEME = "open-source-visual-refresh";
4
4
  export var ALWAYS_VISUAL_REFRESH = true;
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "PACKAGE_SOURCE": "components",
3
- "PACKAGE_VERSION": "3.0.0 (0c7d0313)",
3
+ "PACKAGE_VERSION": "3.0.0 (6e510169)",
4
4
  "THEME": "default",
5
5
  "ALWAYS_VISUAL_REFRESH": false
6
6
  }
@@ -1,3 +1,3 @@
1
1
  {
2
- "commit": "0c7d031375a78addf071d653b6c74207bcf800ac"
2
+ "commit": "6e510169eefdf754926b5cd102e62fc2b07e9438"
3
3
  }
package/package.json CHANGED
@@ -110,7 +110,7 @@
110
110
  "./internal/base-component/index.js",
111
111
  "./internal/base-component/styles.css.js"
112
112
  ],
113
- "version": "3.0.390",
113
+ "version": "3.0.391",
114
114
  "repository": {
115
115
  "type": "git",
116
116
  "url": "https://github.com/cloudscape-design/components.git"
@@ -1,4 +1,4 @@
1
- export { TableRole, GridNavigationProps, GridNavigationAPI } from './interfaces';
1
+ export { TableRole, GridNavigationProps } from './interfaces';
2
2
  export { getTableCellRoleProps, getTableColHeaderRoleProps, getTableHeaderRowRoleProps, getTableRoleProps, getTableRowRoleProps, getTableWrapperRoleProps, } from './table-role-helper';
3
3
  export { useGridNavigation } from './use-grid-navigation';
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"lib/default/","sources":["table/table-role/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEjF,OAAO,EACL,qBAAqB,EACrB,0BAA0B,EAC1B,0BAA0B,EAC1B,iBAAiB,EACjB,oBAAoB,EACpB,wBAAwB,GACzB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"lib/default/","sources":["table/table-role/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAE9D,OAAO,EACL,qBAAqB,EACrB,0BAA0B,EAC1B,0BAA0B,EAC1B,iBAAiB,EACjB,oBAAoB,EACpB,wBAAwB,GACzB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"lib/default/","sources":["table/table-role/index.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AAItC,OAAO,EACL,qBAAqB,EACrB,0BAA0B,EAC1B,0BAA0B,EAC1B,iBAAiB,EACjB,oBAAoB,EACpB,wBAAwB,GACzB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nexport { TableRole, GridNavigationProps, GridNavigationAPI } from './interfaces';\n\nexport {\n getTableCellRoleProps,\n getTableColHeaderRoleProps,\n getTableHeaderRowRoleProps,\n getTableRoleProps,\n getTableRowRoleProps,\n getTableWrapperRoleProps,\n} from './table-role-helper';\n\nexport { useGridNavigation } from './use-grid-navigation';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"lib/default/","sources":["table/table-role/index.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AAItC,OAAO,EACL,qBAAqB,EACrB,0BAA0B,EAC1B,0BAA0B,EAC1B,iBAAiB,EACjB,oBAAoB,EACpB,wBAAwB,GACzB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nexport { TableRole, GridNavigationProps } from './interfaces';\n\nexport {\n getTableCellRoleProps,\n getTableColHeaderRoleProps,\n getTableHeaderRowRoleProps,\n getTableRoleProps,\n getTableRowRoleProps,\n getTableWrapperRoleProps,\n} from './table-role-helper';\n\nexport { useGridNavigation } from './use-grid-navigation';\n"]}
@@ -1,10 +1,9 @@
1
1
  export type TableRole = 'table' | 'grid' | 'grid-default';
2
2
  export interface GridNavigationProps {
3
- tableRole: TableRole;
3
+ active: boolean;
4
4
  pageSize: number;
5
5
  getTable: () => null | HTMLTableElement;
6
- }
7
- export interface GridNavigationAPI {
6
+ isSuppressed?: (focusedElement: HTMLElement) => void;
8
7
  }
9
8
  export interface FocusedCell {
10
9
  rowIndex: number;
@@ -13,6 +12,5 @@ export interface FocusedCell {
13
12
  rowElement: HTMLTableRowElement;
14
13
  cellElement: HTMLTableCellElement;
15
14
  element: HTMLElement;
16
- dialog: boolean;
17
15
  }
18
16
  //# sourceMappingURL=interfaces.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"interfaces.d.ts","sourceRoot":"lib/default/","sources":["table/table-role/interfaces.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,SAAS,GAAG,OAAO,GAAG,MAAM,GAAG,cAAc,CAAC;AAE1D,MAAM,WAAW,mBAAmB;IAClC,SAAS,EAAE,SAAS,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,IAAI,GAAG,gBAAgB,CAAC;CACzC;AAGD,MAAM,WAAW,iBAAiB;CAAG;AAErC,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,mBAAmB,CAAC;IAChC,WAAW,EAAE,oBAAoB,CAAC;IAClC,OAAO,EAAE,WAAW,CAAC;IACrB,MAAM,EAAE,OAAO,CAAC;CACjB"}
1
+ {"version":3,"file":"interfaces.d.ts","sourceRoot":"lib/default/","sources":["table/table-role/interfaces.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,SAAS,GAAG,OAAO,GAAG,MAAM,GAAG,cAAc,CAAC;AAE1D,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,OAAO,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,IAAI,GAAG,gBAAgB,CAAC;IACxC,YAAY,CAAC,EAAE,CAAC,cAAc,EAAE,WAAW,KAAK,IAAI,CAAC;CACtD;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,mBAAmB,CAAC;IAChC,WAAW,EAAE,oBAAoB,CAAC;IAClC,OAAO,EAAE,WAAW,CAAC;CACtB"}
@@ -1 +1 @@
1
- {"version":3,"file":"interfaces.js","sourceRoot":"lib/default/","sources":["table/table-role/interfaces.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nexport type TableRole = 'table' | 'grid' | 'grid-default';\n\nexport interface GridNavigationProps {\n tableRole: TableRole;\n pageSize: number;\n getTable: () => null | HTMLTableElement;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-interface\nexport interface GridNavigationAPI {}\n\nexport interface FocusedCell {\n rowIndex: number;\n colIndex: number;\n elementIndex: number;\n rowElement: HTMLTableRowElement;\n cellElement: HTMLTableCellElement;\n element: HTMLElement;\n dialog: boolean;\n}\n"]}
1
+ {"version":3,"file":"interfaces.js","sourceRoot":"lib/default/","sources":["table/table-role/interfaces.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nexport type TableRole = 'table' | 'grid' | 'grid-default';\n\nexport interface GridNavigationProps {\n active: boolean;\n pageSize: number;\n getTable: () => null | HTMLTableElement;\n isSuppressed?: (focusedElement: HTMLElement) => void;\n}\n\nexport interface FocusedCell {\n rowIndex: number;\n colIndex: number;\n elementIndex: number;\n rowElement: HTMLTableRowElement;\n cellElement: HTMLTableCellElement;\n element: HTMLElement;\n}\n"]}
@@ -1,7 +1,10 @@
1
- import { GridNavigationAPI, GridNavigationProps } from './interfaces';
1
+ import { GridNavigationProps } from './interfaces';
2
2
  /**
3
- * Makes table with role="grid" navigable with keyboard commands.
3
+ * Makes table navigable with keyboard commands.
4
4
  * See https://www.w3.org/WAI/ARIA/apg/patterns/grid
5
+ *
6
+ * The hook attaches the GridNavigationHelper helper when active=true.
7
+ * See GridNavigationHelper for more details.
5
8
  */
6
- export declare function useGridNavigation({ tableRole, pageSize, getTable }: GridNavigationProps): GridNavigationAPI;
9
+ export declare function useGridNavigation({ active, pageSize, getTable, isSuppressed }: GridNavigationProps): void;
7
10
  //# sourceMappingURL=use-grid-navigation.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-grid-navigation.d.ts","sourceRoot":"lib/default/","sources":["table/table-role/use-grid-navigation.ts"],"names":[],"mappings":"AAKA,OAAO,EAAe,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAInF;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE,mBAAmB,GAAG,iBAAiB,CAuB3G"}
1
+ {"version":3,"file":"use-grid-navigation.d.ts","sourceRoot":"lib/default/","sources":["table/table-role/use-grid-navigation.ts"],"names":[],"mappings":"AAaA,OAAO,EAAe,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAKhE;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,mBAAmB,QAmBlG"}
@@ -1,53 +1,71 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { useEffect, useMemo } from 'react';
4
- import { findFocusinCell, moveFocusBy, restoreTableFocusables, updateTableFocusables } from './utils';
4
+ import { defaultIsSuppressed, findFocusinCell, moveFocusBy, muteElementFocusables, restoreElementFocusables, ensureSingleFocusable, getFirstFocusable, } from './utils';
5
5
  import { KeyCode } from '../../internal/keycode';
6
6
  import { nodeContains } from '@cloudscape-design/component-toolkit/dom';
7
+ import { useStableCallback } from '@cloudscape-design/component-toolkit/internal';
7
8
  /**
8
- * Makes table with role="grid" navigable with keyboard commands.
9
+ * Makes table navigable with keyboard commands.
9
10
  * See https://www.w3.org/WAI/ARIA/apg/patterns/grid
11
+ *
12
+ * The hook attaches the GridNavigationHelper helper when active=true.
13
+ * See GridNavigationHelper for more details.
10
14
  */
11
- export function useGridNavigation({ tableRole, pageSize, getTable }) {
12
- const model = useMemo(() => new GridNavigationModel(), []);
15
+ export function useGridNavigation({ active, pageSize, getTable, isSuppressed }) {
16
+ const gridNavigation = useMemo(() => new GridNavigationHelper(), []);
17
+ const getTableStable = useStableCallback(getTable);
18
+ const isSuppressedStable = useStableCallback((element) => { var _a; return (_a = isSuppressed === null || isSuppressed === void 0 ? void 0 : isSuppressed(element)) !== null && _a !== void 0 ? _a : false; });
13
19
  // Initialize the model with the table container assuming it is mounted synchronously and only once.
14
20
  useEffect(() => {
15
- if (tableRole === 'grid') {
16
- const table = getTable();
17
- table && model.init(table);
21
+ if (active) {
22
+ const table = getTableStable();
23
+ table && gridNavigation.init(table, isSuppressedStable);
18
24
  }
19
- return () => model.destroy();
20
- },
21
- // Assuming getTable is stable.
22
- // eslint-disable-next-line react-hooks/exhaustive-deps
23
- [model, tableRole]);
25
+ return () => gridNavigation.cleanup();
26
+ }, [active, gridNavigation, getTableStable, isSuppressedStable]);
24
27
  // Notify the model of the props change.
25
28
  useEffect(() => {
26
- model.update({ pageSize });
27
- }, [model, pageSize]);
28
- return {};
29
+ gridNavigation.update({ pageSize });
30
+ }, [gridNavigation, pageSize]);
29
31
  }
30
- class GridNavigationModel {
32
+ /**
33
+ * This helper encapsulates the grid navigation behaviors which are:
34
+ * 1. Responding to keyboard commands and moving the focus accordingly;
35
+ * 2. Muting table interactive elements for only one to be user-focusable at a time;
36
+ * 3. Suppressing the above behaviors when focusing an element inside a dialog or when instructed by the isSuppressed callback.
37
+ *
38
+ * All behaviors are attached upon initialization and are re-evaluated with every focusin, focusout, and keydown events,
39
+ * and also when a node removal inside the table is observed to ensure consistency at any given moment.
40
+ *
41
+ * When the navigation is suppressed the keyboard commands are no longer intercepted and all table interactive elements are made
42
+ * user-focusable to unblock the Tab navigation. The suppression should only be used for interactive elements inside the table that would
43
+ * otherwise conflict with the navigation. Once the interactive element is deactivated or lose focus the table navigation becomes active again.
44
+ */
45
+ class GridNavigationHelper {
31
46
  constructor() {
32
47
  // Props
33
48
  this._pageSize = 0;
34
49
  this._table = null;
50
+ this._isSuppressed = () => false;
35
51
  // State
36
52
  this.prevFocusedCell = null;
37
53
  this.focusedCell = null;
38
- this.cleanup = () => { };
39
54
  this.onFocusin = (event) => {
55
+ var _a;
40
56
  const cell = findFocusinCell(event);
41
57
  if (!cell) {
42
58
  return;
43
59
  }
44
- if (cell.element !== event.target) {
45
- cell.element.focus();
46
- return;
47
- }
48
60
  this.prevFocusedCell = cell;
49
61
  this.focusedCell = cell;
50
- updateTableFocusables(this.table, cell);
62
+ muteElementFocusables(this.table, this.isSuppressed(cell.element));
63
+ ensureSingleFocusable(this.table, cell);
64
+ // Focusing on cell is not eligible when it contains focusable elements in the content.
65
+ // If content focusables are available - move the focus to the first one.
66
+ if (cell.element === cell.cellElement) {
67
+ (_a = getFirstFocusable(cell.cellElement)) === null || _a === void 0 ? void 0 : _a.focus();
68
+ }
51
69
  };
52
70
  this.onFocusout = () => {
53
71
  this.focusedCell = null;
@@ -71,8 +89,8 @@ class GridNavigationModel {
71
89
  const from = this.focusedCell;
72
90
  const minExtreme = Number.NEGATIVE_INFINITY;
73
91
  const maxExtreme = Number.POSITIVE_INFINITY;
74
- // When focus is inside a dialog do not intercept any keyboard input.
75
- if (from.dialog) {
92
+ // Do not intercept any keys when the navigation is suppressed.
93
+ if (this.isSuppressed(from.element)) {
76
94
  return;
77
95
  }
78
96
  switch (key) {
@@ -112,41 +130,51 @@ class GridNavigationModel {
112
130
  };
113
131
  this.onTableNodeMutation = (mutationRecords) => {
114
132
  var _a;
115
- if (!this.prevFocusedCell) {
116
- return;
133
+ // When focused cell is un-mounted the focusout event handler removes this.cell,
134
+ // while this.prevFocusedCell is retained until the next focusin event.
135
+ const cell = (_a = this.focusedCell) !== null && _a !== void 0 ? _a : this.prevFocusedCell;
136
+ const cellSuppressed = cell ? this.isSuppressed(cell.element) : false;
137
+ // Update table elements focus if new nodes were added.
138
+ if (mutationRecords.some(record => record.addedNodes.length > 0)) {
139
+ muteElementFocusables(this.table, cellSuppressed);
140
+ ensureSingleFocusable(this.table, cell);
117
141
  }
118
- // When focused cell was un-mounted - re-apply focus to the same location.
119
- for (const record of mutationRecords) {
120
- if (record.type === 'childList') {
121
- for (const removedNode of Array.from(record.removedNodes)) {
122
- if (removedNode === this.prevFocusedCell.element || nodeContains(removedNode, this.prevFocusedCell.element)) {
123
- updateTableFocusables(this.table, (_a = this.focusedCell) !== null && _a !== void 0 ? _a : this.prevFocusedCell);
124
- moveFocusBy(this.table, this.prevFocusedCell, { y: 0, x: 0 });
142
+ if (cell) {
143
+ for (const record of mutationRecords) {
144
+ if (record.type === 'childList') {
145
+ // The lost focus in an unmount event is reapplied to the table using the previous cell position.
146
+ // The moveFocusBy takes care of finding the closest position if the previous one no longer exists.
147
+ for (const removedNode of Array.from(record.removedNodes)) {
148
+ if (removedNode === cell.element || nodeContains(removedNode, cell.element)) {
149
+ ensureSingleFocusable(this.table, cell);
150
+ moveFocusBy(this.table, cell, { y: 0, x: 0 });
151
+ }
125
152
  }
126
153
  }
127
154
  }
128
155
  }
129
156
  };
130
157
  }
131
- init(table) {
132
- var _a;
158
+ init(table, isSuppressed) {
133
159
  this._table = table;
160
+ this._isSuppressed = isSuppressed;
134
161
  this.table.addEventListener('focusin', this.onFocusin);
135
162
  this.table.addEventListener('focusout', this.onFocusout);
136
163
  this.table.addEventListener('keydown', this.onKeydown);
137
164
  const tableNodesObserver = new MutationObserver(this.onTableNodeMutation);
138
165
  tableNodesObserver.observe(table, { childList: true, subtree: true });
139
- updateTableFocusables(this.table, (_a = this.focusedCell) !== null && _a !== void 0 ? _a : this.prevFocusedCell);
166
+ muteElementFocusables(this.table, false);
167
+ ensureSingleFocusable(this.table, null);
140
168
  this.cleanup = () => {
141
169
  this.table.removeEventListener('focusin', this.onFocusin);
142
170
  this.table.removeEventListener('focusout', this.onFocusout);
143
171
  this.table.removeEventListener('keydown', this.onKeydown);
144
172
  tableNodesObserver.disconnect();
145
- restoreTableFocusables(this.table);
173
+ restoreElementFocusables(this.table);
146
174
  };
147
175
  }
148
- destroy() {
149
- this.cleanup();
176
+ cleanup() {
177
+ // Do nothing before initialized.
150
178
  }
151
179
  update({ pageSize }) {
152
180
  this._pageSize = pageSize;
@@ -156,9 +184,12 @@ class GridNavigationModel {
156
184
  }
157
185
  get table() {
158
186
  if (!this._table) {
159
- throw new Error('Invariant violation: GridNavigationModel is used before initialization.');
187
+ throw new Error('Invariant violation: GridNavigationHelper is used before initialization.');
160
188
  }
161
189
  return this._table;
162
190
  }
191
+ isSuppressed(focusedElement) {
192
+ return defaultIsSuppressed(focusedElement) || this._isSuppressed(focusedElement);
193
+ }
163
194
  }
164
195
  //# sourceMappingURL=use-grid-navigation.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-grid-navigation.js","sourceRoot":"lib/default/","sources":["table/table-role/use-grid-navigation.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AAEtC,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAEtG,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,0CAA0C,CAAC;AAExE;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAuB;IACtF,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,mBAAmB,EAAE,EAAE,EAAE,CAAC,CAAC;IAE3D,oGAAoG;IACpG,SAAS,CACP,GAAG,EAAE;QACH,IAAI,SAAS,KAAK,MAAM,EAAE;YACxB,MAAM,KAAK,GAAG,QAAQ,EAAE,CAAC;YACzB,KAAK,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;SAC5B;QACD,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;IAC/B,CAAC;IACD,+BAA+B;IAC/B,uDAAuD;IACvD,CAAC,KAAK,EAAE,SAAS,CAAC,CACnB,CAAC;IAEF,wCAAwC;IACxC,SAAS,CAAC,GAAG,EAAE;QACb,KAAK,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC;IAC7B,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC;IAEtB,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,MAAM,mBAAmB;IAAzB;QACE,QAAQ;QACA,cAAS,GAAG,CAAC,CAAC;Q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AC,KAAK,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;QAEtE,qBAAqB,CAAC,IAAI,CAAC,KAAK,EAAE,MAAA,IAAI,CAAC,WAAW,mCAAI,IAAI,CAAC,eAAe,CAAC,CAAC;QAE5E,IAAI,CAAC,OAAO,GAAG,GAAG,EAAE;YAClB,IAAI,CAAC,KAAK,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;YAC1D,IAAI,CAAC,KAAK,CAAC,mBAAmB,CAAC,UAAU,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;YAC5D,IAAI,CAAC,KAAK,CAAC,mBAAmB,CAAC,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,CAAC;YAE1D,kBAAkB,CAAC,UAAU,EAAE,CAAC;YAEhC,sBAAsB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACrC,CAAC,CAAC;IACJ,CAAC;IAEM,OAAO;QACZ,IAAI,CAAC,OAAO,EAAE,CAAC;IACjB,CAAC;IAEM,MAAM,CAAC,EAAE,QAAQ,EAAwB;QAC9C,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;IAC5B,CAAC;IAED,IAAY,QAAQ;QAClB,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED,IAAY,KAAK;QACf,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;YAChB,MAAM,IAAI,KAAK,CAAC,yEAAyE,CAAC,CAAC;SAC5F;QACD,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CAkHF","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { useEffect, useMemo } from 'react';\nimport { findFocusinCell, moveFocusBy, restoreTableFocusables, updateTableFocusables } from './utils';\nimport { FocusedCell, GridNavigationAPI, GridNavigationProps } from './interfaces';\nimport { KeyCode } from '../../internal/keycode';\nimport { nodeContains } from '@cloudscape-design/component-toolkit/dom';\n\n/**\n * Makes table with role=\"grid\" navigable with keyboard commands.\n * See https://www.w3.org/WAI/ARIA/apg/patterns/grid\n */\nexport function useGridNavigation({ tableRole, pageSize, getTable }: GridNavigationProps): GridNavigationAPI {\n const model = useMemo(() => new GridNavigationModel(), []);\n\n // Initialize the model with the table container assuming it is mounted synchronously and only once.\n useEffect(\n () => {\n if (tableRole === 'grid') {\n const table = getTable();\n table && model.init(table);\n }\n return () => model.destroy();\n },\n // Assuming getTable is stable.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [model, tableRole]\n );\n\n // Notify the model of the props change.\n useEffect(() => {\n model.update({ pageSize });\n }, [model, pageSize]);\n\n return {};\n}\n\nclass GridNavigationModel {\n // Props\n private _pageSize = 0;\n private _table: null | HTMLTableElement = null;\n\n // State\n private prevFocusedCell: null | FocusedCell = null;\n private focusedCell: null | FocusedCell = null;\n private cleanup = () => {};\n\n public init(table: HTMLTableElement) {\n this._table = table;\n\n this.table.addEventListener('focusin', this.onFocusin);\n this.table.addEventListener('focusout', this.onFocusout);\n this.table.addEventListener('keydown', this.onKeydown);\n\n const tableNodesObserver = new MutationObserver(this.onTableNodeMutation);\n tableNodesObserver.observe(table, { childList: true, subtree: true });\n\n updateTableFocusables(this.table, this.focusedCell ?? this.prevFocusedCell);\n\n this.cleanup = () => {\n this.table.removeEventListener('focusin', this.onFocusin);\n this.table.removeEventListener('focusout', this.onFocusout);\n this.table.removeEventListener('keydown', this.onKeydown);\n\n tableNodesObserver.disconnect();\n\n restoreTableFocusables(this.table);\n };\n }\n\n public destroy() {\n this.cleanup();\n }\n\n public update({ pageSize }: { pageSize: number }) {\n this._pageSize = pageSize;\n }\n\n private get pageSize() {\n return this._pageSize;\n }\n\n private get table(): HTMLTableElement {\n if (!this._table) {\n throw new Error('Invariant violation: GridNavigationModel is used before initialization.');\n }\n return this._table;\n }\n\n private onFocusin = (event: FocusEvent) => {\n const cell = findFocusinCell(event);\n\n if (!cell) {\n return;\n }\n\n if (cell.element !== event.target) {\n cell.element.focus();\n return;\n }\n\n this.prevFocusedCell = cell;\n this.focusedCell = cell;\n\n updateTableFocusables(this.table, cell);\n };\n\n private onFocusout = () => {\n this.focusedCell = null;\n };\n\n private onKeydown = (event: KeyboardEvent) => {\n if (!this.focusedCell) {\n return;\n }\n\n const ctrlKey = event.ctrlKey ? 1 : 0;\n const altKey = event.altKey ? 1 : 0;\n const shiftKey = event.shiftKey ? 1 : 0;\n const metaKey = event.metaKey ? 1 : 0;\n const numModifiersPressed = ctrlKey + altKey + shiftKey + metaKey;\n\n let key = event.keyCode;\n if (numModifiersPressed === 1 && event.ctrlKey) {\n key = -key;\n } else if (numModifiersPressed) {\n return;\n }\n\n const from = this.focusedCell;\n const minExtreme = Number.NEGATIVE_INFINITY;\n const maxExtreme = Number.POSITIVE_INFINITY;\n\n // When focus is inside a dialog do not intercept any keyboard input.\n if (from.dialog) {\n return;\n }\n\n switch (key) {\n case KeyCode.up:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: -1, x: 0 });\n\n case KeyCode.down:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: 1, x: 0 });\n\n case KeyCode.left:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: 0, x: -1 });\n\n case KeyCode.right:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: 0, x: 1 });\n\n case KeyCode.pageUp:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: -this.pageSize, x: 0 });\n\n case KeyCode.pageDown:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: this.pageSize, x: 0 });\n\n case KeyCode.home:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: 0, x: minExtreme });\n\n case KeyCode.end:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: 0, x: maxExtreme });\n\n case -KeyCode.home:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: minExtreme, x: minExtreme });\n\n case -KeyCode.end:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: maxExtreme, x: maxExtreme });\n\n default:\n return;\n }\n };\n\n private onTableNodeMutation = (mutationRecords: MutationRecord[]) => {\n if (!this.prevFocusedCell) {\n return;\n }\n\n // When focused cell was un-mounted - re-apply focus to the same location.\n for (const record of mutationRecords) {\n if (record.type === 'childList') {\n for (const removedNode of Array.from(record.removedNodes)) {\n if (removedNode === this.prevFocusedCell.element || nodeContains(removedNode, this.prevFocusedCell.element)) {\n updateTableFocusables(this.table, this.focusedCell ?? this.prevFocusedCell);\n moveFocusBy(this.table, this.prevFocusedCell, { y: 0, x: 0 });\n }\n }\n }\n }\n };\n}\n"]}
1
+ {"version":3,"file":"use-grid-navigation.js","sourceRoot":"lib/default/","sources":["table/table-role/use-grid-navigation.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AAEtC,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAC3C,OAAO,EACL,mBAAmB,EACnB,eAAe,EACf,WAAW,EACX,qBAAqB,EACrB,wBAAwB,EACxB,qBAAqB,EACrB,iBAAiB,GAClB,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACjD,OAAO,EAAE,YAAY,EAAE,MAAM,0CAA0C,CAAC;AACxE,OAAO,EAAE,iBAAiB,EAAE,MAAM,+CAA+C,CAAC;AAElF;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAuB;IACjG,MAAM,cAAc,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,oBAAoB,EAAE,EAAE,EAAE,CAAC,CAAC;IAErE,MAAM,cAAc,GAAG,iBAAiB,CAAC,QAAQ,CAAC,CAAC;IACnD,MAAM,kBAAkB,GAAG,iBAAiB,CAAC,CAAC,OAAoB,EAAE,EAAE,WAAC,OAAA,MAAA,YAAY,aAAZ,YA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copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { useEffect, useMemo } from 'react';\nimport {\n defaultIsSuppressed,\n findFocusinCell,\n moveFocusBy,\n muteElementFocusables,\n restoreElementFocusables,\n ensureSingleFocusable,\n getFirstFocusable,\n} from './utils';\nimport { FocusedCell, GridNavigationProps } from './interfaces';\nimport { KeyCode } from '../../internal/keycode';\nimport { nodeContains } from '@cloudscape-design/component-toolkit/dom';\nimport { useStableCallback } from '@cloudscape-design/component-toolkit/internal';\n\n/**\n * Makes table navigable with keyboard commands.\n * See https://www.w3.org/WAI/ARIA/apg/patterns/grid\n *\n * The hook attaches the GridNavigationHelper helper when active=true.\n * See GridNavigationHelper for more details.\n */\nexport function useGridNavigation({ active, pageSize, getTable, isSuppressed }: GridNavigationProps) {\n const gridNavigation = useMemo(() => new GridNavigationHelper(), []);\n\n const getTableStable = useStableCallback(getTable);\n const isSuppressedStable = useStableCallback((element: HTMLElement) => isSuppressed?.(element) ?? false);\n\n // Initialize the model with the table container assuming it is mounted synchronously and only once.\n useEffect(() => {\n if (active) {\n const table = getTableStable();\n table && gridNavigation.init(table, isSuppressedStable);\n }\n return () => gridNavigation.cleanup();\n }, [active, gridNavigation, getTableStable, isSuppressedStable]);\n\n // Notify the model of the props change.\n useEffect(() => {\n gridNavigation.update({ pageSize });\n }, [gridNavigation, pageSize]);\n}\n\n/**\n * This helper encapsulates the grid navigation behaviors which are:\n * 1. Responding to keyboard commands and moving the focus accordingly;\n * 2. Muting table interactive elements for only one to be user-focusable at a time;\n * 3. Suppressing the above behaviors when focusing an element inside a dialog or when instructed by the isSuppressed callback.\n *\n * All behaviors are attached upon initialization and are re-evaluated with every focusin, focusout, and keydown events,\n * and also when a node removal inside the table is observed to ensure consistency at any given moment.\n *\n * When the navigation is suppressed the keyboard commands are no longer intercepted and all table interactive elements are made\n * user-focusable to unblock the Tab navigation. The suppression should only be used for interactive elements inside the table that would\n * otherwise conflict with the navigation. Once the interactive element is deactivated or lose focus the table navigation becomes active again.\n */\nclass GridNavigationHelper {\n // Props\n private _pageSize = 0;\n private _table: null | HTMLTableElement = null;\n private _isSuppressed: (focusedElement: HTMLElement) => boolean = () => false;\n\n // State\n private prevFocusedCell: null | FocusedCell = null;\n private focusedCell: null | FocusedCell = null;\n\n public init(table: HTMLTableElement, isSuppressed: (focusedElement: HTMLElement) => boolean) {\n this._table = table;\n this._isSuppressed = isSuppressed;\n\n this.table.addEventListener('focusin', this.onFocusin);\n this.table.addEventListener('focusout', this.onFocusout);\n this.table.addEventListener('keydown', this.onKeydown);\n\n const tableNodesObserver = new MutationObserver(this.onTableNodeMutation);\n tableNodesObserver.observe(table, { childList: true, subtree: true });\n\n muteElementFocusables(this.table, false);\n ensureSingleFocusable(this.table, null);\n\n this.cleanup = () => {\n this.table.removeEventListener('focusin', this.onFocusin);\n this.table.removeEventListener('focusout', this.onFocusout);\n this.table.removeEventListener('keydown', this.onKeydown);\n\n tableNodesObserver.disconnect();\n\n restoreElementFocusables(this.table);\n };\n }\n\n public cleanup() {\n // Do nothing before initialized.\n }\n\n public update({ pageSize }: { pageSize: number }) {\n this._pageSize = pageSize;\n }\n\n private get pageSize() {\n return this._pageSize;\n }\n\n private get table(): HTMLTableElement {\n if (!this._table) {\n throw new Error('Invariant violation: GridNavigationHelper is used before initialization.');\n }\n return this._table;\n }\n\n private isSuppressed(focusedElement: HTMLElement): boolean {\n return defaultIsSuppressed(focusedElement) || this._isSuppressed(focusedElement);\n }\n\n private onFocusin = (event: FocusEvent) => {\n const cell = findFocusinCell(event);\n if (!cell) {\n return;\n }\n\n this.prevFocusedCell = cell;\n this.focusedCell = cell;\n\n muteElementFocusables(this.table, this.isSuppressed(cell.element));\n ensureSingleFocusable(this.table, cell);\n\n // Focusing on cell is not eligible when it contains focusable elements in the content.\n // If content focusables are available - move the focus to the first one.\n if (cell.element === cell.cellElement) {\n getFirstFocusable(cell.cellElement)?.focus();\n }\n };\n\n private onFocusout = () => {\n this.focusedCell = null;\n };\n\n private onKeydown = (event: KeyboardEvent) => {\n if (!this.focusedCell) {\n return;\n }\n\n const ctrlKey = event.ctrlKey ? 1 : 0;\n const altKey = event.altKey ? 1 : 0;\n const shiftKey = event.shiftKey ? 1 : 0;\n const metaKey = event.metaKey ? 1 : 0;\n const numModifiersPressed = ctrlKey + altKey + shiftKey + metaKey;\n\n let key = event.keyCode;\n if (numModifiersPressed === 1 && event.ctrlKey) {\n key = -key;\n } else if (numModifiersPressed) {\n return;\n }\n\n const from = this.focusedCell;\n const minExtreme = Number.NEGATIVE_INFINITY;\n const maxExtreme = Number.POSITIVE_INFINITY;\n\n // Do not intercept any keys when the navigation is suppressed.\n if (this.isSuppressed(from.element)) {\n return;\n }\n\n switch (key) {\n case KeyCode.up:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: -1, x: 0 });\n\n case KeyCode.down:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: 1, x: 0 });\n\n case KeyCode.left:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: 0, x: -1 });\n\n case KeyCode.right:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: 0, x: 1 });\n\n case KeyCode.pageUp:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: -this.pageSize, x: 0 });\n\n case KeyCode.pageDown:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: this.pageSize, x: 0 });\n\n case KeyCode.home:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: 0, x: minExtreme });\n\n case KeyCode.end:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: 0, x: maxExtreme });\n\n case -KeyCode.home:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: minExtreme, x: minExtreme });\n\n case -KeyCode.end:\n event.preventDefault();\n return moveFocusBy(this.table, from, { y: maxExtreme, x: maxExtreme });\n\n default:\n return;\n }\n };\n\n private onTableNodeMutation = (mutationRecords: MutationRecord[]) => {\n // When focused cell is un-mounted the focusout event handler removes this.cell,\n // while this.prevFocusedCell is retained until the next focusin event.\n const cell = this.focusedCell ?? this.prevFocusedCell;\n const cellSuppressed = cell ? this.isSuppressed(cell.element) : false;\n\n // Update table elements focus if new nodes were added.\n if (mutationRecords.some(record => record.addedNodes.length > 0)) {\n muteElementFocusables(this.table, cellSuppressed);\n ensureSingleFocusable(this.table, cell);\n }\n\n if (cell) {\n for (const record of mutationRecords) {\n if (record.type === 'childList') {\n // The lost focus in an unmount event is reapplied to the table using the previous cell position.\n // The moveFocusBy takes care of finding the closest position if the previous one no longer exists.\n for (const removedNode of Array.from(record.removedNodes)) {\n if (removedNode === cell.element || nodeContains(removedNode, cell.element)) {\n ensureSingleFocusable(this.table, cell);\n moveFocusBy(this.table, cell, { y: 0, x: 0 });\n }\n }\n }\n }\n }\n };\n}\n"]}
@@ -1,7 +1,7 @@
1
1
  import { FocusedCell } from './interfaces';
2
2
  /**
3
3
  * Finds focused cell props corresponding the focused element inside the table.
4
- * The function relies on ARIA colindex/rowindex attributes being set.
4
+ * The function relies on ARIA colindex/rowindex attributes being correctly applied.
5
5
  */
6
6
  export declare function findFocusinCell(event: FocusEvent): null | FocusedCell;
7
7
  /**
@@ -12,8 +12,26 @@ export declare function moveFocusBy(table: HTMLTableElement, from: FocusedCell,
12
12
  x: number;
13
13
  }): void;
14
14
  /**
15
- * Overrides focusability of the table elements to make focus targets controllable with keyboard commands.
15
+ * Makes the cell element, the first interactive element or the first cell of the table user-focusable.
16
16
  */
17
- export declare function updateTableFocusables(table: HTMLTableElement, cell: null | FocusedCell): void;
18
- export declare function restoreTableFocusables(table: HTMLTableElement): void;
17
+ export declare function ensureSingleFocusable(table: HTMLElement, cell: null | FocusedCell): void;
18
+ /**
19
+ * Makes all element focusable children pseudo-focusable unless the grid navigation is suppressed.
20
+ */
21
+ export declare function muteElementFocusables(element: HTMLElement, suppressed: boolean): void;
22
+ /**
23
+ * This cleanup code ensures all cells are no longer focusable but the interactive elements are.
24
+ * Currently there are no use cases for it as we don't expect the navigation to be used conditionally.
25
+ */
26
+ export declare function restoreElementFocusables(element: HTMLTableElement): void;
27
+ /**
28
+ * Returns true if the target element or one of its parents is a dialog or is marked with data-awsui-table-suppress-navigation attribute.
29
+ * This is used to suppress navigation for interactive content without a need to use a custom suppression check.
30
+ */
31
+ export declare function defaultIsSuppressed(target: HTMLElement): boolean;
32
+ /**
33
+ * Returns actually focusable or pseudo-focusable elements to find navigation targets.
34
+ */
35
+ export declare function getFocusables(element: HTMLElement): HTMLElement[];
36
+ export declare function getFirstFocusable(element: HTMLElement): HTMLElement | null;
19
37
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"lib/default/","sources":["table/table-role/utils.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAM3C;;;GAGG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,GAAG,WAAW,CAgCrE;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,gBAAgB,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,QA4BtG;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,gBAAgB,EAAE,IAAI,EAAE,IAAI,GAAG,WAAW,QA8BtF;AAED,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,gBAAgB,QAQ7D"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"lib/default/","sources":["table/table-role/utils.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAO3C;;;GAGG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,GAAG,WAAW,CAuBrE;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,gBAAgB,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,QA4BtG;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,EAAE,IAAI,GAAG,WAAW,QAejF;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,OAAO,QAwB9E;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,gBAAgB,QAQjE;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,WAAW,WAiBtD;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,WAAW,iBAEjD;AAED,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,WAAW,sBAErD"}
@@ -4,101 +4,110 @@ import { getFocusables as getActualFocusables } from '../../internal/components/
4
4
  // For the grid to have a single Tab stop all interactive element indices are updated to be -999.
5
5
  // The elements having tab index -999 are eligible for keyboard navigation but not for Tab navigation.
6
6
  const PSEUDO_FOCUSABLE_TAB_INDEX = -999;
7
+ const FOCUSABLES_SELECTOR = `[tabIndex="0"],[tabIndex="${PSEUDO_FOCUSABLE_TAB_INDEX}"]`;
7
8
  /**
8
9
  * Finds focused cell props corresponding the focused element inside the table.
9
- * The function relies on ARIA colindex/rowindex attributes being set.
10
+ * The function relies on ARIA colindex/rowindex attributes being correctly applied.
10
11
  */
11
12
  export function findFocusinCell(event) {
13
+ var _a, _b;
12
14
  if (!(event.target instanceof HTMLElement)) {
13
15
  return null;
14
16
  }
15
- function focusOnElement(element) {
16
- var _a, _b;
17
- const cellElement = element.closest('td,th');
18
- const rowElement = cellElement === null || cellElement === void 0 ? void 0 : cellElement.closest('tr');
19
- if (!cellElement || !rowElement) {
20
- return null;
21
- }
22
- const colIndex = parseInt((_a = cellElement.getAttribute('aria-colindex')) !== null && _a !== void 0 ? _a : '');
23
- const rowIndex = parseInt((_b = rowElement.getAttribute('aria-rowindex')) !== null && _b !== void 0 ? _b : '');
24
- if (isNaN(colIndex) || isNaN(rowIndex)) {
25
- return null;
26
- }
27
- const cellFocusables = getFocusables(cellElement);
28
- const elementIndex = cellFocusables.indexOf(element);
29
- const dialog = isDialogElement(element);
30
- // Focusing on the cell is not eligible when it contains focusable targets.
31
- if (cellFocusables.length > 0 && elementIndex === -1) {
32
- return focusOnElement(cellFocusables[0]);
33
- }
34
- return { rowIndex, colIndex, rowElement, cellElement, element, elementIndex, dialog };
17
+ const element = event.target;
18
+ const cellElement = element.closest('td,th');
19
+ const rowElement = cellElement === null || cellElement === void 0 ? void 0 : cellElement.closest('tr');
20
+ if (!cellElement || !rowElement) {
21
+ return null;
35
22
  }
36
- return focusOnElement(event.target);
23
+ const colIndex = parseInt((_a = cellElement.getAttribute('aria-colindex')) !== null && _a !== void 0 ? _a : '');
24
+ const rowIndex = parseInt((_b = rowElement.getAttribute('aria-rowindex')) !== null && _b !== void 0 ? _b : '');
25
+ if (isNaN(colIndex) || isNaN(rowIndex)) {
26
+ return null;
27
+ }
28
+ const cellFocusables = getFocusables(cellElement);
29
+ const elementIndex = cellFocusables.indexOf(element);
30
+ return { rowIndex, colIndex, rowElement, cellElement, element, elementIndex };
37
31
  }
38
32
  /**
39
33
  * Moves table focus in the provided direction. The focus can transition between cells or interactive elements inside cells.
40
34
  */
41
35
  export function moveFocusBy(table, from, delta) {
42
36
  var _a;
37
+ // Find next row to move focus into (can be null if the top/bottom is reached).
43
38
  const targetAriaRowIndex = from.rowIndex + delta.y;
44
39
  const targetRow = findTableRowByAriaRowIndex(table, targetAriaRowIndex, delta.y);
45
40
  if (!targetRow) {
46
41
  return;
47
42
  }
48
- // Move focus to the next interactive cell content element if eligible.
43
+ // Move focus to the next interactive cell content element if available.
49
44
  const cellFocusables = getFocusables(from.cellElement);
50
- const eligibleForElementFocus = delta.x && cellFocusables.length > 0;
51
- const targetElementIndex = from.elementIndex === -1 ? -1 : from.elementIndex + delta.x;
52
- if (eligibleForElementFocus && 0 <= targetElementIndex && targetElementIndex < cellFocusables.length) {
53
- focus(cellFocusables[targetElementIndex]);
45
+ const nextElementIndex = from.elementIndex + delta.x;
46
+ if (delta.x && from.elementIndex !== -1 && 0 <= nextElementIndex && nextElementIndex < cellFocusables.length) {
47
+ focus(cellFocusables[nextElementIndex]);
54
48
  return;
55
49
  }
56
- // Find next cell target to focus on.
50
+ // Find next cell to focus or move focus into (can be null if the left/right edge is reached).
57
51
  const targetAriaColIndex = from.colIndex + delta.x;
58
52
  const targetCell = findTableRowCellByAriaColIndex(targetRow, targetAriaColIndex, delta.x);
59
53
  if (!targetCell) {
60
54
  return;
61
55
  }
62
- // Focus on cell interactive content element if available or on the cell itself otherwise.
56
+ // Move focus on the cell interactive content or the cell itself.
63
57
  const targetCellFocusables = getFocusables(targetCell);
64
58
  const focusIndex = delta.x < 0 ? targetCellFocusables.length - 1 : delta.x > 0 ? 0 : from.elementIndex;
65
59
  const focusTarget = (_a = targetCellFocusables[focusIndex]) !== null && _a !== void 0 ? _a : targetCell;
66
60
  focus(focusTarget);
67
61
  }
68
62
  /**
69
- * Overrides focusability of the table elements to make focus targets controllable with keyboard commands.
63
+ * Makes the cell element, the first interactive element or the first cell of the table user-focusable.
70
64
  */
71
- export function updateTableFocusables(table, cell) {
65
+ export function ensureSingleFocusable(table, cell) {
72
66
  var _a;
73
- // Restore default focus behavior and make all cells focusable when focus in on a dialog element.
74
- // This allows existing the dialog cell with Tab or Shift+Tab.
75
- if (cell && cell.dialog) {
76
- for (const focusable of getFocusables(table)) {
67
+ const firstTableCell = table.querySelector('td,th');
68
+ // A single element of the table is made user-focusable.
69
+ // It defaults to the first interactive element of the first cell or the first cell itself otherwise.
70
+ let focusTarget = (_a = (firstTableCell && getFocusables(firstTableCell)[0])) !== null && _a !== void 0 ? _a : firstTableCell;
71
+ // When a navigation-focused element is present in the table it is used for user-navigation instead.
72
+ if (cell && table.contains(cell.element)) {
73
+ focusTarget = cell.element;
74
+ }
75
+ if (focusTarget) {
76
+ focusTarget.tabIndex = 0;
77
+ }
78
+ }
79
+ /**
80
+ * Makes all element focusable children pseudo-focusable unless the grid navigation is suppressed.
81
+ */
82
+ export function muteElementFocusables(element, suppressed) {
83
+ // When grid navigation is suppressed all interactive elements and all cells focus is unmuted to unblock Tab navigation.
84
+ // Leaving the interactive widget using Tab navigation moves the focus to the current or adjacent cell and un-suppresses
85
+ // the navigation when implemented correctly.
86
+ if (suppressed) {
87
+ for (const focusable of getFocusables(element)) {
77
88
  focusable.tabIndex = 0;
78
89
  }
79
90
  return;
80
91
  }
81
- const tableCells = Array.from(table.querySelectorAll('td,th'));
92
+ const tableCells = queryTableCells(element);
93
+ // Assigning pseudo-focusable tab index to all cells and all interactive elements makes them focusable with grid navigation.
82
94
  for (const cell of tableCells) {
83
- cell.tabIndex = PSEUDO_FOCUSABLE_TAB_INDEX;
84
- }
85
- for (const focusable of getActualFocusables(table)) {
86
- focusable.tabIndex = PSEUDO_FOCUSABLE_TAB_INDEX;
87
- }
88
- // The only focusable element of the table.
89
- let focusTarget = tableCells[0];
90
- if (cell && table.contains(cell.element)) {
91
- focusTarget = cell.element;
92
- }
93
- else if (tableCells.length > 0) {
94
- focusTarget = (_a = getFocusables(tableCells[0])[0]) !== null && _a !== void 0 ? _a : focusTarget;
95
+ if (cell !== document.activeElement) {
96
+ cell.tabIndex = PSEUDO_FOCUSABLE_TAB_INDEX;
97
+ }
95
98
  }
96
- if (focusTarget) {
97
- focusTarget.tabIndex = 0;
99
+ for (const focusable of getActualFocusables(element)) {
100
+ if (focusable !== document.activeElement) {
101
+ focusable.tabIndex = PSEUDO_FOCUSABLE_TAB_INDEX;
102
+ }
98
103
  }
99
104
  }
100
- export function restoreTableFocusables(table) {
101
- for (const focusable of getFocusables(table)) {
105
+ /**
106
+ * This cleanup code ensures all cells are no longer focusable but the interactive elements are.
107
+ * Currently there are no use cases for it as we don't expect the navigation to be used conditionally.
108
+ */
109
+ export function restoreElementFocusables(element) {
110
+ for (const focusable of getFocusables(element)) {
102
111
  if (focusable instanceof HTMLTableCellElement) {
103
112
  focusable.tabIndex = -1;
104
113
  }
@@ -108,12 +117,13 @@ export function restoreTableFocusables(table) {
108
117
  }
109
118
  }
110
119
  /**
111
- * Returns true if the target element or one of its parents is a dialog or is marked with data-awsui-table-suppress-navigation.
112
- * For dialog cells when in focus the tab indices are not overridden and keyboard events are not intercepted.
120
+ * Returns true if the target element or one of its parents is a dialog or is marked with data-awsui-table-suppress-navigation attribute.
121
+ * This is used to suppress navigation for interactive content without a need to use a custom suppression check.
113
122
  */
114
- function isDialogElement(target) {
123
+ export function defaultIsSuppressed(target) {
115
124
  let current = target;
116
125
  while (current) {
126
+ // Stop checking for parents upon reaching the cell element as the function only aims at the cell content.
117
127
  const tagName = current.tagName.toLowerCase();
118
128
  if (tagName === 'td' || tagName === 'th') {
119
129
  return false;
@@ -126,9 +136,18 @@ function isDialogElement(target) {
126
136
  }
127
137
  return false;
128
138
  }
129
- function getFocusables(element) {
130
- return Array.from(element.querySelectorAll(`[tabIndex="0"],[tabIndex="${PSEUDO_FOCUSABLE_TAB_INDEX}"]`));
139
+ /**
140
+ * Returns actually focusable or pseudo-focusable elements to find navigation targets.
141
+ */
142
+ export function getFocusables(element) {
143
+ return Array.from(element.querySelectorAll(FOCUSABLES_SELECTOR));
144
+ }
145
+ export function getFirstFocusable(element) {
146
+ return element.querySelector(FOCUSABLES_SELECTOR);
131
147
  }
148
+ /**
149
+ * Finds the closest row to the targetAriaRowIndex+delta in the direction of delta.
150
+ */
132
151
  function findTableRowByAriaRowIndex(table, targetAriaRowIndex, delta) {
133
152
  var _a;
134
153
  let targetRow = null;
@@ -151,6 +170,9 @@ function findTableRowByAriaRowIndex(table, targetAriaRowIndex, delta) {
151
170
  }
152
171
  return targetRow;
153
172
  }
173
+ /**
174
+ * Finds the closest column to the targetAriaColIndex+delta in the direction of delta.
175
+ */
154
176
  function findTableRowCellByAriaColIndex(tableRow, targetAriaColIndex, delta) {
155
177
  var _a;
156
178
  let targetCell = null;
@@ -179,4 +201,11 @@ function focus(element) {
179
201
  element.focus();
180
202
  }
181
203
  }
204
+ function queryTableCells(element) {
205
+ const tableCells = Array.from(element.querySelectorAll('td,th'));
206
+ if (element instanceof HTMLTableCellElement) {
207
+ tableCells.push(element);
208
+ }
209
+ return tableCells;
210
+ }
182
211
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"lib/default/","sources":["table/table-role/utils.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AAEtC,OAAO,EAAE,aAAa,IAAI,mBAAmB,EAAE,MAAM,4CAA4C,CAAC;AAGlG,iGAAiG;AACjG,sGAAsG;AACtG,MAAM,0BAA0B,GAAG,CAAC,GAAG,CAAC;AAExC;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,KAAiB;IAC/C,IAAI,CAAC,CAAC,KAAK,CAAC,MAAM,YAAY,WAAW,CAAC,EAAE;QAC1C,OAAO,IAAI,CAAC;KACb;IAED,SAAS,cAAc,CAAC,OAAoB;;QAC1C,MAAM,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAAgC,CAAC;QAC5E,MAAM,UAAU,GAAG,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,OAAO,CAAC,IAAI,CAAC,CAAC;QAE9C,IAAI,CAAC,WAAW,IAAI,CAAC,UAAU,EAAE;YAC/B,OAAO,IAAI,CAAC;SACb;QAED,MAAM,QAAQ,GAAG,QAAQ,CAAC,MAAA,WAAW,CAAC,YAAY,CAAC,eAAe,CAAC,mCAAI,EAAE,CAAC,CAAC;QAC3E,MAAM,QAAQ,GAAG,QAAQ,CAAC,MAAA,UAAU,CAAC,YAAY,CAAC,eAAe,CAAC,mCAAI,EAAE,CAAC,CAAC;QAC1E,IAAI,KAAK,CAAC,QAAQ,CAAC,IAAI,KAAK,CAAC,QAAQ,C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I,YAAY,EAAE;QAClC,MAAM,WAAW,GAAG,QAAQ,CAAC,MAAA,OAAO,CAAC,YAAY,CAAC,eAAe,CAAC,mCAAI,EAAE,CAAC,CAAC;QAC1E,UAAU,GAAG,OAA+B,CAAC;QAE7C,IAAI,WAAW,KAAK,kBAAkB,EAAE;YACtC,MAAM;SACP;QACD,IAAI,KAAK,IAAI,CAAC,IAAI,WAAW,GAAG,kBAAkB,EAAE;YAClD,MAAM;SACP;QACD,IAAI,KAAK,GAAG,CAAC,IAAI,WAAW,GAAG,kBAAkB,EAAE;YACjD,MAAM;SACP;KACF;IACD,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,SAAS,KAAK,CAAC,OAA2B;IACxC,IAAI,OAAO,EAAE;QACX,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;QACrB,OAAO,CAAC,KAAK,EAAE,CAAC;KACjB;AACH,CAAC","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { getFocusables as getActualFocusables } from '../../internal/components/focus-lock/utils';\nimport { FocusedCell } from './interfaces';\n\n// For the grid to have a single Tab stop all interactive element indices are updated to be -999.\n// The elements having tab index -999 are eligible for keyboard navigation but not for Tab navigation.\nconst PSEUDO_FOCUSABLE_TAB_INDEX = -999;\n\n/**\n * Finds focused cell props corresponding the focused element inside the table.\n * The function relies on ARIA colindex/rowindex attributes being set.\n */\nexport function findFocusinCell(event: FocusEvent): null | FocusedCell {\n if (!(event.target instanceof HTMLElement)) {\n return null;\n }\n\n function focusOnElement(element: HTMLElement): null | FocusedCell {\n const cellElement = element.closest('td,th') as null | HTMLTableCellElement;\n const rowElement = cellElement?.closest('tr');\n\n if (!cellElement || !rowElement) {\n return null;\n }\n\n const colIndex = parseInt(cellElement.getAttribute('aria-colindex') ?? '');\n const rowIndex = parseInt(rowElement.getAttribute('aria-rowindex') ?? '');\n if (isNaN(colIndex) || isNaN(rowIndex)) {\n return null;\n }\n\n const cellFocusables = getFocusables(cellElement);\n const elementIndex = cellFocusables.indexOf(element);\n const dialog = isDialogElement(element);\n\n // Focusing on the cell is not eligible when it contains focusable targets.\n if (cellFocusables.length > 0 && elementIndex === -1) {\n return focusOnElement(cellFocusables[0]);\n }\n\n return { rowIndex, colIndex, rowElement, cellElement, element, elementIndex, dialog };\n }\n\n return focusOnElement(event.target);\n}\n\n/**\n * Moves table focus in the provided direction. The focus can transition between cells or interactive elements inside cells.\n */\nexport function moveFocusBy(table: HTMLTableElement, from: FocusedCell, delta: { y: number; x: number }) {\n const targetAriaRowIndex = from.rowIndex + delta.y;\n const targetRow = findTableRowByAriaRowIndex(table, targetAriaRowIndex, delta.y);\n if (!targetRow) {\n return;\n }\n\n // Move focus to the next interactive cell content element if eligible.\n const cellFocusables = getFocusables(from.cellElement);\n const eligibleForElementFocus = delta.x && cellFocusables.length > 0;\n const targetElementIndex = from.elementIndex === -1 ? -1 : from.elementIndex + delta.x;\n if (eligibleForElementFocus && 0 <= targetElementIndex && targetElementIndex < cellFocusables.length) {\n focus(cellFocusables[targetElementIndex]);\n return;\n }\n\n // Find next cell target to focus on.\n const targetAriaColIndex = from.colIndex + delta.x;\n const targetCell = findTableRowCellByAriaColIndex(targetRow, targetAriaColIndex, delta.x);\n if (!targetCell) {\n return;\n }\n\n // Focus on cell interactive content element if available or on the cell itself otherwise.\n const targetCellFocusables = getFocusables(targetCell);\n const focusIndex = delta.x < 0 ? targetCellFocusables.length - 1 : delta.x > 0 ? 0 : from.elementIndex;\n const focusTarget = targetCellFocusables[focusIndex] ?? targetCell;\n focus(focusTarget);\n}\n\n/**\n * Overrides focusability of the table elements to make focus targets controllable with keyboard commands.\n */\nexport function updateTableFocusables(table: HTMLTableElement, cell: null | FocusedCell) {\n // Restore default focus behavior and make all cells focusable when focus in on a dialog element.\n // This allows existing the dialog cell with Tab or Shift+Tab.\n if (cell && cell.dialog) {\n for (const focusable of getFocusables(table)) {\n focusable.tabIndex = 0;\n }\n return;\n }\n\n const tableCells = Array.from(table.querySelectorAll('td,th') as NodeListOf<HTMLTableCellElement>);\n\n for (const cell of tableCells) {\n cell.tabIndex = PSEUDO_FOCUSABLE_TAB_INDEX;\n }\n for (const focusable of getActualFocusables(table)) {\n focusable.tabIndex = PSEUDO_FOCUSABLE_TAB_INDEX;\n }\n\n // The only focusable element of the table.\n let focusTarget: undefined | HTMLElement = tableCells[0];\n\n if (cell && table.contains(cell.element)) {\n focusTarget = cell.element;\n } else if (tableCells.length > 0) {\n focusTarget = getFocusables(tableCells[0])[0] ?? focusTarget;\n }\n if (focusTarget) {\n focusTarget.tabIndex = 0;\n }\n}\n\nexport function restoreTableFocusables(table: HTMLTableElement) {\n for (const focusable of getFocusables(table)) {\n if (focusable instanceof HTMLTableCellElement) {\n focusable.tabIndex = -1;\n } else {\n focusable.tabIndex = 0;\n }\n }\n}\n\n/**\n * Returns true if the target element or one of its parents is a dialog or is marked with data-awsui-table-suppress-navigation.\n * For dialog cells when in focus the tab indices are not overridden and keyboard events are not intercepted.\n */\nfunction isDialogElement(target: HTMLElement) {\n let current: null | HTMLElement = target;\n while (current) {\n const tagName = current.tagName.toLowerCase();\n if (tagName === 'td' || tagName === 'th') {\n return false;\n }\n if (\n current.getAttribute('role') === 'dialog' ||\n current.getAttribute('data-awsui-table-suppress-navigation') === 'true'\n ) {\n return true;\n }\n current = current.parentElement;\n }\n return false;\n}\n\nfunction getFocusables(element: HTMLElement) {\n return Array.from(\n element.querySelectorAll(`[tabIndex=\"0\"],[tabIndex=\"${PSEUDO_FOCUSABLE_TAB_INDEX}\"]`)\n ) as HTMLElement[];\n}\n\nfunction findTableRowByAriaRowIndex(table: HTMLTableElement, targetAriaRowIndex: number, delta: number) {\n let targetRow: null | HTMLTableRowElement = null;\n const rowElements = Array.from(table.querySelectorAll('tr[aria-rowindex]'));\n if (delta < 0) {\n rowElements.reverse();\n }\n for (const element of rowElements) {\n const rowIndex = parseInt(element.getAttribute('aria-rowindex') ?? '');\n targetRow = element as HTMLTableRowElement;\n\n if (rowIndex === targetAriaRowIndex) {\n break;\n }\n if (delta >= 0 && rowIndex > targetAriaRowIndex) {\n break;\n }\n if (delta < 0 && rowIndex < targetAriaRowIndex) {\n break;\n }\n }\n return targetRow;\n}\n\nfunction findTableRowCellByAriaColIndex(tableRow: HTMLTableRowElement, targetAriaColIndex: number, delta: number) {\n let targetCell: null | HTMLTableCellElement = null;\n const cellElements = Array.from(tableRow.querySelectorAll('td[aria-colindex],th[aria-colindex]'));\n if (delta < 0) {\n cellElements.reverse();\n }\n for (const element of cellElements) {\n const columnIndex = parseInt(element.getAttribute('aria-colindex') ?? '');\n targetCell = element as HTMLTableCellElement;\n\n if (columnIndex === targetAriaColIndex) {\n break;\n }\n if (delta >= 0 && columnIndex > targetAriaColIndex) {\n break;\n }\n if (delta < 0 && columnIndex < targetAriaColIndex) {\n break;\n }\n }\n return targetCell;\n}\n\nfunction focus(element: null | HTMLElement) {\n if (element) {\n element.tabIndex = 0;\n element.focus();\n }\n}\n"]}
1
+ {"version":3,"file":"utils.js","sourceRoot":"lib/default/","sources":["table/table-role/utils.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AAEtC,OAAO,EAAE,aAAa,IAAI,mBAAmB,EAAE,MAAM,4CAA4C,CAAC;AAGlG,iGAAiG;AACjG,sGAAsG;AACtG,MAAM,0BAA0B,GAAG,CAAC,GAAG,CAAC;AACxC,MAAM,mBAAmB,GAAG,6BAA6B,0BAA0B,IAAI,CAAC;AAExF;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC,KAAiB;;IAC/C,IAAI,CAAC,CAAC,KAAK,CAAC,MAAM,YAAY,WAAW,CAAC,EAAE;QAC1C,OAAO,IAAI,CAAC;KACb;IACD,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,CAAC;IAE7B,MAAM,WAAW,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAAgC,CAAC;IAC5E,MAAM,UAAU,GAAG,WAAW,aAAX,WAAW,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AACnB,CAAC;AAED;;GAEG;AACH,SAAS,8BAA8B,CAAC,QAA6B,EAAE,kBAA0B,EAAE,KAAa;;IAC9G,IAAI,UAAU,GAAgC,IAAI,CAAC;IACnD,MAAM,YAAY,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,gBAAgB,CAAC,qCAAqC,CAAC,CAAC,CAAC;IAClG,IAAI,KAAK,GAAG,CAAC,EAAE;QACb,YAAY,CAAC,OAAO,EAAE,CAAC;KACxB;IACD,KAAK,MAAM,OAAO,IAAI,YAAY,EAAE;QAClC,MAAM,WAAW,GAAG,QAAQ,CAAC,MAAA,OAAO,CAAC,YAAY,CAAC,eAAe,CAAC,mCAAI,EAAE,CAAC,CAAC;QAC1E,UAAU,GAAG,OAA+B,CAAC;QAE7C,IAAI,WAAW,KAAK,kBAAkB,EAAE;YACtC,MAAM;SACP;QACD,IAAI,KAAK,IAAI,CAAC,IAAI,WAAW,GAAG,kBAAkB,EAAE;YAClD,MAAM;SACP;QACD,IAAI,KAAK,GAAG,CAAC,IAAI,WAAW,GAAG,kBAAkB,EAAE;YACjD,MAAM;SACP;KACF;IACD,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,SAAS,KAAK,CAAC,OAA2B;IACxC,IAAI,OAAO,EAAE;QACX,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;QACrB,OAAO,CAAC,KAAK,EAAE,CAAC;KACjB;AACH,CAAC;AAED,SAAS,eAAe,CAAC,OAAoB;IAC3C,MAAM,UAAU,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,OAAO,CAAqC,CAAC,CAAC;IACrG,IAAI,OAAO,YAAY,oBAAoB,EAAE;QAC3C,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;KAC1B;IAED,OAAO,UAAU,CAAC;AACpB,CAAC","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { getFocusables as getActualFocusables } from '../../internal/components/focus-lock/utils';\nimport { FocusedCell } from './interfaces';\n\n// For the grid to have a single Tab stop all interactive element indices are updated to be -999.\n// The elements having tab index -999 are eligible for keyboard navigation but not for Tab navigation.\nconst PSEUDO_FOCUSABLE_TAB_INDEX = -999;\nconst FOCUSABLES_SELECTOR = `[tabIndex=\"0\"],[tabIndex=\"${PSEUDO_FOCUSABLE_TAB_INDEX}\"]`;\n\n/**\n * Finds focused cell props corresponding the focused element inside the table.\n * The function relies on ARIA colindex/rowindex attributes being correctly applied.\n */\nexport function findFocusinCell(event: FocusEvent): null | FocusedCell {\n if (!(event.target instanceof HTMLElement)) {\n return null;\n }\n const element = event.target;\n\n const cellElement = element.closest('td,th') as null | HTMLTableCellElement;\n const rowElement = cellElement?.closest('tr');\n\n if (!cellElement || !rowElement) {\n return null;\n }\n\n const colIndex = parseInt(cellElement.getAttribute('aria-colindex') ?? '');\n const rowIndex = parseInt(rowElement.getAttribute('aria-rowindex') ?? '');\n if (isNaN(colIndex) || isNaN(rowIndex)) {\n return null;\n }\n\n const cellFocusables = getFocusables(cellElement);\n const elementIndex = cellFocusables.indexOf(element);\n\n return { rowIndex, colIndex, rowElement, cellElement, element, elementIndex };\n}\n\n/**\n * Moves table focus in the provided direction. The focus can transition between cells or interactive elements inside cells.\n */\nexport function moveFocusBy(table: HTMLTableElement, from: FocusedCell, delta: { y: number; x: number }) {\n // Find next row to move focus into (can be null if the top/bottom is reached).\n const targetAriaRowIndex = from.rowIndex + delta.y;\n const targetRow = findTableRowByAriaRowIndex(table, targetAriaRowIndex, delta.y);\n if (!targetRow) {\n return;\n }\n\n // Move focus to the next interactive cell content element if available.\n const cellFocusables = getFocusables(from.cellElement);\n const nextElementIndex = from.elementIndex + delta.x;\n if (delta.x && from.elementIndex !== -1 && 0 <= nextElementIndex && nextElementIndex < cellFocusables.length) {\n focus(cellFocusables[nextElementIndex]);\n return;\n }\n\n // Find next cell to focus or move focus into (can be null if the left/right edge is reached).\n const targetAriaColIndex = from.colIndex + delta.x;\n const targetCell = findTableRowCellByAriaColIndex(targetRow, targetAriaColIndex, delta.x);\n if (!targetCell) {\n return;\n }\n\n // Move focus on the cell interactive content or the cell itself.\n const targetCellFocusables = getFocusables(targetCell);\n const focusIndex = delta.x < 0 ? targetCellFocusables.length - 1 : delta.x > 0 ? 0 : from.elementIndex;\n const focusTarget = targetCellFocusables[focusIndex] ?? targetCell;\n focus(focusTarget);\n}\n\n/**\n * Makes the cell element, the first interactive element or the first cell of the table user-focusable.\n */\nexport function ensureSingleFocusable(table: HTMLElement, cell: null | FocusedCell) {\n const firstTableCell = table.querySelector('td,th') as null | HTMLTableCellElement;\n\n // A single element of the table is made user-focusable.\n // It defaults to the first interactive element of the first cell or the first cell itself otherwise.\n let focusTarget: null | HTMLElement = (firstTableCell && getFocusables(firstTableCell)[0]) ?? firstTableCell;\n\n // When a navigation-focused element is present in the table it is used for user-navigation instead.\n if (cell && table.contains(cell.element)) {\n focusTarget = cell.element;\n }\n\n if (focusTarget) {\n focusTarget.tabIndex = 0;\n }\n}\n\n/**\n * Makes all element focusable children pseudo-focusable unless the grid navigation is suppressed.\n */\nexport function muteElementFocusables(element: HTMLElement, suppressed: boolean) {\n // When grid navigation is suppressed all interactive elements and all cells focus is unmuted to unblock Tab navigation.\n // Leaving the interactive widget using Tab navigation moves the focus to the current or adjacent cell and un-suppresses\n // the navigation when implemented correctly.\n if (suppressed) {\n for (const focusable of getFocusables(element)) {\n focusable.tabIndex = 0;\n }\n return;\n }\n\n const tableCells = queryTableCells(element);\n\n // Assigning pseudo-focusable tab index to all cells and all interactive elements makes them focusable with grid navigation.\n for (const cell of tableCells) {\n if (cell !== document.activeElement) {\n cell.tabIndex = PSEUDO_FOCUSABLE_TAB_INDEX;\n }\n }\n for (const focusable of getActualFocusables(element)) {\n if (focusable !== document.activeElement) {\n focusable.tabIndex = PSEUDO_FOCUSABLE_TAB_INDEX;\n }\n }\n}\n\n/**\n * This cleanup code ensures all cells are no longer focusable but the interactive elements are.\n * Currently there are no use cases for it as we don't expect the navigation to be used conditionally.\n */\nexport function restoreElementFocusables(element: HTMLTableElement) {\n for (const focusable of getFocusables(element)) {\n if (focusable instanceof HTMLTableCellElement) {\n focusable.tabIndex = -1;\n } else {\n focusable.tabIndex = 0;\n }\n }\n}\n\n/**\n * Returns true if the target element or one of its parents is a dialog or is marked with data-awsui-table-suppress-navigation attribute.\n * This is used to suppress navigation for interactive content without a need to use a custom suppression check.\n */\nexport function defaultIsSuppressed(target: HTMLElement) {\n let current: null | HTMLElement = target;\n while (current) {\n // Stop checking for parents upon reaching the cell element as the function only aims at the cell content.\n const tagName = current.tagName.toLowerCase();\n if (tagName === 'td' || tagName === 'th') {\n return false;\n }\n if (\n current.getAttribute('role') === 'dialog' ||\n current.getAttribute('data-awsui-table-suppress-navigation') === 'true'\n ) {\n return true;\n }\n current = current.parentElement;\n }\n return false;\n}\n\n/**\n * Returns actually focusable or pseudo-focusable elements to find navigation targets.\n */\nexport function getFocusables(element: HTMLElement) {\n return Array.from(element.querySelectorAll(FOCUSABLES_SELECTOR)) as HTMLElement[];\n}\n\nexport function getFirstFocusable(element: HTMLElement) {\n return element.querySelector(FOCUSABLES_SELECTOR) as null | HTMLElement;\n}\n\n/**\n * Finds the closest row to the targetAriaRowIndex+delta in the direction of delta.\n */\nfunction findTableRowByAriaRowIndex(table: HTMLTableElement, targetAriaRowIndex: number, delta: number) {\n let targetRow: null | HTMLTableRowElement = null;\n const rowElements = Array.from(table.querySelectorAll('tr[aria-rowindex]'));\n if (delta < 0) {\n rowElements.reverse();\n }\n for (const element of rowElements) {\n const rowIndex = parseInt(element.getAttribute('aria-rowindex') ?? '');\n targetRow = element as HTMLTableRowElement;\n\n if (rowIndex === targetAriaRowIndex) {\n break;\n }\n if (delta >= 0 && rowIndex > targetAriaRowIndex) {\n break;\n }\n if (delta < 0 && rowIndex < targetAriaRowIndex) {\n break;\n }\n }\n return targetRow;\n}\n\n/**\n * Finds the closest column to the targetAriaColIndex+delta in the direction of delta.\n */\nfunction findTableRowCellByAriaColIndex(tableRow: HTMLTableRowElement, targetAriaColIndex: number, delta: number) {\n let targetCell: null | HTMLTableCellElement = null;\n const cellElements = Array.from(tableRow.querySelectorAll('td[aria-colindex],th[aria-colindex]'));\n if (delta < 0) {\n cellElements.reverse();\n }\n for (const element of cellElements) {\n const columnIndex = parseInt(element.getAttribute('aria-colindex') ?? '');\n targetCell = element as HTMLTableCellElement;\n\n if (columnIndex === targetAriaColIndex) {\n break;\n }\n if (delta >= 0 && columnIndex > targetAriaColIndex) {\n break;\n }\n if (delta < 0 && columnIndex < targetAriaColIndex) {\n break;\n }\n }\n return targetCell;\n}\n\nfunction focus(element: null | HTMLElement) {\n if (element) {\n element.tabIndex = 0;\n element.focus();\n }\n}\n\nfunction queryTableCells(element: HTMLElement) {\n const tableCells = Array.from(element.querySelectorAll('td,th') as NodeListOf<HTMLTableCellElement>);\n if (element instanceof HTMLTableCellElement) {\n tableCells.push(element);\n }\n\n return tableCells;\n}\n"]}