@neetechs/ui 0.0.1 → 0.0.3
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/LICENSE +21 -0
- package/README.md +10 -0
- package/dist/ai/ai-action-button/ai-action-button.css +269 -269
- package/dist/ai/ai-approval-card/ai-approval-card.css +320 -320
- package/dist/ai/ai-conversation-panel/ai-conversation-panel.css +403 -403
- package/dist/ai/ai-prompt-input/ai-prompt-input.css +377 -377
- package/dist/ai/ai-proposal-card/ai-proposal-card.css +358 -358
- package/dist/ai/ai-suggestion-card/ai-suggestion-card.css +293 -293
- package/dist/ai/ai-tool-call/ai-tool-call.css +255 -255
- package/dist/ai/index.css +1 -1
- package/dist/appearance/cache.d.ts +50 -0
- package/dist/appearance/cache.d.ts.map +1 -0
- package/dist/appearance/cache.js +126 -0
- package/dist/appearance/cache.js.map +1 -0
- package/dist/appearance/cookie.d.ts +18 -0
- package/dist/appearance/cookie.d.ts.map +1 -0
- package/dist/appearance/cookie.js +56 -0
- package/dist/appearance/cookie.js.map +1 -0
- package/dist/appearance/index.d.ts +8 -0
- package/dist/appearance/index.d.ts.map +1 -0
- package/dist/appearance/index.js +5 -0
- package/dist/appearance/index.js.map +1 -0
- package/dist/appearance/preference.d.ts +13 -0
- package/dist/appearance/preference.d.ts.map +1 -0
- package/dist/appearance/preference.js +26 -0
- package/dist/appearance/preference.js.map +1 -0
- package/dist/appearance/theme-controller.d.ts +80 -0
- package/dist/appearance/theme-controller.d.ts.map +1 -0
- package/dist/appearance/theme-controller.js +235 -0
- package/dist/appearance/theme-controller.js.map +1 -0
- package/dist/appearance/types.d.ts +21 -0
- package/dist/appearance/types.d.ts.map +1 -0
- package/dist/appearance/types.js +2 -0
- package/dist/appearance/types.js.map +1 -0
- package/dist/components/alert/alert.css +216 -216
- package/dist/components/avatar/avatar.css +218 -218
- package/dist/components/badge/badge.css +174 -174
- package/dist/components/breadcrumb/breadcrumb.css +204 -204
- package/dist/components/button/button.css +166 -166
- package/dist/components/card/card.css +219 -219
- package/dist/components/checkbox/checkbox.css +186 -186
- package/dist/components/chip/chip.css +267 -267
- package/dist/components/data-grid/data-grid.css +368 -368
- package/dist/components/dialog/dialog.css +274 -274
- package/dist/components/drawer/drawer.css +402 -402
- package/dist/components/dropdown/dropdown.css +297 -297
- package/dist/components/icon-button/icon-button.css +157 -157
- package/dist/components/input/input.css +162 -162
- package/dist/components/menu/menu.css +313 -313
- package/dist/components/modal/modal.css +317 -317
- package/dist/components/pagination/pagination.css +249 -249
- package/dist/components/panel/panel.css +282 -282
- package/dist/components/progress/progress.css +247 -247
- package/dist/components/radio/radio.css +163 -163
- package/dist/components/segmented-control/segmented-control.css +204 -204
- package/dist/components/select/select.css +102 -102
- package/dist/components/skeleton/skeleton.css +262 -262
- package/dist/components/split-button/split-button.css +284 -284
- package/dist/components/switch/switch.css +180 -180
- package/dist/components/tabs/tabs.css +288 -288
- package/dist/components/textarea/textarea.css +85 -85
- package/dist/components/toast/toast.css +322 -322
- package/dist/components/tooltip/tooltip.css +254 -254
- package/dist/domain/billing/index.css +497 -497
- package/dist/domain/calendar/day-slice/day-slice.css +382 -382
- package/dist/domain/calendar/day-timeline/day-timeline.css +323 -323
- package/dist/domain/calendar/event-card/event-card.css +336 -336
- package/dist/domain/calendar/month-grid/month-grid.css +419 -419
- package/dist/domain/calendar/week-grid/week-grid.css +469 -469
- package/dist/domain/companies/index.css +525 -525
- package/dist/index.css +5 -5
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/patterns/command-palette/command-palette.css +438 -438
- package/dist/patterns/company-switcher/company-switcher.css +362 -362
- package/dist/patterns/empty-state/empty-state.css +258 -258
- package/dist/patterns/error-state/error-state.css +271 -271
- package/dist/patterns/loading-state/loading-state.css +287 -287
- package/dist/patterns/notification-center/notification-center.css +437 -437
- package/dist/patterns/profile-menu/profile-menu.css +296 -296
- package/dist/patterns/search-bar/search-bar.css +443 -443
- package/dist/patterns/workspace-switcher/workspace-switcher.css +379 -379
- package/dist/shell/app-shell/app-shell.css +67 -67
- package/dist/shell/content-frame/content-frame.css +93 -93
- package/dist/shell/header/header.css +177 -177
- package/dist/shell/index.css +5 -5
- package/dist/shell/mobile-nav/mobile-nav.css +170 -170
- package/dist/shell/right-drawer/right-drawer.css +107 -107
- package/dist/shell/sidebar/sidebar.css +145 -145
- package/dist/styles/animations/motion.css +161 -161
- package/dist/styles/base.css +81 -81
- package/dist/styles/reset.css +50 -50
- package/dist/styles/themes/dark.css +119 -119
- package/dist/styles/themes/high-contrast.css +118 -118
- package/dist/styles/themes/light.css +118 -118
- package/dist/styles/tokens/breakpoints.css +14 -14
- package/dist/styles/tokens/color.css +96 -96
- package/dist/styles/tokens/elevation.css +31 -31
- package/dist/styles/tokens/motion.css +35 -35
- package/dist/styles/tokens/radius.css +20 -20
- package/dist/styles/tokens/spacing.css +49 -49
- package/dist/styles/tokens/typography.css +83 -83
- package/dist/styles/tokens/z-index.css +14 -14
- package/dist/styles/utilities/layout.css +134 -134
- package/dist/styles/utilities/text.css +151 -151
- package/dist/styles/utilities/visibility.css +47 -47
- package/docs/theming.md +227 -0
- package/package.json +24 -17
- package/src/ai/ai-action-button/ai-action-button.css +269 -269
- package/src/ai/ai-approval-card/ai-approval-card.css +320 -320
- package/src/ai/ai-conversation-panel/ai-conversation-panel.css +403 -403
- package/src/ai/ai-prompt-input/ai-prompt-input.css +377 -377
- package/src/ai/ai-proposal-card/ai-proposal-card.css +358 -358
- package/src/ai/ai-suggestion-card/ai-suggestion-card.css +293 -293
- package/src/ai/ai-tool-call/ai-tool-call.css +255 -255
- package/src/ai/index.css +1 -1
- package/src/appearance/cache.ts +160 -0
- package/src/appearance/cookie.ts +68 -0
- package/src/appearance/index.ts +38 -0
- package/src/appearance/preference.ts +35 -0
- package/src/appearance/theme-controller.ts +383 -0
- package/src/appearance/types.ts +23 -0
- package/src/behaviors/dialog.ts +167 -167
- package/src/behaviors/drawer.ts +176 -176
- package/src/behaviors/dropdown.ts +280 -280
- package/src/behaviors/focus-trap.ts +260 -260
- package/src/behaviors/ids.ts +47 -47
- package/src/behaviors/index.ts +6 -6
- package/src/behaviors/keyboard.ts +302 -302
- package/src/behaviors/overlay.ts +230 -230
- package/src/components/alert/alert.css +216 -216
- package/src/components/avatar/avatar.css +218 -218
- package/src/components/badge/badge.css +174 -174
- package/src/components/breadcrumb/breadcrumb.css +204 -204
- package/src/components/button/button.css +166 -166
- package/src/components/card/card.css +219 -219
- package/src/components/checkbox/checkbox.css +186 -186
- package/src/components/chip/chip.css +267 -267
- package/src/components/data-grid/data-grid.css +368 -368
- package/src/components/dialog/dialog.css +274 -274
- package/src/components/drawer/drawer.css +402 -402
- package/src/components/dropdown/dropdown.css +297 -297
- package/src/components/icon-button/icon-button.css +157 -157
- package/src/components/input/input.css +162 -162
- package/src/components/menu/menu.css +313 -313
- package/src/components/modal/modal.css +317 -317
- package/src/components/pagination/pagination.css +249 -249
- package/src/components/panel/panel.css +282 -282
- package/src/components/progress/progress.css +247 -247
- package/src/components/radio/radio.css +163 -163
- package/src/components/segmented-control/segmented-control.css +204 -204
- package/src/components/select/select.css +102 -102
- package/src/components/skeleton/skeleton.css +262 -262
- package/src/components/split-button/split-button.css +284 -284
- package/src/components/switch/switch.css +180 -180
- package/src/components/tabs/tabs.css +288 -288
- package/src/components/textarea/textarea.css +85 -85
- package/src/components/toast/toast.css +322 -322
- package/src/components/tooltip/tooltip.css +254 -254
- package/src/domain/billing/index.css +497 -497
- package/src/domain/calendar/day-slice/day-slice.css +382 -382
- package/src/domain/calendar/day-timeline/day-timeline.css +323 -323
- package/src/domain/calendar/event-card/event-card.css +336 -336
- package/src/domain/calendar/month-grid/month-grid.css +419 -419
- package/src/domain/calendar/week-grid/week-grid.css +469 -469
- package/src/domain/companies/index.css +525 -525
- package/src/index.css +5 -5
- package/src/index.ts +6 -5
- package/src/patterns/command-palette/command-palette.css +438 -438
- package/src/patterns/company-switcher/company-switcher.css +362 -362
- package/src/patterns/empty-state/empty-state.css +258 -258
- package/src/patterns/error-state/error-state.css +271 -271
- package/src/patterns/loading-state/loading-state.css +287 -287
- package/src/patterns/notification-center/notification-center.css +437 -437
- package/src/patterns/profile-menu/profile-menu.css +296 -296
- package/src/patterns/search-bar/search-bar.css +443 -443
- package/src/patterns/workspace-switcher/workspace-switcher.css +379 -379
- package/src/shell/app-shell/app-shell.css +67 -67
- package/src/shell/content-frame/content-frame.css +93 -93
- package/src/shell/header/header.css +177 -177
- package/src/shell/index.css +5 -5
- package/src/shell/mobile-nav/mobile-nav.css +170 -170
- package/src/shell/right-drawer/right-drawer.css +107 -107
- package/src/shell/sidebar/sidebar.css +145 -145
- package/src/styles/animations/motion.css +161 -161
- package/src/styles/base.css +81 -81
- package/src/styles/reset.css +50 -50
- package/src/styles/themes/dark.css +119 -119
- package/src/styles/themes/high-contrast.css +118 -118
- package/src/styles/themes/light.css +118 -118
- package/src/styles/tokens/breakpoints.css +14 -14
- package/src/styles/tokens/color.css +96 -96
- package/src/styles/tokens/elevation.css +31 -31
- package/src/styles/tokens/motion.css +35 -35
- package/src/styles/tokens/radius.css +20 -20
- package/src/styles/tokens/spacing.css +49 -49
- package/src/styles/tokens/typography.css +83 -83
- package/src/styles/tokens/z-index.css +14 -14
- package/src/styles/utilities/layout.css +134 -134
- package/src/styles/utilities/text.css +151 -151
- package/src/styles/utilities/visibility.css +47 -47
|
@@ -1,261 +1,261 @@
|
|
|
1
|
-
import type { NtDisposable, NtFocusOptions } from '../types/index.js';
|
|
2
|
-
import { NT_KEYS } from './keyboard.js';
|
|
3
|
-
|
|
4
|
-
export const NT_FOCUSABLE_SELECTOR = [
|
|
5
|
-
'a[href]',
|
|
6
|
-
'area[href]',
|
|
7
|
-
'button:not([disabled])',
|
|
8
|
-
'input:not([disabled])',
|
|
9
|
-
'select:not([disabled])',
|
|
10
|
-
'textarea:not([disabled])',
|
|
11
|
-
'iframe',
|
|
12
|
-
'object',
|
|
13
|
-
'embed',
|
|
14
|
-
'[contenteditable="true"]',
|
|
15
|
-
'[tabindex]:not([tabindex="-1"])',
|
|
16
|
-
].join(',');
|
|
17
|
-
|
|
18
|
-
export interface NtFocusTrapOptions {
|
|
19
|
-
initialFocus?: HTMLElement | (() => HTMLElement | null) | null;
|
|
20
|
-
fallbackFocus?: HTMLElement | (() => HTMLElement | null) | null;
|
|
21
|
-
restoreFocus?: boolean;
|
|
22
|
-
preventScroll?: boolean;
|
|
23
|
-
escapeDeactivates?: boolean;
|
|
24
|
-
onEscape?: () => void;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface NtFocusTrapController extends NtDisposable {
|
|
28
|
-
readonly root: HTMLElement;
|
|
29
|
-
readonly active: boolean;
|
|
30
|
-
activate(): void;
|
|
31
|
-
deactivate(): void;
|
|
32
|
-
focusFirst(): void;
|
|
33
|
-
focusLast(): void;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function ntIsFocusable(element: HTMLElement): boolean {
|
|
37
|
-
if (element.hasAttribute('disabled')) {
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (element.getAttribute('aria-disabled') === 'true') {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (element.tabIndex < 0 && !element.matches(NT_FOCUSABLE_SELECTOR)) {
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const style = window.getComputedStyle(element);
|
|
50
|
-
|
|
51
|
-
if (style.display === 'none' || style.visibility === 'hidden') {
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return element.getClientRects().length > 0;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function ntGetFocusableElements(root: HTMLElement): HTMLElement[] {
|
|
59
|
-
return Array.from(root.querySelectorAll<HTMLElement>(NT_FOCUSABLE_SELECTOR))
|
|
60
|
-
.filter(ntIsFocusable);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function ntResolveFocusTarget(
|
|
64
|
-
target: HTMLElement | (() => HTMLElement | null) | null | undefined,
|
|
65
|
-
): HTMLElement | null {
|
|
66
|
-
if (!target) {
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
return typeof target === 'function' ? target() : target;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export function ntFocusElement(
|
|
74
|
-
element: HTMLElement | null,
|
|
75
|
-
options: NtFocusOptions = {},
|
|
76
|
-
): boolean {
|
|
77
|
-
if (!element) {
|
|
78
|
-
return false;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
element.focus({
|
|
82
|
-
preventScroll: options.preventScroll ?? true,
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
return true;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export function ntFocusFirst(
|
|
89
|
-
root: HTMLElement,
|
|
90
|
-
options: NtFocusOptions = {},
|
|
91
|
-
): boolean {
|
|
92
|
-
const first = ntGetFocusableElements(root)[0] ?? null;
|
|
93
|
-
return ntFocusElement(first, options);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function ntFocusLast(
|
|
97
|
-
root: HTMLElement,
|
|
98
|
-
options: NtFocusOptions = {},
|
|
99
|
-
): boolean {
|
|
100
|
-
const items = ntGetFocusableElements(root);
|
|
101
|
-
const last = items.at(-1) ?? null;
|
|
102
|
-
return ntFocusElement(last, options);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export function ntCreateFocusTrap(
|
|
106
|
-
root: HTMLElement,
|
|
107
|
-
options: NtFocusTrapOptions = {},
|
|
108
|
-
): NtFocusTrapController {
|
|
109
|
-
const {
|
|
110
|
-
initialFocus = null,
|
|
111
|
-
fallbackFocus = null,
|
|
112
|
-
restoreFocus = true,
|
|
113
|
-
preventScroll = true,
|
|
114
|
-
escapeDeactivates = true,
|
|
115
|
-
onEscape,
|
|
116
|
-
} = options;
|
|
117
|
-
|
|
118
|
-
const ownerDocument = root.ownerDocument;
|
|
119
|
-
let active = false;
|
|
120
|
-
let previousActiveElement: HTMLElement | null = null;
|
|
121
|
-
|
|
122
|
-
function getFallbackFocus(): HTMLElement {
|
|
123
|
-
return ntResolveFocusTarget(fallbackFocus) ?? root;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function focusInitial(): void {
|
|
127
|
-
const target = ntResolveFocusTarget(initialFocus);
|
|
128
|
-
|
|
129
|
-
if (ntFocusElement(target, { preventScroll })) {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
if (ntFocusFirst(root, { preventScroll })) {
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
ntFocusElement(getFallbackFocus(), { preventScroll });
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function focusFirst(): void {
|
|
141
|
-
if (!ntFocusFirst(root, { preventScroll })) {
|
|
142
|
-
ntFocusElement(getFallbackFocus(), { preventScroll });
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function focusLast(): void {
|
|
147
|
-
if (!ntFocusLast(root, { preventScroll })) {
|
|
148
|
-
ntFocusElement(getFallbackFocus(), { preventScroll });
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function handleKeyDown(event: KeyboardEvent): void {
|
|
153
|
-
if (!active) {
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
if (event.key === NT_KEYS.escape && escapeDeactivates) {
|
|
158
|
-
event.preventDefault();
|
|
159
|
-
onEscape?.();
|
|
160
|
-
deactivate();
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (event.key !== NT_KEYS.tab) {
|
|
165
|
-
return;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const focusable = ntGetFocusableElements(root);
|
|
169
|
-
|
|
170
|
-
if (focusable.length === 0) {
|
|
171
|
-
event.preventDefault();
|
|
172
|
-
ntFocusElement(getFallbackFocus(), { preventScroll });
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
const first = focusable[0];
|
|
177
|
-
const last = focusable.at(-1);
|
|
178
|
-
const current = ownerDocument.activeElement;
|
|
179
|
-
|
|
180
|
-
if (!first || !last) {
|
|
181
|
-
return;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
if (event.shiftKey && current === first) {
|
|
185
|
-
event.preventDefault();
|
|
186
|
-
ntFocusElement(last, { preventScroll });
|
|
187
|
-
return;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
if (!event.shiftKey && current === last) {
|
|
191
|
-
event.preventDefault();
|
|
192
|
-
ntFocusElement(first, { preventScroll });
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
function handleFocusIn(event: FocusEvent): void {
|
|
197
|
-
if (!active) {
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
const target = event.target;
|
|
202
|
-
|
|
203
|
-
if (target instanceof Node && root.contains(target)) {
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
focusFirst();
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
function activate(): void {
|
|
211
|
-
if (active) {
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
active = true;
|
|
216
|
-
|
|
217
|
-
const activeElement = ownerDocument.activeElement;
|
|
218
|
-
|
|
219
|
-
previousActiveElement =
|
|
220
|
-
activeElement instanceof HTMLElement ? activeElement : null;
|
|
221
|
-
|
|
222
|
-
ownerDocument.addEventListener('keydown', handleKeyDown);
|
|
223
|
-
ownerDocument.addEventListener('focusin', handleFocusIn);
|
|
224
|
-
|
|
225
|
-
queueMicrotask(focusInitial);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
function deactivate(): void {
|
|
229
|
-
if (!active) {
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
active = false;
|
|
234
|
-
|
|
235
|
-
ownerDocument.removeEventListener('keydown', handleKeyDown);
|
|
236
|
-
ownerDocument.removeEventListener('focusin', handleFocusIn);
|
|
237
|
-
|
|
238
|
-
if (restoreFocus) {
|
|
239
|
-
ntFocusElement(previousActiveElement, { preventScroll });
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
previousActiveElement = null;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
return {
|
|
246
|
-
root,
|
|
247
|
-
|
|
248
|
-
get active() {
|
|
249
|
-
return active;
|
|
250
|
-
},
|
|
251
|
-
|
|
252
|
-
activate,
|
|
253
|
-
deactivate,
|
|
254
|
-
focusFirst,
|
|
255
|
-
focusLast,
|
|
256
|
-
|
|
257
|
-
destroy() {
|
|
258
|
-
deactivate();
|
|
259
|
-
},
|
|
260
|
-
};
|
|
1
|
+
import type { NtDisposable, NtFocusOptions } from '../types/index.js';
|
|
2
|
+
import { NT_KEYS } from './keyboard.js';
|
|
3
|
+
|
|
4
|
+
export const NT_FOCUSABLE_SELECTOR = [
|
|
5
|
+
'a[href]',
|
|
6
|
+
'area[href]',
|
|
7
|
+
'button:not([disabled])',
|
|
8
|
+
'input:not([disabled])',
|
|
9
|
+
'select:not([disabled])',
|
|
10
|
+
'textarea:not([disabled])',
|
|
11
|
+
'iframe',
|
|
12
|
+
'object',
|
|
13
|
+
'embed',
|
|
14
|
+
'[contenteditable="true"]',
|
|
15
|
+
'[tabindex]:not([tabindex="-1"])',
|
|
16
|
+
].join(',');
|
|
17
|
+
|
|
18
|
+
export interface NtFocusTrapOptions {
|
|
19
|
+
initialFocus?: HTMLElement | (() => HTMLElement | null) | null;
|
|
20
|
+
fallbackFocus?: HTMLElement | (() => HTMLElement | null) | null;
|
|
21
|
+
restoreFocus?: boolean;
|
|
22
|
+
preventScroll?: boolean;
|
|
23
|
+
escapeDeactivates?: boolean;
|
|
24
|
+
onEscape?: () => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface NtFocusTrapController extends NtDisposable {
|
|
28
|
+
readonly root: HTMLElement;
|
|
29
|
+
readonly active: boolean;
|
|
30
|
+
activate(): void;
|
|
31
|
+
deactivate(): void;
|
|
32
|
+
focusFirst(): void;
|
|
33
|
+
focusLast(): void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function ntIsFocusable(element: HTMLElement): boolean {
|
|
37
|
+
if (element.hasAttribute('disabled')) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (element.getAttribute('aria-disabled') === 'true') {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (element.tabIndex < 0 && !element.matches(NT_FOCUSABLE_SELECTOR)) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const style = window.getComputedStyle(element);
|
|
50
|
+
|
|
51
|
+
if (style.display === 'none' || style.visibility === 'hidden') {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return element.getClientRects().length > 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function ntGetFocusableElements(root: HTMLElement): HTMLElement[] {
|
|
59
|
+
return Array.from(root.querySelectorAll<HTMLElement>(NT_FOCUSABLE_SELECTOR))
|
|
60
|
+
.filter(ntIsFocusable);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function ntResolveFocusTarget(
|
|
64
|
+
target: HTMLElement | (() => HTMLElement | null) | null | undefined,
|
|
65
|
+
): HTMLElement | null {
|
|
66
|
+
if (!target) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return typeof target === 'function' ? target() : target;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function ntFocusElement(
|
|
74
|
+
element: HTMLElement | null,
|
|
75
|
+
options: NtFocusOptions = {},
|
|
76
|
+
): boolean {
|
|
77
|
+
if (!element) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
element.focus({
|
|
82
|
+
preventScroll: options.preventScroll ?? true,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function ntFocusFirst(
|
|
89
|
+
root: HTMLElement,
|
|
90
|
+
options: NtFocusOptions = {},
|
|
91
|
+
): boolean {
|
|
92
|
+
const first = ntGetFocusableElements(root)[0] ?? null;
|
|
93
|
+
return ntFocusElement(first, options);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function ntFocusLast(
|
|
97
|
+
root: HTMLElement,
|
|
98
|
+
options: NtFocusOptions = {},
|
|
99
|
+
): boolean {
|
|
100
|
+
const items = ntGetFocusableElements(root);
|
|
101
|
+
const last = items.at(-1) ?? null;
|
|
102
|
+
return ntFocusElement(last, options);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function ntCreateFocusTrap(
|
|
106
|
+
root: HTMLElement,
|
|
107
|
+
options: NtFocusTrapOptions = {},
|
|
108
|
+
): NtFocusTrapController {
|
|
109
|
+
const {
|
|
110
|
+
initialFocus = null,
|
|
111
|
+
fallbackFocus = null,
|
|
112
|
+
restoreFocus = true,
|
|
113
|
+
preventScroll = true,
|
|
114
|
+
escapeDeactivates = true,
|
|
115
|
+
onEscape,
|
|
116
|
+
} = options;
|
|
117
|
+
|
|
118
|
+
const ownerDocument = root.ownerDocument;
|
|
119
|
+
let active = false;
|
|
120
|
+
let previousActiveElement: HTMLElement | null = null;
|
|
121
|
+
|
|
122
|
+
function getFallbackFocus(): HTMLElement {
|
|
123
|
+
return ntResolveFocusTarget(fallbackFocus) ?? root;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function focusInitial(): void {
|
|
127
|
+
const target = ntResolveFocusTarget(initialFocus);
|
|
128
|
+
|
|
129
|
+
if (ntFocusElement(target, { preventScroll })) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (ntFocusFirst(root, { preventScroll })) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
ntFocusElement(getFallbackFocus(), { preventScroll });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function focusFirst(): void {
|
|
141
|
+
if (!ntFocusFirst(root, { preventScroll })) {
|
|
142
|
+
ntFocusElement(getFallbackFocus(), { preventScroll });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function focusLast(): void {
|
|
147
|
+
if (!ntFocusLast(root, { preventScroll })) {
|
|
148
|
+
ntFocusElement(getFallbackFocus(), { preventScroll });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function handleKeyDown(event: KeyboardEvent): void {
|
|
153
|
+
if (!active) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (event.key === NT_KEYS.escape && escapeDeactivates) {
|
|
158
|
+
event.preventDefault();
|
|
159
|
+
onEscape?.();
|
|
160
|
+
deactivate();
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (event.key !== NT_KEYS.tab) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const focusable = ntGetFocusableElements(root);
|
|
169
|
+
|
|
170
|
+
if (focusable.length === 0) {
|
|
171
|
+
event.preventDefault();
|
|
172
|
+
ntFocusElement(getFallbackFocus(), { preventScroll });
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const first = focusable[0];
|
|
177
|
+
const last = focusable.at(-1);
|
|
178
|
+
const current = ownerDocument.activeElement;
|
|
179
|
+
|
|
180
|
+
if (!first || !last) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (event.shiftKey && current === first) {
|
|
185
|
+
event.preventDefault();
|
|
186
|
+
ntFocusElement(last, { preventScroll });
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (!event.shiftKey && current === last) {
|
|
191
|
+
event.preventDefault();
|
|
192
|
+
ntFocusElement(first, { preventScroll });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function handleFocusIn(event: FocusEvent): void {
|
|
197
|
+
if (!active) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const target = event.target;
|
|
202
|
+
|
|
203
|
+
if (target instanceof Node && root.contains(target)) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
focusFirst();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function activate(): void {
|
|
211
|
+
if (active) {
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
active = true;
|
|
216
|
+
|
|
217
|
+
const activeElement = ownerDocument.activeElement;
|
|
218
|
+
|
|
219
|
+
previousActiveElement =
|
|
220
|
+
activeElement instanceof HTMLElement ? activeElement : null;
|
|
221
|
+
|
|
222
|
+
ownerDocument.addEventListener('keydown', handleKeyDown);
|
|
223
|
+
ownerDocument.addEventListener('focusin', handleFocusIn);
|
|
224
|
+
|
|
225
|
+
queueMicrotask(focusInitial);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function deactivate(): void {
|
|
229
|
+
if (!active) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
active = false;
|
|
234
|
+
|
|
235
|
+
ownerDocument.removeEventListener('keydown', handleKeyDown);
|
|
236
|
+
ownerDocument.removeEventListener('focusin', handleFocusIn);
|
|
237
|
+
|
|
238
|
+
if (restoreFocus) {
|
|
239
|
+
ntFocusElement(previousActiveElement, { preventScroll });
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
previousActiveElement = null;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
root,
|
|
247
|
+
|
|
248
|
+
get active() {
|
|
249
|
+
return active;
|
|
250
|
+
},
|
|
251
|
+
|
|
252
|
+
activate,
|
|
253
|
+
deactivate,
|
|
254
|
+
focusFirst,
|
|
255
|
+
focusLast,
|
|
256
|
+
|
|
257
|
+
destroy() {
|
|
258
|
+
deactivate();
|
|
259
|
+
},
|
|
260
|
+
};
|
|
261
261
|
}
|
package/src/behaviors/ids.ts
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
let ntIdCounter = 0;
|
|
2
|
-
|
|
3
|
-
export function ntCreateId(prefix = 'nt'): string {
|
|
4
|
-
ntIdCounter += 1;
|
|
5
|
-
return `${prefix}-${ntIdCounter}`;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function ntEnsureId(element: HTMLElement, prefix = 'nt'): string {
|
|
9
|
-
const existingId = element.getAttribute('id');
|
|
10
|
-
|
|
11
|
-
if (existingId) {
|
|
12
|
-
return existingId;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const id = ntCreateId(prefix);
|
|
16
|
-
element.setAttribute('id', id);
|
|
17
|
-
return id;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function ntSetAriaControls(
|
|
21
|
-
trigger: HTMLElement,
|
|
22
|
-
content: HTMLElement,
|
|
23
|
-
prefix = 'nt-content',
|
|
24
|
-
): string {
|
|
25
|
-
const contentId = ntEnsureId(content, prefix);
|
|
26
|
-
trigger.setAttribute('aria-controls', contentId);
|
|
27
|
-
return contentId;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function ntSetAriaLabelledBy(
|
|
31
|
-
target: HTMLElement,
|
|
32
|
-
labelElement: HTMLElement,
|
|
33
|
-
prefix = 'nt-label',
|
|
34
|
-
): string {
|
|
35
|
-
const labelId = ntEnsureId(labelElement, prefix);
|
|
36
|
-
target.setAttribute('aria-labelledby', labelId);
|
|
37
|
-
return labelId;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function ntSetAriaDescribedBy(
|
|
41
|
-
target: HTMLElement,
|
|
42
|
-
descriptionElement: HTMLElement,
|
|
43
|
-
prefix = 'nt-description',
|
|
44
|
-
): string {
|
|
45
|
-
const descriptionId = ntEnsureId(descriptionElement, prefix);
|
|
46
|
-
target.setAttribute('aria-describedby', descriptionId);
|
|
47
|
-
return descriptionId;
|
|
1
|
+
let ntIdCounter = 0;
|
|
2
|
+
|
|
3
|
+
export function ntCreateId(prefix = 'nt'): string {
|
|
4
|
+
ntIdCounter += 1;
|
|
5
|
+
return `${prefix}-${ntIdCounter}`;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function ntEnsureId(element: HTMLElement, prefix = 'nt'): string {
|
|
9
|
+
const existingId = element.getAttribute('id');
|
|
10
|
+
|
|
11
|
+
if (existingId) {
|
|
12
|
+
return existingId;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const id = ntCreateId(prefix);
|
|
16
|
+
element.setAttribute('id', id);
|
|
17
|
+
return id;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function ntSetAriaControls(
|
|
21
|
+
trigger: HTMLElement,
|
|
22
|
+
content: HTMLElement,
|
|
23
|
+
prefix = 'nt-content',
|
|
24
|
+
): string {
|
|
25
|
+
const contentId = ntEnsureId(content, prefix);
|
|
26
|
+
trigger.setAttribute('aria-controls', contentId);
|
|
27
|
+
return contentId;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function ntSetAriaLabelledBy(
|
|
31
|
+
target: HTMLElement,
|
|
32
|
+
labelElement: HTMLElement,
|
|
33
|
+
prefix = 'nt-label',
|
|
34
|
+
): string {
|
|
35
|
+
const labelId = ntEnsureId(labelElement, prefix);
|
|
36
|
+
target.setAttribute('aria-labelledby', labelId);
|
|
37
|
+
return labelId;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function ntSetAriaDescribedBy(
|
|
41
|
+
target: HTMLElement,
|
|
42
|
+
descriptionElement: HTMLElement,
|
|
43
|
+
prefix = 'nt-description',
|
|
44
|
+
): string {
|
|
45
|
+
const descriptionId = ntEnsureId(descriptionElement, prefix);
|
|
46
|
+
target.setAttribute('aria-describedby', descriptionId);
|
|
47
|
+
return descriptionId;
|
|
48
48
|
}
|
package/src/behaviors/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from './ids.js';
|
|
2
|
-
export * from './keyboard.js';
|
|
3
|
-
export * from './focus-trap.js';
|
|
4
|
-
export * from './overlay.js';
|
|
5
|
-
export * from './dialog.js';
|
|
6
|
-
export * from './drawer.js';
|
|
1
|
+
export * from './ids.js';
|
|
2
|
+
export * from './keyboard.js';
|
|
3
|
+
export * from './focus-trap.js';
|
|
4
|
+
export * from './overlay.js';
|
|
5
|
+
export * from './dialog.js';
|
|
6
|
+
export * from './drawer.js';
|
|
7
7
|
export * from './dropdown.js';
|