-
Notifications
You must be signed in to change notification settings - Fork 0
/
focusgroup.js
93 lines (73 loc) · 2.9 KB
/
focusgroup.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
/* Copyright (c) 2016 Tobias Buschor https://goo.gl/gl0mbf | MIT License https://goo.gl/HgajeK */
addEventListener('keydown', e => {
if (e.defaultPrevented) return;
const isVertical = (e.code === 'ArrowUp' || e.code === 'ArrowDown');
const isHorizontal = (e.code === 'ArrowLeft' || e.code === 'ArrowRight');
if (!isVertical && !isHorizontal) return;
const {target, container} = eventTargets(e);
if (!container) return;
if (isHorizontal) {
if (target.tagName === 'INPUT' && (target.type !== 'checkbox' && target.type !== 'radio')) return;
if (target.tagName === 'TEXTAREA') return;
}
if (isVertical) {
if (target.tagName === 'SELECT') return;
if (target.tagName === 'TEXTAREA') return;
}
const tmp = container.getAttribute('u1-focusgroup').split(' ');
const options = Object.fromEntries(tmp.map(o => [o, true]));
if (!options.horizontal && !options.vertical) {
options.horizontal = true;
options.vertical = true;
}
if (!options.horizontal && isHorizontal) return;
if (!options.vertical && isVertical) return;
let direction = 0;
if (e.code === 'ArrowLeft' || e.code === 'ArrowUp') direction = -1;
if (e.code === 'ArrowRight' || e.code === 'ArrowDown') direction = 1;
if (direction === 0) return;
const items = groupItems(container);
if (items.length < 2) return;
const index = items.indexOf(target);
if (index === -1) {
console.warn('should not happen, got a focusgroup-key-event, but the target is not focusable!');
return;
}
let next = items[index + direction];
if (!next && options.wrap) next = direction === 1 ? items.at(0) : items.at(-1);
if (!next) return;
next.focus();
e.preventDefault();
});
addEventListener('focusin', e => {
const {target, container} = eventTargets(e);
if (!container) return;
const remember = container.getAttribute('u1-focusgroup').split(' ').includes('remember');
if (!remember) return;
groupItems(container).forEach(el => el.setAttribute('tabindex', el === target ? '0' : '-1'));
});
function groupItems(container) {
const selector = ' :not(u1-focusgroup) a[href], button, input, select, textarea, [contenteditable], [tabindex]';
const elements = [...container.querySelectorAll(selector)];
// handle slotted elements, beta
const slots = container.matches('slot') ? [container] : container.querySelectorAll('slot'); // TODO: handle slotted elements
slots.forEach(slot => {
const slotted = slot.assignedElements({flatten: true}).filter(el => el.matches(selector));
elements.push(...slotted);
});
return elements.filter(el => !el.disabled);
}
function eventTargets(event){
let target = event.target;
let container = target.closest('[u1-focusgroup]');
if (!container) { // inside shadow dom
target = event.originalTarget; // only for firefox?
if (!target) return {};
container = target.closest('[u1-focusgroup]');
}
if (!container) container = target.assignedSlot?.closest('[u1-focusgroup]'); // if slotted, ok?
return {
target,
container
};
}