@oscarpalmer/oui 0.8.0 → 0.10.0
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.
- package/dist/css/a11y.css +3 -0
- package/dist/css/badge.css +30 -22
- package/dist/css/button.css +89 -0
- package/dist/css/color.css +103 -99
- package/dist/css/flex.css +2 -0
- package/dist/css/form.css +354 -52
- package/dist/css/message.css +30 -22
- package/dist/css/reset.css +66 -50
- package/dist/css/table.css +4 -1
- package/dist/js/focus-trap.cjs +147 -0
- package/dist/js/focus-trap.js +146 -0
- package/dist/js/index.cjs +2 -0
- package/dist/js/index.js +1 -0
- package/package.json +59 -6
- package/src/css/badge.scss +33 -22
- package/src/css/button.scss +121 -0
- package/src/css/color.scss +101 -99
- package/src/css/form.scss +233 -60
- package/src/css/message.scss +36 -23
- package/src/css/reset.scss +12 -17
- package/src/css/table.scss +3 -1
- package/src/js/focus-trap.ts +181 -0
- package/src/js/index.ts +1 -0
- package/types/focus-trap.d.cts +3 -0
- package/types/focus-trap.d.ts +1 -0
- package/types/index.d.cts +3 -0
- package/types/index.d.ts +1 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import {clamp} from '@oscarpalmer/atoms/number';
|
|
2
|
+
import {on} from '@oscarpalmer/toretto/event';
|
|
3
|
+
import {findAncestor} from '@oscarpalmer/toretto/find';
|
|
4
|
+
import {getFocusable} from '@oscarpalmer/toretto/focusable';
|
|
5
|
+
|
|
6
|
+
class FocusTrap {
|
|
7
|
+
active = true;
|
|
8
|
+
|
|
9
|
+
constructor(private element: HTMLElement) {
|
|
10
|
+
element.setAttribute('aria-modal', 'true');
|
|
11
|
+
element.setAttribute('tabindex', '-1');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
activate() {
|
|
15
|
+
this.element.setAttribute('aria-modal', 'true');
|
|
16
|
+
|
|
17
|
+
this.active = true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
destroy() {
|
|
21
|
+
this.active = false;
|
|
22
|
+
this.element = undefined as never;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
inactivate() {
|
|
26
|
+
this.element.setAttribute('aria-modal', 'false');
|
|
27
|
+
|
|
28
|
+
this.active = false;
|
|
29
|
+
|
|
30
|
+
inactive.add(this);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function addFocusTraps(nodes: Node[] | NodeList): void {
|
|
35
|
+
const elements = [...nodes].filter(
|
|
36
|
+
node => node instanceof HTMLElement,
|
|
37
|
+
) as HTMLElement[];
|
|
38
|
+
|
|
39
|
+
const {length} = elements;
|
|
40
|
+
|
|
41
|
+
for (let index = 0; index < length; index += 1) {
|
|
42
|
+
const element = elements[index];
|
|
43
|
+
|
|
44
|
+
if (element.hasAttribute(selector)) {
|
|
45
|
+
focusTraps.set(element, new FocusTrap(element));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function onEscape(event: KeyboardEvent): void {
|
|
51
|
+
if (event.key !== 'Escape') {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const element = findAncestor(
|
|
56
|
+
document.activeElement as never,
|
|
57
|
+
`[${selector}]`,
|
|
58
|
+
) as HTMLElement;
|
|
59
|
+
|
|
60
|
+
focusTraps.get(element)?.inactivate();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function onFocusOut(): void {
|
|
64
|
+
if (inactive.size === 0) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
requestAnimationFrame(() => {
|
|
69
|
+
const element = findAncestor(
|
|
70
|
+
document.activeElement as never,
|
|
71
|
+
`[${selector}]`,
|
|
72
|
+
) as HTMLElement;
|
|
73
|
+
|
|
74
|
+
if (!focusTraps.has(element)) {
|
|
75
|
+
for (const focusTrap of inactive) {
|
|
76
|
+
focusTrap.activate();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function onTab(event: KeyboardEvent): void {
|
|
83
|
+
if (event.key !== 'Tab') {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const element = findAncestor(
|
|
88
|
+
event.target as never,
|
|
89
|
+
`[${selector}]`,
|
|
90
|
+
) as HTMLElement;
|
|
91
|
+
|
|
92
|
+
if (!(focusTraps.get(element)?.active ?? false)) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
event.preventDefault();
|
|
97
|
+
|
|
98
|
+
const elements = getFocusable(element);
|
|
99
|
+
const {length} = elements;
|
|
100
|
+
|
|
101
|
+
const index = elements.indexOf(event.target as HTMLElement);
|
|
102
|
+
|
|
103
|
+
if (index === -1 || length === 0) {
|
|
104
|
+
if (length === 0) {
|
|
105
|
+
element.focus();
|
|
106
|
+
} else {
|
|
107
|
+
(elements[0] as HTMLElement).focus();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const next = clamp(index + (event.shiftKey ? -1 : 1), 0, length - 1, true);
|
|
114
|
+
|
|
115
|
+
if (next !== index) {
|
|
116
|
+
(elements[next] as HTMLElement).focus();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function removeFocusTraps(nodes: Node[] | NodeList): void {
|
|
121
|
+
const elements = [...nodes].filter(
|
|
122
|
+
node => node instanceof HTMLElement,
|
|
123
|
+
) as HTMLElement[];
|
|
124
|
+
|
|
125
|
+
const {length} = elements;
|
|
126
|
+
|
|
127
|
+
for (let index = 0; index < length; index += 1) {
|
|
128
|
+
const element = elements[index];
|
|
129
|
+
|
|
130
|
+
focusTraps.get(element)?.destroy();
|
|
131
|
+
focusTraps.delete(element);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const focusTraps = new WeakMap<HTMLElement, FocusTrap>();
|
|
136
|
+
const inactive = new Set<FocusTrap>();
|
|
137
|
+
|
|
138
|
+
const observer = new MutationObserver(entries => {
|
|
139
|
+
const {length} = entries;
|
|
140
|
+
|
|
141
|
+
for (let index = 0; index < length; index += 1) {
|
|
142
|
+
const entry = entries[index];
|
|
143
|
+
|
|
144
|
+
if (entry.type === 'attributes' && entry.target instanceof HTMLElement) {
|
|
145
|
+
if (entry.target.hasAttribute(selector)) {
|
|
146
|
+
addFocusTraps([entry.target]);
|
|
147
|
+
} else {
|
|
148
|
+
removeFocusTraps([entry.target]);
|
|
149
|
+
}
|
|
150
|
+
} else if (entry.type === 'childList') {
|
|
151
|
+
addFocusTraps(entry.addedNodes);
|
|
152
|
+
removeFocusTraps(entry.removedNodes);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const selector = 'oui-focus-trap';
|
|
158
|
+
|
|
159
|
+
observer.observe(document, {
|
|
160
|
+
attributeFilter: [selector],
|
|
161
|
+
attributes: true,
|
|
162
|
+
childList: true,
|
|
163
|
+
subtree: true,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
requestAnimationFrame(() => {
|
|
167
|
+
addFocusTraps(document.querySelectorAll(`[${selector}]`));
|
|
168
|
+
|
|
169
|
+
on(document, 'focusout', onFocusOut, {
|
|
170
|
+
capture: true,
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
on(document, 'keydown', onTab, {
|
|
174
|
+
capture: true,
|
|
175
|
+
passive: false,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
on(document, 'keydown', onEscape, {
|
|
179
|
+
passive: false,
|
|
180
|
+
});
|
|
181
|
+
});
|
package/src/js/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './focus-trap';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './focus-trap';
|