@barqjs/aria 0.0.0-stage → 0.2.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +293 -2
- package/package.json +347 -4
- package/src/breadcrumbs.tsx +296 -0
- package/src/button.tsx +342 -0
- package/src/calendar.tsx +1389 -0
- package/src/checkbox.tsx +570 -0
- package/src/collections.ts +976 -0
- package/src/color.ts +562 -0
- package/src/colorpicker.tsx +1478 -0
- package/src/combobox.tsx +2481 -0
- package/src/command.tsx +1184 -0
- package/src/date.ts +652 -0
- package/src/datefield.tsx +951 -0
- package/src/datepicker.tsx +556 -0
- package/src/dialog.tsx +890 -0
- package/src/disclosure.tsx +659 -0
- package/src/dom.ts +706 -0
- package/src/drawer.ts +674 -0
- package/src/focus.ts +1370 -0
- package/src/form.tsx +94 -0
- package/src/gridlist.tsx +563 -0
- package/src/i18n.ts +823 -0
- package/src/index.ts +1270 -0
- package/src/interactions/description.ts +73 -0
- package/src/interactions/flags.ts +35 -0
- package/src/interactions/focus-events.ts +218 -0
- package/src/interactions/focusable.ts +86 -0
- package/src/interactions/hover.ts +213 -0
- package/src/interactions/interact-outside.ts +123 -0
- package/src/interactions/keyboard.ts +105 -0
- package/src/interactions/listeners.ts +78 -0
- package/src/interactions/long-press.ts +160 -0
- package/src/interactions/modality.ts +416 -0
- package/src/interactions/move.ts +257 -0
- package/src/interactions/open-link.ts +193 -0
- package/src/interactions/press.ts +898 -0
- package/src/interactions/prevent-focus.ts +102 -0
- package/src/interactions/scroll-wheel.ts +43 -0
- package/src/interactions/text-selection.ts +77 -0
- package/src/interactions.ts +81 -0
- package/src/label.ts +128 -0
- package/src/link.tsx +381 -0
- package/src/listbox.tsx +721 -0
- package/src/live.ts +204 -0
- package/src/menu.tsx +2290 -0
- package/src/menubar.tsx +575 -0
- package/src/navigation.ts +735 -0
- package/src/numberfield.tsx +838 -0
- package/src/otp.ts +430 -0
- package/src/overlays.ts +1247 -0
- package/src/platform.ts +74 -0
- package/src/presence.ts +164 -0
- package/src/questionnaire.tsx +1712 -0
- package/src/radio.tsx +567 -0
- package/src/register-dom.ts +12 -0
- package/src/scroller.ts +1165 -0
- package/src/select.tsx +1665 -0
- package/src/selection.ts +1415 -0
- package/src/slider.tsx +1042 -0
- package/src/splitter.ts +1152 -0
- package/src/static-collection.ts +211 -0
- package/src/switch.tsx +186 -0
- package/src/table.tsx +0 -0
- package/src/tabs.tsx +1018 -0
- package/src/tag.tsx +452 -0
- package/src/textfield.tsx +417 -0
- package/src/toast.ts +658 -0
- package/src/toggle-input.tsx +48 -0
- package/src/toggle.ts +321 -0
- package/src/togglegroup.ts +365 -0
- package/src/toolbar.tsx +179 -0
- package/src/tooltip.tsx +657 -0
- package/src/utils.ts +758 -0
- package/src/validation.ts +447 -0
- package/src/virtualizer.tsx +674 -0
|
@@ -0,0 +1,735 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A navigation menu's state: which item is open, and which way it moved.
|
|
3
|
+
*
|
|
4
|
+
* This is the piece `@barqjs/ui`'s `<NavigationMenu>` needed and nothing here
|
|
5
|
+
* had. It is not a menu: a menu opens one panel at a time from a press and
|
|
6
|
+
* closes on choosing, and a navigation menu opens on HOVER, keeps the panel
|
|
7
|
+
* while the pointer travels to it, and slides sideways when the pointer moves
|
|
8
|
+
* to the next trigger rather than closing and reopening.
|
|
9
|
+
*
|
|
10
|
+
* Three things follow from that and each is a decision rather than a detail.
|
|
11
|
+
*
|
|
12
|
+
* **Opening is immediate once one is open.** The first panel waits, so brushing
|
|
13
|
+
* the bar on the way somewhere else opens nothing; after that the menu is
|
|
14
|
+
* "active" and moving between triggers is instant, because a delay there reads
|
|
15
|
+
* as lag rather than as intent.
|
|
16
|
+
*
|
|
17
|
+
* **Closing waits.** The pointer has to cross the gap between the trigger and
|
|
18
|
+
* the panel, and a menu that closes the moment it leaves the trigger cannot be
|
|
19
|
+
* reached at all.
|
|
20
|
+
*
|
|
21
|
+
* **The previous key is kept**, because the panel's animation depends on which
|
|
22
|
+
* side it came from. That is upstream's `data-motion`, and it is the only
|
|
23
|
+
* reason a navigation menu looks different from a row of popovers.
|
|
24
|
+
*
|
|
25
|
+
* {@link navigationMenu} is the other half: the keyboard and the pointer. Its
|
|
26
|
+
* decisions:
|
|
27
|
+
*
|
|
28
|
+
* **The bar is a row of Tab stops, and the arrows are a shortcut along it.**
|
|
29
|
+
* This is the disclosure navigation pattern, not a menubar: every trigger and
|
|
30
|
+
* every top-level link stays in the Tab order, because a site's navigation
|
|
31
|
+
* that Tab skips is navigation a keyboard user has to learn a second key for.
|
|
32
|
+
*
|
|
33
|
+
* **Tab reads a panel straight after its trigger, wherever the panel is.** A
|
|
34
|
+
* shared viewport sits after the whole list in the document, so the browser's
|
|
35
|
+
* own order would visit every other trigger first. The order is corrected only
|
|
36
|
+
* when the two disagree, so a panel rendered beside its trigger is left to the
|
|
37
|
+
* browser entirely.
|
|
38
|
+
*
|
|
39
|
+
* **The key that points at the panel goes into it.** ArrowDown under a
|
|
40
|
+
* horizontal bar, the inline-end arrow beside a vertical one. It opens the
|
|
41
|
+
* panel first when it is closed, where Radix moves along the bar instead: an
|
|
42
|
+
* arrow pointing at where a panel will appear and going sideways is the
|
|
43
|
+
* surprise, and the horizontal arrows already do that.
|
|
44
|
+
*
|
|
45
|
+
* **A press closes only what a press opened.** Reaching a trigger with a mouse
|
|
46
|
+
* means entering it first, which opens the panel, so a press that toggles
|
|
47
|
+
* finds it open and shuts it again.
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
import { createSignal, createEffect, createUniqueId, type Accessor } from "@barqjs/core";
|
|
51
|
+
import { isServer } from "@barqjs/web";
|
|
52
|
+
import { resizeObserver } from "@barqjs/primitives/observers";
|
|
53
|
+
import { ref as makeRef } from "@barqjs/primitives/refs";
|
|
54
|
+
|
|
55
|
+
import type { Key } from "./collections.ts";
|
|
56
|
+
import { activeElement, contains, ownerDocument, targetElement } from "./dom.ts";
|
|
57
|
+
import { focusableWalker } from "./focus.ts";
|
|
58
|
+
import { useLocale } from "./i18n.ts";
|
|
59
|
+
import type { ElementRef } from "./interactions/press.ts";
|
|
60
|
+
import { presence } from "./presence.ts";
|
|
61
|
+
import type { Direction, Orientation } from "./selection.ts";
|
|
62
|
+
import { access, id, type DOMProps, type MaybeAccessor } from "./utils.ts";
|
|
63
|
+
|
|
64
|
+
/** How long the FIRST panel waits before opening on hover. */
|
|
65
|
+
const OPEN_DELAY = 200;
|
|
66
|
+
/** How long any panel stays after the pointer leaves it and its trigger. */
|
|
67
|
+
const CLOSE_DELAY = 300;
|
|
68
|
+
/** How long after closing a hover still opens at once. */
|
|
69
|
+
const SKIP_DELAY = 300;
|
|
70
|
+
|
|
71
|
+
export interface NavigationMenuStateOptions {
|
|
72
|
+
value?: MaybeAccessor<Key | null | undefined>;
|
|
73
|
+
defaultValue?: MaybeAccessor<Key | null | undefined>;
|
|
74
|
+
/** How long the first panel waits. @default 200 */
|
|
75
|
+
delay?: MaybeAccessor<number | undefined>;
|
|
76
|
+
/** How long a panel stays once the pointer has left. @default 300 */
|
|
77
|
+
closeDelay?: MaybeAccessor<number | undefined>;
|
|
78
|
+
/**
|
|
79
|
+
* How long after the last panel closed a hover opens the next one with no
|
|
80
|
+
* wait. Zero makes every first open wait. @default 300
|
|
81
|
+
*/
|
|
82
|
+
skipDelay?: MaybeAccessor<number | undefined>;
|
|
83
|
+
onValueChange?: (value: Key | null) => void;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type MotionDirection = "from-start" | "from-end" | "to-start" | "to-end" | null;
|
|
87
|
+
|
|
88
|
+
export interface NavigationMenuState {
|
|
89
|
+
/** The open item, or `null`. */
|
|
90
|
+
readonly value: Accessor<Key | null>;
|
|
91
|
+
/** What was open immediately before, which is what the direction is measured from. */
|
|
92
|
+
readonly previous: Accessor<Key | null>;
|
|
93
|
+
/** Open it now, with no delay. A press and the keyboard both want this. */
|
|
94
|
+
open(value: Key): void;
|
|
95
|
+
/** Open it after the delay, unless something is already open. */
|
|
96
|
+
openSoon(value: Key): void;
|
|
97
|
+
close(): void;
|
|
98
|
+
/** Close after the delay, so the pointer can cross the gap to the panel. */
|
|
99
|
+
closeSoon(): void;
|
|
100
|
+
/** Cancel a pending close, which is what entering the panel does. */
|
|
101
|
+
keep(): void;
|
|
102
|
+
/**
|
|
103
|
+
* Which way a panel is arriving or leaving, given the order of the items.
|
|
104
|
+
*
|
|
105
|
+
* `null` when there is nothing to compare against, and the caller writes no
|
|
106
|
+
* `data-motion` at all rather than writing a direction it made up.
|
|
107
|
+
*/
|
|
108
|
+
motion(value: Key, order: readonly Key[]): MotionDirection;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function navigationMenuState(options: NavigationMenuStateOptions = {}): NavigationMenuState {
|
|
112
|
+
const [inner, setInner] = createSignal<Key | null>(access(options.defaultValue) ?? null);
|
|
113
|
+
const [previous, setPrevious] = createSignal<Key | null>(null);
|
|
114
|
+
let openTimer: ReturnType<typeof setTimeout> | null = null;
|
|
115
|
+
let closeTimer: ReturnType<typeof setTimeout> | null = null;
|
|
116
|
+
// Someone who has just had a panel open is still using the bar, so the next
|
|
117
|
+
// hover is not the accidental brush the first delay is there to ignore.
|
|
118
|
+
let warmTimer: ReturnType<typeof setTimeout> | null = null;
|
|
119
|
+
let warm = false;
|
|
120
|
+
|
|
121
|
+
const clear = (timer: ReturnType<typeof setTimeout> | null): null => {
|
|
122
|
+
if (timer !== null) clearTimeout(timer);
|
|
123
|
+
return null;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const value = (): Key | null => access(options.value) ?? inner();
|
|
127
|
+
|
|
128
|
+
const set = (next: Key | null): void => {
|
|
129
|
+
const now = value();
|
|
130
|
+
if (now === next) return;
|
|
131
|
+
warmTimer = clear(warmTimer);
|
|
132
|
+
warm = false;
|
|
133
|
+
if (next === null) {
|
|
134
|
+
const lasts = access(options.skipDelay) ?? SKIP_DELAY;
|
|
135
|
+
if (lasts > 0) {
|
|
136
|
+
warm = true;
|
|
137
|
+
warmTimer = setTimeout(() => {
|
|
138
|
+
warm = false;
|
|
139
|
+
warmTimer = null;
|
|
140
|
+
}, lasts);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
setPrevious(now);
|
|
144
|
+
if (access(options.value) === undefined) setInner(next);
|
|
145
|
+
options.onValueChange?.(next);
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const open = (next: Key): void => {
|
|
149
|
+
openTimer = clear(openTimer);
|
|
150
|
+
closeTimer = clear(closeTimer);
|
|
151
|
+
set(next);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
value,
|
|
156
|
+
previous,
|
|
157
|
+
open,
|
|
158
|
+
openSoon(next) {
|
|
159
|
+
closeTimer = clear(closeTimer);
|
|
160
|
+
// Already open: move NOW. A delay between triggers reads as lag, where
|
|
161
|
+
// the same delay before the first one reads as not having meant it.
|
|
162
|
+
if (value() !== null || warm) {
|
|
163
|
+
open(next);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
openTimer = clear(openTimer);
|
|
167
|
+
openTimer = setTimeout(() => open(next), access(options.delay) ?? OPEN_DELAY);
|
|
168
|
+
},
|
|
169
|
+
close() {
|
|
170
|
+
openTimer = clear(openTimer);
|
|
171
|
+
closeTimer = clear(closeTimer);
|
|
172
|
+
set(null);
|
|
173
|
+
},
|
|
174
|
+
closeSoon() {
|
|
175
|
+
openTimer = clear(openTimer);
|
|
176
|
+
closeTimer = clear(closeTimer);
|
|
177
|
+
closeTimer = setTimeout(() => set(null), access(options.closeDelay) ?? CLOSE_DELAY);
|
|
178
|
+
},
|
|
179
|
+
keep() {
|
|
180
|
+
closeTimer = clear(closeTimer);
|
|
181
|
+
},
|
|
182
|
+
motion(item, order) {
|
|
183
|
+
const now = value();
|
|
184
|
+
const before = previous();
|
|
185
|
+
const at = order.indexOf(item);
|
|
186
|
+
if (at < 0) return null;
|
|
187
|
+
|
|
188
|
+
// Arriving: which side of the one that was open is this?
|
|
189
|
+
if (now === item) {
|
|
190
|
+
if (before === null) return null;
|
|
191
|
+
const from = order.indexOf(before);
|
|
192
|
+
if (from < 0) return null;
|
|
193
|
+
return from < at ? "from-end" : "from-start";
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Leaving: which side is the one that took its place?
|
|
197
|
+
if (before === item && now !== null) {
|
|
198
|
+
const to = order.indexOf(now);
|
|
199
|
+
if (to < 0) return null;
|
|
200
|
+
return to < at ? "to-end" : "to-start";
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return null;
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// ---------------------------------------------------------------------------
|
|
209
|
+
// The keyboard and the pointer
|
|
210
|
+
// ---------------------------------------------------------------------------
|
|
211
|
+
|
|
212
|
+
/** On every trigger and link: what the arrow keys move between. */
|
|
213
|
+
const ITEM = "data-barq-navigation-item";
|
|
214
|
+
/** On every panel, so a link inside one is not counted as part of the bar. */
|
|
215
|
+
const CONTENT = "data-barq-navigation-content";
|
|
216
|
+
|
|
217
|
+
export interface NavigationMenuOptions {
|
|
218
|
+
/** The `<nav>`. */
|
|
219
|
+
ref: ElementRef;
|
|
220
|
+
/** @default "horizontal" */
|
|
221
|
+
orientation?: MaybeAccessor<Orientation | undefined>;
|
|
222
|
+
/** @default the direction in scope */
|
|
223
|
+
direction?: MaybeAccessor<Direction | undefined>;
|
|
224
|
+
"aria-label"?: MaybeAccessor<string | undefined>;
|
|
225
|
+
"aria-labelledby"?: MaybeAccessor<string | undefined>;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export interface NavigationMenuItemOptions {
|
|
229
|
+
/** What identifies the item to the shared state. @default a generated id */
|
|
230
|
+
key?: Key | undefined;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export interface NavigationMenuTriggerOptions {
|
|
234
|
+
isDisabled?: MaybeAccessor<boolean | undefined>;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export interface NavigationMenuLinkOptions {
|
|
238
|
+
/** The link is the current page. */
|
|
239
|
+
isActive?: MaybeAccessor<boolean | undefined>;
|
|
240
|
+
/**
|
|
241
|
+
* Called when the link is chosen, before the menu closes. Cancelling the
|
|
242
|
+
* event keeps the menu open.
|
|
243
|
+
*/
|
|
244
|
+
onSelect?: (event: Event) => void;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export interface NavigationMenuItemResult {
|
|
248
|
+
key: Key;
|
|
249
|
+
isOpen: Accessor<boolean>;
|
|
250
|
+
contentId: Accessor<string>;
|
|
251
|
+
/** The panel's element, once it exists. */
|
|
252
|
+
content: Accessor<HTMLElement | null>;
|
|
253
|
+
triggerRef: (element: HTMLElement | null) => void;
|
|
254
|
+
contentRef: (element: HTMLElement | null) => void;
|
|
255
|
+
triggerProps: (options?: NavigationMenuTriggerOptions) => DOMProps;
|
|
256
|
+
contentProps: DOMProps;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export interface NavigationMenuResult {
|
|
260
|
+
rootProps: DOMProps;
|
|
261
|
+
listProps: DOMProps;
|
|
262
|
+
viewportProps: DOMProps;
|
|
263
|
+
indicatorProps: DOMProps;
|
|
264
|
+
/** Every item, in the order it was declared: the motion is measured from it. */
|
|
265
|
+
order: Accessor<Key[]>;
|
|
266
|
+
orientation: Accessor<Orientation>;
|
|
267
|
+
/** The shared viewport's element, once it exists. */
|
|
268
|
+
viewport: Accessor<HTMLElement | null>;
|
|
269
|
+
viewportRef: (element: HTMLElement | null) => void;
|
|
270
|
+
item: (options?: NavigationMenuItemOptions) => NavigationMenuItemResult;
|
|
271
|
+
linkProps: (options?: NavigationMenuLinkOptions) => DOMProps;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
interface ItemRecord {
|
|
275
|
+
trigger: Accessor<HTMLElement | null>;
|
|
276
|
+
content: Accessor<HTMLElement | null>;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const MOVES = new Set(["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown", "Home", "End"]);
|
|
280
|
+
|
|
281
|
+
function isModified(event: KeyboardEvent): boolean {
|
|
282
|
+
return event.altKey || event.ctrlKey || event.metaKey;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function tabbablesIn(root: Element): HTMLElement[] {
|
|
286
|
+
const walker = focusableWalker(root, { tabbable: true });
|
|
287
|
+
const found: HTMLElement[] = [];
|
|
288
|
+
let node = walker.nextNode();
|
|
289
|
+
while (node !== null) {
|
|
290
|
+
found.push(node as HTMLElement);
|
|
291
|
+
node = walker.nextNode();
|
|
292
|
+
}
|
|
293
|
+
return found;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function mouseOnly(handler: () => void): (event: PointerEvent) => void {
|
|
297
|
+
return (event) => {
|
|
298
|
+
if (event.pointerType === "mouse") handler();
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* The behaviour of a navigation menu, as props for the elements that make it.
|
|
304
|
+
*
|
|
305
|
+
* ```tsx
|
|
306
|
+
* const state = navigationMenuState();
|
|
307
|
+
* const menu = navigationMenu({ ref }, state);
|
|
308
|
+
* const products = menu.item({ key: "products" });
|
|
309
|
+
*
|
|
310
|
+
* <nav {...menu.rootProps} ref={ref.set}>
|
|
311
|
+
* <ul {...menu.listProps}>
|
|
312
|
+
* <li>
|
|
313
|
+
* <button {...products.triggerProps()} ref={products.triggerRef}>Products</button>
|
|
314
|
+
* <div {...products.contentProps} ref={products.contentRef}>
|
|
315
|
+
* <a {...menu.linkProps()} href="/analytics">Analytics</a>
|
|
316
|
+
* </div>
|
|
317
|
+
* </li>
|
|
318
|
+
* </ul>
|
|
319
|
+
* </nav>
|
|
320
|
+
* ```
|
|
321
|
+
*/
|
|
322
|
+
export function navigationMenu(
|
|
323
|
+
options: NavigationMenuOptions,
|
|
324
|
+
state: NavigationMenuState,
|
|
325
|
+
): NavigationMenuResult {
|
|
326
|
+
const locale = useLocale();
|
|
327
|
+
const orientation = (): Orientation => access(options.orientation) ?? "horizontal";
|
|
328
|
+
const direction = (): Direction => access(options.direction) ?? locale().direction;
|
|
329
|
+
const root = (): Element | null => (access(options.ref) as Element | null) ?? null;
|
|
330
|
+
|
|
331
|
+
const items = new Map<Key, ItemRecord>();
|
|
332
|
+
// `ownedWrite`: an item registers from a component body. The array beside it
|
|
333
|
+
// answers "already registered" within one render, where the signal still
|
|
334
|
+
// holds what the last flush published.
|
|
335
|
+
const declared: Key[] = [];
|
|
336
|
+
const [order, setOrder] = createSignal<Key[]>([], { ownedWrite: true });
|
|
337
|
+
|
|
338
|
+
const viewport = makeRef<HTMLElement>();
|
|
339
|
+
const [viewportSize, setViewportSize] = createSignal<{ width: number; height: number } | null>(
|
|
340
|
+
null,
|
|
341
|
+
);
|
|
342
|
+
const [indicator, setIndicator] = createSignal<{ offset: number; size: number } | null>(null);
|
|
343
|
+
// The viewport is a surface of its own, with a size it keeps from the last
|
|
344
|
+
// panel. Left in view while nothing is open, it is an empty box under the bar.
|
|
345
|
+
const viewportGate = presence({ isOpen: () => state.value() !== null, ref: viewport });
|
|
346
|
+
|
|
347
|
+
/** Set by a hover that did the opening, cleared by the press that follows. */
|
|
348
|
+
let viaHover = false;
|
|
349
|
+
/** The item whose panel takes focus as soon as it exists. */
|
|
350
|
+
let entering: Key | null = null;
|
|
351
|
+
|
|
352
|
+
const openRecord = (): ItemRecord | null => {
|
|
353
|
+
const key = state.value();
|
|
354
|
+
return key === null ? null : (items.get(key) ?? null);
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
const focusInto = (content: HTMLElement, end: "first" | "last"): boolean => {
|
|
358
|
+
const inside = tabbablesIn(content);
|
|
359
|
+
const target = end === "first" ? inside[0] : inside[inside.length - 1];
|
|
360
|
+
if (target === undefined) return false;
|
|
361
|
+
target.focus();
|
|
362
|
+
return true;
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
/** Close, handing focus back to the trigger when it was inside the panel. */
|
|
366
|
+
const dismiss = (): void => {
|
|
367
|
+
const record = openRecord();
|
|
368
|
+
const content = record?.content() ?? null;
|
|
369
|
+
const held = content !== null && contains(content, activeElement(ownerDocument(content)));
|
|
370
|
+
state.close();
|
|
371
|
+
if (held) record?.trigger()?.focus();
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
/** Move along `group` for an arrow, Home or End. */
|
|
375
|
+
const move = (event: KeyboardEvent, group: HTMLElement[]): void => {
|
|
376
|
+
if (event.defaultPrevented || isModified(event) || event.shiftKey) return;
|
|
377
|
+
if (!MOVES.has(event.key)) return;
|
|
378
|
+
const current = targetElement(event)?.closest<HTMLElement>(`[${ITEM}]`) ?? null;
|
|
379
|
+
if (current === null) return;
|
|
380
|
+
|
|
381
|
+
const backwards = [direction() === "rtl" ? "ArrowRight" : "ArrowLeft", "ArrowUp", "End"];
|
|
382
|
+
let candidates = backwards.includes(event.key) ? group.toReversed() : group;
|
|
383
|
+
if (event.key.startsWith("Arrow"))
|
|
384
|
+
candidates = candidates.slice(candidates.indexOf(current) + 1);
|
|
385
|
+
|
|
386
|
+
candidates[0]?.focus();
|
|
387
|
+
event.preventDefault();
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
const itemsOf = (container: Element, content: Element | null): HTMLElement[] =>
|
|
391
|
+
[...container.querySelectorAll<HTMLElement>(`[${ITEM}]`)].filter(
|
|
392
|
+
(element) =>
|
|
393
|
+
element.closest(`[${CONTENT}]`) === content &&
|
|
394
|
+
!(element instanceof HTMLButtonElement && element.disabled),
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Tab through the open panel as if it came straight after its trigger.
|
|
399
|
+
*
|
|
400
|
+
* Nothing is done when the document's own order already says that, which is
|
|
401
|
+
* every menu with no shared viewport.
|
|
402
|
+
*/
|
|
403
|
+
const onTab = (event: KeyboardEvent): void => {
|
|
404
|
+
const record = openRecord();
|
|
405
|
+
const trigger = record?.trigger() ?? null;
|
|
406
|
+
const content = record?.content() ?? null;
|
|
407
|
+
if (trigger === null || content === null) return;
|
|
408
|
+
|
|
409
|
+
const doc = ownerDocument(trigger);
|
|
410
|
+
const active = activeElement(doc);
|
|
411
|
+
const all = tabbablesIn(doc.body);
|
|
412
|
+
const inside = all.filter((element) => contains(content, element));
|
|
413
|
+
if (inside.length === 0) return;
|
|
414
|
+
|
|
415
|
+
const outside = all.filter((element) => !contains(content, element));
|
|
416
|
+
const at = outside.indexOf(trigger);
|
|
417
|
+
if (at < 0) return;
|
|
418
|
+
|
|
419
|
+
const read = [...outside.slice(0, at + 1), ...inside, ...outside.slice(at + 1)];
|
|
420
|
+
const step = event.shiftKey ? -1 : 1;
|
|
421
|
+
const next = read[read.indexOf(active as HTMLElement) + step];
|
|
422
|
+
if (active === null || !read.includes(active as HTMLElement) || next === undefined) return;
|
|
423
|
+
if (all[all.indexOf(active as HTMLElement) + step] === next) return;
|
|
424
|
+
|
|
425
|
+
event.preventDefault();
|
|
426
|
+
next.focus();
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
const onRootKeyDown = (event: KeyboardEvent): void => {
|
|
430
|
+
if (event.defaultPrevented || isModified(event)) return;
|
|
431
|
+
if (event.key === "Tab") {
|
|
432
|
+
onTab(event);
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
if (event.key === "Escape" && state.value() !== null) {
|
|
436
|
+
// Handled here, so a dialog the bar sits in does not close with it.
|
|
437
|
+
event.preventDefault();
|
|
438
|
+
event.stopPropagation();
|
|
439
|
+
dismiss();
|
|
440
|
+
}
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
const onListKeyDown = (event: KeyboardEvent): void => {
|
|
444
|
+
const list = event.currentTarget as Element;
|
|
445
|
+
const inPanel = targetElement(event)?.closest(`[${CONTENT}]`) ?? null;
|
|
446
|
+
if (inPanel !== null) return;
|
|
447
|
+
move(event, itemsOf(list, null));
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
if (!isServer) {
|
|
451
|
+
// While something is open, the rest of the page can close it: a press
|
|
452
|
+
// anywhere that is not a trigger or the panel, focus going elsewhere, and
|
|
453
|
+
// Escape from wherever the keyboard happens to be.
|
|
454
|
+
createEffect(
|
|
455
|
+
() => (state.value() === null ? null : root()),
|
|
456
|
+
(nav) => {
|
|
457
|
+
if (nav === null) return undefined;
|
|
458
|
+
const doc = ownerDocument(nav);
|
|
459
|
+
|
|
460
|
+
const onPointerDown = (event: Event): void => {
|
|
461
|
+
const target = targetElement(event);
|
|
462
|
+
if (target === null) return;
|
|
463
|
+
if (contains(nav, target)) {
|
|
464
|
+
const content = openRecord()?.content() ?? null;
|
|
465
|
+
const view = viewport();
|
|
466
|
+
if (
|
|
467
|
+
(content !== null && contains(content, target)) ||
|
|
468
|
+
(view !== null && contains(view, target)) ||
|
|
469
|
+
target.closest(`[${ITEM}][aria-expanded]`) !== null
|
|
470
|
+
) {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
state.close();
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
const onFocusIn = (event: Event): void => {
|
|
478
|
+
const target = targetElement(event);
|
|
479
|
+
if (target !== null && !contains(nav, target)) state.close();
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
const onKeyDown = (event: KeyboardEvent): void => {
|
|
483
|
+
if (event.key !== "Escape" || event.defaultPrevented) return;
|
|
484
|
+
if (contains(nav, targetElement(event))) return;
|
|
485
|
+
state.close();
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
doc.addEventListener("pointerdown", onPointerDown, true);
|
|
489
|
+
doc.addEventListener("focusin", onFocusIn, true);
|
|
490
|
+
doc.addEventListener("keydown", onKeyDown);
|
|
491
|
+
return () => {
|
|
492
|
+
doc.removeEventListener("pointerdown", onPointerDown, true);
|
|
493
|
+
doc.removeEventListener("focusin", onFocusIn, true);
|
|
494
|
+
doc.removeEventListener("keydown", onKeyDown);
|
|
495
|
+
};
|
|
496
|
+
},
|
|
497
|
+
);
|
|
498
|
+
|
|
499
|
+
// Where the open trigger is along the bar. Kept when the menu closes, so
|
|
500
|
+
// the indicator fades out where it was rather than jumping to the corner.
|
|
501
|
+
const placement = () => [root(), openRecord()?.trigger() ?? null, orientation()] as const;
|
|
502
|
+
const place = ([nav, trigger, along]: ReturnType<typeof placement>): void => {
|
|
503
|
+
if (nav === null || trigger === null) return;
|
|
504
|
+
const bar = nav.getBoundingClientRect();
|
|
505
|
+
const box = trigger.getBoundingClientRect();
|
|
506
|
+
setIndicator(
|
|
507
|
+
along === "horizontal"
|
|
508
|
+
? { offset: box.left - bar.left, size: box.width }
|
|
509
|
+
: { offset: box.top - bar.top, size: box.height },
|
|
510
|
+
);
|
|
511
|
+
};
|
|
512
|
+
createEffect(placement, place);
|
|
513
|
+
resizeObserver(root, () => place(placement()));
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const register = (key: Key): void => {
|
|
517
|
+
// Declaration order, and it has to be stable: the motion direction is an
|
|
518
|
+
// index comparison, so a list that reorders itself would animate the wrong
|
|
519
|
+
// way. An item registers once and stays where it was.
|
|
520
|
+
if (declared.includes(key)) return;
|
|
521
|
+
declared.push(key);
|
|
522
|
+
setOrder([...declared]);
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
const item = (itemOptions: NavigationMenuItemOptions = {}): NavigationMenuItemResult => {
|
|
526
|
+
const key = itemOptions.key ?? createUniqueId();
|
|
527
|
+
const trigger = makeRef<HTMLElement>();
|
|
528
|
+
const content = makeRef<HTMLElement>();
|
|
529
|
+
const triggerId = id();
|
|
530
|
+
const contentId = id();
|
|
531
|
+
const isOpen = (): boolean => state.value() === key;
|
|
532
|
+
/** One pointer visit opens once, however many moves it is made of. */
|
|
533
|
+
let visited = false;
|
|
534
|
+
|
|
535
|
+
register(key);
|
|
536
|
+
items.set(key, { trigger, content });
|
|
537
|
+
|
|
538
|
+
const enter = (): void => {
|
|
539
|
+
if (!isOpen()) {
|
|
540
|
+
viaHover = false;
|
|
541
|
+
state.open(key);
|
|
542
|
+
}
|
|
543
|
+
const element = content();
|
|
544
|
+
if (element !== null && element.isConnected && focusInto(element, "first")) return;
|
|
545
|
+
entering = key;
|
|
546
|
+
};
|
|
547
|
+
|
|
548
|
+
if (!isServer) {
|
|
549
|
+
// The panel is built after the key that asked for it, and a portalled one
|
|
550
|
+
// is attached later still, so the focus waits for it.
|
|
551
|
+
createEffect(content, (element) => {
|
|
552
|
+
if (element === null || entering !== key) return undefined;
|
|
553
|
+
let attempts = 4;
|
|
554
|
+
const attempt = (): void => {
|
|
555
|
+
if (entering !== key) return;
|
|
556
|
+
if (element.isConnected) {
|
|
557
|
+
entering = null;
|
|
558
|
+
focusInto(element, "first");
|
|
559
|
+
} else if (attempts-- > 0) {
|
|
560
|
+
queueMicrotask(attempt);
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
attempt();
|
|
564
|
+
return undefined;
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
// The viewport grows to whatever is open, so the panel says how big it
|
|
568
|
+
// is. `offsetWidth`, not `getBoundingClientRect`: every panel enters with
|
|
569
|
+
// a scale animation, and a transformed box reports what it is PAINTED at.
|
|
570
|
+
const shown = (): HTMLElement | null => (isOpen() ? content() : null);
|
|
571
|
+
const report = (element: HTMLElement | null): void => {
|
|
572
|
+
if (element === null) return;
|
|
573
|
+
setViewportSize({ width: element.offsetWidth, height: element.offsetHeight });
|
|
574
|
+
};
|
|
575
|
+
createEffect(shown, report);
|
|
576
|
+
resizeObserver(shown, () => report(shown()));
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
const onContentKeyDown = (event: KeyboardEvent): void => {
|
|
580
|
+
const element = event.currentTarget as Element;
|
|
581
|
+
// A panel inside this one answers for its own links.
|
|
582
|
+
if (targetElement(event)?.closest(`[${CONTENT}]`) !== element) return;
|
|
583
|
+
move(event, itemsOf(element, element));
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
return {
|
|
587
|
+
key,
|
|
588
|
+
isOpen,
|
|
589
|
+
contentId,
|
|
590
|
+
content,
|
|
591
|
+
triggerRef: trigger.set,
|
|
592
|
+
contentRef: content.set,
|
|
593
|
+
triggerProps: (triggerOptions = {}) => {
|
|
594
|
+
const isDisabled = (): boolean => access(triggerOptions.isDisabled) === true;
|
|
595
|
+
return {
|
|
596
|
+
id: triggerId,
|
|
597
|
+
[ITEM]: "",
|
|
598
|
+
disabled: () => isDisabled() || undefined,
|
|
599
|
+
"aria-expanded": () => (isOpen() ? "true" : "false"),
|
|
600
|
+
"aria-controls": () => (isOpen() ? contentId() : undefined),
|
|
601
|
+
"data-state": () => (isOpen() ? "open" : "closed"),
|
|
602
|
+
"data-disabled": () => (isDisabled() ? "" : undefined),
|
|
603
|
+
onPointerMove: mouseOnly(() => {
|
|
604
|
+
if (isDisabled() || visited) return;
|
|
605
|
+
visited = true;
|
|
606
|
+
// Only when the visit DID the opening. Arriving at a trigger that
|
|
607
|
+
// is already open changes nothing, and marking it hover-opened
|
|
608
|
+
// would make the press that follows do nothing for ever.
|
|
609
|
+
if (!isOpen()) viaHover = true;
|
|
610
|
+
state.openSoon(key);
|
|
611
|
+
}),
|
|
612
|
+
onPointerLeave: mouseOnly(() => {
|
|
613
|
+
visited = false;
|
|
614
|
+
if (!isDisabled()) state.closeSoon();
|
|
615
|
+
}),
|
|
616
|
+
onClick: () => {
|
|
617
|
+
if (isOpen() && !viaHover) {
|
|
618
|
+
state.close();
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
viaHover = false;
|
|
622
|
+
state.open(key);
|
|
623
|
+
},
|
|
624
|
+
onKeyDown: (event: KeyboardEvent) => {
|
|
625
|
+
if (isModified(event) || event.shiftKey) return;
|
|
626
|
+
const into =
|
|
627
|
+
orientation() === "horizontal"
|
|
628
|
+
? "ArrowDown"
|
|
629
|
+
: direction() === "rtl"
|
|
630
|
+
? "ArrowLeft"
|
|
631
|
+
: "ArrowRight";
|
|
632
|
+
if (event.key !== into) return;
|
|
633
|
+
event.preventDefault();
|
|
634
|
+
enter();
|
|
635
|
+
},
|
|
636
|
+
};
|
|
637
|
+
},
|
|
638
|
+
contentProps: {
|
|
639
|
+
id: contentId,
|
|
640
|
+
[CONTENT]: "",
|
|
641
|
+
"aria-labelledby": triggerId,
|
|
642
|
+
"data-state": () => (isOpen() ? "open" : "closed"),
|
|
643
|
+
"data-motion": () => state.motion(key, order()) ?? undefined,
|
|
644
|
+
"data-orientation": orientation,
|
|
645
|
+
onPointerEnter: () => state.keep(),
|
|
646
|
+
onPointerLeave: mouseOnly(() => state.closeSoon()),
|
|
647
|
+
onKeyDown: onContentKeyDown,
|
|
648
|
+
},
|
|
649
|
+
};
|
|
650
|
+
};
|
|
651
|
+
|
|
652
|
+
const linkProps = (linkOptions: NavigationMenuLinkOptions = {}): DOMProps => {
|
|
653
|
+
const isActive = (): boolean => access(linkOptions.isActive) === true;
|
|
654
|
+
return {
|
|
655
|
+
[ITEM]: "",
|
|
656
|
+
"data-active": () => (isActive() ? "true" : undefined),
|
|
657
|
+
"aria-current": () => (isActive() ? "page" : undefined),
|
|
658
|
+
onClick: (event: MouseEvent) => {
|
|
659
|
+
const link = event.currentTarget as HTMLElement;
|
|
660
|
+
const select = new CustomEvent("navigationMenu.linkSelect", {
|
|
661
|
+
bubbles: true,
|
|
662
|
+
cancelable: true,
|
|
663
|
+
});
|
|
664
|
+
if (linkOptions.onSelect !== undefined) {
|
|
665
|
+
link.addEventListener(select.type, linkOptions.onSelect, { once: true });
|
|
666
|
+
}
|
|
667
|
+
link.dispatchEvent(select);
|
|
668
|
+
// A modified click opens the link somewhere else and leaves this page,
|
|
669
|
+
// and its menu, as they were.
|
|
670
|
+
if (select.defaultPrevented || event.metaKey || event.ctrlKey) return;
|
|
671
|
+
const content = openRecord()?.content() ?? null;
|
|
672
|
+
if (content !== null && contains(content, link)) dismiss();
|
|
673
|
+
},
|
|
674
|
+
};
|
|
675
|
+
};
|
|
676
|
+
|
|
677
|
+
return {
|
|
678
|
+
order,
|
|
679
|
+
orientation,
|
|
680
|
+
viewport,
|
|
681
|
+
viewportRef: viewport.set,
|
|
682
|
+
item,
|
|
683
|
+
linkProps,
|
|
684
|
+
rootProps: {
|
|
685
|
+
"aria-label": () =>
|
|
686
|
+
access(options["aria-label"]) ??
|
|
687
|
+
(access(options["aria-labelledby"]) === undefined ? "Main" : undefined),
|
|
688
|
+
"aria-labelledby": () => access(options["aria-labelledby"]),
|
|
689
|
+
"data-orientation": orientation,
|
|
690
|
+
onKeyDown: onRootKeyDown,
|
|
691
|
+
},
|
|
692
|
+
listProps: {
|
|
693
|
+
"data-orientation": orientation,
|
|
694
|
+
onKeyDown: onListKeyDown,
|
|
695
|
+
},
|
|
696
|
+
viewportProps: {
|
|
697
|
+
hidden: () => !viewportGate.isPresent() || undefined,
|
|
698
|
+
"data-state": () => (state.value() === null ? "closed" : "open"),
|
|
699
|
+
"data-closed": () => (viewportGate.isExiting() ? "" : undefined),
|
|
700
|
+
"data-orientation": orientation,
|
|
701
|
+
onPointerEnter: () => state.keep(),
|
|
702
|
+
onPointerLeave: mouseOnly(() => state.closeSoon()),
|
|
703
|
+
// The measured box arrives as custom properties, so how the viewport
|
|
704
|
+
// grows into it is the stylesheet's decision. Radix's names are written
|
|
705
|
+
// beside these, for a class list copied from a Radix design system.
|
|
706
|
+
style: () => {
|
|
707
|
+
const box = viewportSize();
|
|
708
|
+
if (box === null) return {};
|
|
709
|
+
const width = `${String(box.width)}px`;
|
|
710
|
+
const height = `${String(box.height)}px`;
|
|
711
|
+
return {
|
|
712
|
+
"--barq-viewport-width": width,
|
|
713
|
+
"--barq-viewport-height": height,
|
|
714
|
+
"--radix-navigation-menu-viewport-width": width,
|
|
715
|
+
"--radix-navigation-menu-viewport-height": height,
|
|
716
|
+
height: "var(--barq-viewport-height)",
|
|
717
|
+
};
|
|
718
|
+
},
|
|
719
|
+
},
|
|
720
|
+
indicatorProps: {
|
|
721
|
+
"aria-hidden": "true",
|
|
722
|
+
"data-state": () => (state.value() === null ? "hidden" : "visible"),
|
|
723
|
+
"data-orientation": orientation,
|
|
724
|
+
style: () => {
|
|
725
|
+
const at = indicator();
|
|
726
|
+
if (at === null) return {};
|
|
727
|
+
const size = `${String(at.size)}px`;
|
|
728
|
+
const offset = `${String(at.offset)}px`;
|
|
729
|
+
return orientation() === "horizontal"
|
|
730
|
+
? { position: "absolute", left: "0px", width: size, transform: `translateX(${offset})` }
|
|
731
|
+
: { position: "absolute", top: "0px", height: size, transform: `translateY(${offset})` };
|
|
732
|
+
},
|
|
733
|
+
},
|
|
734
|
+
};
|
|
735
|
+
}
|