@baukit/navigation 0.18.3 → 0.19.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/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -14
- package/dist/index.js.map +1 -1
- package/dist/native-menu.d.ts +22 -0
- package/dist/native-menu.d.ts.map +1 -0
- package/dist/native-menu.js +226 -0
- package/dist/native-menu.js.map +1 -0
- package/dist/native-primitives.d.ts +51 -0
- package/dist/native-primitives.d.ts.map +1 -0
- package/dist/native-primitives.js +125 -0
- package/dist/native-primitives.js.map +1 -0
- package/dist/native-profile.d.ts +34 -0
- package/dist/native-profile.d.ts.map +1 -0
- package/dist/native-profile.js +67 -0
- package/dist/native-profile.js.map +1 -0
- package/dist/native-section-picker.d.ts +14 -0
- package/dist/native-section-picker.d.ts.map +1 -0
- package/dist/native-section-picker.js +46 -0
- package/dist/native-section-picker.js.map +1 -0
- package/dist/native-styles.d.ts +106 -0
- package/dist/native-styles.d.ts.map +1 -0
- package/dist/native-styles.js +50 -0
- package/dist/native-styles.js.map +1 -0
- package/dist/native-theme.d.ts +49 -0
- package/dist/native-theme.d.ts.map +1 -0
- package/dist/native-theme.js +56 -0
- package/dist/native-theme.js.map +1 -0
- package/dist/native.d.ts +9 -52
- package/dist/native.d.ts.map +1 -1
- package/dist/native.js +148 -536
- package/dist/native.js.map +1 -1
- package/dist/use-navigation-state.d.ts +9 -8
- package/dist/use-navigation-state.d.ts.map +1 -1
- package/dist/use-navigation-state.js +3 -2
- package/dist/use-navigation-state.js.map +1 -1
- package/dist/web-link.d.ts +20 -0
- package/dist/web-link.d.ts.map +1 -0
- package/dist/web-link.js +23 -0
- package/dist/web-link.js.map +1 -0
- package/dist/web-menu.d.ts +19 -0
- package/dist/web-menu.d.ts.map +1 -0
- package/dist/web-menu.js +96 -0
- package/dist/web-menu.js.map +1 -0
- package/dist/web-profile.d.ts +16 -0
- package/dist/web-profile.d.ts.map +1 -0
- package/dist/web-profile.js +58 -0
- package/dist/web-profile.js.map +1 -0
- package/dist/web-section-picker.d.ts +13 -0
- package/dist/web-section-picker.d.ts.map +1 -0
- package/dist/web-section-picker.js +71 -0
- package/dist/web-section-picker.js.map +1 -0
- package/dist/web.d.ts +7 -25
- package/dist/web.d.ts.map +1 -1
- package/dist/web.js +113 -257
- package/dist/web.js.map +1 -1
- package/package.json +7 -7
- package/src/index.ts +22 -16
- package/src/native-menu.tsx +436 -0
- package/src/native-primitives.tsx +308 -0
- package/src/native-profile.tsx +153 -0
- package/src/native-section-picker.tsx +102 -0
- package/src/native-styles.ts +50 -0
- package/src/native-theme.ts +108 -0
- package/src/native.tsx +355 -1086
- package/src/use-navigation-state.ts +20 -5
- package/src/web-link.tsx +51 -0
- package/src/web-menu.tsx +220 -0
- package/src/web-profile.tsx +201 -0
- package/src/web-section-picker.tsx +128 -0
- package/src/web.tsx +256 -598
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { useEffect, useReducer } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
navigationReducer,
|
|
4
|
+
resolveActiveNavigation,
|
|
5
|
+
type ActiveNavigation,
|
|
6
|
+
type NavigationItem,
|
|
7
|
+
} from './index.js';
|
|
3
8
|
|
|
4
9
|
export interface CollapseProps {
|
|
5
10
|
readonly collapsed?: boolean;
|
|
@@ -7,13 +12,23 @@ export interface CollapseProps {
|
|
|
7
12
|
readonly onCollapsedChange?: (collapsed: boolean) => void;
|
|
8
13
|
}
|
|
9
14
|
|
|
15
|
+
export interface NavigationStateHandle<Icon> {
|
|
16
|
+
readonly active: ActiveNavigation<Icon>;
|
|
17
|
+
readonly collapsed: boolean;
|
|
18
|
+
readonly openIds: readonly string[];
|
|
19
|
+
readonly setCollapsed: (value: boolean) => void;
|
|
20
|
+
readonly toggleGroup: (id: string) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
10
23
|
export function useNavigationState<Icon>(
|
|
11
24
|
items: readonly NavigationItem<Icon>[],
|
|
12
25
|
pathname: string,
|
|
13
26
|
props: CollapseProps,
|
|
14
|
-
) {
|
|
27
|
+
): NavigationStateHandle<Icon> {
|
|
15
28
|
const active = resolveActiveNavigation(items, pathname);
|
|
16
|
-
const
|
|
29
|
+
const activeItem = active.item;
|
|
30
|
+
const activeId =
|
|
31
|
+
activeItem !== null && (activeItem.children?.length ?? 0) > 0 ? activeItem.id : null;
|
|
17
32
|
const [state, dispatch] = useReducer(navigationReducer, {
|
|
18
33
|
collapsed: props.defaultCollapsed ?? false,
|
|
19
34
|
openIds: activeId === null ? [] : [activeId],
|
|
@@ -22,11 +37,11 @@ export function useNavigationState<Icon>(
|
|
|
22
37
|
dispatch({ type: 'enter-section', id: activeId });
|
|
23
38
|
}, [activeId]);
|
|
24
39
|
const collapsed = props.collapsed ?? state.collapsed;
|
|
25
|
-
function setCollapsed(value: boolean) {
|
|
40
|
+
function setCollapsed(value: boolean): void {
|
|
26
41
|
dispatch({ type: 'set-collapsed', collapsed: value });
|
|
27
42
|
props.onCollapsedChange?.(value);
|
|
28
43
|
}
|
|
29
|
-
function toggleGroup(id: string) {
|
|
44
|
+
function toggleGroup(id: string): void {
|
|
30
45
|
if (collapsed) setCollapsed(false);
|
|
31
46
|
dispatch({ type: 'toggle-group', id, collapsed });
|
|
32
47
|
}
|
package/src/web-link.tsx
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { ComponentPropsWithRef, MouseEvent, ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
export interface NavigationIconState {
|
|
4
|
+
readonly active: boolean;
|
|
5
|
+
readonly size: number;
|
|
6
|
+
}
|
|
7
|
+
export type NavigationIcon = (state: NavigationIconState) => ReactNode;
|
|
8
|
+
export type NavigationLinkProps = ComponentPropsWithRef<'a'> & {
|
|
9
|
+
readonly href: string;
|
|
10
|
+
readonly replace?: boolean;
|
|
11
|
+
};
|
|
12
|
+
export type NavigationLinkRenderer = (props: NavigationLinkProps) => ReactNode;
|
|
13
|
+
export type Navigate = (
|
|
14
|
+
href: string,
|
|
15
|
+
event: MouseEvent<HTMLAnchorElement>,
|
|
16
|
+
options?: { readonly replace: boolean },
|
|
17
|
+
) => void;
|
|
18
|
+
|
|
19
|
+
export function isNavigationClick(event: MouseEvent<HTMLAnchorElement>): boolean {
|
|
20
|
+
return (
|
|
21
|
+
!event.defaultPrevented &&
|
|
22
|
+
event.button === 0 &&
|
|
23
|
+
!event.metaKey &&
|
|
24
|
+
!event.ctrlKey &&
|
|
25
|
+
!event.shiftKey &&
|
|
26
|
+
!event.altKey
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function follow(
|
|
31
|
+
href: string,
|
|
32
|
+
event: MouseEvent<HTMLAnchorElement>,
|
|
33
|
+
onNavigate?: Navigate,
|
|
34
|
+
): void {
|
|
35
|
+
if (!isNavigationClick(event) || onNavigate === undefined) return;
|
|
36
|
+
event.preventDefault();
|
|
37
|
+
onNavigate(href, event);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function Link({
|
|
41
|
+
renderLink,
|
|
42
|
+
replace,
|
|
43
|
+
children,
|
|
44
|
+
...props
|
|
45
|
+
}: NavigationLinkProps & { readonly renderLink: NavigationLinkRenderer | undefined }): ReactNode {
|
|
46
|
+
if (renderLink !== undefined) {
|
|
47
|
+
const linkProps = { ...props, children };
|
|
48
|
+
return renderLink(replace === undefined ? linkProps : { ...linkProps, replace });
|
|
49
|
+
}
|
|
50
|
+
return <a {...props}>{children}</a>;
|
|
51
|
+
}
|
package/src/web-menu.tsx
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { useEffect, useRef, type KeyboardEvent, type ReactNode, type RefObject } from 'react';
|
|
2
|
+
import { flushSync } from 'react-dom';
|
|
3
|
+
import { useRovingMenu } from '@baukit/a11y-core/web';
|
|
4
|
+
import { resolveActiveMenuEntry, type NavigationProfileMenuEntry } from './index.js';
|
|
5
|
+
import {
|
|
6
|
+
follow,
|
|
7
|
+
isNavigationClick,
|
|
8
|
+
Link,
|
|
9
|
+
type Navigate,
|
|
10
|
+
type NavigationLinkRenderer,
|
|
11
|
+
} from './web-link.js';
|
|
12
|
+
|
|
13
|
+
export interface MenuProps {
|
|
14
|
+
readonly replace?: boolean;
|
|
15
|
+
readonly entries: readonly NavigationProfileMenuEntry[];
|
|
16
|
+
readonly id: string;
|
|
17
|
+
readonly label: string;
|
|
18
|
+
readonly closeLabel: string;
|
|
19
|
+
readonly triggerRef: RefObject<HTMLButtonElement | null>;
|
|
20
|
+
readonly onClose: () => void;
|
|
21
|
+
readonly onNavigate: Navigate | undefined;
|
|
22
|
+
readonly renderLink: NavigationLinkRenderer | undefined;
|
|
23
|
+
readonly pathname: string;
|
|
24
|
+
readonly maxHeight?: number | undefined;
|
|
25
|
+
readonly subtitle?: string | undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function useFocusFirstItem(menuRef: RefObject<HTMLDivElement | null>): void {
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
const menu = menuRef.current;
|
|
31
|
+
const first = menu?.querySelector<HTMLElement>('[role="menuitem"]:not([aria-disabled="true"])');
|
|
32
|
+
(first ?? menu)?.focus();
|
|
33
|
+
}, [menuRef]);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function useCloseOnOutsidePress(
|
|
37
|
+
containerRef: RefObject<HTMLDivElement | null>,
|
|
38
|
+
triggerRef: RefObject<HTMLButtonElement | null>,
|
|
39
|
+
onClose: () => void,
|
|
40
|
+
): void {
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
function outside(event: PointerEvent): void {
|
|
43
|
+
const target = event.target;
|
|
44
|
+
if (!(target instanceof Node)) return;
|
|
45
|
+
if (containerRef.current?.contains(target) === true) return;
|
|
46
|
+
if (triggerRef.current?.contains(target) === true) return;
|
|
47
|
+
onClose();
|
|
48
|
+
}
|
|
49
|
+
document.addEventListener('pointerdown', outside);
|
|
50
|
+
return () => {
|
|
51
|
+
document.removeEventListener('pointerdown', outside);
|
|
52
|
+
};
|
|
53
|
+
}, [containerRef, onClose, triggerRef]);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
type ItemProps = ReturnType<ReturnType<typeof useRovingMenu>['itemProps']>;
|
|
57
|
+
|
|
58
|
+
interface MenuEntryProps {
|
|
59
|
+
readonly entry: NavigationProfileMenuEntry;
|
|
60
|
+
readonly active: boolean;
|
|
61
|
+
readonly itemProps: ItemProps;
|
|
62
|
+
readonly replace: boolean | undefined;
|
|
63
|
+
readonly renderLink: NavigationLinkRenderer | undefined;
|
|
64
|
+
readonly onNavigate: Navigate | undefined;
|
|
65
|
+
readonly onCloseAndRestore: () => void;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function MenuEntry({
|
|
69
|
+
entry,
|
|
70
|
+
active,
|
|
71
|
+
itemProps,
|
|
72
|
+
replace,
|
|
73
|
+
renderLink,
|
|
74
|
+
onNavigate,
|
|
75
|
+
onCloseAndRestore,
|
|
76
|
+
}: MenuEntryProps): ReactNode {
|
|
77
|
+
const props = {
|
|
78
|
+
...itemProps,
|
|
79
|
+
className: 'bk-navigation-item',
|
|
80
|
+
role: 'menuitem',
|
|
81
|
+
'aria-disabled': entry.disabled === true ? true : undefined,
|
|
82
|
+
'data-active': active ? 'page' : undefined,
|
|
83
|
+
'data-tone': entry.tone,
|
|
84
|
+
} as const;
|
|
85
|
+
const href = entry.href;
|
|
86
|
+
if (href === undefined) {
|
|
87
|
+
return (
|
|
88
|
+
<button
|
|
89
|
+
{...props}
|
|
90
|
+
type="button"
|
|
91
|
+
disabled={entry.disabled}
|
|
92
|
+
onClick={() => {
|
|
93
|
+
onCloseAndRestore();
|
|
94
|
+
entry.onSelect();
|
|
95
|
+
}}
|
|
96
|
+
>
|
|
97
|
+
{entry.label}
|
|
98
|
+
</button>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
return (
|
|
102
|
+
<Link
|
|
103
|
+
{...props}
|
|
104
|
+
{...(replace === undefined ? {} : { replace })}
|
|
105
|
+
renderLink={renderLink}
|
|
106
|
+
href={href}
|
|
107
|
+
aria-current={active ? 'page' : undefined}
|
|
108
|
+
onClick={(event) => {
|
|
109
|
+
if (entry.disabled === true) {
|
|
110
|
+
event.preventDefault();
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (!isNavigationClick(event)) return;
|
|
114
|
+
onCloseAndRestore();
|
|
115
|
+
follow(href, event, onNavigate);
|
|
116
|
+
}}
|
|
117
|
+
onKeyDown={(event) => {
|
|
118
|
+
props.onKeyDown(event);
|
|
119
|
+
if (event.key === ' ' && entry.disabled !== true) {
|
|
120
|
+
event.preventDefault();
|
|
121
|
+
event.currentTarget.click();
|
|
122
|
+
}
|
|
123
|
+
}}
|
|
124
|
+
>
|
|
125
|
+
{entry.label}
|
|
126
|
+
</Link>
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function useMenuKeyHandler(
|
|
131
|
+
triggerRef: RefObject<HTMLButtonElement | null>,
|
|
132
|
+
onClose: () => void,
|
|
133
|
+
closeAndRestore: () => void,
|
|
134
|
+
): (event: KeyboardEvent<HTMLDivElement>) => void {
|
|
135
|
+
return (event) => {
|
|
136
|
+
if (event.key === 'Escape') {
|
|
137
|
+
event.preventDefault();
|
|
138
|
+
event.stopPropagation();
|
|
139
|
+
closeAndRestore();
|
|
140
|
+
}
|
|
141
|
+
if (event.key === 'Tab') {
|
|
142
|
+
// Move focus to the trigger before removing the focused menu item. The browser continues Tab normally.
|
|
143
|
+
triggerRef.current?.focus();
|
|
144
|
+
onClose();
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function Menu({
|
|
150
|
+
replace,
|
|
151
|
+
entries,
|
|
152
|
+
id,
|
|
153
|
+
label,
|
|
154
|
+
closeLabel,
|
|
155
|
+
triggerRef,
|
|
156
|
+
onClose,
|
|
157
|
+
onNavigate,
|
|
158
|
+
renderLink,
|
|
159
|
+
pathname,
|
|
160
|
+
maxHeight,
|
|
161
|
+
subtitle,
|
|
162
|
+
}: MenuProps): ReactNode {
|
|
163
|
+
const activeEntry = resolveActiveMenuEntry(entries, pathname);
|
|
164
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
165
|
+
const menuRef = useRef<HTMLDivElement>(null);
|
|
166
|
+
const roving = useRovingMenu({
|
|
167
|
+
active: true,
|
|
168
|
+
options: entries.map((entry) => ({ disabled: entry.disabled })),
|
|
169
|
+
});
|
|
170
|
+
useFocusFirstItem(menuRef);
|
|
171
|
+
useCloseOnOutsidePress(containerRef, triggerRef, onClose);
|
|
172
|
+
function closeAndRestore(): void {
|
|
173
|
+
flushSync(onClose);
|
|
174
|
+
triggerRef.current?.focus();
|
|
175
|
+
}
|
|
176
|
+
const onKeyDown = useMenuKeyHandler(triggerRef, onClose, closeAndRestore);
|
|
177
|
+
return (
|
|
178
|
+
// The wrapper only handles keys that bubble up from the menu items and the close button.
|
|
179
|
+
<div
|
|
180
|
+
className="bk-navigation-menu"
|
|
181
|
+
role="presentation"
|
|
182
|
+
style={maxHeight === undefined ? undefined : { maxHeight }}
|
|
183
|
+
ref={containerRef}
|
|
184
|
+
onKeyDown={onKeyDown}
|
|
185
|
+
>
|
|
186
|
+
<div
|
|
187
|
+
className="bk-navigation-menu-content"
|
|
188
|
+
id={id}
|
|
189
|
+
role="menu"
|
|
190
|
+
aria-label={subtitle === undefined ? label : `${label}, ${subtitle}`}
|
|
191
|
+
tabIndex={-1}
|
|
192
|
+
ref={menuRef}
|
|
193
|
+
>
|
|
194
|
+
{subtitle === undefined ? null : (
|
|
195
|
+
<div className="bk-navigation-menu-profile" aria-hidden="true">
|
|
196
|
+
<span className="bk-navigation-label">{label}</span>
|
|
197
|
+
<span className="bk-navigation-subtitle">{subtitle}</span>
|
|
198
|
+
</div>
|
|
199
|
+
)}
|
|
200
|
+
<div className="bk-navigation-menu-items">
|
|
201
|
+
{entries.map((entry, index) => (
|
|
202
|
+
<MenuEntry
|
|
203
|
+
key={entry.id}
|
|
204
|
+
entry={entry}
|
|
205
|
+
active={entry === activeEntry}
|
|
206
|
+
itemProps={roving.itemProps(index)}
|
|
207
|
+
replace={replace}
|
|
208
|
+
renderLink={renderLink}
|
|
209
|
+
onNavigate={onNavigate}
|
|
210
|
+
onCloseAndRestore={closeAndRestore}
|
|
211
|
+
/>
|
|
212
|
+
))}
|
|
213
|
+
</div>
|
|
214
|
+
</div>
|
|
215
|
+
<button type="button" className="bk-navigation-menu-close" onClick={closeAndRestore}>
|
|
216
|
+
{closeLabel}
|
|
217
|
+
</button>
|
|
218
|
+
</div>
|
|
219
|
+
);
|
|
220
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useId,
|
|
3
|
+
useRef,
|
|
4
|
+
useState,
|
|
5
|
+
type Dispatch,
|
|
6
|
+
type ReactNode,
|
|
7
|
+
type RefObject,
|
|
8
|
+
type SetStateAction,
|
|
9
|
+
} from 'react';
|
|
10
|
+
import type { useRovingMenu } from '@baukit/a11y-core/web';
|
|
11
|
+
import {
|
|
12
|
+
getNavigationProfileLabel,
|
|
13
|
+
NAVIGATION_DIMENSIONS,
|
|
14
|
+
navigationMatches,
|
|
15
|
+
resolveActiveMenuEntry,
|
|
16
|
+
type NavigationProfile,
|
|
17
|
+
} from './index.js';
|
|
18
|
+
import { follow, Link, type Navigate, type NavigationLinkRenderer } from './web-link.js';
|
|
19
|
+
import { Menu } from './web-menu.js';
|
|
20
|
+
|
|
21
|
+
export type RovingItemProps = ReturnType<ReturnType<typeof useRovingMenu>['itemProps']>;
|
|
22
|
+
|
|
23
|
+
function Avatar({
|
|
24
|
+
profile,
|
|
25
|
+
active,
|
|
26
|
+
}: {
|
|
27
|
+
readonly profile: NavigationProfile;
|
|
28
|
+
readonly active: boolean;
|
|
29
|
+
}): ReactNode {
|
|
30
|
+
const [failedUrl, setFailedUrl] = useState<string>();
|
|
31
|
+
if (profile.renderAvatar !== undefined) {
|
|
32
|
+
return (
|
|
33
|
+
<span className="bk-navigation-avatar bk-navigation-avatar-custom" aria-hidden="true">
|
|
34
|
+
{profile.renderAvatar({ active, size: NAVIGATION_DIMENSIONS.avatar })}
|
|
35
|
+
</span>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
return (
|
|
39
|
+
<span className="bk-navigation-avatar" aria-hidden="true">
|
|
40
|
+
{profile.imageUrl !== undefined && failedUrl !== profile.imageUrl ? (
|
|
41
|
+
<img
|
|
42
|
+
src={profile.imageUrl}
|
|
43
|
+
alt=""
|
|
44
|
+
onError={() => {
|
|
45
|
+
setFailedUrl(profile.imageUrl);
|
|
46
|
+
}}
|
|
47
|
+
/>
|
|
48
|
+
) : (
|
|
49
|
+
profile.initials
|
|
50
|
+
)}
|
|
51
|
+
</span>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function ProfileContent({
|
|
56
|
+
profile,
|
|
57
|
+
active,
|
|
58
|
+
}: {
|
|
59
|
+
readonly profile: NavigationProfile;
|
|
60
|
+
readonly active: boolean;
|
|
61
|
+
}): ReactNode {
|
|
62
|
+
return (
|
|
63
|
+
<>
|
|
64
|
+
<Avatar profile={profile} active={active} />
|
|
65
|
+
<span className="bk-navigation-profile-copy">
|
|
66
|
+
<span className="bk-navigation-label">{profile.label}</span>
|
|
67
|
+
{profile.subtitle === undefined ? null : (
|
|
68
|
+
<span className="bk-navigation-subtitle">{profile.subtitle}</span>
|
|
69
|
+
)}
|
|
70
|
+
</span>
|
|
71
|
+
</>
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function profileIsActive(profile: NavigationProfile, pathname: string): boolean {
|
|
76
|
+
return profile.href !== undefined
|
|
77
|
+
? navigationMatches({ id: 'profile', label: profile.label, href: profile.href }, pathname)
|
|
78
|
+
: resolveActiveMenuEntry(profile.menu, pathname) !== null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface MenuTriggerProps {
|
|
82
|
+
readonly common: object;
|
|
83
|
+
readonly itemProps: RovingItemProps;
|
|
84
|
+
readonly triggerRef: RefObject<HTMLButtonElement | null>;
|
|
85
|
+
readonly open: boolean;
|
|
86
|
+
readonly menuId: string;
|
|
87
|
+
readonly setOpen: Dispatch<SetStateAction<boolean>>;
|
|
88
|
+
readonly children: ReactNode;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function MenuTrigger({
|
|
92
|
+
common,
|
|
93
|
+
itemProps,
|
|
94
|
+
triggerRef,
|
|
95
|
+
open,
|
|
96
|
+
menuId,
|
|
97
|
+
setOpen,
|
|
98
|
+
children,
|
|
99
|
+
}: MenuTriggerProps): ReactNode {
|
|
100
|
+
return (
|
|
101
|
+
<button
|
|
102
|
+
{...common}
|
|
103
|
+
type="button"
|
|
104
|
+
ref={(node) => {
|
|
105
|
+
triggerRef.current = node;
|
|
106
|
+
itemProps.ref(node);
|
|
107
|
+
}}
|
|
108
|
+
aria-haspopup="menu"
|
|
109
|
+
aria-expanded={open}
|
|
110
|
+
aria-controls={open ? menuId : undefined}
|
|
111
|
+
onClick={() => {
|
|
112
|
+
setOpen((value) => !value);
|
|
113
|
+
}}
|
|
114
|
+
onKeyDown={(event) => {
|
|
115
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
116
|
+
event.preventDefault();
|
|
117
|
+
setOpen(true);
|
|
118
|
+
} else itemProps.onKeyDown(event);
|
|
119
|
+
}}
|
|
120
|
+
>
|
|
121
|
+
{children}
|
|
122
|
+
</button>
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface ProfileProps {
|
|
127
|
+
readonly closeLabel: string;
|
|
128
|
+
readonly profile: NavigationProfile;
|
|
129
|
+
readonly pathname: string;
|
|
130
|
+
readonly collapsed: boolean;
|
|
131
|
+
readonly itemProps: RovingItemProps;
|
|
132
|
+
readonly onNavigate: Navigate | undefined;
|
|
133
|
+
readonly renderLink: NavigationLinkRenderer | undefined;
|
|
134
|
+
}
|
|
135
|
+
export function Profile({
|
|
136
|
+
closeLabel,
|
|
137
|
+
profile,
|
|
138
|
+
pathname,
|
|
139
|
+
collapsed,
|
|
140
|
+
itemProps,
|
|
141
|
+
onNavigate,
|
|
142
|
+
renderLink,
|
|
143
|
+
}: ProfileProps): ReactNode {
|
|
144
|
+
const [open, setOpen] = useState(false);
|
|
145
|
+
const triggerRef = useRef<HTMLButtonElement>(null);
|
|
146
|
+
const id = useId();
|
|
147
|
+
const active = profileIsActive(profile, pathname);
|
|
148
|
+
const profileLabel = getNavigationProfileLabel(profile);
|
|
149
|
+
const common = {
|
|
150
|
+
...itemProps,
|
|
151
|
+
className: 'bk-navigation-item',
|
|
152
|
+
'aria-label': profileLabel,
|
|
153
|
+
'data-active': active ? 'page' : undefined,
|
|
154
|
+
title: collapsed ? profileLabel : undefined,
|
|
155
|
+
};
|
|
156
|
+
const content = <ProfileContent profile={profile} active={active} />;
|
|
157
|
+
return (
|
|
158
|
+
<div className="bk-navigation-profile">
|
|
159
|
+
{profile.href !== undefined ? (
|
|
160
|
+
<Link
|
|
161
|
+
{...common}
|
|
162
|
+
renderLink={renderLink}
|
|
163
|
+
href={profile.href}
|
|
164
|
+
aria-current={active ? 'page' : undefined}
|
|
165
|
+
onClick={(event) => {
|
|
166
|
+
follow(profile.href, event, onNavigate);
|
|
167
|
+
}}
|
|
168
|
+
>
|
|
169
|
+
{content}
|
|
170
|
+
</Link>
|
|
171
|
+
) : (
|
|
172
|
+
<MenuTrigger
|
|
173
|
+
common={common}
|
|
174
|
+
itemProps={itemProps}
|
|
175
|
+
triggerRef={triggerRef}
|
|
176
|
+
open={open}
|
|
177
|
+
menuId={id}
|
|
178
|
+
setOpen={setOpen}
|
|
179
|
+
>
|
|
180
|
+
{content}
|
|
181
|
+
</MenuTrigger>
|
|
182
|
+
)}
|
|
183
|
+
{open && profile.menu !== undefined ? (
|
|
184
|
+
<Menu
|
|
185
|
+
closeLabel={closeLabel}
|
|
186
|
+
entries={profile.menu}
|
|
187
|
+
id={id}
|
|
188
|
+
label={profile.label}
|
|
189
|
+
subtitle={profile.subtitle}
|
|
190
|
+
triggerRef={triggerRef}
|
|
191
|
+
onClose={() => {
|
|
192
|
+
setOpen(false);
|
|
193
|
+
}}
|
|
194
|
+
pathname={pathname}
|
|
195
|
+
onNavigate={onNavigate}
|
|
196
|
+
renderLink={renderLink}
|
|
197
|
+
/>
|
|
198
|
+
) : null}
|
|
199
|
+
</div>
|
|
200
|
+
);
|
|
201
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { useId, useLayoutEffect, useRef, useState, type ReactNode, type RefObject } from 'react';
|
|
2
|
+
import { announce } from '@baukit/a11y-core/web';
|
|
3
|
+
import { resolveActiveNavigation, type NavigationItem } from './index.js';
|
|
4
|
+
import type { Navigate, NavigationIcon, NavigationLinkRenderer } from './web-link.js';
|
|
5
|
+
import { Menu } from './web-menu.js';
|
|
6
|
+
|
|
7
|
+
const SECTION_MENU_GAP = 8;
|
|
8
|
+
|
|
9
|
+
function useSectionMenuHeight(
|
|
10
|
+
open: boolean,
|
|
11
|
+
triggerRef: RefObject<HTMLButtonElement | null>,
|
|
12
|
+
): number | undefined {
|
|
13
|
+
const [height, setHeight] = useState<number>();
|
|
14
|
+
const measuredHeight = useRef<number | undefined>(undefined);
|
|
15
|
+
useLayoutEffect(() => {
|
|
16
|
+
const trigger = triggerRef.current;
|
|
17
|
+
if (!open || trigger === null) return;
|
|
18
|
+
function measure(): void {
|
|
19
|
+
const bar = document.querySelector<HTMLElement>(".bk-navigation[data-layout='bar']");
|
|
20
|
+
if (trigger === null) return;
|
|
21
|
+
const viewportBottom =
|
|
22
|
+
(window.visualViewport?.height ?? window.innerHeight) +
|
|
23
|
+
(window.visualViewport?.offsetTop ?? 0);
|
|
24
|
+
const bottom = Math.min(viewportBottom, bar?.getBoundingClientRect().top ?? viewportBottom);
|
|
25
|
+
const nextHeight = Math.max(
|
|
26
|
+
0,
|
|
27
|
+
bottom - trigger.getBoundingClientRect().bottom - SECTION_MENU_GAP * 2,
|
|
28
|
+
);
|
|
29
|
+
if (measuredHeight.current === nextHeight) return;
|
|
30
|
+
measuredHeight.current = nextHeight;
|
|
31
|
+
setHeight(nextHeight);
|
|
32
|
+
}
|
|
33
|
+
measure();
|
|
34
|
+
const observer = 'ResizeObserver' in window ? new ResizeObserver(measure) : undefined;
|
|
35
|
+
for (
|
|
36
|
+
let element: HTMLElement | null = trigger;
|
|
37
|
+
element !== null;
|
|
38
|
+
element = element.parentElement
|
|
39
|
+
) {
|
|
40
|
+
observer?.observe(element);
|
|
41
|
+
}
|
|
42
|
+
const navigation = document.querySelector('.bk-navigation');
|
|
43
|
+
if (navigation !== null) observer?.observe(navigation);
|
|
44
|
+
window.addEventListener('resize', measure);
|
|
45
|
+
window.addEventListener('scroll', measure, true);
|
|
46
|
+
window.visualViewport?.addEventListener('resize', measure);
|
|
47
|
+
return () => {
|
|
48
|
+
observer?.disconnect();
|
|
49
|
+
window.removeEventListener('resize', measure);
|
|
50
|
+
window.removeEventListener('scroll', measure, true);
|
|
51
|
+
window.visualViewport?.removeEventListener('resize', measure);
|
|
52
|
+
};
|
|
53
|
+
}, [open, triggerRef]);
|
|
54
|
+
return height;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface SectionPickerProps {
|
|
58
|
+
readonly replace?: boolean;
|
|
59
|
+
readonly closeLabel: string;
|
|
60
|
+
readonly item: NavigationItem<NavigationIcon>;
|
|
61
|
+
readonly pathname: string;
|
|
62
|
+
readonly onNavigate?: Navigate;
|
|
63
|
+
readonly renderLink?: NavigationLinkRenderer;
|
|
64
|
+
}
|
|
65
|
+
export function SectionPicker({
|
|
66
|
+
replace = false,
|
|
67
|
+
item,
|
|
68
|
+
pathname,
|
|
69
|
+
onNavigate,
|
|
70
|
+
renderLink,
|
|
71
|
+
closeLabel,
|
|
72
|
+
}: SectionPickerProps): ReactNode {
|
|
73
|
+
const [open, setOpen] = useState(false);
|
|
74
|
+
const triggerRef = useRef<HTMLButtonElement>(null);
|
|
75
|
+
const maxHeight = useSectionMenuHeight(open, triggerRef);
|
|
76
|
+
const id = useId();
|
|
77
|
+
const active = resolveActiveNavigation([item], pathname).subItem ?? item.children?.[0];
|
|
78
|
+
const entries = item.children ?? [];
|
|
79
|
+
if (entries.length === 0) return null;
|
|
80
|
+
const label = `${item.label}, ${active?.label ?? ''}`;
|
|
81
|
+
return (
|
|
82
|
+
<nav aria-label={item.label} className="bk-navigation-picker">
|
|
83
|
+
<button
|
|
84
|
+
className="bk-navigation-picker-trigger"
|
|
85
|
+
type="button"
|
|
86
|
+
ref={triggerRef}
|
|
87
|
+
aria-label={label}
|
|
88
|
+
aria-expanded={open}
|
|
89
|
+
aria-haspopup="menu"
|
|
90
|
+
aria-controls={open ? id : undefined}
|
|
91
|
+
onClick={() => {
|
|
92
|
+
setOpen((value) => !value);
|
|
93
|
+
}}
|
|
94
|
+
>
|
|
95
|
+
<span>
|
|
96
|
+
<span className="bk-navigation-picker-section">{item.label}</span>
|
|
97
|
+
<span>{active?.label}</span>
|
|
98
|
+
</span>
|
|
99
|
+
<span aria-hidden="true">⌄</span>
|
|
100
|
+
</button>
|
|
101
|
+
{open ? (
|
|
102
|
+
<Menu
|
|
103
|
+
{...(replace ? { replace } : {})}
|
|
104
|
+
closeLabel={closeLabel}
|
|
105
|
+
entries={entries}
|
|
106
|
+
maxHeight={maxHeight}
|
|
107
|
+
id={id}
|
|
108
|
+
label={item.label}
|
|
109
|
+
triggerRef={triggerRef}
|
|
110
|
+
onClose={() => {
|
|
111
|
+
setOpen(false);
|
|
112
|
+
}}
|
|
113
|
+
pathname={pathname}
|
|
114
|
+
onNavigate={
|
|
115
|
+
onNavigate === undefined
|
|
116
|
+
? undefined
|
|
117
|
+
: (href, event) => {
|
|
118
|
+
announce(entries.find((entry) => entry.href === href)?.label ?? item.label);
|
|
119
|
+
if (replace) onNavigate(href, event, { replace: true });
|
|
120
|
+
else onNavigate(href, event);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
renderLink={renderLink}
|
|
124
|
+
/>
|
|
125
|
+
) : null}
|
|
126
|
+
</nav>
|
|
127
|
+
);
|
|
128
|
+
}
|