-
Notifications
You must be signed in to change notification settings - Fork 25
/
list-item-generic-layout.js
530 lines (453 loc) · 16 KB
/
list-item-generic-layout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
import { css, html, LitElement } from 'lit';
import { findComposedAncestor, isComposedAncestor } from '../../helpers/dom.js';
import { getComposedActiveElement, getFirstFocusableDescendant, getFocusableDescendants, getLastFocusableDescendant, getNextFocusable, getPreviousFocusable } from '../../helpers/focus.js';
import { isInteractiveDescendant } from '../../mixins/interactive/interactive-mixin.js';
import { RtlMixin } from '../../mixins/rtl/rtl-mixin.js';
const keyCodes = {
DOWN: 40,
END: 35,
ENTER: 13,
HOME: 36,
LEFT: 37,
PAGEUP: 33,
PAGEDOWN: 34,
RIGHT: 39,
SPACE: 32,
UP: 38
};
/**
* A component for generating a list item's layout with forced focus ordering and grid support.
* Focusable items placed in the "content" slot will have their focus removed; use the content-action
* slot for such items.
* @slot outside-control - Control associated on the far left, e.g., a drag-n-drop handle
* @slot outside-control-action - An action area associated with the outside control
* @slot control - Main control beside the outside control, e.g., a checkbox
* @slot control-action - Action area associated with the main control
* @slot content - Content of the list item, such as that in a list-item-content component.
* @slot content-action - Action associated with the content, such as a navigation link
* @slot actions - Other actions for the list item on the far right, such as a context menu
* @slot nested - Optional `d2l-list` for creating nested lists
*/
class ListItemGenericLayout extends RtlMixin(LitElement) {
static get properties() {
return {
/**
* How to align content in the nested slot
* @type {'content'|'control'}
*/
alignNested: { type: String, reflect: true, attribute: 'align-nested' },
/**
* Whether to constrain actions so they do not fill the item. Required if slotted content is interactive.
* @type {boolean}
*/
noPrimaryAction: { type: Boolean, attribute: 'no-primary-action', reflect: true },
/**
* @ignore
*/
// eslint-disable-next-line lit/no-native-attributes
role: { type: String, reflect: true },
/**
* Specifies whether the grid is active or not
* @type {boolean}
*/
gridActive: { type: Boolean, attribute: 'grid-active' }
};
}
static get styles() {
return css`
:host {
display: grid;
grid-template-columns:
[start outside-control-start] minmax(0, min-content)
[color-start outside-control-end] minmax(0, min-content)
[expand-collapse-start color-end] minmax(0, min-content)
[control-start expand-collapse-end] minmax(0, min-content)
[control-end content-start] minmax(0, auto)
[content-end actions-start] minmax(0, min-content)
[end actions-end];
grid-template-rows:
[start add-top-start] minmax(0, min-content)
[add-top-end main-start] minmax(0, min-content)
[main-end nested-start] minmax(0, min-content)
[nested-end add-start] minmax(0, min-content)
[add-end end];
}
:host([align-nested="control"]) ::slotted([slot="nested"]) {
grid-column: control-start / end;
}
::slotted([slot="drop-target"]) {
grid-column: start / end;
}
::slotted([slot="outside-control"]),
::slotted([slot="color-indicator"]),
::slotted([slot="expand-collapse"]),
::slotted([slot="control"]),
::slotted([slot="content"]),
::slotted([slot="actions"]),
::slotted([slot="outside-control-action"]),
::slotted([slot="before-content"]),
::slotted([slot="control-action"]),
::slotted([slot="content-action"]),
::slotted([slot="outside-control-container"]),
::slotted([slot="control-container"]),
::slotted([slot="drop-target"]) {
grid-row: 2 / 3;
}
::slotted([slot="outside-control"]) {
grid-column: outside-control-start / outside-control-end;
}
::slotted([slot="outside-control"]:not(.handle-only)) {
pointer-events: none;
}
::slotted([slot="expand-collapse"]) {
cursor: pointer;
grid-column: expand-collapse-start / expand-collapse-end;
}
::slotted([slot="control"]) {
grid-column: control-start / control-end;
pointer-events: none;
width: 2.2rem;
}
::slotted([slot="content"]) {
grid-column: content-start / content-end;
}
::slotted([slot="color-indicator"]) {
grid-column: color-start / color-end;
}
::slotted([slot="before-content"]) {
grid-column: color-start / content-start;
}
::slotted([slot="control-action"]) ~ ::slotted([slot="content"]),
::slotted([slot="outside-control-action"]) ~ ::slotted([slot="content"]) {
pointer-events: unset;
}
slot[name="actions"] {
white-space: nowrap;
}
::slotted([slot="actions"]) {
grid-column: actions-start / actions-end;
justify-self: end;
}
::slotted([slot="outside-control-action"]) {
grid-column: start / end;
}
:host([no-primary-action]) ::slotted([slot="outside-control-action"]) {
grid-column: start / outside-control-end;
}
::slotted([slot="content-action"]) {
grid-column: content-start / end;
}
:host([no-primary-action]) ::slotted([slot="content-action"]) {
display: none;
}
::slotted([slot="control-action"]) {
grid-column-start: control-start;
}
:host(:not([no-primary-action])) ::slotted([slot="control-action"]),
:host(:not([no-primary-action])) ::slotted([slot="outside-control-action"]) {
grid-column-end: end;
}
::slotted([slot="outside-control-container"]) {
grid-column: start / end;
}
::slotted([slot="control-container"]) {
grid-column: color-start / end;
}
::slotted([slot="nested"]) {
grid-column: content-start / end;
grid-row: nested;
}
::slotted([slot="add"]) {
grid-row: add;
}
::slotted([slot="add-top"]) {
grid-row: add-top;
}
::slotted([slot="add-top"]),
::slotted([slot="add"]) {
grid-column: color-start / end;
}
`;
}
constructor() {
super();
this.alignNested = 'content';
this.noPrimaryAction = false;
this._preventFocus = {
handleEvent(event) {
// target content slot only for now - can add others later
const slot = (event.path || event.composedPath()).find((node) =>
node.nodeName === 'SLOT' && ['content'].includes(node.name)
);
console.warn(`${slot.name} area should not have focusable items in it. Consider using href or creating a custom list-item.`);
},
capture: true
};
}
connectedCallback() {
super.connectedCallback();
this.role = this.gridActive ? 'gridrow' : undefined;
}
firstUpdated() {
this.addEventListener('keydown', this._onKeydown.bind(this));
}
render() {
return html`
<slot name="add-top" class="d2l-cell" data-cell-num="10"></slot>
<slot name="control-container"></slot>
<slot name="outside-control-container"></slot>
<slot name="before-content"></slot>
<slot name="content-action" class="d2l-cell" data-cell-num="6"></slot>
<slot name="outside-control" class="d2l-cell" data-cell-num="2"></slot>
<slot name="outside-control-action" class="d2l-cell" data-cell-num="1"></slot>
<slot name="color-indicator"></slot>
<slot name="expand-collapse" class="d2l-cell" data-cell-num="4"></slot>
<slot name="content" class="d2l-cell" data-cell-num="8" @focus="${!this.noPrimaryAction ? this._preventFocus : null}"></slot>
<slot name="control-action" class="d2l-cell" data-cell-num="3"></slot>
<slot name="control" class="d2l-cell" data-cell-num="5"></slot>
<slot name="actions" class="d2l-cell" data-cell-num="7"></slot>
<slot name="drop-target"></slot>
<slot name="nested"></slot>
<slot name="add" class="d2l-cell" data-cell-num="9"></slot>
`;
}
_focusCellItem(focusInfo) {
const cell = this.shadowRoot?.querySelector(`[data-cell-num="${focusInfo.cellNum}"]`);
if (!cell) return;
let focusable;
const focusables = getFocusableDescendants(cell, { deep: true, predicate: elem => !isInteractiveDescendant(elem) });
if (focusInfo.index <= focusables.length - 1) focusable = focusables[focusInfo.index];
else if (focusables.length > 0) focusable = focusables[focusables.length - 1];
if (focusable) focusable.focus();
return focusable;
}
_focusFirstCell() {
this._focusNextCell(1);
}
_focusFirstRow() {
const list = findComposedAncestor(this, (node) => node.tagName === 'D2L-LIST');
const row = list.firstElementChild.shadowRoot.querySelector('[role="gridrow"]');
if (this.dir === 'rtl') {
row._focusLastCell();
} else {
row._focusFirstCell();
}
}
_focusLastCell() {
let cell = null;
let focusable = null;
let num = 1;
do {
cell = this.shadowRoot && this.shadowRoot.querySelector(`[data-cell-num="${num++}"]`);
if (cell) {
focusable = getLastFocusableDescendant(cell) || focusable;
}
} while (cell);
focusable.focus();
}
_focusLastRow() {
const list = findComposedAncestor(this, (node) => node.tagName === 'D2L-LIST');
const row = list.lastElementChild.shadowRoot.querySelector('[role="gridrow"]');
if (this.dir === 'rtl') {
row._focusFirstCell();
} else {
row._focusLastCell();
}
}
_focusNextCell(num, forward = true) {
let cell = null;
let focusable = null;
do {
cell = this.shadowRoot && this.shadowRoot.querySelector(`[data-cell-num="${num}"]`);
if (cell) {
focusable = forward ? getFirstFocusableDescendant(cell) : getLastFocusableDescendant(cell);
}
if (focusable) focusable.focus();
forward ? ++num : --num;
} while (cell && !focusable);
if (!cell) {
// wrap to first/last item
if (forward) this._focusFirstCell();
else this._focusLastCell();
}
return focusable;
}
_focusNextRow(focusInfo, previous = false, num = 1) {
const curListItem = findComposedAncestor(this, node => node.role === 'rowgroup');
let listItem = curListItem;
while (num > 0) {
const tempListItem = (previous ? this._getPreviousFlattenedListItem(listItem) : this._getNextFlattenedListItem(listItem));
if (tempListItem) listItem = tempListItem;
else break;
num--;
}
if (!listItem) return;
const listItemRow = listItem.shadowRoot.querySelector('[role="gridrow"]');
const focusedCellItem = listItemRow._focusCellItem(focusInfo);
if (!focusedCellItem) {
// could not focus on same cell in adjacent list-item so try general focus on item
if (!listItem._tryFocus()) {
// ultimate fallback to generic method for getting next/previous focusable
const nextFocusable = previous ? getPreviousFocusable(listItem) : getNextFocusable(listItem);
const nextListItem = findComposedAncestor(nextFocusable, (node) => node.role === 'rowgroup' || node.role === 'listitem');
if (nextListItem && this._isContainedInSameRootList(curListItem, nextListItem)) {
nextFocusable.focus();
}
}
}
}
_focusNextWithinRow(focusInfo, focusables) {
if (focusInfo.index === focusables.length - 1) this._focusNextCell(focusInfo.cellNum + 1);
else focusables[focusInfo.index + 1].focus();
}
_focusPreviousWithinRow(focusInfo, focusables) {
if (focusInfo.index === 0) this._focusNextCell(focusInfo.cellNum - 1, false);
else focusables[focusInfo.index - 1].focus();
}
_getNextFlattenedListItem(listItem) {
// check for nested list first; this check needs to account for standard list-items as well as custom
const nestedList = listItem.querySelector('[slot="nested"]') || listItem.shadowRoot.querySelector('d2l-list');
if (nestedList && (!listItem.expandable || (listItem.expandable && listItem.expanded))) {
const nestedListItem = [...nestedList.children].find(node => node.role === 'rowgroup');
if (nestedListItem) return nestedListItem;
}
const getNextListItem = listItem => {
// check for sibling list-item
let nextElement = listItem.nextElementSibling;
while (nextElement) {
if (nextElement.role === 'rowgroup') return nextElement;
nextElement = nextElement.nextElementSibling;
}
// no sibling list-item was found so check for sibling of parent list-item if nested, recursively if necessary
const list = findComposedAncestor(listItem, node => node.tagName === 'D2L-LIST');
if (list.slot !== 'nested' && !(list.parentNode.tagName === 'SLOT' && list.parentNode.name === 'nested')) return;
const parentListItem = findComposedAncestor(list, node => node.role === 'rowgroup');
return getNextListItem(parentListItem);
};
// check for sibling list-item or ancestors sibling list-items
return getNextListItem(listItem);
}
_getPreviousFlattenedListItem(listItem) {
let previousElement = listItem.previousElementSibling;
// try to get the previous list-item in the current list sub-tree including nested
while (previousElement) {
if (previousElement.role === 'rowgroup') {
let nestedList;
do {
// this check needs to account for standard list-items as well as custom
nestedList = previousElement.querySelector('[slot="nested"]') || previousElement.shadowRoot.querySelector('d2l-list');
if (nestedList) {
const nestedListItems = [...nestedList.children].filter(node => node.role === 'rowgroup');
if (nestedListItems.length) {
previousElement = nestedListItems[nestedListItems.length - 1];
} else {
break;
}
}
} while (nestedList);
return previousElement;
}
previousElement = previousElement.previousElementSibling;
}
// no previous list-item was found in the current list sub-tree so get the parent list item if currently in nested
const list = findComposedAncestor(listItem, node => node.tagName === 'D2L-LIST');
// this check needs to account for standard list-items as well as custom
if (list.slot === 'nested' || (list.parentNode.tagName === 'SLOT' && list.parentNode.name === 'nested')) {
const parentListItem = findComposedAncestor(list, node => node.role === 'rowgroup');
return parentListItem;
}
}
_isContainedInSameRootList(item, node) {
const rootList = item?.getRootList?.(item);
return isComposedAncestor(rootList, node);
}
_onKeydown(e) {
if (!this.gridActive) return;
let preventDefault = true;
const node = getComposedActiveElement();
const cell = findComposedAncestor(node, parent => parent.classList?.contains('d2l-cell'));
if (!cell) return;
const focusables = getFocusableDescendants(cell, { deep: true, predicate: elem => !isInteractiveDescendant(elem) });
const focusInfo = {
cellNum: parseInt(cell.getAttribute('data-cell-num')),
index: focusables.findIndex(elem => elem === node)
};
switch (e.keyCode) {
case keyCodes.RIGHT:
if (this.dir === 'rtl') {
this._focusPreviousWithinRow(focusInfo, focusables);
} else {
this._focusNextWithinRow(focusInfo, focusables);
}
break;
case keyCodes.LEFT:
if (this.dir === 'rtl') {
this._focusNextWithinRow(focusInfo, focusables);
} else {
this._focusPreviousWithinRow(focusInfo, focusables);
}
break;
case keyCodes.UP:
// move to above row, focus same item within the cell
this._focusNextRow(focusInfo, true);
break;
case keyCodes.DOWN:
// move to below row, focus same item within the cell
this._focusNextRow(focusInfo);
break;
case keyCodes.HOME:
if (this.dir === 'rtl') {
if (e.ctrlKey) {
this._focusFirstRow();
} else {
// focus last cell
this._focusLastCell();
}
} else {
if (e.ctrlKey) {
// focus first item of first row
this._focusFirstRow();
} else {
// focus first cell
this._focusFirstCell();
}
}
break;
case keyCodes.END:
if (this.dir === 'rtl') {
if (e.ctrlKey) {
// focus first item of last row
this._focusLastRow();
} else {
// focus first cell
this._focusFirstCell();
}
} else {
if (e.ctrlKey) {
// focus last item of last row
this._focusLastRow();
} else {
// focus last cell
this._focusLastCell();
}
}
break;
case keyCodes.PAGEUP:
// focus five rows up
this._focusNextRow(focusInfo, true, 5);
break;
case keyCodes.PAGEDOWN:
// focus five rows down
this._focusNextRow(focusInfo, false, 5);
break;
default:
preventDefault = false;
}
if (preventDefault) {
e.preventDefault();
e.stopPropagation();
}
return;
}
}
customElements.define('d2l-list-item-generic-layout', ListItemGenericLayout);