apex-grid 3.5.0 → 4.0.0-beta.1

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.
Files changed (215) hide show
  1. package/CHANGELOG.md +573 -0
  2. package/LICENSE.md +125 -0
  3. package/README.md +143 -23
  4. package/components/cell.d.ts +43 -5
  5. package/components/cell.js +225 -64
  6. package/components/cell.js.map +1 -1
  7. package/components/filter-row.d.ts +2 -0
  8. package/components/filter-row.js +82 -28
  9. package/components/filter-row.js.map +1 -1
  10. package/components/grid.d.ts +281 -49
  11. package/components/grid.js +600 -159
  12. package/components/grid.js.map +1 -1
  13. package/components/group-header-row.js +28 -25
  14. package/components/group-header-row.js.map +1 -1
  15. package/components/header-row.d.ts +6 -10
  16. package/components/header-row.js +12 -28
  17. package/components/header-row.js.map +1 -1
  18. package/components/header.js +71 -22
  19. package/components/header.js.map +1 -1
  20. package/components/paginator.d.ts +20 -5
  21. package/components/paginator.js +54 -17
  22. package/components/paginator.js.map +1 -1
  23. package/components/row.d.ts +19 -3
  24. package/components/row.js +211 -56
  25. package/components/row.js.map +1 -1
  26. package/components/toolbar.d.ts +5 -1
  27. package/components/toolbar.js +6 -2
  28. package/components/toolbar.js.map +1 -1
  29. package/components/virtualize-rows.d.ts +38 -0
  30. package/components/virtualize-rows.js +286 -0
  31. package/components/virtualize-rows.js.map +1 -0
  32. package/components/virtualizer.d.ts +111 -1
  33. package/components/virtualizer.js +192 -5
  34. package/components/virtualizer.js.map +1 -1
  35. package/controllers/dom.d.ts +14 -7
  36. package/controllers/dom.js +45 -27
  37. package/controllers/dom.js.map +1 -1
  38. package/controllers/editing.d.ts +104 -37
  39. package/controllers/editing.js +230 -78
  40. package/controllers/editing.js.map +1 -1
  41. package/controllers/expansion.d.ts +14 -5
  42. package/controllers/expansion.js +24 -1
  43. package/controllers/expansion.js.map +1 -1
  44. package/controllers/filter.d.ts +10 -2
  45. package/controllers/filter.js +62 -17
  46. package/controllers/filter.js.map +1 -1
  47. package/controllers/history.d.ts +45 -8
  48. package/controllers/history.js +58 -20
  49. package/controllers/history.js.map +1 -1
  50. package/controllers/navigation.d.ts +130 -13
  51. package/controllers/navigation.js +474 -60
  52. package/controllers/navigation.js.map +1 -1
  53. package/controllers/pagination.d.ts +13 -6
  54. package/controllers/pagination.js +29 -22
  55. package/controllers/pagination.js.map +1 -1
  56. package/controllers/reorder.js +38 -22
  57. package/controllers/reorder.js.map +1 -1
  58. package/controllers/resize.d.ts +17 -2
  59. package/controllers/resize.js +16 -6
  60. package/controllers/resize.js.map +1 -1
  61. package/controllers/row-pin.d.ts +26 -2
  62. package/controllers/row-pin.js +24 -11
  63. package/controllers/row-pin.js.map +1 -1
  64. package/controllers/row-reorder.d.ts +40 -12
  65. package/controllers/row-reorder.js +137 -43
  66. package/controllers/row-reorder.js.map +1 -1
  67. package/controllers/selection.d.ts +33 -4
  68. package/controllers/selection.js +49 -11
  69. package/controllers/selection.js.map +1 -1
  70. package/controllers/sort.d.ts +22 -2
  71. package/controllers/sort.js +30 -25
  72. package/controllers/sort.js.map +1 -1
  73. package/controllers/state.d.ts +74 -7
  74. package/controllers/state.js +117 -36
  75. package/controllers/state.js.map +1 -1
  76. package/controllers/tree.d.ts +15 -10
  77. package/controllers/tree.js +0 -0
  78. package/controllers/tree.js.map +1 -1
  79. package/custom-elements.json +5091 -1023
  80. package/docs/assets/hierarchy.js +1 -1
  81. package/docs/assets/highlight.css +15 -8
  82. package/docs/assets/navigation.js +1 -1
  83. package/docs/assets/search.js +1 -1
  84. package/docs/classes/ApexGrid.html +233 -87
  85. package/docs/classes/ApexGridPaginator.html +39 -8
  86. package/docs/classes/ApexGridToolbar.html +11 -7
  87. package/docs/functions/localize.html +2 -2
  88. package/docs/hierarchy.html +1 -1
  89. package/docs/index.html +159 -26
  90. package/docs/interfaces/ApexCellValidationFailedEvent.html +3 -2
  91. package/docs/interfaces/ApexCellValueChangedEvent.html +4 -2
  92. package/docs/interfaces/ApexCellValueChangingEvent.html +3 -2
  93. package/docs/interfaces/ApexGridEventMap.html +5 -2
  94. package/docs/interfaces/ApexPageChangedEvent.html +3 -1
  95. package/docs/interfaces/ApexRowEditEndedEvent.html +2 -1
  96. package/docs/interfaces/ApexRowEditStartedEvent.html +2 -1
  97. package/docs/interfaces/ApexRowMovingEvent.html +9 -3
  98. package/docs/interfaces/ApexRowPinningEvent.html +1 -1
  99. package/docs/interfaces/ApexTreeRowExpandedEvent.html +4 -1
  100. package/docs/interfaces/ApexTreeRowExpandingEvent.html +4 -1
  101. package/docs/interfaces/BaseApexCellContext.html +18 -5
  102. package/docs/interfaces/BaseApexEditorContext.html +20 -5
  103. package/docs/interfaces/BaseFilterExpression.html +3 -3
  104. package/docs/interfaces/CSVExportOptions.html +12 -4
  105. package/docs/interfaces/DataPipelineConfiguration.html +4 -9
  106. package/docs/interfaces/ExportOptions.html +5 -3
  107. package/docs/interfaces/GridExpansionConfiguration.html +6 -2
  108. package/docs/interfaces/GridRowReorderingConfiguration.html +1 -1
  109. package/docs/interfaces/GridSelectionConfiguration.html +19 -4
  110. package/docs/interfaces/PaginationConfiguration.html +11 -4
  111. package/docs/interfaces/PaginationState.html +4 -2
  112. package/docs/interfaces/SetStateResult.html +3 -2
  113. package/docs/interfaces/ValidatorContext.html +7 -2
  114. package/docs/media/_token-values.scss +118 -0
  115. package/docs/media/package.json +9 -5
  116. package/docs/modules.html +1 -1
  117. package/docs/types/DataPipelineParams.html +2 -2
  118. package/docs/types/ExportSource.html +6 -4
  119. package/docs/types/OperandKeys.html +9 -0
  120. package/docs/types/PaginationMode.html +5 -4
  121. package/docs/variables/EN_LOCALE.html +2 -2
  122. package/i18n/en.d.ts +7 -0
  123. package/i18n/en.js +7 -0
  124. package/i18n/en.js.map +1 -1
  125. package/i18n/es.js +7 -0
  126. package/i18n/es.js.map +1 -1
  127. package/internal/column-types.d.ts +38 -2
  128. package/internal/column-types.js +83 -55
  129. package/internal/column-types.js.map +1 -1
  130. package/internal/constants.d.ts +1 -2
  131. package/internal/constants.js +0 -4
  132. package/internal/constants.js.map +1 -1
  133. package/internal/document-styles.d.ts +30 -0
  134. package/internal/document-styles.js +48 -0
  135. package/internal/document-styles.js.map +1 -0
  136. package/internal/export.d.ts +45 -7
  137. package/internal/export.js +39 -34
  138. package/internal/export.js.map +1 -1
  139. package/internal/feature-module.d.ts +67 -3
  140. package/internal/feature-module.js +6 -0
  141. package/internal/feature-module.js.map +1 -1
  142. package/internal/flip.d.ts +28 -16
  143. package/internal/flip.js +42 -10
  144. package/internal/flip.js.map +1 -1
  145. package/internal/index.d.ts +7 -5
  146. package/internal/index.js +6 -4
  147. package/internal/index.js.map +1 -1
  148. package/internal/normalize-case.d.ts +8 -1
  149. package/internal/normalize-case.js +3 -2
  150. package/internal/normalize-case.js.map +1 -1
  151. package/internal/state-snapshot.d.ts +60 -8
  152. package/internal/state-snapshot.js +87 -19
  153. package/internal/state-snapshot.js.map +1 -1
  154. package/internal/types.d.ts +92 -21
  155. package/internal/types.js.map +1 -1
  156. package/internal/utils.d.ts +34 -6
  157. package/internal/utils.js +30 -21
  158. package/internal/utils.js.map +1 -1
  159. package/internal/validators.js +1 -0
  160. package/internal/validators.js.map +1 -1
  161. package/operations/filter/operands/number.js +9 -6
  162. package/operations/filter/operands/number.js.map +1 -1
  163. package/operations/filter/operands/string.js +2 -3
  164. package/operations/filter/operands/string.js.map +1 -1
  165. package/operations/filter/types.d.ts +14 -2
  166. package/operations/filter/types.js.map +1 -1
  167. package/operations/filter.d.ts +6 -0
  168. package/operations/filter.js +23 -7
  169. package/operations/filter.js.map +1 -1
  170. package/operations/quick-filter.js +2 -5
  171. package/operations/quick-filter.js.map +1 -1
  172. package/operations/sort.d.ts +21 -2
  173. package/operations/sort.js +41 -14
  174. package/operations/sort.js.map +1 -1
  175. package/package.json +9 -4
  176. package/setup.d.ts +11 -0
  177. package/setup.js +9 -19
  178. package/setup.js.map +1 -1
  179. package/styles/_token-values.css.d.ts +1 -0
  180. package/styles/_token-values.css.js +3 -0
  181. package/styles/_token-values.css.js.map +1 -0
  182. package/styles/_tokens.css.js +1 -1
  183. package/styles/_tokens.css.js.map +1 -1
  184. package/styles/body-cell/body-cell.css.js +1 -1
  185. package/styles/body-cell/body-cell.css.js.map +1 -1
  186. package/styles/body-cell/cell-renderers.css.js +1 -1
  187. package/styles/body-cell/cell-renderers.css.js.map +1 -1
  188. package/styles/body-row/body-row.css.js +1 -1
  189. package/styles/body-row/body-row.css.js.map +1 -1
  190. package/styles/document-tokens.css.d.ts +1 -0
  191. package/styles/document-tokens.css.js +3 -0
  192. package/styles/document-tokens.css.js.map +1 -0
  193. package/styles/filter-row/filter-row.css.js +1 -1
  194. package/styles/filter-row/filter-row.css.js.map +1 -1
  195. package/styles/grid/_grid.base.css.js +1 -1
  196. package/styles/grid/_grid.base.css.js.map +1 -1
  197. package/styles/grid/grid.css.js +1 -1
  198. package/styles/grid/grid.css.js.map +1 -1
  199. package/styles/group-header-row/group-header-row.css.js +1 -1
  200. package/styles/group-header-row/group-header-row.css.js.map +1 -1
  201. package/styles/header-cell/header-cell.css.js +1 -1
  202. package/styles/header-cell/header-cell.css.js.map +1 -1
  203. package/styles/header-row/header-row.base.css.js +1 -1
  204. package/styles/header-row/header-row.base.css.js.map +1 -1
  205. package/styles/paginator/paginator.css.js +1 -1
  206. package/styles/paginator/paginator.css.js.map +1 -1
  207. package/styles/toolbar/toolbar.css.js +1 -1
  208. package/styles/toolbar/toolbar.css.js.map +1 -1
  209. package/tokens.css +78 -0
  210. package/LICENSE +0 -80
  211. package/docs/media/LICENSE +0 -80
  212. package/docs/media/_tokens.scss +0 -214
  213. package/operations/pagination.d.ts +0 -19
  214. package/operations/pagination.js +0 -12
  215. package/operations/pagination.js.map +0 -1
