@neetechs/ui 0.0.1
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/README.md +640 -0
- package/dist/ai/ai-action-button/ai-action-button.css +270 -0
- package/dist/ai/ai-approval-card/ai-approval-card.css +321 -0
- package/dist/ai/ai-conversation-panel/ai-conversation-panel.css +404 -0
- package/dist/ai/ai-prompt-input/ai-prompt-input.css +378 -0
- package/dist/ai/ai-proposal-card/ai-proposal-card.css +359 -0
- package/dist/ai/ai-suggestion-card/ai-suggestion-card.css +294 -0
- package/dist/ai/ai-tool-call/ai-tool-call.css +256 -0
- package/dist/ai/index.css +7 -0
- package/dist/behaviors/dialog.d.ts +27 -0
- package/dist/behaviors/dialog.d.ts.map +1 -0
- package/dist/behaviors/dialog.js +97 -0
- package/dist/behaviors/dialog.js.map +1 -0
- package/dist/behaviors/drawer.d.ts +29 -0
- package/dist/behaviors/drawer.d.ts.map +1 -0
- package/dist/behaviors/drawer.js +102 -0
- package/dist/behaviors/drawer.js.map +1 -0
- package/dist/behaviors/dropdown.d.ts +26 -0
- package/dist/behaviors/dropdown.d.ts.map +1 -0
- package/dist/behaviors/dropdown.js +178 -0
- package/dist/behaviors/dropdown.js.map +1 -0
- package/dist/behaviors/focus-trap.d.ts +26 -0
- package/dist/behaviors/focus-trap.d.ts.map +1 -0
- package/dist/behaviors/focus-trap.js +170 -0
- package/dist/behaviors/focus-trap.js.map +1 -0
- package/dist/behaviors/ids.d.ts +6 -0
- package/dist/behaviors/ids.d.ts.map +1 -0
- package/dist/behaviors/ids.js +30 -0
- package/dist/behaviors/ids.js.map +1 -0
- package/dist/behaviors/index.d.ts +8 -0
- package/dist/behaviors/index.d.ts.map +1 -0
- package/dist/behaviors/index.js +8 -0
- package/dist/behaviors/index.js.map +1 -0
- package/dist/behaviors/keyboard.d.ts +28 -0
- package/dist/behaviors/keyboard.d.ts.map +1 -0
- package/dist/behaviors/keyboard.js +199 -0
- package/dist/behaviors/keyboard.js.map +1 -0
- package/dist/behaviors/overlay.d.ts +21 -0
- package/dist/behaviors/overlay.d.ts.map +1 -0
- package/dist/behaviors/overlay.js +145 -0
- package/dist/behaviors/overlay.js.map +1 -0
- package/dist/components/alert/alert.css +217 -0
- package/dist/components/avatar/avatar.css +219 -0
- package/dist/components/badge/badge.css +175 -0
- package/dist/components/breadcrumb/breadcrumb.css +205 -0
- package/dist/components/button/button.css +167 -0
- package/dist/components/card/card.css +220 -0
- package/dist/components/checkbox/checkbox.css +187 -0
- package/dist/components/chip/chip.css +268 -0
- package/dist/components/data-grid/data-grid.css +369 -0
- package/dist/components/dialog/dialog.css +275 -0
- package/dist/components/drawer/drawer.css +403 -0
- package/dist/components/dropdown/dropdown.css +298 -0
- package/dist/components/icon-button/icon-button.css +158 -0
- package/dist/components/index.css +30 -0
- package/dist/components/input/input.css +163 -0
- package/dist/components/menu/menu.css +314 -0
- package/dist/components/modal/modal.css +318 -0
- package/dist/components/pagination/pagination.css +250 -0
- package/dist/components/panel/panel.css +283 -0
- package/dist/components/progress/progress.css +248 -0
- package/dist/components/radio/radio.css +164 -0
- package/dist/components/segmented-control/segmented-control.css +205 -0
- package/dist/components/select/select.css +103 -0
- package/dist/components/skeleton/skeleton.css +263 -0
- package/dist/components/split-button/split-button.css +285 -0
- package/dist/components/switch/switch.css +181 -0
- package/dist/components/table/table.css +0 -0
- package/dist/components/tabs/tabs.css +289 -0
- package/dist/components/textarea/textarea.css +86 -0
- package/dist/components/toast/toast.css +323 -0
- package/dist/components/tooltip/tooltip.css +255 -0
- package/dist/domain/billing/index.css +498 -0
- package/dist/domain/calendar/day-slice/day-slice.css +383 -0
- package/dist/domain/calendar/day-timeline/day-timeline.css +324 -0
- package/dist/domain/calendar/event-card/event-card.css +337 -0
- package/dist/domain/calendar/index.css +5 -0
- package/dist/domain/calendar/month-grid/month-grid.css +420 -0
- package/dist/domain/calendar/week-grid/week-grid.css +470 -0
- package/dist/domain/companies/index.css +526 -0
- package/dist/domain/index.css +3 -0
- package/dist/index.css +6 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/patterns/command-palette/command-palette.css +439 -0
- package/dist/patterns/company-switcher/company-switcher.css +363 -0
- package/dist/patterns/empty-state/empty-state.css +259 -0
- package/dist/patterns/error-state/error-state.css +272 -0
- package/dist/patterns/index.css +9 -0
- package/dist/patterns/loading-state/loading-state.css +288 -0
- package/dist/patterns/notification-center/notification-center.css +438 -0
- package/dist/patterns/profile-menu/profile-menu.css +297 -0
- package/dist/patterns/search-bar/search-bar.css +444 -0
- package/dist/patterns/workspace-switcher/workspace-switcher.css +380 -0
- package/dist/shell/app-shell/app-shell.css +68 -0
- package/dist/shell/content-frame/content-frame.css +94 -0
- package/dist/shell/header/header.css +178 -0
- package/dist/shell/index.css +6 -0
- package/dist/shell/mobile-nav/mobile-nav.css +171 -0
- package/dist/shell/right-drawer/right-drawer.css +108 -0
- package/dist/shell/sidebar/sidebar.css +146 -0
- package/dist/styles/animations/motion.css +162 -0
- package/dist/styles/base.css +82 -0
- package/dist/styles/index.css +21 -0
- package/dist/styles/reset.css +51 -0
- package/dist/styles/themes/dark.css +120 -0
- package/dist/styles/themes/high-contrast.css +119 -0
- package/dist/styles/themes/light.css +119 -0
- package/dist/styles/tokens/breakpoints.css +15 -0
- package/dist/styles/tokens/color.css +97 -0
- package/dist/styles/tokens/elevation.css +32 -0
- package/dist/styles/tokens/index.css +8 -0
- package/dist/styles/tokens/motion.css +36 -0
- package/dist/styles/tokens/radius.css +21 -0
- package/dist/styles/tokens/spacing.css +50 -0
- package/dist/styles/tokens/typography.css +84 -0
- package/dist/styles/tokens/z-index.css +15 -0
- package/dist/styles/utilities/layout.css +135 -0
- package/dist/styles/utilities/text.css +152 -0
- package/dist/styles/utilities/visibility.css +48 -0
- package/dist/types/index.d.ts +65 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/docs/accessibility.md +0 -0
- package/docs/component-guidelines.md +0 -0
- package/docs/migration.md +0 -0
- package/docs/theming.md +0 -0
- package/package.json +85 -0
- package/src/ai/ai-action-button/ai-action-button.css +270 -0
- package/src/ai/ai-approval-card/ai-approval-card.css +321 -0
- package/src/ai/ai-conversation-panel/ai-conversation-panel.css +404 -0
- package/src/ai/ai-prompt-input/ai-prompt-input.css +378 -0
- package/src/ai/ai-proposal-card/ai-proposal-card.css +359 -0
- package/src/ai/ai-suggestion-card/ai-suggestion-card.css +294 -0
- package/src/ai/ai-tool-call/ai-tool-call.css +256 -0
- package/src/ai/index.css +7 -0
- package/src/behaviors/dialog.ts +168 -0
- package/src/behaviors/drawer.ts +177 -0
- package/src/behaviors/dropdown.ts +281 -0
- package/src/behaviors/focus-trap.ts +261 -0
- package/src/behaviors/ids.ts +48 -0
- package/src/behaviors/index.ts +7 -0
- package/src/behaviors/keyboard.ts +303 -0
- package/src/behaviors/overlay.ts +231 -0
- package/src/components/alert/alert.css +217 -0
- package/src/components/avatar/avatar.css +219 -0
- package/src/components/badge/badge.css +175 -0
- package/src/components/breadcrumb/breadcrumb.css +205 -0
- package/src/components/button/button.css +167 -0
- package/src/components/card/card.css +220 -0
- package/src/components/checkbox/checkbox.css +187 -0
- package/src/components/chip/chip.css +268 -0
- package/src/components/data-grid/data-grid.css +369 -0
- package/src/components/dialog/dialog.css +275 -0
- package/src/components/drawer/drawer.css +403 -0
- package/src/components/dropdown/dropdown.css +298 -0
- package/src/components/icon-button/icon-button.css +158 -0
- package/src/components/index.css +30 -0
- package/src/components/input/input.css +163 -0
- package/src/components/menu/menu.css +314 -0
- package/src/components/modal/modal.css +318 -0
- package/src/components/pagination/pagination.css +250 -0
- package/src/components/panel/panel.css +283 -0
- package/src/components/progress/progress.css +248 -0
- package/src/components/radio/radio.css +164 -0
- package/src/components/segmented-control/segmented-control.css +205 -0
- package/src/components/select/select.css +103 -0
- package/src/components/skeleton/skeleton.css +263 -0
- package/src/components/split-button/split-button.css +285 -0
- package/src/components/switch/switch.css +181 -0
- package/src/components/table/table.css +0 -0
- package/src/components/tabs/tabs.css +289 -0
- package/src/components/textarea/textarea.css +86 -0
- package/src/components/toast/toast.css +323 -0
- package/src/components/tooltip/tooltip.css +255 -0
- package/src/domain/billing/index.css +498 -0
- package/src/domain/calendar/day-slice/day-slice.css +383 -0
- package/src/domain/calendar/day-timeline/day-timeline.css +324 -0
- package/src/domain/calendar/event-card/event-card.css +337 -0
- package/src/domain/calendar/index.css +5 -0
- package/src/domain/calendar/month-grid/month-grid.css +420 -0
- package/src/domain/calendar/week-grid/week-grid.css +470 -0
- package/src/domain/companies/index.css +526 -0
- package/src/domain/index.css +3 -0
- package/src/index.css +6 -0
- package/src/index.ts +5 -0
- package/src/patterns/command-palette/command-palette.css +439 -0
- package/src/patterns/company-switcher/company-switcher.css +363 -0
- package/src/patterns/empty-state/empty-state.css +259 -0
- package/src/patterns/error-state/error-state.css +272 -0
- package/src/patterns/index.css +9 -0
- package/src/patterns/loading-state/loading-state.css +288 -0
- package/src/patterns/notification-center/notification-center.css +438 -0
- package/src/patterns/profile-menu/profile-menu.css +297 -0
- package/src/patterns/search-bar/search-bar.css +444 -0
- package/src/patterns/workspace-switcher/workspace-switcher.css +380 -0
- package/src/shell/app-shell/app-shell.css +68 -0
- package/src/shell/content-frame/content-frame.css +94 -0
- package/src/shell/header/header.css +178 -0
- package/src/shell/index.css +6 -0
- package/src/shell/mobile-nav/mobile-nav.css +171 -0
- package/src/shell/right-drawer/right-drawer.css +108 -0
- package/src/shell/sidebar/sidebar.css +146 -0
- package/src/styles/animations/motion.css +162 -0
- package/src/styles/base.css +82 -0
- package/src/styles/index.css +21 -0
- package/src/styles/reset.css +51 -0
- package/src/styles/themes/dark.css +120 -0
- package/src/styles/themes/high-contrast.css +119 -0
- package/src/styles/themes/light.css +119 -0
- package/src/styles/tokens/breakpoints.css +15 -0
- package/src/styles/tokens/color.css +97 -0
- package/src/styles/tokens/elevation.css +32 -0
- package/src/styles/tokens/index.css +8 -0
- package/src/styles/tokens/motion.css +36 -0
- package/src/styles/tokens/radius.css +21 -0
- package/src/styles/tokens/spacing.css +50 -0
- package/src/styles/tokens/typography.css +84 -0
- package/src/styles/tokens/z-index.css +15 -0
- package/src/styles/utilities/layout.css +135 -0
- package/src/styles/utilities/text.css +152 -0
- package/src/styles/utilities/visibility.css +48 -0
- package/src/types/index.ts +125 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import type { NtDisposable, NtSide } from '../types/index.js';
|
|
2
|
+
import {
|
|
3
|
+
ntCreateFocusTrap,
|
|
4
|
+
type NtFocusTrapController,
|
|
5
|
+
type NtFocusTrapOptions,
|
|
6
|
+
} from './focus-trap.js';
|
|
7
|
+
import {
|
|
8
|
+
ntCreateOverlay,
|
|
9
|
+
type NtCreateOverlayOptions,
|
|
10
|
+
type NtOverlayController,
|
|
11
|
+
} from './overlay.js';
|
|
12
|
+
import { ntSetAriaControls, ntSetAriaLabelledBy } from './ids.js';
|
|
13
|
+
|
|
14
|
+
export interface NtDrawerOptions {
|
|
15
|
+
trigger?: HTMLElement | null;
|
|
16
|
+
drawer: HTMLElement;
|
|
17
|
+
backdrop?: HTMLElement | null;
|
|
18
|
+
title?: HTMLElement | null;
|
|
19
|
+
side?: NtSide;
|
|
20
|
+
initialOpen?: boolean;
|
|
21
|
+
closeOnEscape?: boolean;
|
|
22
|
+
closeOnOutsideClick?: boolean;
|
|
23
|
+
lockScroll?: boolean;
|
|
24
|
+
restoreFocus?: boolean;
|
|
25
|
+
initialFocus?: NtFocusTrapOptions['initialFocus'];
|
|
26
|
+
onOpen?: () => void;
|
|
27
|
+
onClose?: () => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface NtDrawerController extends NtDisposable {
|
|
31
|
+
readonly drawer: HTMLElement;
|
|
32
|
+
readonly trigger: HTMLElement | null;
|
|
33
|
+
readonly backdrop: HTMLElement | null;
|
|
34
|
+
readonly side: NtSide;
|
|
35
|
+
readonly isOpen: boolean;
|
|
36
|
+
open(): void;
|
|
37
|
+
close(): void;
|
|
38
|
+
toggle(): void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function ntCreateDrawer(options: NtDrawerOptions): NtDrawerController {
|
|
42
|
+
const {
|
|
43
|
+
trigger = null,
|
|
44
|
+
drawer,
|
|
45
|
+
backdrop = null,
|
|
46
|
+
title = null,
|
|
47
|
+
side = 'right',
|
|
48
|
+
initialOpen = false,
|
|
49
|
+
closeOnEscape = true,
|
|
50
|
+
closeOnOutsideClick = true,
|
|
51
|
+
lockScroll = true,
|
|
52
|
+
restoreFocus = true,
|
|
53
|
+
initialFocus = null,
|
|
54
|
+
onOpen,
|
|
55
|
+
onClose,
|
|
56
|
+
} = options;
|
|
57
|
+
|
|
58
|
+
let isOpen = false;
|
|
59
|
+
|
|
60
|
+
const overlayOptions: NtCreateOverlayOptions = {
|
|
61
|
+
closeOnEscape,
|
|
62
|
+
closeOnOutsideClick,
|
|
63
|
+
lockScroll,
|
|
64
|
+
outsideElements: trigger ? [trigger] : [],
|
|
65
|
+
onClose: () => {
|
|
66
|
+
close();
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const overlay: NtOverlayController = ntCreateOverlay(drawer, overlayOptions);
|
|
71
|
+
|
|
72
|
+
const focusTrap: NtFocusTrapController = ntCreateFocusTrap(drawer, {
|
|
73
|
+
initialFocus,
|
|
74
|
+
fallbackFocus: drawer,
|
|
75
|
+
restoreFocus,
|
|
76
|
+
escapeDeactivates: false,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
function syncAttributes(): void {
|
|
80
|
+
drawer.setAttribute('role', 'dialog');
|
|
81
|
+
drawer.setAttribute('aria-modal', 'true');
|
|
82
|
+
drawer.dataset.state = isOpen ? 'open' : 'closed';
|
|
83
|
+
drawer.dataset.side = side;
|
|
84
|
+
|
|
85
|
+
if (!drawer.classList.contains(`nt-drawer--${side}`)) {
|
|
86
|
+
drawer.classList.add(`nt-drawer--${side}`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (backdrop) {
|
|
90
|
+
backdrop.dataset.state = isOpen ? 'open' : 'closed';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (trigger) {
|
|
94
|
+
trigger.setAttribute('aria-expanded', String(isOpen));
|
|
95
|
+
ntSetAriaControls(trigger, drawer, 'nt-drawer');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (title) {
|
|
99
|
+
ntSetAriaLabelledBy(drawer, title, 'nt-drawer-title');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (isOpen) {
|
|
103
|
+
drawer.removeAttribute('hidden');
|
|
104
|
+
backdrop?.removeAttribute('hidden');
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
drawer.setAttribute('hidden', '');
|
|
109
|
+
backdrop?.setAttribute('hidden', '');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function open(): void {
|
|
113
|
+
if (isOpen) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
isOpen = true;
|
|
118
|
+
syncAttributes();
|
|
119
|
+
overlay.activate();
|
|
120
|
+
focusTrap.activate();
|
|
121
|
+
onOpen?.();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function close(): void {
|
|
125
|
+
if (!isOpen) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
isOpen = false;
|
|
130
|
+
focusTrap.deactivate();
|
|
131
|
+
overlay.deactivate();
|
|
132
|
+
syncAttributes();
|
|
133
|
+
onClose?.();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function toggle(): void {
|
|
137
|
+
if (isOpen) {
|
|
138
|
+
close();
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
open();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const triggerListener = () => {
|
|
146
|
+
toggle();
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
trigger?.addEventListener('click', triggerListener);
|
|
150
|
+
|
|
151
|
+
syncAttributes();
|
|
152
|
+
|
|
153
|
+
if (initialOpen) {
|
|
154
|
+
open();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
drawer,
|
|
159
|
+
trigger,
|
|
160
|
+
backdrop,
|
|
161
|
+
side,
|
|
162
|
+
|
|
163
|
+
get isOpen() {
|
|
164
|
+
return isOpen;
|
|
165
|
+
},
|
|
166
|
+
|
|
167
|
+
open,
|
|
168
|
+
close,
|
|
169
|
+
toggle,
|
|
170
|
+
|
|
171
|
+
destroy() {
|
|
172
|
+
trigger?.removeEventListener('click', triggerListener);
|
|
173
|
+
focusTrap.destroy();
|
|
174
|
+
overlay.destroy();
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
NtAlign,
|
|
3
|
+
NtDisposable,
|
|
4
|
+
NtRovingFocusController,
|
|
5
|
+
NtSide,
|
|
6
|
+
} from '../types/index.js';
|
|
7
|
+
import { ntSetAriaControls } from './ids.js';
|
|
8
|
+
import {
|
|
9
|
+
NT_KEYS,
|
|
10
|
+
ntCreateRovingFocus,
|
|
11
|
+
ntIsActivationKey,
|
|
12
|
+
} from './keyboard.js';
|
|
13
|
+
import {
|
|
14
|
+
ntCreateOverlay,
|
|
15
|
+
type NtCreateOverlayOptions,
|
|
16
|
+
type NtOverlayController,
|
|
17
|
+
} from './overlay.js';
|
|
18
|
+
|
|
19
|
+
export interface NtDropdownOptions {
|
|
20
|
+
trigger: HTMLElement;
|
|
21
|
+
content: HTMLElement;
|
|
22
|
+
itemSelector?: string;
|
|
23
|
+
side?: NtSide;
|
|
24
|
+
align?: NtAlign;
|
|
25
|
+
initialOpen?: boolean;
|
|
26
|
+
closeOnEscape?: boolean;
|
|
27
|
+
closeOnOutsideClick?: boolean;
|
|
28
|
+
closeOnSelect?: boolean;
|
|
29
|
+
loopFocus?: boolean;
|
|
30
|
+
onOpen?: () => void;
|
|
31
|
+
onClose?: () => void;
|
|
32
|
+
onSelect?: (item: HTMLElement, event: Event) => void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface NtDropdownController extends NtDisposable {
|
|
36
|
+
readonly trigger: HTMLElement;
|
|
37
|
+
readonly content: HTMLElement;
|
|
38
|
+
readonly isOpen: boolean;
|
|
39
|
+
open(): void;
|
|
40
|
+
close(): void;
|
|
41
|
+
toggle(): void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const DEFAULT_ITEM_SELECTOR = [
|
|
45
|
+
'[role="menuitem"]',
|
|
46
|
+
'[role="option"]',
|
|
47
|
+
'.nt-dropdown__item',
|
|
48
|
+
'.nt-menu__item',
|
|
49
|
+
].join(',');
|
|
50
|
+
|
|
51
|
+
export function ntCreateDropdown(
|
|
52
|
+
options: NtDropdownOptions,
|
|
53
|
+
): NtDropdownController {
|
|
54
|
+
const {
|
|
55
|
+
trigger,
|
|
56
|
+
content,
|
|
57
|
+
itemSelector = DEFAULT_ITEM_SELECTOR,
|
|
58
|
+
side = 'bottom',
|
|
59
|
+
align = 'start',
|
|
60
|
+
initialOpen = false,
|
|
61
|
+
closeOnEscape = true,
|
|
62
|
+
closeOnOutsideClick = true,
|
|
63
|
+
closeOnSelect = true,
|
|
64
|
+
loopFocus = true,
|
|
65
|
+
onOpen,
|
|
66
|
+
onClose,
|
|
67
|
+
onSelect,
|
|
68
|
+
} = options;
|
|
69
|
+
|
|
70
|
+
let isOpen = false;
|
|
71
|
+
let rovingFocus: NtRovingFocusController | null = null;
|
|
72
|
+
|
|
73
|
+
const overlayOptions: NtCreateOverlayOptions = {
|
|
74
|
+
closeOnEscape,
|
|
75
|
+
closeOnOutsideClick,
|
|
76
|
+
lockScroll: false,
|
|
77
|
+
outsideElements: [trigger],
|
|
78
|
+
onClose: () => {
|
|
79
|
+
close();
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const overlay: NtOverlayController = ntCreateOverlay(content, overlayOptions);
|
|
84
|
+
|
|
85
|
+
function syncAttributes(): void {
|
|
86
|
+
trigger.setAttribute('aria-haspopup', 'menu');
|
|
87
|
+
trigger.setAttribute('aria-expanded', String(isOpen));
|
|
88
|
+
ntSetAriaControls(trigger, content, 'nt-dropdown');
|
|
89
|
+
|
|
90
|
+
content.dataset.state = isOpen ? 'open' : 'closed';
|
|
91
|
+
content.dataset.side = side;
|
|
92
|
+
content.dataset.align = align;
|
|
93
|
+
|
|
94
|
+
if (isOpen) {
|
|
95
|
+
content.removeAttribute('hidden');
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
content.setAttribute('hidden', '');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function ensureRovingFocus(): void {
|
|
103
|
+
if (rovingFocus) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
rovingFocus = ntCreateRovingFocus({
|
|
108
|
+
root: content,
|
|
109
|
+
itemSelector,
|
|
110
|
+
orientation: 'vertical',
|
|
111
|
+
loop: loopFocus,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function focusFirstItem(): void {
|
|
116
|
+
ensureRovingFocus();
|
|
117
|
+
rovingFocus?.focusFirst();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function focusLastItem(): void {
|
|
121
|
+
ensureRovingFocus();
|
|
122
|
+
rovingFocus?.focusLast();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function open(): void {
|
|
126
|
+
if (isOpen) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
isOpen = true;
|
|
131
|
+
syncAttributes();
|
|
132
|
+
overlay.activate();
|
|
133
|
+
ensureRovingFocus();
|
|
134
|
+
onOpen?.();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function close(): void {
|
|
138
|
+
if (!isOpen) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
isOpen = false;
|
|
143
|
+
overlay.deactivate();
|
|
144
|
+
syncAttributes();
|
|
145
|
+
trigger.focus({ preventScroll: true });
|
|
146
|
+
onClose?.();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function toggle(): void {
|
|
150
|
+
if (isOpen) {
|
|
151
|
+
close();
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
open();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const handleTriggerClick = () => {
|
|
159
|
+
toggle();
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const handleTriggerKeyDown = (event: KeyboardEvent) => {
|
|
163
|
+
if (event.key === NT_KEYS.arrowDown) {
|
|
164
|
+
event.preventDefault();
|
|
165
|
+
open();
|
|
166
|
+
queueMicrotask(focusFirstItem);
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (event.key === NT_KEYS.arrowUp) {
|
|
171
|
+
event.preventDefault();
|
|
172
|
+
open();
|
|
173
|
+
queueMicrotask(focusLastItem);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (ntIsActivationKey(event)) {
|
|
178
|
+
event.preventDefault();
|
|
179
|
+
toggle();
|
|
180
|
+
|
|
181
|
+
if (!isOpen) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
queueMicrotask(focusFirstItem);
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const handleContentClick = (event: MouseEvent) => {
|
|
190
|
+
const target = event.target;
|
|
191
|
+
|
|
192
|
+
if (!(target instanceof HTMLElement)) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const item = target.closest<HTMLElement>(itemSelector);
|
|
197
|
+
|
|
198
|
+
if (!item || !content.contains(item)) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (
|
|
203
|
+
item.hasAttribute('disabled') ||
|
|
204
|
+
item.getAttribute('aria-disabled') === 'true' ||
|
|
205
|
+
item.dataset.disabled === 'true'
|
|
206
|
+
) {
|
|
207
|
+
event.preventDefault();
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
onSelect?.(item, event);
|
|
212
|
+
|
|
213
|
+
if (closeOnSelect) {
|
|
214
|
+
close();
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const handleContentKeyDown = (event: KeyboardEvent) => {
|
|
219
|
+
if (event.key === NT_KEYS.escape && closeOnEscape) {
|
|
220
|
+
event.preventDefault();
|
|
221
|
+
close();
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (!ntIsActivationKey(event)) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const target = event.target;
|
|
230
|
+
|
|
231
|
+
if (!(target instanceof HTMLElement)) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const item = target.closest<HTMLElement>(itemSelector);
|
|
236
|
+
|
|
237
|
+
if (!item || !content.contains(item)) {
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
event.preventDefault();
|
|
242
|
+
onSelect?.(item, event);
|
|
243
|
+
|
|
244
|
+
if (closeOnSelect) {
|
|
245
|
+
close();
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
trigger.addEventListener('click', handleTriggerClick);
|
|
250
|
+
trigger.addEventListener('keydown', handleTriggerKeyDown);
|
|
251
|
+
content.addEventListener('click', handleContentClick);
|
|
252
|
+
content.addEventListener('keydown', handleContentKeyDown);
|
|
253
|
+
|
|
254
|
+
syncAttributes();
|
|
255
|
+
|
|
256
|
+
if (initialOpen) {
|
|
257
|
+
open();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return {
|
|
261
|
+
trigger,
|
|
262
|
+
content,
|
|
263
|
+
|
|
264
|
+
get isOpen() {
|
|
265
|
+
return isOpen;
|
|
266
|
+
},
|
|
267
|
+
|
|
268
|
+
open,
|
|
269
|
+
close,
|
|
270
|
+
toggle,
|
|
271
|
+
|
|
272
|
+
destroy() {
|
|
273
|
+
trigger.removeEventListener('click', handleTriggerClick);
|
|
274
|
+
trigger.removeEventListener('keydown', handleTriggerKeyDown);
|
|
275
|
+
content.removeEventListener('click', handleContentClick);
|
|
276
|
+
content.removeEventListener('keydown', handleContentKeyDown);
|
|
277
|
+
rovingFocus?.destroy();
|
|
278
|
+
overlay.destroy();
|
|
279
|
+
},
|
|
280
|
+
};
|
|
281
|
+
}
|
|
@@ -0,0 +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
|
+
};
|
|
261
|
+
}
|