@neetechs/ui 0.0.2 → 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 +23 -16
- 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,303 +1,303 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
NtDisposable,
|
|
3
|
-
NtRovingFocusController,
|
|
4
|
-
NtRovingFocusOptions,
|
|
5
|
-
} from '../types/index.js';
|
|
6
|
-
|
|
7
|
-
export const NT_KEYS = {
|
|
8
|
-
arrowDown: 'ArrowDown',
|
|
9
|
-
arrowLeft: 'ArrowLeft',
|
|
10
|
-
arrowRight: 'ArrowRight',
|
|
11
|
-
arrowUp: 'ArrowUp',
|
|
12
|
-
end: 'End',
|
|
13
|
-
enter: 'Enter',
|
|
14
|
-
escape: 'Escape',
|
|
15
|
-
home: 'Home',
|
|
16
|
-
space: ' ',
|
|
17
|
-
tab: 'Tab',
|
|
18
|
-
} as const;
|
|
19
|
-
|
|
20
|
-
export type NtKeyboardHandler = (event: KeyboardEvent) => void;
|
|
21
|
-
|
|
22
|
-
export function ntIsEscapeKey(event: KeyboardEvent): boolean {
|
|
23
|
-
return event.key === NT_KEYS.escape;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function ntIsEnterKey(event: KeyboardEvent): boolean {
|
|
27
|
-
return event.key === NT_KEYS.enter;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function ntIsSpaceKey(event: KeyboardEvent): boolean {
|
|
31
|
-
return event.key === NT_KEYS.space || event.code === 'Space';
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function ntIsActivationKey(event: KeyboardEvent): boolean {
|
|
35
|
-
return ntIsEnterKey(event) || ntIsSpaceKey(event);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function ntIsArrowKey(event: KeyboardEvent): boolean {
|
|
39
|
-
return (
|
|
40
|
-
event.key === NT_KEYS.arrowUp ||
|
|
41
|
-
event.key === NT_KEYS.arrowRight ||
|
|
42
|
-
event.key === NT_KEYS.arrowDown ||
|
|
43
|
-
event.key === NT_KEYS.arrowLeft
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function ntIsTextInputElement(element: EventTarget | null): boolean {
|
|
48
|
-
if (!(element instanceof HTMLElement)) {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const tagName = element.tagName.toLowerCase();
|
|
53
|
-
|
|
54
|
-
return (
|
|
55
|
-
tagName === 'input' ||
|
|
56
|
-
tagName === 'textarea' ||
|
|
57
|
-
tagName === 'select' ||
|
|
58
|
-
element.isContentEditable
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function ntOnKey(
|
|
63
|
-
element: HTMLElement | Document,
|
|
64
|
-
key: string,
|
|
65
|
-
handler: NtKeyboardHandler,
|
|
66
|
-
): NtDisposable {
|
|
67
|
-
const listener: EventListener = (event) => {
|
|
68
|
-
if (!(event instanceof KeyboardEvent)) {
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (event.key === key) {
|
|
73
|
-
handler(event);
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
element.addEventListener('keydown', listener);
|
|
78
|
-
|
|
79
|
-
return {
|
|
80
|
-
destroy() {
|
|
81
|
-
element.removeEventListener('keydown', listener);
|
|
82
|
-
},
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export function ntOnEscape(
|
|
87
|
-
element: HTMLElement | Document,
|
|
88
|
-
handler: NtKeyboardHandler,
|
|
89
|
-
): NtDisposable {
|
|
90
|
-
return ntOnKey(element, NT_KEYS.escape, handler);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function ntOnActivation(
|
|
94
|
-
element: HTMLElement,
|
|
95
|
-
handler: NtKeyboardHandler,
|
|
96
|
-
): NtDisposable {
|
|
97
|
-
const listener: EventListener = (event) => {
|
|
98
|
-
if (!(event instanceof KeyboardEvent)) {
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (!ntIsActivationKey(event)) {
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
event.preventDefault();
|
|
107
|
-
handler(event);
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
element.addEventListener('keydown', listener);
|
|
111
|
-
|
|
112
|
-
return {
|
|
113
|
-
destroy() {
|
|
114
|
-
element.removeEventListener('keydown', listener);
|
|
115
|
-
},
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export function ntGetEnabledItems(
|
|
120
|
-
root: HTMLElement,
|
|
121
|
-
itemSelector: string,
|
|
122
|
-
): HTMLElement[] {
|
|
123
|
-
return Array.from(root.querySelectorAll<HTMLElement>(itemSelector)).filter(
|
|
124
|
-
(item) =>
|
|
125
|
-
!item.hasAttribute('disabled') &&
|
|
126
|
-
item.getAttribute('aria-disabled') !== 'true' &&
|
|
127
|
-
item.dataset.disabled !== 'true',
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export function ntFocusItem(
|
|
132
|
-
items: HTMLElement[],
|
|
133
|
-
index: number,
|
|
134
|
-
options: FocusOptions = { preventScroll: true },
|
|
135
|
-
): number {
|
|
136
|
-
const item = items[index];
|
|
137
|
-
|
|
138
|
-
if (!item) {
|
|
139
|
-
return -1;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
item.focus(options);
|
|
143
|
-
return index;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export function ntMoveIndex(
|
|
147
|
-
currentIndex: number,
|
|
148
|
-
itemCount: number,
|
|
149
|
-
direction: 1 | -1,
|
|
150
|
-
loop = true,
|
|
151
|
-
): number {
|
|
152
|
-
if (itemCount <= 0) {
|
|
153
|
-
return -1;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const nextIndex = currentIndex + direction;
|
|
157
|
-
|
|
158
|
-
if (nextIndex >= 0 && nextIndex < itemCount) {
|
|
159
|
-
return nextIndex;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (!loop) {
|
|
163
|
-
return currentIndex;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return direction > 0 ? 0 : itemCount - 1;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
export function ntCreateRovingFocus(
|
|
170
|
-
options: NtRovingFocusOptions,
|
|
171
|
-
): NtRovingFocusController {
|
|
172
|
-
const {
|
|
173
|
-
root,
|
|
174
|
-
itemSelector,
|
|
175
|
-
orientation = 'vertical',
|
|
176
|
-
loop = true,
|
|
177
|
-
initialIndex = 0,
|
|
178
|
-
} = options;
|
|
179
|
-
|
|
180
|
-
let currentIndex = initialIndex;
|
|
181
|
-
|
|
182
|
-
function getItems(): HTMLElement[] {
|
|
183
|
-
return ntGetEnabledItems(root, itemSelector);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function setTabIndexes(items: HTMLElement[]): void {
|
|
187
|
-
items.forEach((item, index) => {
|
|
188
|
-
item.tabIndex = index === currentIndex ? 0 : -1;
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
function focusAt(index: number): void {
|
|
193
|
-
const items = getItems();
|
|
194
|
-
|
|
195
|
-
if (items.length === 0) {
|
|
196
|
-
currentIndex = -1;
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const normalizedIndex = Math.max(0, Math.min(index, items.length - 1));
|
|
201
|
-
currentIndex = ntFocusItem(items, normalizedIndex);
|
|
202
|
-
setTabIndexes(items);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
function focusFirst(): void {
|
|
206
|
-
focusAt(0);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function focusLast(): void {
|
|
210
|
-
const items = getItems();
|
|
211
|
-
focusAt(items.length - 1);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
function focusNext(): void {
|
|
215
|
-
const items = getItems();
|
|
216
|
-
focusAt(ntMoveIndex(currentIndex, items.length, 1, loop));
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
function focusPrevious(): void {
|
|
220
|
-
const items = getItems();
|
|
221
|
-
focusAt(ntMoveIndex(currentIndex, items.length, -1, loop));
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const onKeyDown: EventListener = (event) => {
|
|
225
|
-
if (!(event instanceof KeyboardEvent)) {
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const isHorizontal =
|
|
230
|
-
orientation === 'horizontal' || orientation === 'both';
|
|
231
|
-
const isVertical = orientation === 'vertical' || orientation === 'both';
|
|
232
|
-
|
|
233
|
-
if (event.key === NT_KEYS.home) {
|
|
234
|
-
event.preventDefault();
|
|
235
|
-
focusFirst();
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
if (event.key === NT_KEYS.end) {
|
|
240
|
-
event.preventDefault();
|
|
241
|
-
focusLast();
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
if (
|
|
246
|
-
(isVertical && event.key === NT_KEYS.arrowDown) ||
|
|
247
|
-
(isHorizontal && event.key === NT_KEYS.arrowRight)
|
|
248
|
-
) {
|
|
249
|
-
event.preventDefault();
|
|
250
|
-
focusNext();
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
if (
|
|
255
|
-
(isVertical && event.key === NT_KEYS.arrowUp) ||
|
|
256
|
-
(isHorizontal && event.key === NT_KEYS.arrowLeft)
|
|
257
|
-
) {
|
|
258
|
-
event.preventDefault();
|
|
259
|
-
focusPrevious();
|
|
260
|
-
}
|
|
261
|
-
};
|
|
262
|
-
|
|
263
|
-
const onFocusIn = (event: FocusEvent) => {
|
|
264
|
-
const items = getItems();
|
|
265
|
-
const target = event.target;
|
|
266
|
-
|
|
267
|
-
if (!(target instanceof HTMLElement)) {
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
const index = items.indexOf(target);
|
|
272
|
-
|
|
273
|
-
if (index < 0) {
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
currentIndex = index;
|
|
278
|
-
setTabIndexes(items);
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
setTabIndexes(getItems());
|
|
282
|
-
|
|
283
|
-
root.addEventListener('keydown', onKeyDown);
|
|
284
|
-
root.addEventListener('focusin', onFocusIn);
|
|
285
|
-
|
|
286
|
-
return {
|
|
287
|
-
root,
|
|
288
|
-
|
|
289
|
-
get currentIndex() {
|
|
290
|
-
return currentIndex;
|
|
291
|
-
},
|
|
292
|
-
|
|
293
|
-
focusFirst,
|
|
294
|
-
focusLast,
|
|
295
|
-
focusNext,
|
|
296
|
-
focusPrevious,
|
|
297
|
-
|
|
298
|
-
destroy() {
|
|
299
|
-
root.removeEventListener('keydown', onKeyDown);
|
|
300
|
-
root.removeEventListener('focusin', onFocusIn);
|
|
301
|
-
},
|
|
302
|
-
};
|
|
1
|
+
import type {
|
|
2
|
+
NtDisposable,
|
|
3
|
+
NtRovingFocusController,
|
|
4
|
+
NtRovingFocusOptions,
|
|
5
|
+
} from '../types/index.js';
|
|
6
|
+
|
|
7
|
+
export const NT_KEYS = {
|
|
8
|
+
arrowDown: 'ArrowDown',
|
|
9
|
+
arrowLeft: 'ArrowLeft',
|
|
10
|
+
arrowRight: 'ArrowRight',
|
|
11
|
+
arrowUp: 'ArrowUp',
|
|
12
|
+
end: 'End',
|
|
13
|
+
enter: 'Enter',
|
|
14
|
+
escape: 'Escape',
|
|
15
|
+
home: 'Home',
|
|
16
|
+
space: ' ',
|
|
17
|
+
tab: 'Tab',
|
|
18
|
+
} as const;
|
|
19
|
+
|
|
20
|
+
export type NtKeyboardHandler = (event: KeyboardEvent) => void;
|
|
21
|
+
|
|
22
|
+
export function ntIsEscapeKey(event: KeyboardEvent): boolean {
|
|
23
|
+
return event.key === NT_KEYS.escape;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function ntIsEnterKey(event: KeyboardEvent): boolean {
|
|
27
|
+
return event.key === NT_KEYS.enter;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function ntIsSpaceKey(event: KeyboardEvent): boolean {
|
|
31
|
+
return event.key === NT_KEYS.space || event.code === 'Space';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function ntIsActivationKey(event: KeyboardEvent): boolean {
|
|
35
|
+
return ntIsEnterKey(event) || ntIsSpaceKey(event);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function ntIsArrowKey(event: KeyboardEvent): boolean {
|
|
39
|
+
return (
|
|
40
|
+
event.key === NT_KEYS.arrowUp ||
|
|
41
|
+
event.key === NT_KEYS.arrowRight ||
|
|
42
|
+
event.key === NT_KEYS.arrowDown ||
|
|
43
|
+
event.key === NT_KEYS.arrowLeft
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function ntIsTextInputElement(element: EventTarget | null): boolean {
|
|
48
|
+
if (!(element instanceof HTMLElement)) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const tagName = element.tagName.toLowerCase();
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
tagName === 'input' ||
|
|
56
|
+
tagName === 'textarea' ||
|
|
57
|
+
tagName === 'select' ||
|
|
58
|
+
element.isContentEditable
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function ntOnKey(
|
|
63
|
+
element: HTMLElement | Document,
|
|
64
|
+
key: string,
|
|
65
|
+
handler: NtKeyboardHandler,
|
|
66
|
+
): NtDisposable {
|
|
67
|
+
const listener: EventListener = (event) => {
|
|
68
|
+
if (!(event instanceof KeyboardEvent)) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (event.key === key) {
|
|
73
|
+
handler(event);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
element.addEventListener('keydown', listener);
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
destroy() {
|
|
81
|
+
element.removeEventListener('keydown', listener);
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function ntOnEscape(
|
|
87
|
+
element: HTMLElement | Document,
|
|
88
|
+
handler: NtKeyboardHandler,
|
|
89
|
+
): NtDisposable {
|
|
90
|
+
return ntOnKey(element, NT_KEYS.escape, handler);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function ntOnActivation(
|
|
94
|
+
element: HTMLElement,
|
|
95
|
+
handler: NtKeyboardHandler,
|
|
96
|
+
): NtDisposable {
|
|
97
|
+
const listener: EventListener = (event) => {
|
|
98
|
+
if (!(event instanceof KeyboardEvent)) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (!ntIsActivationKey(event)) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
event.preventDefault();
|
|
107
|
+
handler(event);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
element.addEventListener('keydown', listener);
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
destroy() {
|
|
114
|
+
element.removeEventListener('keydown', listener);
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function ntGetEnabledItems(
|
|
120
|
+
root: HTMLElement,
|
|
121
|
+
itemSelector: string,
|
|
122
|
+
): HTMLElement[] {
|
|
123
|
+
return Array.from(root.querySelectorAll<HTMLElement>(itemSelector)).filter(
|
|
124
|
+
(item) =>
|
|
125
|
+
!item.hasAttribute('disabled') &&
|
|
126
|
+
item.getAttribute('aria-disabled') !== 'true' &&
|
|
127
|
+
item.dataset.disabled !== 'true',
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function ntFocusItem(
|
|
132
|
+
items: HTMLElement[],
|
|
133
|
+
index: number,
|
|
134
|
+
options: FocusOptions = { preventScroll: true },
|
|
135
|
+
): number {
|
|
136
|
+
const item = items[index];
|
|
137
|
+
|
|
138
|
+
if (!item) {
|
|
139
|
+
return -1;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
item.focus(options);
|
|
143
|
+
return index;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function ntMoveIndex(
|
|
147
|
+
currentIndex: number,
|
|
148
|
+
itemCount: number,
|
|
149
|
+
direction: 1 | -1,
|
|
150
|
+
loop = true,
|
|
151
|
+
): number {
|
|
152
|
+
if (itemCount <= 0) {
|
|
153
|
+
return -1;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const nextIndex = currentIndex + direction;
|
|
157
|
+
|
|
158
|
+
if (nextIndex >= 0 && nextIndex < itemCount) {
|
|
159
|
+
return nextIndex;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (!loop) {
|
|
163
|
+
return currentIndex;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return direction > 0 ? 0 : itemCount - 1;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function ntCreateRovingFocus(
|
|
170
|
+
options: NtRovingFocusOptions,
|
|
171
|
+
): NtRovingFocusController {
|
|
172
|
+
const {
|
|
173
|
+
root,
|
|
174
|
+
itemSelector,
|
|
175
|
+
orientation = 'vertical',
|
|
176
|
+
loop = true,
|
|
177
|
+
initialIndex = 0,
|
|
178
|
+
} = options;
|
|
179
|
+
|
|
180
|
+
let currentIndex = initialIndex;
|
|
181
|
+
|
|
182
|
+
function getItems(): HTMLElement[] {
|
|
183
|
+
return ntGetEnabledItems(root, itemSelector);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function setTabIndexes(items: HTMLElement[]): void {
|
|
187
|
+
items.forEach((item, index) => {
|
|
188
|
+
item.tabIndex = index === currentIndex ? 0 : -1;
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function focusAt(index: number): void {
|
|
193
|
+
const items = getItems();
|
|
194
|
+
|
|
195
|
+
if (items.length === 0) {
|
|
196
|
+
currentIndex = -1;
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const normalizedIndex = Math.max(0, Math.min(index, items.length - 1));
|
|
201
|
+
currentIndex = ntFocusItem(items, normalizedIndex);
|
|
202
|
+
setTabIndexes(items);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function focusFirst(): void {
|
|
206
|
+
focusAt(0);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function focusLast(): void {
|
|
210
|
+
const items = getItems();
|
|
211
|
+
focusAt(items.length - 1);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function focusNext(): void {
|
|
215
|
+
const items = getItems();
|
|
216
|
+
focusAt(ntMoveIndex(currentIndex, items.length, 1, loop));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function focusPrevious(): void {
|
|
220
|
+
const items = getItems();
|
|
221
|
+
focusAt(ntMoveIndex(currentIndex, items.length, -1, loop));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const onKeyDown: EventListener = (event) => {
|
|
225
|
+
if (!(event instanceof KeyboardEvent)) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const isHorizontal =
|
|
230
|
+
orientation === 'horizontal' || orientation === 'both';
|
|
231
|
+
const isVertical = orientation === 'vertical' || orientation === 'both';
|
|
232
|
+
|
|
233
|
+
if (event.key === NT_KEYS.home) {
|
|
234
|
+
event.preventDefault();
|
|
235
|
+
focusFirst();
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (event.key === NT_KEYS.end) {
|
|
240
|
+
event.preventDefault();
|
|
241
|
+
focusLast();
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (
|
|
246
|
+
(isVertical && event.key === NT_KEYS.arrowDown) ||
|
|
247
|
+
(isHorizontal && event.key === NT_KEYS.arrowRight)
|
|
248
|
+
) {
|
|
249
|
+
event.preventDefault();
|
|
250
|
+
focusNext();
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (
|
|
255
|
+
(isVertical && event.key === NT_KEYS.arrowUp) ||
|
|
256
|
+
(isHorizontal && event.key === NT_KEYS.arrowLeft)
|
|
257
|
+
) {
|
|
258
|
+
event.preventDefault();
|
|
259
|
+
focusPrevious();
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const onFocusIn = (event: FocusEvent) => {
|
|
264
|
+
const items = getItems();
|
|
265
|
+
const target = event.target;
|
|
266
|
+
|
|
267
|
+
if (!(target instanceof HTMLElement)) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const index = items.indexOf(target);
|
|
272
|
+
|
|
273
|
+
if (index < 0) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
currentIndex = index;
|
|
278
|
+
setTabIndexes(items);
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
setTabIndexes(getItems());
|
|
282
|
+
|
|
283
|
+
root.addEventListener('keydown', onKeyDown);
|
|
284
|
+
root.addEventListener('focusin', onFocusIn);
|
|
285
|
+
|
|
286
|
+
return {
|
|
287
|
+
root,
|
|
288
|
+
|
|
289
|
+
get currentIndex() {
|
|
290
|
+
return currentIndex;
|
|
291
|
+
},
|
|
292
|
+
|
|
293
|
+
focusFirst,
|
|
294
|
+
focusLast,
|
|
295
|
+
focusNext,
|
|
296
|
+
focusPrevious,
|
|
297
|
+
|
|
298
|
+
destroy() {
|
|
299
|
+
root.removeEventListener('keydown', onKeyDown);
|
|
300
|
+
root.removeEventListener('focusin', onFocusIn);
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
303
|
}
|