@@ -1,15 +1,31 @@
1
- import { NAVIGATION_STATE, SENTINEL_NODE } from '../internal/constants.js';
2
- import { GRID_ROW_TAG } from '../internal/tags.js';
1
+ import { revealWithin } from '../components/virtualizer.js';
2
+ import { SENTINEL_NODE } from '../internal/constants.js';
3
+ import { GRID_BODY, GRID_CELL_TAG, GRID_ROW_TAG } from '../internal/tags.js';
3
4
  import { getDisplayColumns, isRTL } from '../internal/utils.js';
5
+ const RELATIVE_KEYS = new Set([
6
+ 'ArrowDown',
7
+ 'ArrowUp',
8
+ 'ArrowLeft',
9
+ 'ArrowRight',
10
+ 'Home',
11
+ 'End',
12
+ 'PageUp',
13
+ 'PageDown',
14
+ ]);
15
+ function deepActiveElement() {
16
+ let el = document.activeElement;
17
+ while (el?.shadowRoot?.activeElement)
18
+ el = el.shadowRoot.activeElement;
19
+ return el;
20
+ }
4
21
  export class NavigationController {
5
22
  get virtualizer() {
6
23
  return this.host.scrollContainer;
7
24
  }
8
25
  get nextNode() {
9
- const node = this.state.get('current');
10
- return node === SENTINEL_NODE
26
+ return this._active === SENTINEL_NODE
11
27
  ? { column: this.firstColumn, row: 0 }
12
- : { ...node };
28
+ : { ...this._active };
13
29
  }
14
30
  get columns() {
15
31
  return getDisplayColumns(this.host.columns);
@@ -21,49 +37,133 @@ export class NavigationController {
21
37
  const first = this.visibleColumns.at(0);
22
38
  return (first?.key ?? this.host.columns[0]?.key);
23
39
  }
40
+ get navigable() {
41
+ return this.host.pageItems.length > 0 && this.visibleColumns.length > 0;
42
+ }
24
43
  getPreviousColumn(key) {
25
44
  const columns = this.visibleColumns;
26
45
  const idx = columns.findIndex((column) => column.key === key);
27
- return columns[Math.max(idx - 1, 0)].key;
46
+ return columns[Math.max(idx - 1, 0)]?.key ?? key;
28
47
  }
29
48
  getNextColumn(key) {
30
49
  const columns = this.visibleColumns;
31
50
  const idx = columns.findIndex((column) => column.key === key);
32
- return columns[Math.min(idx + 1, columns.length - 1)].key;
51
+ return columns[Math.min(idx + 1, columns.length - 1)]?.key ?? key;
52
+ }
53
+ findRow(index) {
54
+ return Array.from(this.virtualizer.querySelectorAll(GRID_ROW_TAG)).find((row) => row.index === index);
55
+ }
56
+ findCell(node) {
57
+ const row = this.findRow(node.row);
58
+ if (!row)
59
+ return undefined;
60
+ return (row.presentedCell ??
61
+ row.cells.find((cell) => cell.column.key === node.column));
33
62
  }
34
63
  scrollToCell(node) {
35
- const row = Array.from(this.virtualizer.querySelectorAll(GRID_ROW_TAG)).find((row) => row.index === node.row);
36
- if (row) {
37
- row.cells
38
- .find((cell) => cell.column.key === node.column)
39
- ?.scrollIntoView({ block: 'nearest' });
64
+ const cell = this.findCell(node);
65
+ if (cell) {
66
+ revealWithin(this.host, cell, {
67
+ block: 'nearest',
68
+ inline: 'nearest',
69
+ insets: this.scrollInsets(),
70
+ });
40
71
  }
41
72
  }
42
- findActiveCell() {
43
- const row = Array.from(this.virtualizer.querySelectorAll(GRID_ROW_TAG)).find((el) => el.index === this.active.row);
44
- return row?.cells.find((cell) => cell.column.key === this.active.column);
73
+ scrollToRow(index) {
74
+ this.virtualizer
75
+ .element(index)
76
+ ?.scrollIntoView({ block: 'nearest', insets: this.scrollInsets() });
45
77
  }
46
- focusActiveCell() {
47
- const node = this.state.get('current');
48
- if (!node || node === SENTINEL_NODE)
78
+ #revealing;
79
+ revealCell(node) {
80
+ this.#revealing = node;
81
+ if (this.findCell(node)) {
82
+ this.scrollToCell(node);
49
83
  return;
50
- void (async () => {
51
- await this.host.updateComplete;
52
- let cell = this.findActiveCell();
53
- if (!cell) {
54
- await new Promise(requestAnimationFrame);
55
- cell = this.findActiveCell();
84
+ }
85
+ this.scrollToRow(node.row);
86
+ void this.virtualizer.settled().then(() => {
87
+ if (this.#revealing === node)
88
+ this.scrollToCell(node);
89
+ });
90
+ }
91
+ towardsInlineEnd(key) {
92
+ return isRTL(this.host) === (key === 'ArrowLeft');
93
+ }
94
+ scrollInsets() {
95
+ const host = this.host;
96
+ const root = host.shadowRoot;
97
+ if (!root)
98
+ return { top: 0, right: 0, bottom: 0, left: 0 };
99
+ const box = host.getBoundingClientRect();
100
+ const top = box.top + host.clientTop;
101
+ const left = box.left + host.clientLeft;
102
+ const bottom = top + host.clientHeight;
103
+ const right = left + host.clientWidth;
104
+ let insetTop = 0;
105
+ for (const el of root.querySelectorAll('apex-grid-header-row, [part~="pinned-top"]')) {
106
+ insetTop = Math.max(insetTop, el.getBoundingClientRect().bottom - top);
107
+ }
108
+ let insetBottom = 0;
109
+ for (const el of root.querySelectorAll('apex-grid-paginator, [part~="pinned-bottom"]')) {
110
+ insetBottom = Math.max(insetBottom, bottom - el.getBoundingClientRect().top);
111
+ }
112
+ let insetLeft = 0;
113
+ let insetRight = 0;
114
+ const headerRow = root.querySelector('apex-grid-header-row');
115
+ const pinned = headerRow?.shadowRoot?.querySelectorAll('[data-pinned="start"], [data-pinned="end"]');
116
+ if (pinned?.length) {
117
+ const rtl = isRTL(host);
118
+ for (const el of pinned) {
119
+ const rect = el.getBoundingClientRect();
120
+ const atLeft = (el.getAttribute('data-pinned') === 'start') !== rtl;
121
+ if (atLeft)
122
+ insetLeft = Math.max(insetLeft, rect.right - left);
123
+ else
124
+ insetRight = Math.max(insetRight, right - rect.left);
56
125
  }
57
- cell?.focus({ preventScroll: true });
58
- })();
126
+ }
127
+ const clamp = (inset) => Math.max(0, Math.round(inset));
128
+ return {
129
+ top: clamp(insetTop),
130
+ right: clamp(insetRight),
131
+ bottom: clamp(insetBottom),
132
+ left: clamp(insetLeft),
133
+ };
134
+ }
135
+ focusActiveCell(reveal = false) {
136
+ void this.#focusActive(reveal);
137
+ }
138
+ async #focusActive(reveal = false) {
139
+ const node = this._active;
140
+ if (node === SENTINEL_NODE)
141
+ return;
142
+ await this.host.updateComplete;
143
+ let cell = this.findCell(node);
144
+ if (!cell) {
145
+ await this.virtualizer.settled();
146
+ cell = this.findCell(node);
147
+ }
148
+ if (!cell) {
149
+ await new Promise(requestAnimationFrame);
150
+ cell = this.findCell(node);
151
+ }
152
+ if (!cell && this._active === node) {
153
+ await this.virtualizer.revealed();
154
+ cell = this.findCell(node);
155
+ }
156
+ if (!cell || this._active !== node)
157
+ return;
158
+ if (reveal)
159
+ this.scrollToCell(node);
160
+ cell.focus({ preventScroll: true });
59
161
  }
60
162
  get active() {
61
163
  return this._active;
62
164
  }
63
165
  set active(node) {
64
166
  this._active = node;
65
- this.state.set('previous', this._active);
66
- this.state.set('current', node);
67
167
  this.host.requestUpdate();
68
168
  }
69
169
  constructor(host) {
@@ -79,10 +179,258 @@ export class NavigationController {
79
179
  PageDown: this.pageDown,
80
180
  ' ': this.toggleSelection,
81
181
  }));
82
- this.state = NAVIGATION_STATE;
83
182
  this._active = SENTINEL_NODE;
183
+ this.#revealing = null;
184
+ this.#ownsFocus = false;
185
+ this.#focusedRecord = null;
186
+ this.#focusTarget = null;
187
+ this.#blurred = null;
188
+ this.#moved = null;
189
+ this.#reconcile = 0;
190
+ this.#reconciling = 0;
191
+ this.#parked = null;
192
+ this.#focusingBody = false;
193
+ this.#detached = null;
84
194
  this.host.addController(this);
85
195
  }
196
+ get ownsFocus() {
197
+ this.#settleLoss();
198
+ return this.#ownsFocus;
199
+ }
200
+ #ownsFocus;
201
+ #settleLoss() {
202
+ if (!this.#ownsFocus || this.#reconciling || this.#parked || this.#detached)
203
+ return;
204
+ if (this.#lost())
205
+ this.#ownsFocus = false;
206
+ }
207
+ #focusedRecord;
208
+ #focusTarget;
209
+ #blurred;
210
+ #moved;
211
+ #reconcile;
212
+ #reconciling;
213
+ #parked;
214
+ #focusingBody;
215
+ #detached;
216
+ get forwardsBodyFocus() {
217
+ return !this.#parked && !this.#focusingBody;
218
+ }
219
+ focusEntered(event, owned) {
220
+ this.#blurred = null;
221
+ this.#moved = null;
222
+ this.#ownsFocus = owned;
223
+ const el = event.composedPath()[0];
224
+ const row = owned ? this.#locate(el)?.row : undefined;
225
+ this.#focusedRecord = row?.data ?? null;
226
+ this.#focusTarget = owned ? { el, row } : null;
227
+ }
228
+ focusLeft(event, owned) {
229
+ if (!owned)
230
+ return;
231
+ if (event.relatedTarget) {
232
+ this.#ownsFocus = false;
233
+ return;
234
+ }
235
+ const target = event.composedPath()[0];
236
+ const from = this.#locate(target)?.row;
237
+ this.#blurred = target;
238
+ queueMicrotask(() => {
239
+ if (this.#blurred !== target)
240
+ return;
241
+ this.#blurred = null;
242
+ if (!this.#reconciling) {
243
+ this.#ownsFocus = false;
244
+ return;
245
+ }
246
+ if (!target.isConnected)
247
+ return;
248
+ const to = this.#locate(target)?.row;
249
+ if (to && to !== from)
250
+ this.#moved = target;
251
+ else
252
+ this.#ownsFocus = false;
253
+ });
254
+ }
255
+ reconcileFocus() {
256
+ this.#settleLoss();
257
+ if (!this.#ownsFocus || !this.host.rowRecycling)
258
+ return;
259
+ const token = ++this.#reconcile;
260
+ this.#reconciling = token;
261
+ void (async () => {
262
+ try {
263
+ await Promise.resolve();
264
+ await this.virtualizer.rendered();
265
+ if (token !== this.#reconcile || this.#parked)
266
+ return;
267
+ if (!this.#ownsFocus || !this.host.isConnected)
268
+ return;
269
+ const focused = deepActiveElement();
270
+ if (focused && focused !== document.body) {
271
+ const at = this.#locate(focused);
272
+ if (!at?.row)
273
+ return;
274
+ const active = this._active;
275
+ const onActive = at.row.index === active.row && (!at.cell || at.cell.column.key === active.column);
276
+ if (onActive || at.row.data === this.#focusedRecord)
277
+ return;
278
+ }
279
+ await this.#restoreFocus();
280
+ }
281
+ finally {
282
+ if (token === this.#reconcile) {
283
+ this.#reconciling = 0;
284
+ this.#moved = null;
285
+ if (!this.#parked && this.#lost())
286
+ this.#ownsFocus = false;
287
+ }
288
+ }
289
+ })();
290
+ }
291
+ async #restoreFocus() {
292
+ const moved = this.#moved ?? this.#silentlyMoved();
293
+ this.#moved = null;
294
+ if (moved?.isConnected && moved instanceof HTMLElement) {
295
+ const at = this.#locate(moved);
296
+ if (at?.row && at.row.data === this.#focusedRecord) {
297
+ moved.focus({ preventScroll: true });
298
+ return;
299
+ }
300
+ }
301
+ const edit = this.editing?.activeCell;
302
+ const row = edit && this.#renderedRows().find((each) => each.data === edit.data);
303
+ const cell = row?.cells.find((each) => each.column.key === edit?.columnKey);
304
+ if (cell) {
305
+ await cell.updateComplete;
306
+ const editor = cell.shadowRoot?.querySelector('input, [data-apex-editor]');
307
+ if (this.#lost())
308
+ (editor ?? cell).focus({ preventScroll: true });
309
+ return;
310
+ }
311
+ if (this._active !== SENTINEL_NODE && this.virtualizer.isRendered(this._active.row)) {
312
+ await this.#focusActive();
313
+ return;
314
+ }
315
+ this.#focusBody();
316
+ }
317
+ #silentlyMoved() {
318
+ const target = this.#focusTarget;
319
+ if (!target?.el.isConnected || !this.#lost())
320
+ return null;
321
+ const to = this.#locate(target.el)?.row;
322
+ return to && to !== target.row ? target.el : null;
323
+ }
324
+ parkFocus() {
325
+ this.#settleLoss();
326
+ if (!this.host.rowRecycling || !this.#ownsFocus)
327
+ return null;
328
+ const at = this.#locate(deepActiveElement());
329
+ const park = { record: at?.row?.data ?? null };
330
+ this.#parked = park;
331
+ return async () => {
332
+ await this.virtualizer.rendered();
333
+ if (this.#parked !== park)
334
+ return;
335
+ this.#parked = null;
336
+ if (!this.#ownsFocus || !this.host.isConnected)
337
+ return;
338
+ const focused = deepActiveElement();
339
+ if (focused && focused !== document.body) {
340
+ const now = this.#locate(focused);
341
+ if (!now?.row)
342
+ return;
343
+ if (now.cell && now.cell.editing)
344
+ return;
345
+ if (now.row.data === park.record)
346
+ return;
347
+ }
348
+ this.#focusBody();
349
+ };
350
+ }
351
+ #focusBody() {
352
+ this.#focusingBody = true;
353
+ try {
354
+ this.virtualizer.focus({ preventScroll: true });
355
+ }
356
+ finally {
357
+ this.#focusingBody = false;
358
+ }
359
+ }
360
+ #lost() {
361
+ const focused = deepActiveElement();
362
+ return !focused || focused === document.body;
363
+ }
364
+ #renderedRows() {
365
+ return Array.from(this.virtualizer.querySelectorAll(GRID_ROW_TAG));
366
+ }
367
+ #locate(el) {
368
+ const body = this.virtualizer;
369
+ if (!body)
370
+ return null;
371
+ let row;
372
+ let cell;
373
+ let node = el instanceof Node ? el : null;
374
+ while (node) {
375
+ if (node === body)
376
+ return { row, cell };
377
+ if (node instanceof Element) {
378
+ const name = node.localName;
379
+ if (name === GRID_BODY)
380
+ return null;
381
+ if (!cell && name === GRID_CELL_TAG)
382
+ cell = node;
383
+ if (!row && name === GRID_ROW_TAG)
384
+ row = node;
385
+ }
386
+ node = node instanceof ShadowRoot ? node.host : node.parentNode;
387
+ }
388
+ return null;
389
+ }
390
+ hostUpdate() {
391
+ const node = this._active;
392
+ if (node === SENTINEL_NODE)
393
+ return;
394
+ const count = this.host.pageItems.length;
395
+ if (!count || !this.visibleColumns.length) {
396
+ this._active = SENTINEL_NODE;
397
+ return;
398
+ }
399
+ const edit = this.editing?.activeCell;
400
+ if (edit && node.row === edit.rowIndex && node.column === edit.columnKey) {
401
+ const index = this.host.pageItems.indexOf(edit.data);
402
+ if (index >= 0 && index !== edit.rowIndex && this.virtualizer.isRendered(index)) {
403
+ this._active = { column: node.column, row: index };
404
+ return;
405
+ }
406
+ }
407
+ const row = Math.min(node.row, count - 1);
408
+ const column = this.#nearestVisibleColumn(node.column);
409
+ if (row !== node.row || column !== node.column) {
410
+ this._active = { column, row };
411
+ }
412
+ }
413
+ #nearestVisibleColumn(key) {
414
+ const columns = this.columns;
415
+ const index = columns.findIndex((column) => column.key === key);
416
+ if (index < 0)
417
+ return this.firstColumn;
418
+ for (let distance = 0; distance < columns.length; distance++) {
419
+ for (const at of [index + distance, index - distance]) {
420
+ const column = columns[at];
421
+ if (column && !column.hidden)
422
+ return column.key;
423
+ }
424
+ }
425
+ return this.firstColumn;
426
+ }
427
+ isPresentedRow(index) {
428
+ const data = this.host.pageItems[index];
429
+ if (!data)
430
+ return false;
431
+ const state = this.host.stateController;
432
+ return state.presentRow(data, { columns: this.host.columns, rowIndex: index }) !== null;
433
+ }
86
434
  home() {
87
435
  this.active = Object.assign(this.nextNode, { column: this.firstColumn });
88
436
  this.scrollToCell(this.active);
@@ -94,74 +442,105 @@ export class NavigationController {
94
442
  }
95
443
  ctrlHome() {
96
444
  this.active = { column: this.firstColumn, row: 0 };
97
- this.virtualizer.element(0)?.scrollIntoView({ block: 'nearest' });
98
- this.scrollToCell(this.active);
445
+ this.scrollToRow(0);
99
446
  }
100
447
  ctrlEnd() {
101
- const row = Math.max(0, this.host.pageItems.length - 1);
448
+ const row = this.host.pageItems.length - 1;
102
449
  const last = this.visibleColumns.at(-1)?.key ?? this.firstColumn;
103
450
  this.active = { column: last, row };
104
- this.virtualizer.element(row)?.scrollIntoView({ block: 'nearest' });
105
- this.scrollToCell(this.active);
451
+ this.scrollToRow(row);
106
452
  }
107
453
  pageStep() {
108
- const rowEl = this.virtualizer.element(this.nextNode.row);
109
- const rowHeight = rowEl?.offsetHeight || 0;
110
- const viewport = this.host.clientHeight || 0;
111
- if (!rowHeight || !viewport)
454
+ const host = this.host;
455
+ let rowHeight = 0;
456
+ for (const row of this.virtualizer.querySelectorAll(GRID_ROW_TAG)) {
457
+ const height = row.offsetHeight;
458
+ if (height > 0 && (!rowHeight || height < rowHeight))
459
+ rowHeight = height;
460
+ }
461
+ const { top, bottom } = this.scrollInsets();
462
+ const viewport = host.clientHeight - top - bottom;
463
+ if (!rowHeight || viewport <= 0)
112
464
  return 10;
113
- const rows = Math.floor(viewport / rowHeight) - 1;
114
- return rows > 0 ? rows : 10;
465
+ return Math.max(1, Math.floor(viewport / rowHeight) - 1);
115
466
  }
116
467
  pageUp() {
117
468
  const next = this.nextNode;
118
469
  this.active = Object.assign(next, { row: Math.max(0, next.row - this.pageStep()) });
119
- this.virtualizer.element(this.active.row)?.scrollIntoView({ block: 'nearest' });
470
+ this.scrollToRow(this.active.row);
120
471
  }
121
472
  pageDown() {
122
473
  const next = this.nextNode;
123
474
  this.active = Object.assign(next, {
124
475
  row: Math.min(this.host.pageItems.length - 1, next.row + this.pageStep()),
125
476
  });
126
- this.virtualizer.element(this.active.row)?.scrollIntoView({ block: 'nearest' });
477
+ this.scrollToRow(this.active.row);
127
478
  }
128
479
  arrowDown() {
129
480
  const next = this.nextNode;
130
481
  this.active = Object.assign(next, {
131
482
  row: Math.min(next.row + 1, this.host.pageItems.length - 1),
132
483
  });
133
- this.virtualizer.element(next.row)?.scrollIntoView({ block: 'nearest' });
484
+ this.scrollToRow(next.row);
134
485
  }
135
486
  arrowUp() {
136
487
  const next = this.nextNode;
137
488
  this.active = Object.assign(next, { row: Math.max(0, next.row - 1) });
138
- this.virtualizer.element(next.row)?.scrollIntoView({ block: 'nearest' });
489
+ this.scrollToRow(next.row);
139
490
  }
140
491
  arrowLeft() {
141
- this.#moveInline('left');
492
+ this.#moveInline('ArrowLeft');
142
493
  }
143
494
  arrowRight() {
144
- this.#moveInline('right');
495
+ this.#moveInline('ArrowRight');
145
496
  }
146
- #moveInline(direction) {
147
- const towardsEnd = isRTL(this.host) === (direction === 'left');
497
+ #moveInline(key) {
148
498
  const next = this.nextNode;
149
- const column = towardsEnd
499
+ const column = this.towardsInlineEnd(key)
150
500
  ? this.getNextColumn(next.column)
151
501
  : this.getPreviousColumn(next.column);
152
502
  this.active = Object.assign(next, { column });
153
503
  this.scrollToCell(this.active);
154
504
  }
155
505
  toggleSelection() {
156
- const data = this.host.pageItems[this.active.row];
157
- if (!data)
506
+ const data = this.host.pageItems[this._active.row];
507
+ if (!data || this.isPresentedRow(this._active.row))
158
508
  return;
159
509
  void this.host.toggleRowSelection(data);
160
510
  }
161
- hostConnected() { }
511
+ hostConnected() {
512
+ const detached = this.#detached;
513
+ this.#detached = null;
514
+ if (detached)
515
+ this.#refocusAfterMove(detached.node);
516
+ }
162
517
  hostDisconnected() {
518
+ const dropped = this.#blurred !== null || (this.#ownsFocus && this.#lost());
519
+ const moved = dropped && this.host.rowRecycling;
520
+ const detached = moved ? { node: this._active } : null;
163
521
  this.active = SENTINEL_NODE;
164
- this.state = NAVIGATION_STATE;
522
+ this.#detached = detached;
523
+ if (!detached)
524
+ this.#ownsFocus = false;
525
+ if (detached) {
526
+ setTimeout(() => {
527
+ if (this.#detached !== detached)
528
+ return;
529
+ this.#detached = null;
530
+ this.#ownsFocus = false;
531
+ });
532
+ }
533
+ }
534
+ #refocusAfterMove(node) {
535
+ if (node !== SENTINEL_NODE)
536
+ this.active = node;
537
+ void (async () => {
538
+ await this.host.updateComplete;
539
+ if (!this.#lost() || this._active !== node || !this.host.isConnected)
540
+ return;
541
+ const cell = node === SENTINEL_NODE ? undefined : this.findCell(node);
542
+ (cell ?? this.virtualizer).focus({ preventScroll: true });
543
+ })();
165
544
  }
166
545
  get editing() {
167
546
  return this.host.stateController.editing;
@@ -171,8 +550,7 @@ export class NavigationController {
171
550
  return false;
172
551
  if (event.ctrlKey || event.metaKey || event.altKey)
173
552
  return false;
174
- const node = this.state.get('current');
175
- if (!node || node === SENTINEL_NODE)
553
+ if (this._active === SENTINEL_NODE || this.isPresentedRow(this._active.row))
176
554
  return false;
177
555
  const editing = this.editing;
178
556
  if (!editing?.enabled)
@@ -196,10 +574,12 @@ export class NavigationController {
196
574
  case 'ArrowUp':
197
575
  case 'ArrowDown': {
198
576
  event.preventDefault();
577
+ const row = reorder.grabbed;
199
578
  const next = reorder.moveGrabbed(event.key === 'ArrowDown' ? 1 : -1);
200
- if (next >= 0) {
579
+ if (next >= 0 && row) {
201
580
  this.active = Object.assign(this.nextNode, { row: next });
202
- this.virtualizer.element(next)?.scrollIntoView({ block: 'nearest' });
581
+ this.scrollToRow(next);
582
+ this.#followRecord(row);
203
583
  }
204
584
  return true;
205
585
  }
@@ -208,20 +588,39 @@ export class NavigationController {
208
588
  event.preventDefault();
209
589
  reorder.drop();
210
590
  return true;
211
- case 'Escape':
591
+ case 'Escape': {
212
592
  event.preventDefault();
593
+ const row = reorder.grabbed;
213
594
  reorder.cancelGrab();
595
+ if (row)
596
+ this.#followRecord(row);
214
597
  return true;
598
+ }
215
599
  default:
216
600
  return false;
217
601
  }
218
602
  }
219
- if (event.key === ' ' && this.active.row >= 0) {
603
+ if (event.key === ' ' && this.active.row >= 0 && !this.isPresentedRow(this.active.row)) {
220
604
  event.preventDefault();
221
605
  return reorder.grab(this.active.row);
222
606
  }
223
607
  return false;
224
608
  }
609
+ #followRecord(record) {
610
+ const node = this._active;
611
+ void this.host.pipelineSettled().then(() => {
612
+ if (this._active !== node || node === SENTINEL_NODE)
613
+ return;
614
+ const row = this.host.pageItems.indexOf(record);
615
+ if (row < 0)
616
+ return;
617
+ if (row !== node.row) {
618
+ this.active = { ...node, row };
619
+ this.scrollToRow(row);
620
+ }
621
+ this.focusActiveCell();
622
+ });
623
+ }
225
624
  navigate(event) {
226
625
  if (this.handleReorderKey(event))
227
626
  return;
@@ -239,18 +638,33 @@ export class NavigationController {
239
638
  }
240
639
  if (event.key === 'Home' || event.key === 'End') {
241
640
  event.preventDefault();
641
+ if (!this.navigable)
642
+ return;
242
643
  if (event.key === 'Home') {
243
644
  this.ctrlHome();
244
645
  }
245
646
  else {
246
647
  this.ctrlEnd();
247
648
  }
248
- this.focusActiveCell();
649
+ this.focusActiveCell(true);
249
650
  return;
250
651
  }
251
652
  }
252
653
  if (this.handleEditKey(event))
253
654
  return;
655
+ if (RELATIVE_KEYS.has(event.key)) {
656
+ event.preventDefault();
657
+ if (!this.navigable)
658
+ return;
659
+ if (this._active === SENTINEL_NODE) {
660
+ this.active = { column: this.firstColumn, row: 0 };
661
+ if (event.key !== 'End' && event.key !== 'PageDown') {
662
+ this.scrollToRow(0);
663
+ this.focusActiveCell(true);
664
+ return;
665
+ }
666
+ }
667
+ }
254
668
  if (this.handlers.has(event.key)) {
255
669
  event.preventDefault();
256
670
  this.handlers.get(event.key).call(this);