@baukit/navigation 0.7.1 → 0.7.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/README.md +24 -6
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -2
- package/dist/index.js.map +1 -1
- package/dist/native.d.ts +19 -5
- package/dist/native.d.ts.map +1 -1
- package/dist/native.js +194 -89
- package/dist/native.js.map +1 -1
- package/dist/web.css +28 -0
- package/dist/web.css.d.ts +1 -0
- package/dist/web.d.ts +3 -3
- package/dist/web.d.ts.map +1 -1
- package/dist/web.js +47 -42
- package/dist/web.js.map +1 -1
- package/package.json +10 -5
- package/src/index.test.ts +226 -0
- package/src/index.ts +189 -0
- package/src/native.browser.test.tsx +183 -0
- package/src/native.test.tsx +833 -0
- package/src/native.tsx +970 -0
- package/src/use-navigation-state.ts +34 -0
- package/src/web-css.d.ts +4 -0
- package/src/web.browser.test.tsx +460 -0
- package/src/web.css +257 -0
- package/src/web.css.d.ts +1 -0
- package/src/web.test.tsx +483 -0
- package/src/web.tsx +618 -0
package/src/web.tsx
ADDED
|
@@ -0,0 +1,618 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useEffect,
|
|
3
|
+
useId,
|
|
4
|
+
useLayoutEffect,
|
|
5
|
+
useRef,
|
|
6
|
+
useState,
|
|
7
|
+
useSyncExternalStore,
|
|
8
|
+
type ComponentPropsWithRef,
|
|
9
|
+
type MouseEvent,
|
|
10
|
+
type ReactNode,
|
|
11
|
+
type RefObject,
|
|
12
|
+
} from 'react';
|
|
13
|
+
import {
|
|
14
|
+
announce,
|
|
15
|
+
useAriaHiddenInert,
|
|
16
|
+
useReducedMotionPreference,
|
|
17
|
+
useRovingMenu,
|
|
18
|
+
} from '@baukit/a11y-core/web';
|
|
19
|
+
import {
|
|
20
|
+
getNavigationLayout,
|
|
21
|
+
getNavigationProfileLabel,
|
|
22
|
+
NAVIGATION_DIMENSIONS,
|
|
23
|
+
navigationMatches,
|
|
24
|
+
nextSectionHref,
|
|
25
|
+
resolveActiveNavigation,
|
|
26
|
+
resolveActiveMenuEntry,
|
|
27
|
+
validateNavigation,
|
|
28
|
+
type NavigationItem,
|
|
29
|
+
type NavigationProfile,
|
|
30
|
+
type NavigationProfileMenuEntry,
|
|
31
|
+
} from './index.js';
|
|
32
|
+
import { useNavigationState, type CollapseProps } from './use-navigation-state.js';
|
|
33
|
+
|
|
34
|
+
export interface NavigationIconState {
|
|
35
|
+
readonly active: boolean;
|
|
36
|
+
readonly size: number;
|
|
37
|
+
}
|
|
38
|
+
export type NavigationIcon = (state: NavigationIconState) => ReactNode;
|
|
39
|
+
export type NavigationLinkProps = ComponentPropsWithRef<'a'> & { readonly href: string };
|
|
40
|
+
export type NavigationLinkRenderer = (props: NavigationLinkProps) => ReactNode;
|
|
41
|
+
export type Navigate = (href: string, event: MouseEvent<HTMLAnchorElement>) => void;
|
|
42
|
+
|
|
43
|
+
export function isNavigationClick(event: MouseEvent<HTMLAnchorElement>): boolean {
|
|
44
|
+
return (
|
|
45
|
+
!event.defaultPrevented &&
|
|
46
|
+
event.button === 0 &&
|
|
47
|
+
!event.metaKey &&
|
|
48
|
+
!event.ctrlKey &&
|
|
49
|
+
!event.shiftKey &&
|
|
50
|
+
!event.altKey
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function follow(href: string, event: MouseEvent<HTMLAnchorElement>, onNavigate?: Navigate) {
|
|
55
|
+
if (!isNavigationClick(event) || onNavigate === undefined) return;
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
onNavigate(href, event);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function subscribeWidth(change: () => void) {
|
|
61
|
+
window.addEventListener('resize', change);
|
|
62
|
+
return () => {
|
|
63
|
+
window.removeEventListener('resize', change);
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function viewportWidth() {
|
|
67
|
+
return window.innerWidth;
|
|
68
|
+
}
|
|
69
|
+
function serverWidth() {
|
|
70
|
+
return 0;
|
|
71
|
+
}
|
|
72
|
+
function Link({
|
|
73
|
+
renderLink,
|
|
74
|
+
...props
|
|
75
|
+
}: NavigationLinkProps & { readonly renderLink: NavigationLinkRenderer | undefined }) {
|
|
76
|
+
return renderLink === undefined ? <a {...props} /> : renderLink(props);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
interface MenuProps {
|
|
80
|
+
readonly entries: readonly NavigationProfileMenuEntry[];
|
|
81
|
+
readonly id: string;
|
|
82
|
+
readonly label: string;
|
|
83
|
+
readonly triggerRef: RefObject<HTMLButtonElement | null>;
|
|
84
|
+
readonly onClose: () => void;
|
|
85
|
+
readonly onNavigate: Navigate | undefined;
|
|
86
|
+
readonly renderLink: NavigationLinkRenderer | undefined;
|
|
87
|
+
readonly pathname: string;
|
|
88
|
+
readonly maxHeight?: number | undefined;
|
|
89
|
+
readonly subtitle?: string | undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function Menu({
|
|
93
|
+
entries,
|
|
94
|
+
id,
|
|
95
|
+
label,
|
|
96
|
+
triggerRef,
|
|
97
|
+
onClose,
|
|
98
|
+
onNavigate,
|
|
99
|
+
renderLink,
|
|
100
|
+
pathname,
|
|
101
|
+
maxHeight,
|
|
102
|
+
subtitle,
|
|
103
|
+
}: MenuProps) {
|
|
104
|
+
const activeEntry = resolveActiveMenuEntry(entries, pathname);
|
|
105
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
106
|
+
const roving = useRovingMenu({
|
|
107
|
+
active: true,
|
|
108
|
+
options: entries.map((entry) => ({ disabled: entry.disabled })),
|
|
109
|
+
});
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
const menu = containerRef.current;
|
|
112
|
+
const first = menu?.querySelector<HTMLElement>('[role="menuitem"]:not([aria-disabled="true"])');
|
|
113
|
+
(first ?? menu)?.focus();
|
|
114
|
+
}, []);
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
function outside(event: PointerEvent) {
|
|
117
|
+
if (
|
|
118
|
+
!(event.target instanceof Node) ||
|
|
119
|
+
containerRef.current?.contains(event.target) ||
|
|
120
|
+
triggerRef.current?.contains(event.target)
|
|
121
|
+
)
|
|
122
|
+
return;
|
|
123
|
+
onClose();
|
|
124
|
+
}
|
|
125
|
+
document.addEventListener('pointerdown', outside);
|
|
126
|
+
return () => {
|
|
127
|
+
document.removeEventListener('pointerdown', outside);
|
|
128
|
+
};
|
|
129
|
+
}, [onClose, triggerRef]);
|
|
130
|
+
function closeAndRestore() {
|
|
131
|
+
onClose();
|
|
132
|
+
triggerRef.current?.focus();
|
|
133
|
+
}
|
|
134
|
+
return (
|
|
135
|
+
<div
|
|
136
|
+
className="bk-navigation-menu"
|
|
137
|
+
style={maxHeight === undefined ? undefined : { maxHeight }}
|
|
138
|
+
id={id}
|
|
139
|
+
role="menu"
|
|
140
|
+
aria-label={subtitle === undefined ? label : `${label}, ${subtitle}`}
|
|
141
|
+
tabIndex={-1}
|
|
142
|
+
ref={containerRef}
|
|
143
|
+
onKeyDown={(event) => {
|
|
144
|
+
if (event.key === 'Escape') {
|
|
145
|
+
event.preventDefault();
|
|
146
|
+
event.stopPropagation();
|
|
147
|
+
closeAndRestore();
|
|
148
|
+
}
|
|
149
|
+
if (event.key === 'Tab') {
|
|
150
|
+
// Move focus to the trigger before removing the focused menu item. The browser continues Tab normally.
|
|
151
|
+
triggerRef.current?.focus();
|
|
152
|
+
onClose();
|
|
153
|
+
}
|
|
154
|
+
}}
|
|
155
|
+
>
|
|
156
|
+
{subtitle === undefined ? null : (
|
|
157
|
+
<div className="bk-navigation-menu-profile" aria-hidden="true">
|
|
158
|
+
<span className="bk-navigation-label">{label}</span>
|
|
159
|
+
<span className="bk-navigation-subtitle">{subtitle}</span>
|
|
160
|
+
</div>
|
|
161
|
+
)}
|
|
162
|
+
{entries.map((entry, index) => {
|
|
163
|
+
const props = {
|
|
164
|
+
...roving.itemProps(index),
|
|
165
|
+
className: 'bk-navigation-item',
|
|
166
|
+
role: 'menuitem',
|
|
167
|
+
'aria-disabled': entry.disabled === true ? true : undefined,
|
|
168
|
+
'data-active': entry === activeEntry ? 'page' : undefined,
|
|
169
|
+
} as const;
|
|
170
|
+
return entry.href !== undefined ? (
|
|
171
|
+
<Link
|
|
172
|
+
{...props}
|
|
173
|
+
renderLink={renderLink}
|
|
174
|
+
href={entry.href}
|
|
175
|
+
key={entry.id}
|
|
176
|
+
aria-current={entry === activeEntry ? 'page' : undefined}
|
|
177
|
+
onClick={(event) => {
|
|
178
|
+
if (entry.disabled === true) {
|
|
179
|
+
event.preventDefault();
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (!isNavigationClick(event)) return;
|
|
183
|
+
closeAndRestore();
|
|
184
|
+
follow(entry.href, event, onNavigate);
|
|
185
|
+
}}
|
|
186
|
+
onKeyDown={(event) => {
|
|
187
|
+
props.onKeyDown(event);
|
|
188
|
+
if (event.key === ' ' && entry.disabled !== true) {
|
|
189
|
+
event.preventDefault();
|
|
190
|
+
event.currentTarget.click();
|
|
191
|
+
}
|
|
192
|
+
}}
|
|
193
|
+
>
|
|
194
|
+
{entry.label}
|
|
195
|
+
</Link>
|
|
196
|
+
) : (
|
|
197
|
+
<button
|
|
198
|
+
{...props}
|
|
199
|
+
type="button"
|
|
200
|
+
disabled={entry.disabled}
|
|
201
|
+
key={entry.id}
|
|
202
|
+
onClick={() => {
|
|
203
|
+
closeAndRestore();
|
|
204
|
+
entry.onSelect();
|
|
205
|
+
}}
|
|
206
|
+
>
|
|
207
|
+
{entry.label}
|
|
208
|
+
</button>
|
|
209
|
+
);
|
|
210
|
+
})}
|
|
211
|
+
</div>
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function Avatar({
|
|
216
|
+
profile,
|
|
217
|
+
active,
|
|
218
|
+
}: {
|
|
219
|
+
readonly profile: NavigationProfile;
|
|
220
|
+
readonly active: boolean;
|
|
221
|
+
}) {
|
|
222
|
+
const [failedUrl, setFailedUrl] = useState<string>();
|
|
223
|
+
if (profile.renderAvatar !== undefined) {
|
|
224
|
+
return (
|
|
225
|
+
<span className="bk-navigation-avatar bk-navigation-avatar-custom" aria-hidden="true">
|
|
226
|
+
{profile.renderAvatar({ active, size: NAVIGATION_DIMENSIONS.avatar })}
|
|
227
|
+
</span>
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
return (
|
|
231
|
+
<span className="bk-navigation-avatar" aria-hidden="true">
|
|
232
|
+
{profile.imageUrl !== undefined && failedUrl !== profile.imageUrl ? (
|
|
233
|
+
<img
|
|
234
|
+
src={profile.imageUrl}
|
|
235
|
+
alt=""
|
|
236
|
+
onError={() => {
|
|
237
|
+
setFailedUrl(profile.imageUrl);
|
|
238
|
+
}}
|
|
239
|
+
/>
|
|
240
|
+
) : (
|
|
241
|
+
profile.initials
|
|
242
|
+
)}
|
|
243
|
+
</span>
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
interface ProfileProps {
|
|
248
|
+
readonly profile: NavigationProfile;
|
|
249
|
+
readonly pathname: string;
|
|
250
|
+
readonly collapsed: boolean;
|
|
251
|
+
readonly itemProps: ReturnType<ReturnType<typeof useRovingMenu>['itemProps']>;
|
|
252
|
+
readonly onNavigate: Navigate | undefined;
|
|
253
|
+
readonly renderLink: NavigationLinkRenderer | undefined;
|
|
254
|
+
}
|
|
255
|
+
function Profile({
|
|
256
|
+
profile,
|
|
257
|
+
pathname,
|
|
258
|
+
collapsed,
|
|
259
|
+
itemProps,
|
|
260
|
+
onNavigate,
|
|
261
|
+
renderLink,
|
|
262
|
+
}: ProfileProps) {
|
|
263
|
+
const [open, setOpen] = useState(false);
|
|
264
|
+
const triggerRef = useRef<HTMLButtonElement>(null);
|
|
265
|
+
const id = useId();
|
|
266
|
+
const active =
|
|
267
|
+
profile.href !== undefined
|
|
268
|
+
? navigationMatches({ id: 'profile', label: profile.label, href: profile.href }, pathname)
|
|
269
|
+
: resolveActiveMenuEntry(profile.menu, pathname) !== null;
|
|
270
|
+
const content = (
|
|
271
|
+
<>
|
|
272
|
+
<Avatar profile={profile} active={active} />
|
|
273
|
+
<span className="bk-navigation-profile-copy">
|
|
274
|
+
<span className="bk-navigation-label">{profile.label}</span>
|
|
275
|
+
{profile.subtitle === undefined ? null : (
|
|
276
|
+
<span className="bk-navigation-subtitle">{profile.subtitle}</span>
|
|
277
|
+
)}
|
|
278
|
+
</span>
|
|
279
|
+
</>
|
|
280
|
+
);
|
|
281
|
+
const common = {
|
|
282
|
+
...itemProps,
|
|
283
|
+
className: 'bk-navigation-item',
|
|
284
|
+
'aria-label': getNavigationProfileLabel(profile),
|
|
285
|
+
'data-active': active ? 'page' : undefined,
|
|
286
|
+
title: collapsed ? getNavigationProfileLabel(profile) : undefined,
|
|
287
|
+
};
|
|
288
|
+
return (
|
|
289
|
+
<div className="bk-navigation-profile">
|
|
290
|
+
{profile.href !== undefined ? (
|
|
291
|
+
<Link
|
|
292
|
+
{...common}
|
|
293
|
+
renderLink={renderLink}
|
|
294
|
+
href={profile.href}
|
|
295
|
+
aria-current={active ? 'page' : undefined}
|
|
296
|
+
onClick={(event) => {
|
|
297
|
+
follow(profile.href, event, onNavigate);
|
|
298
|
+
}}
|
|
299
|
+
>
|
|
300
|
+
{content}
|
|
301
|
+
</Link>
|
|
302
|
+
) : (
|
|
303
|
+
<button
|
|
304
|
+
{...common}
|
|
305
|
+
type="button"
|
|
306
|
+
ref={(node) => {
|
|
307
|
+
triggerRef.current = node;
|
|
308
|
+
itemProps.ref(node);
|
|
309
|
+
}}
|
|
310
|
+
aria-haspopup="menu"
|
|
311
|
+
aria-expanded={open}
|
|
312
|
+
aria-controls={open ? id : undefined}
|
|
313
|
+
onClick={() => {
|
|
314
|
+
setOpen((value) => !value);
|
|
315
|
+
}}
|
|
316
|
+
onKeyDown={(event) => {
|
|
317
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
318
|
+
event.preventDefault();
|
|
319
|
+
setOpen(true);
|
|
320
|
+
} else itemProps.onKeyDown(event);
|
|
321
|
+
}}
|
|
322
|
+
>
|
|
323
|
+
{content}
|
|
324
|
+
</button>
|
|
325
|
+
)}
|
|
326
|
+
{open && profile.menu !== undefined ? (
|
|
327
|
+
<Menu
|
|
328
|
+
entries={profile.menu}
|
|
329
|
+
id={id}
|
|
330
|
+
label={profile.label}
|
|
331
|
+
subtitle={profile.subtitle}
|
|
332
|
+
triggerRef={triggerRef}
|
|
333
|
+
onClose={() => {
|
|
334
|
+
setOpen(false);
|
|
335
|
+
}}
|
|
336
|
+
pathname={pathname}
|
|
337
|
+
onNavigate={onNavigate}
|
|
338
|
+
renderLink={renderLink}
|
|
339
|
+
/>
|
|
340
|
+
) : null}
|
|
341
|
+
</div>
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export interface AppNavigationProps extends CollapseProps {
|
|
346
|
+
readonly items: readonly NavigationItem<NavigationIcon>[];
|
|
347
|
+
readonly profile?: NavigationProfile;
|
|
348
|
+
readonly pathname: string;
|
|
349
|
+
readonly onNavigate?: Navigate;
|
|
350
|
+
readonly renderLink?: NavigationLinkRenderer;
|
|
351
|
+
readonly label: string;
|
|
352
|
+
readonly collapseLabel: string;
|
|
353
|
+
readonly expandLabel: string;
|
|
354
|
+
readonly width?: number;
|
|
355
|
+
readonly className?: string;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export function AppNavigation(props: AppNavigationProps) {
|
|
359
|
+
const { items, pathname, profile, onNavigate, renderLink, label, collapseLabel, expandLabel } =
|
|
360
|
+
props;
|
|
361
|
+
validateNavigation(items, profile);
|
|
362
|
+
useAriaHiddenInert();
|
|
363
|
+
const measuredWidth = useSyncExternalStore(subscribeWidth, viewportWidth, serverWidth);
|
|
364
|
+
const layout = getNavigationLayout(props.width ?? measuredWidth);
|
|
365
|
+
const state = useNavigationState(items, pathname, props);
|
|
366
|
+
const collapsed = layout === 'rail' && state.collapsed;
|
|
367
|
+
const { reducedMotion, resolved } = useReducedMotionPreference();
|
|
368
|
+
const id = useId();
|
|
369
|
+
const visible = items.flatMap((item) =>
|
|
370
|
+
layout === 'rail' && !collapsed && state.openIds.includes(item.id)
|
|
371
|
+
? [item, ...(item.children ?? [])]
|
|
372
|
+
: [item],
|
|
373
|
+
);
|
|
374
|
+
const activeId = visible.some((item) => item.id === state.active.subItem?.id)
|
|
375
|
+
? state.active.subItem?.id
|
|
376
|
+
: state.active.item?.id;
|
|
377
|
+
const roving = useRovingMenu({
|
|
378
|
+
active: true,
|
|
379
|
+
options: [
|
|
380
|
+
...visible.map((item) => ({
|
|
381
|
+
selected: item.id === activeId,
|
|
382
|
+
})),
|
|
383
|
+
...(profile === undefined ? [] : [{}]),
|
|
384
|
+
],
|
|
385
|
+
});
|
|
386
|
+
return (
|
|
387
|
+
<nav
|
|
388
|
+
aria-label={label}
|
|
389
|
+
className={`bk-navigation ${props.className ?? ''}`}
|
|
390
|
+
data-testid="primary-navigation"
|
|
391
|
+
data-layout={layout}
|
|
392
|
+
data-collapsed={collapsed}
|
|
393
|
+
data-motion={!resolved ? 'unresolved' : reducedMotion ? 'reduced' : 'standard'}
|
|
394
|
+
>
|
|
395
|
+
{layout === 'rail' ? (
|
|
396
|
+
<button
|
|
397
|
+
className="bk-navigation-toggle"
|
|
398
|
+
type="button"
|
|
399
|
+
aria-label={collapsed ? expandLabel : collapseLabel}
|
|
400
|
+
aria-expanded={!collapsed}
|
|
401
|
+
onClick={() => {
|
|
402
|
+
state.setCollapsed(!collapsed);
|
|
403
|
+
}}
|
|
404
|
+
>
|
|
405
|
+
<span aria-hidden="true">{collapsed ? '›' : '‹'}</span>
|
|
406
|
+
</button>
|
|
407
|
+
) : null}
|
|
408
|
+
<div className="bk-navigation-items">
|
|
409
|
+
{items.map((item, index) => {
|
|
410
|
+
const active = state.active.item?.id === item.id;
|
|
411
|
+
const ancestor =
|
|
412
|
+
active && layout === 'rail' && !collapsed && state.active.subItem !== null;
|
|
413
|
+
const children = item.children ?? [];
|
|
414
|
+
const open = !collapsed && state.openIds.includes(item.id);
|
|
415
|
+
const groupId = `${id}-${String(index)}`;
|
|
416
|
+
const itemProps = roving.itemProps(visible.indexOf(item));
|
|
417
|
+
const content = (
|
|
418
|
+
<>
|
|
419
|
+
<span className="bk-navigation-icon" aria-hidden="true">
|
|
420
|
+
{item.icon({ active, size: 24 })}
|
|
421
|
+
</span>
|
|
422
|
+
<span className="bk-navigation-label">{item.label}</span>
|
|
423
|
+
</>
|
|
424
|
+
);
|
|
425
|
+
const common = {
|
|
426
|
+
...itemProps,
|
|
427
|
+
className: 'bk-navigation-item',
|
|
428
|
+
'aria-label': item.label,
|
|
429
|
+
'data-active': active ? (ancestor ? 'ancestor' : 'page') : undefined,
|
|
430
|
+
title: collapsed ? item.label : undefined,
|
|
431
|
+
};
|
|
432
|
+
const href = layout === 'bar' && active ? nextSectionHref(item, pathname) : item.href;
|
|
433
|
+
return (
|
|
434
|
+
<div className="bk-navigation-section" key={item.id} data-open={open}>
|
|
435
|
+
{layout === 'rail' && children.length > 0 ? (
|
|
436
|
+
<button
|
|
437
|
+
{...common}
|
|
438
|
+
type="button"
|
|
439
|
+
aria-expanded={open}
|
|
440
|
+
aria-controls={groupId}
|
|
441
|
+
onClick={() => {
|
|
442
|
+
state.toggleGroup(item.id);
|
|
443
|
+
}}
|
|
444
|
+
>
|
|
445
|
+
{content}
|
|
446
|
+
</button>
|
|
447
|
+
) : (
|
|
448
|
+
<Link
|
|
449
|
+
{...common}
|
|
450
|
+
renderLink={renderLink}
|
|
451
|
+
href={href}
|
|
452
|
+
aria-current={active ? 'page' : undefined}
|
|
453
|
+
onClick={(event) => {
|
|
454
|
+
follow(href, event, onNavigate);
|
|
455
|
+
}}
|
|
456
|
+
>
|
|
457
|
+
{content}
|
|
458
|
+
</Link>
|
|
459
|
+
)}
|
|
460
|
+
{layout === 'rail' && children.length > 0 ? (
|
|
461
|
+
<div
|
|
462
|
+
className="bk-navigation-children"
|
|
463
|
+
id={groupId}
|
|
464
|
+
role="group"
|
|
465
|
+
aria-label={item.label}
|
|
466
|
+
hidden={!open}
|
|
467
|
+
>
|
|
468
|
+
{children.map((child) => {
|
|
469
|
+
const selected = state.active.subItem?.id === child.id;
|
|
470
|
+
return (
|
|
471
|
+
<Link
|
|
472
|
+
{...roving.itemProps(visible.indexOf(child))}
|
|
473
|
+
renderLink={renderLink}
|
|
474
|
+
key={child.id}
|
|
475
|
+
className="bk-navigation-item"
|
|
476
|
+
href={child.href}
|
|
477
|
+
aria-current={selected ? 'page' : undefined}
|
|
478
|
+
data-active={selected ? 'page' : undefined}
|
|
479
|
+
onClick={(event) => {
|
|
480
|
+
follow(child.href, event, onNavigate);
|
|
481
|
+
}}
|
|
482
|
+
>
|
|
483
|
+
{child.icon === undefined ? null : (
|
|
484
|
+
<span className="bk-navigation-icon" aria-hidden="true">
|
|
485
|
+
{child.icon({ active: selected, size: 20 })}
|
|
486
|
+
</span>
|
|
487
|
+
)}
|
|
488
|
+
{child.label}
|
|
489
|
+
</Link>
|
|
490
|
+
);
|
|
491
|
+
})}
|
|
492
|
+
</div>
|
|
493
|
+
) : null}
|
|
494
|
+
</div>
|
|
495
|
+
);
|
|
496
|
+
})}
|
|
497
|
+
</div>
|
|
498
|
+
{profile === undefined ? null : (
|
|
499
|
+
<Profile
|
|
500
|
+
profile={profile}
|
|
501
|
+
pathname={pathname}
|
|
502
|
+
collapsed={collapsed}
|
|
503
|
+
itemProps={roving.itemProps(visible.length)}
|
|
504
|
+
onNavigate={onNavigate}
|
|
505
|
+
renderLink={renderLink}
|
|
506
|
+
/>
|
|
507
|
+
)}
|
|
508
|
+
</nav>
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
const SECTION_MENU_GAP = 8;
|
|
513
|
+
|
|
514
|
+
function useSectionMenuHeight(open: boolean, triggerRef: RefObject<HTMLButtonElement | null>) {
|
|
515
|
+
const [height, setHeight] = useState<number>();
|
|
516
|
+
const measuredHeight = useRef<number | undefined>(undefined);
|
|
517
|
+
useLayoutEffect(() => {
|
|
518
|
+
const trigger = triggerRef.current;
|
|
519
|
+
if (!open || trigger === null) return;
|
|
520
|
+
function measure() {
|
|
521
|
+
const bar = document.querySelector<HTMLElement>(".bk-navigation[data-layout='bar']");
|
|
522
|
+
if (trigger === null) return;
|
|
523
|
+
const viewportBottom =
|
|
524
|
+
(window.visualViewport?.height ?? window.innerHeight) +
|
|
525
|
+
(window.visualViewport?.offsetTop ?? 0);
|
|
526
|
+
const bottom = Math.min(viewportBottom, bar?.getBoundingClientRect().top ?? viewportBottom);
|
|
527
|
+
const nextHeight = Math.max(
|
|
528
|
+
0,
|
|
529
|
+
bottom - trigger.getBoundingClientRect().bottom - SECTION_MENU_GAP * 2,
|
|
530
|
+
);
|
|
531
|
+
if (measuredHeight.current === nextHeight) return;
|
|
532
|
+
measuredHeight.current = nextHeight;
|
|
533
|
+
setHeight(nextHeight);
|
|
534
|
+
}
|
|
535
|
+
measure();
|
|
536
|
+
const observer = 'ResizeObserver' in window ? new ResizeObserver(measure) : undefined;
|
|
537
|
+
for (
|
|
538
|
+
let element: HTMLElement | null = trigger;
|
|
539
|
+
element !== null;
|
|
540
|
+
element = element.parentElement
|
|
541
|
+
) {
|
|
542
|
+
observer?.observe(element);
|
|
543
|
+
}
|
|
544
|
+
const navigation = document.querySelector('.bk-navigation');
|
|
545
|
+
if (navigation !== null) observer?.observe(navigation);
|
|
546
|
+
window.addEventListener('resize', measure);
|
|
547
|
+
window.addEventListener('scroll', measure, true);
|
|
548
|
+
window.visualViewport?.addEventListener('resize', measure);
|
|
549
|
+
return () => {
|
|
550
|
+
observer?.disconnect();
|
|
551
|
+
window.removeEventListener('resize', measure);
|
|
552
|
+
window.removeEventListener('scroll', measure, true);
|
|
553
|
+
window.visualViewport?.removeEventListener('resize', measure);
|
|
554
|
+
};
|
|
555
|
+
}, [open, triggerRef]);
|
|
556
|
+
return height;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
export interface SectionPickerProps {
|
|
560
|
+
readonly item: NavigationItem<NavigationIcon>;
|
|
561
|
+
readonly pathname: string;
|
|
562
|
+
readonly onNavigate?: Navigate;
|
|
563
|
+
readonly renderLink?: NavigationLinkRenderer;
|
|
564
|
+
}
|
|
565
|
+
export function SectionPicker({ item, pathname, onNavigate, renderLink }: SectionPickerProps) {
|
|
566
|
+
const [open, setOpen] = useState(false);
|
|
567
|
+
const triggerRef = useRef<HTMLButtonElement>(null);
|
|
568
|
+
const maxHeight = useSectionMenuHeight(open, triggerRef);
|
|
569
|
+
const id = useId();
|
|
570
|
+
const active = resolveActiveNavigation([item], pathname).subItem ?? item.children?.[0];
|
|
571
|
+
const entries = item.children ?? [];
|
|
572
|
+
if (entries.length === 0) return null;
|
|
573
|
+
const label = `${item.label}, ${active?.label ?? ''}`;
|
|
574
|
+
return (
|
|
575
|
+
<nav aria-label={item.label} className="bk-navigation-picker">
|
|
576
|
+
<button
|
|
577
|
+
className="bk-navigation-picker-trigger"
|
|
578
|
+
type="button"
|
|
579
|
+
ref={triggerRef}
|
|
580
|
+
aria-label={label}
|
|
581
|
+
aria-expanded={open}
|
|
582
|
+
aria-haspopup="menu"
|
|
583
|
+
aria-controls={open ? id : undefined}
|
|
584
|
+
onClick={() => {
|
|
585
|
+
setOpen((value) => !value);
|
|
586
|
+
}}
|
|
587
|
+
>
|
|
588
|
+
<span>
|
|
589
|
+
<span className="bk-navigation-picker-section">{item.label}</span>
|
|
590
|
+
<span>{active?.label}</span>
|
|
591
|
+
</span>
|
|
592
|
+
<span aria-hidden="true">⌄</span>
|
|
593
|
+
</button>
|
|
594
|
+
{open ? (
|
|
595
|
+
<Menu
|
|
596
|
+
entries={entries}
|
|
597
|
+
maxHeight={maxHeight}
|
|
598
|
+
id={id}
|
|
599
|
+
label={item.label}
|
|
600
|
+
triggerRef={triggerRef}
|
|
601
|
+
onClose={() => {
|
|
602
|
+
setOpen(false);
|
|
603
|
+
}}
|
|
604
|
+
pathname={pathname}
|
|
605
|
+
onNavigate={
|
|
606
|
+
onNavigate === undefined
|
|
607
|
+
? undefined
|
|
608
|
+
: (href, event) => {
|
|
609
|
+
announce(entries.find((entry) => entry.href === href)?.label ?? item.label);
|
|
610
|
+
onNavigate(href, event);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
renderLink={renderLink}
|
|
614
|
+
/>
|
|
615
|
+
) : null}
|
|
616
|
+
</nav>
|
|
617
|
+
);
|
|
618
|
+
}
|