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