@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.
Files changed (70) hide show
  1. package/dist/index.d.ts.map +1 -1
  2. package/dist/index.js +19 -14
  3. package/dist/index.js.map +1 -1
  4. package/dist/native-menu.d.ts +22 -0
  5. package/dist/native-menu.d.ts.map +1 -0
  6. package/dist/native-menu.js +226 -0
  7. package/dist/native-menu.js.map +1 -0
  8. package/dist/native-primitives.d.ts +51 -0
  9. package/dist/native-primitives.d.ts.map +1 -0
  10. package/dist/native-primitives.js +125 -0
  11. package/dist/native-primitives.js.map +1 -0
  12. package/dist/native-profile.d.ts +34 -0
  13. package/dist/native-profile.d.ts.map +1 -0
  14. package/dist/native-profile.js +67 -0
  15. package/dist/native-profile.js.map +1 -0
  16. package/dist/native-section-picker.d.ts +14 -0
  17. package/dist/native-section-picker.d.ts.map +1 -0
  18. package/dist/native-section-picker.js +46 -0
  19. package/dist/native-section-picker.js.map +1 -0
  20. package/dist/native-styles.d.ts +106 -0
  21. package/dist/native-styles.d.ts.map +1 -0
  22. package/dist/native-styles.js +50 -0
  23. package/dist/native-styles.js.map +1 -0
  24. package/dist/native-theme.d.ts +49 -0
  25. package/dist/native-theme.d.ts.map +1 -0
  26. package/dist/native-theme.js +56 -0
  27. package/dist/native-theme.js.map +1 -0
  28. package/dist/native.d.ts +9 -52
  29. package/dist/native.d.ts.map +1 -1
  30. package/dist/native.js +148 -536
  31. package/dist/native.js.map +1 -1
  32. package/dist/use-navigation-state.d.ts +9 -8
  33. package/dist/use-navigation-state.d.ts.map +1 -1
  34. package/dist/use-navigation-state.js +3 -2
  35. package/dist/use-navigation-state.js.map +1 -1
  36. package/dist/web-link.d.ts +20 -0
  37. package/dist/web-link.d.ts.map +1 -0
  38. package/dist/web-link.js +23 -0
  39. package/dist/web-link.js.map +1 -0
  40. package/dist/web-menu.d.ts +19 -0
  41. package/dist/web-menu.d.ts.map +1 -0
  42. package/dist/web-menu.js +96 -0
  43. package/dist/web-menu.js.map +1 -0
  44. package/dist/web-profile.d.ts +16 -0
  45. package/dist/web-profile.d.ts.map +1 -0
  46. package/dist/web-profile.js +58 -0
  47. package/dist/web-profile.js.map +1 -0
  48. package/dist/web-section-picker.d.ts +13 -0
  49. package/dist/web-section-picker.d.ts.map +1 -0
  50. package/dist/web-section-picker.js +71 -0
  51. package/dist/web-section-picker.js.map +1 -0
  52. package/dist/web.d.ts +7 -25
  53. package/dist/web.d.ts.map +1 -1
  54. package/dist/web.js +113 -257
  55. package/dist/web.js.map +1 -1
  56. package/package.json +7 -7
  57. package/src/index.ts +22 -16
  58. package/src/native-menu.tsx +436 -0
  59. package/src/native-primitives.tsx +308 -0
  60. package/src/native-profile.tsx +153 -0
  61. package/src/native-section-picker.tsx +102 -0
  62. package/src/native-styles.ts +50 -0
  63. package/src/native-theme.ts +108 -0
  64. package/src/native.tsx +355 -1086
  65. package/src/use-navigation-state.ts +20 -5
  66. package/src/web-link.tsx +51 -0
  67. package/src/web-menu.tsx +220 -0
  68. package/src/web-profile.tsx +201 -0
  69. package/src/web-section-picker.tsx +128 -0
  70. package/src/web.tsx +256 -598
package/dist/native.js CHANGED
@@ -1,387 +1,47 @@
1
- import { createElement as _createElement } from "react";
2
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
- import { useEffect, useCallback, useLayoutEffect, useId, useRef, useState, } from 'react';
4
- import { Image, Modal, Platform, Pressable, ScrollView, StyleSheet, Text, View, useWindowDimensions, } from 'react-native';
5
- import { announce, asFocusTarget, hostElement, useAriaHiddenInert, useOverlayA11y, useReducedMotionPreference, useRovingMenu, } from '@baukit/a11y-core';
6
- import { getNavigationLayout, getNavigationBarHeight, getNavigationBarVisualHeight, getNavigationBarLineHeight, NAVIGATION_BAR_DIMENSIONS, NAVIGATION_DIMENSIONS, navigationMatches, nextSectionHref, resolveActiveNavigation, resolveActiveMenuEntry, validateNavigation, } from './index.js';
7
- import { useNavigationState } from './use-navigation-state.js';
8
- function barMetrics(theme) {
9
- return {
10
- ...(theme === undefined ? {} : { spacing: theme.spacing }),
11
- ...(theme?.typography?.barFontSize === undefined
12
- ? {}
13
- : { fontSize: theme.typography.barFontSize }),
14
- ...(theme?.typography?.lineHeight === undefined
15
- ? {}
16
- : { lineHeight: theme.typography.lineHeight }),
17
- };
18
- }
19
- function textStyle(theme, selected = false, bar = false) {
20
- const typography = theme.typography;
21
- return {
22
- fontFamily: selected
23
- ? (typography?.activeFontFamily ?? typography?.fontFamily)
24
- : typography?.fontFamily,
25
- fontSize: bar
26
- ? (typography?.barFontSize ?? NAVIGATION_BAR_DIMENSIONS.labelFontSize)
27
- : (typography?.fontSize ?? 14),
28
- fontWeight: selected
29
- ? (typography?.activeFontWeight ?? '700')
30
- : (typography?.fontWeight ?? '400'),
31
- lineHeight: bar ? getNavigationBarLineHeight(barMetrics(theme)) : typography?.lineHeight,
32
- letterSpacing: typography?.letterSpacing,
33
- };
34
- }
35
- function showsFocusRing(event) {
36
- if (Platform.OS !== 'web')
37
- return true;
38
- const target = event.nativeEvent?.target;
39
- return !(target instanceof Element) || target.matches(':focus-visible');
40
- }
41
- function FocusablePressable({ theme, selected = false, unfocusedBorderColor, style, onFocus, onBlur, ...props }) {
42
- const [focused, setFocused] = useState(false);
43
- return (_jsx(Pressable, { ...props, onFocus: (event) => {
44
- setFocused(showsFocusRing(event));
45
- onFocus?.(event);
46
- }, onBlur: (event) => {
47
- setFocused(false);
48
- onBlur?.(event);
49
- }, style: [
50
- style,
51
- {
52
- minHeight: Platform.OS === 'android'
53
- ? NAVIGATION_DIMENSIONS.androidTarget
54
- : NAVIGATION_DIMENSIONS.target,
55
- minWidth: Platform.OS === 'android'
56
- ? NAVIGATION_DIMENSIONS.androidTarget
57
- : NAVIGATION_DIMENSIONS.target,
58
- borderWidth: NAVIGATION_BAR_DIMENSIONS.targetBorder,
59
- borderColor: focused
60
- ? selected
61
- ? theme.activeText
62
- : theme.focus
63
- : (unfocusedBorderColor ?? 'transparent'),
64
- },
65
- ] }));
66
- }
67
- function isModifiedPress(event) {
68
- const native = event.nativeEvent;
69
- if (typeof native !== 'object' || native === null)
70
- return false;
71
- return (('button' in native && native.button !== undefined && native.button !== 0) ||
72
- ('metaKey' in native && native.metaKey === true) ||
73
- ('ctrlKey' in native && native.ctrlKey === true) ||
74
- ('shiftKey' in native && native.shiftKey === true) ||
75
- ('altKey' in native && native.altKey === true));
76
- }
77
- function webLink(href, active) {
78
- return Platform.OS === 'web'
79
- ? { href, 'aria-current': active ? 'page' : undefined }
80
- : {};
81
- }
82
- function follow(href, event, onNavigate) {
83
- if (isModifiedPress(event))
84
- return;
85
- event.preventDefault();
86
- onNavigate(href);
87
- }
88
- function Target({ label, subtitle, selected = false, ancestor = false, expanded, controls, menu, href, onPress, theme, bar = false, collapsed = false, children, roving, triggerRef, testID, }) {
89
- return (_jsxs(FocusablePressable, { theme: theme, selected: selected && !ancestor, dataSet: { active: selected ? (ancestor ? 'ancestor' : 'page') : 'false' }, ...roving, ...(href === undefined ? {} : webLink(href, selected)), ...(Platform.OS === 'web'
90
- ? {
91
- title: collapsed ? label : undefined,
92
- 'aria-controls': controls,
93
- 'aria-haspopup': menu ? 'menu' : undefined,
94
- }
95
- : {}), accessibilityLabel: subtitle === undefined ? label : `${label}, ${subtitle}`, accessibilityRole: href === undefined ? 'button' : 'link', accessibilityState: { selected, ...(expanded === undefined ? {} : { expanded }) }, ref: (node) => {
96
- roving?.ref(node);
97
- if (triggerRef !== undefined)
98
- triggerRef.current = node;
99
- }, onPress: onPress, testID: testID, style: [
100
- styles.target,
101
- bar ? styles.barTarget : styles.railTarget,
102
- collapsed && styles.collapsedTarget,
103
- {
104
- borderRadius: theme.radius,
105
- gap: theme.spacing,
106
- backgroundColor: selected && !ancestor ? theme.activeBackground : theme.background,
107
- },
108
- ], children: [children, !collapsed ? (_jsxs(View, { style: styles.labelGroup, children: [_jsx(Text, { numberOfLines: 1, style: {
109
- ...textStyle(theme, selected && !ancestor, bar),
110
- color: ancestor ? theme.ancestorText : selected ? theme.activeText : theme.text,
111
- }, children: label }), !bar && subtitle !== undefined ? (_jsx(Text, { numberOfLines: 1, style: {
112
- ...textStyle(theme),
113
- color: theme.muted,
114
- fontSize: theme.typography?.subtitleFontSize ?? 12,
115
- }, children: subtitle })) : null] })) : null] }));
116
- }
117
- function Decorative({ children, style, }) {
118
- return (_jsx(View, { style: style, accessible: false, accessibilityElementsHidden: true, importantForAccessibility: "no-hide-descendants", "aria-hidden": true, children: children }));
119
- }
120
- const MENU_WIDTH = 280;
121
- const MENU_EDGE = 8;
122
- function menuModalPadding(insets = {}) {
123
- const native = Platform.OS !== 'web';
124
- const edge = (inset) => MENU_EDGE + (native ? (inset ?? 0) : 0);
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useEffect, useId, useRef, useState } from 'react';
3
+ import { ScrollView, Text, View, useWindowDimensions, } from 'react-native';
4
+ import { useAriaHiddenInert, useRovingMenu } from '@baukit/a11y-core';
5
+ import { getNavigationLayout, getNavigationBarHeight, NAVIGATION_BAR_DIMENSIONS, NAVIGATION_DIMENSIONS, nextSectionHref, validateNavigation, } from './index.js';
6
+ import { Menu } from './native-menu.js';
7
+ import { compactVisualStyle, Decorative, follow, Target } from './native-primitives.js';
8
+ import { Profile, profileIsActive, useCompactBarScroll, } from './native-profile.js';
9
+ import { styles } from './native-styles.js';
10
+ import { barMetrics, textStyle, } from './native-theme.js';
11
+ import { useNavigationState, } from './use-navigation-state.js';
12
+ import { useNavigationBarHeight as profileBarHeight } from './native-profile.js';
13
+ import { SectionPicker as Picker } from './native-section-picker.js';
14
+ // A local binding keeps the export a plain property, so Jest can spy on it like before the split.
15
+ export const useNavigationBarHeight = profileBarHeight;
16
+ const CHILD_ICON_SIZE = 20;
17
+ function rendersSomething(node) {
18
+ return node !== null && node !== undefined && typeof node !== 'boolean';
19
+ }
20
+ function useSlotContent(props, bar, collapsed) {
21
+ const slotState = { layout: bar ? 'bar' : 'rail', collapsed };
22
+ const brand = props.renderBrand?.(slotState);
23
+ const accessory = props.renderAccessory?.(slotState);
125
24
  return {
126
- paddingTop: edge(insets.top),
127
- paddingBottom: edge(insets.bottom),
128
- paddingLeft: edge(insets.left),
129
- paddingRight: edge(insets.right),
25
+ brand,
26
+ accessory,
27
+ hasBrand: rendersSomething(brand),
28
+ hasAccessory: rendersSomething(accessory),
130
29
  };
131
30
  }
132
- function Menu({ subtitle, renderBackground, entries, label, closeLabel, pathname, visible, onClose, onNavigate, triggerRef, theme, id, insets, }) {
133
- const activeEntry = resolveActiveMenuEntry(entries, pathname);
134
- const containerRef = useRef(null);
135
- const closeRef = useRef(null);
136
- const pendingFocus = useRef(null);
137
- const presented = useRef(false);
138
- const pendingSelection = useRef(null);
139
- const finishSelection = useCallback(() => {
140
- const selection = pendingSelection.current;
141
- pendingSelection.current = null;
142
- selection?.();
143
- }, []);
144
- useEffect(() => {
145
- if (visible) {
146
- pendingSelection.current = null;
147
- return;
148
- }
149
- if (Platform.OS === 'ios' || pendingSelection.current === null)
150
- return;
151
- // Android removes an unanimated Modal in the close commit and has no onDismiss event.
152
- const frame = requestAnimationFrame(finishSelection);
153
- return () => {
154
- cancelAnimationFrame(frame);
155
- };
156
- }, [visible, finishSelection]);
157
- useEffect(() => () => {
158
- pendingSelection.current = null;
159
- }, []);
160
- const [webPresented, setWebPresented] = useState(false);
161
- const [webPosition, setWebPosition] = useState();
162
- const deferFocus = useCallback((task) => {
163
- if (presented.current)
164
- task();
165
- else
166
- pendingFocus.current = task;
167
- return {
168
- cancel: () => {
169
- pendingFocus.current = null;
170
- presented.current = false;
171
- },
172
- };
173
- }, []);
174
- const roving = useRovingMenu({
175
- active: visible,
176
- options: entries.map((entry) => ({
177
- disabled: entry.disabled,
178
- selected: entry === activeEntry,
179
- })),
180
- });
181
- const { containerProps, backgroundProps } = useOverlayA11y({
182
- active: visible && (Platform.OS !== 'web' || webPresented),
183
- deferFocus,
184
- containerRef,
185
- triggerRef,
186
- initialFocusRef: roving.activeIndex === null ? closeRef : roving.initialFocusRef,
187
- onEscape: onClose,
188
- });
189
- const { reducedMotion, resolved } = useReducedMotionPreference();
190
- const motion = !resolved ? 'unresolved' : reducedMotion ? 'reduced' : 'standard';
31
+ function useBarHeightReport(bar, barHeight, onBarHeightChange) {
191
32
  useEffect(() => {
192
- if (visible)
193
- return;
194
- presented.current = false;
195
- setWebPresented(false);
196
- }, [visible]);
197
- useLayoutEffect(() => {
198
- if (!visible || Platform.OS !== 'web')
199
- return;
200
- function positionMenu() {
201
- const trigger = hostElement(triggerRef);
202
- if (!(trigger instanceof HTMLElement))
203
- return;
204
- const rect = trigger.getBoundingClientRect();
205
- const width = Math.min(MENU_WIDTH, window.innerWidth - 2 * MENU_EDGE);
206
- const below = window.innerHeight - rect.bottom;
207
- const above = rect.top >= below;
208
- setWebPosition({
209
- position: 'absolute',
210
- left: Math.max(MENU_EDGE, Math.min(rect.left, window.innerWidth - width - MENU_EDGE)),
211
- width,
212
- maxHeight: Math.max(NAVIGATION_DIMENSIONS.target, (above ? rect.top : below) - 2 * MENU_EDGE),
213
- ...(above
214
- ? { bottom: window.innerHeight - rect.top + MENU_EDGE }
215
- : { top: rect.bottom + MENU_EDGE }),
216
- });
217
- }
218
- positionMenu();
219
- window.addEventListener('resize', positionMenu);
220
- window.addEventListener('scroll', positionMenu, true);
221
- return () => {
222
- window.removeEventListener('resize', positionMenu);
223
- window.removeEventListener('scroll', positionMenu, true);
224
- };
225
- }, [visible, triggerRef]);
226
- useEffect(() => {
227
- if (!visible || Platform.OS !== 'web')
228
- return;
229
- function outside(event) {
230
- const menu = hostElement(containerRef);
231
- const trigger = hostElement(triggerRef);
232
- if (!(event.target instanceof Node) ||
233
- (menu instanceof Node && menu.contains(event.target)) ||
234
- (trigger instanceof Node && trigger.contains(event.target)))
235
- return;
236
- onClose();
237
- }
238
- document.addEventListener('pointerdown', outside);
239
- return () => {
240
- document.removeEventListener('pointerdown', outside);
241
- };
242
- }, [visible, triggerRef, onClose, roving.activeIndex, roving.initialFocusRef]);
243
- function closeAndRestore() {
244
- onClose();
245
- if (Platform.OS === 'web')
246
- asFocusTarget(triggerRef)?.focus();
247
- }
248
- const menu = (_jsxs(View, { ...containerProps, onKeyDown: (event) => {
249
- if (event.nativeEvent.key === 'Escape') {
250
- event.preventDefault();
251
- closeAndRestore();
252
- }
253
- if (Platform.OS === 'web' && event.nativeEvent.key === 'Tab')
254
- closeAndRestore();
255
- }, ref: containerRef, accessibilityViewIsModal: Platform.OS !== 'web', accessibilityLabel: subtitle === undefined ? label : `${label}, ${subtitle}`, role: "menu", nativeID: id, testID: "navigation-menu", style: [
256
- styles.menu,
257
- {
258
- backgroundColor: theme.background,
259
- borderColor: theme.border,
260
- borderRadius: theme.radius,
261
- },
262
- Platform.OS === 'web' && webPosition,
263
- ], dataSet: { motion }, children: [subtitle === undefined ? null : (_jsx(Decorative, { children: _jsxs(View, { style: styles.menuProfile, children: [_jsx(Text, { numberOfLines: 1, style: { ...textStyle(theme), color: theme.text }, children: label }), _jsx(Text, { numberOfLines: 1, style: {
264
- ...textStyle(theme),
265
- color: theme.muted,
266
- fontSize: theme.typography?.subtitleFontSize ?? 12,
267
- }, children: subtitle })] }) })), _jsx(ScrollView, { children: entries.map((entry, index) => (_createElement(FocusablePressable, { theme: theme, selected: entry === activeEntry, dataSet: { active: entry === activeEntry ? 'page' : 'false', tone: entry.tone }, ...roving.itemProps(index), key: entry.id, accessibilityRole: "menuitem", accessibilityLabel: entry.label, accessibilityState: {
268
- disabled: entry.disabled === true,
269
- selected: entry === activeEntry,
270
- }, disabled: entry.disabled, ...(entry.href === undefined ? {} : webLink(entry.href, entry === activeEntry)), onPress: (event) => {
271
- if (isModifiedPress(event) || pendingSelection.current !== null)
272
- return;
273
- event.preventDefault();
274
- pendingSelection.current =
275
- entry.href !== undefined
276
- ? () => {
277
- onNavigate(entry.href);
278
- }
279
- : entry.onSelect;
280
- closeAndRestore();
281
- }, style: [
282
- styles.menuItem,
283
- {
284
- paddingHorizontal: theme.spacing,
285
- backgroundColor: entry === activeEntry && entry.tone !== 'danger'
286
- ? theme.activeBackground
287
- : theme.background,
288
- },
289
- ] },
290
- _jsx(Text, { style: {
291
- color: entry.tone === 'danger'
292
- ? theme.danger
293
- : entry === activeEntry
294
- ? theme.activeText
295
- : theme.text,
296
- ...textStyle(theme, entry === activeEntry),
297
- }, children: entry.label })))) }), _jsx(FocusablePressable, { theme: theme, ref: closeRef, accessibilityRole: "button", accessibilityLabel: closeLabel, onPress: closeAndRestore, style: styles.menuItem, children: _jsx(Text, { style: { ...textStyle(theme), color: theme.text }, children: closeLabel }) })] }));
298
- return (_jsxs(_Fragment, { children: [renderBackground(backgroundProps), _jsx(Modal, { visible: visible, transparent: true, animationType: "none", onRequestClose: onClose, onDismiss: () => {
299
- if (!visible)
300
- finishSelection();
301
- }, onShow: () => {
302
- if (!visible)
303
- return;
304
- presented.current = true;
305
- const focus = pendingFocus.current;
306
- pendingFocus.current = null;
307
- focus?.();
308
- if (Platform.OS === 'web')
309
- setWebPresented(true);
310
- }, children: _jsx(View, { style: [styles.modal, menuModalPadding(insets)], children: menu }) })] }));
311
- }
312
- function compactVisualStyle(fontScale) {
313
- return { height: getNavigationBarVisualHeight(fontScale), justifyContent: 'center' };
314
- }
315
- function Avatar({ profile, active, theme, style, }) {
316
- const [failedUrl, setFailedUrl] = useState();
317
- if (profile.renderAvatar !== undefined) {
318
- return (_jsx(Decorative, { style: style, children: profile.renderAvatar({ active, size: NAVIGATION_DIMENSIONS.avatar }) }));
319
- }
320
- return (_jsx(Decorative, { style: style, children: profile.imageUrl !== undefined && failedUrl !== profile.imageUrl ? (_jsx(Image, { source: { uri: profile.imageUrl }, onError: () => {
321
- setFailedUrl(profile.imageUrl);
322
- }, style: styles.avatar })) : (_jsx(View, { style: [styles.avatar, { backgroundColor: theme.activeBackground }], children: _jsx(Text, { allowFontScaling: false, style: { ...textStyle(theme), color: theme.activeText }, children: profile.initials }) })) }));
323
- }
324
- function profileIsActive(profile, pathname) {
325
- return profile.href !== undefined
326
- ? navigationMatches({ id: 'profile', label: profile.label, href: profile.href }, pathname)
327
- : resolveActiveMenuEntry(profile.menu, pathname) !== null;
328
- }
329
- function Profile({ profile, open, onOpen, triggerRef, id, theme, bar, collapsed, pathname, onNavigate, roving, onLayout, }) {
330
- const { fontScale } = useWindowDimensions();
331
- const active = profileIsActive(profile, pathname);
332
- return (_jsx(View, { style: bar ? styles.barSection : styles.profile, testID: "navigation-profile", onLayout: onLayout, children: _jsx(Target, { label: profile.label, subtitle: profile.subtitle, selected: active, theme: theme, bar: bar, collapsed: collapsed, roving: roving, triggerRef: triggerRef, ...(profile.href === undefined
333
- ? { expanded: open, controls: id, menu: true }
334
- : { href: profile.href }), onPress: (event) => {
335
- if (profile.href !== undefined)
336
- follow(profile.href, event, onNavigate);
337
- else
338
- onOpen();
339
- }, children: _jsx(Avatar, { profile: profile, active: active, theme: theme, style: bar ? compactVisualStyle(fontScale) : undefined }) }) }));
33
+ if (bar)
34
+ onBarHeightChange?.(barHeight);
35
+ }, [bar, barHeight, onBarHeightChange]);
340
36
  }
341
- export function useNavigationBarHeight(bottomInset = 0, theme, slotHeight = 0) {
342
- const { fontScale } = useWindowDimensions();
343
- return getNavigationBarHeight(fontScale, bottomInset, { ...barMetrics(theme), slotHeight });
37
+ function visibleItems(items, expanded, openIds) {
38
+ return items.flatMap((item) => expanded && openIds.includes(item.id) ? [item, ...(item.children ?? [])] : [item]);
344
39
  }
345
- function useCompactBarScroll(selectedIndex) {
346
- const ref = useRef(null);
347
- const viewport = useRef(0);
348
- const content = useRef(0);
349
- const offset = useRef(0);
350
- const frames = useRef(new Map());
351
- const revealSelected = useCallback(() => {
352
- const frame = frames.current.get(selectedIndex);
353
- if (frame === undefined || viewport.current === 0 || content.current === 0)
354
- return;
355
- if (frame.x >= offset.current && frame.x + frame.width <= offset.current + viewport.current)
356
- return;
357
- const x = Math.max(0, Math.min(frame.x + (frame.width - viewport.current) / 2, content.current - viewport.current));
358
- ref.current?.scrollTo({ x, animated: false });
359
- offset.current = x;
360
- }, [selectedIndex]);
361
- useEffect(revealSelected, [revealSelected]);
362
- return {
363
- scrollProps: {
364
- ref,
365
- onLayout: (event) => {
366
- viewport.current = event.nativeEvent.layout.width;
367
- revealSelected();
368
- },
369
- onContentSizeChange: (width) => {
370
- content.current = width;
371
- revealSelected();
372
- },
373
- onScroll: (event) => {
374
- offset.current = event.nativeEvent.contentOffset.x;
375
- },
376
- },
377
- itemLayout: (index) => (event) => {
378
- frames.current.set(index, event.nativeEvent.layout);
379
- revealSelected();
380
- },
381
- };
40
+ function compactSelectedIndex(items, activeId, profileActive) {
41
+ return profileActive ? items.length : items.findIndex((item) => item.id === activeId);
382
42
  }
383
- export function AppNavigation(props) {
384
- const { items, profile, pathname, onNavigate, theme, insets = {}, label, closeLabel, collapseLabel, expandLabel, } = props;
43
+ function useAppNavigationModel(props) {
44
+ const { items, profile, pathname, theme, insets = {} } = props;
385
45
  const [slotHeight, setSlotHeight] = useState(0);
386
46
  const [profileOpen, setProfileOpen] = useState(false);
387
47
  const profileTriggerRef = useRef(null);
@@ -392,182 +52,134 @@ export function AppNavigation(props) {
392
52
  const bar = getNavigationLayout(props.width ?? dimensions.width) === 'bar';
393
53
  const state = useNavigationState(items, pathname, props);
394
54
  const collapsed = !bar && state.collapsed;
395
- const slotState = { layout: bar ? 'bar' : 'rail', collapsed };
396
- const brand = props.renderBrand?.(slotState);
397
- const accessory = props.renderAccessory?.(slotState);
398
- const hasBrand = brand != null && typeof brand !== 'boolean';
399
- const hasAccessory = accessory != null && typeof accessory !== 'boolean';
400
- const hasSlots = hasBrand || hasAccessory;
55
+ const slots = useSlotContent(props, bar, collapsed);
401
56
  const barHeight = getNavigationBarHeight(dimensions.fontScale, insets.bottom, {
402
57
  ...barMetrics(theme),
403
- slotHeight: hasSlots ? slotHeight : 0,
58
+ slotHeight: slots.hasBrand || slots.hasAccessory ? slotHeight : 0,
404
59
  });
405
- useEffect(() => {
406
- if (bar)
407
- props.onBarHeightChange?.(barHeight);
408
- }, [bar, barHeight, props.onBarHeightChange]);
60
+ useBarHeightReport(bar, barHeight, props.onBarHeightChange);
409
61
  const id = useId();
410
- const visible = items.flatMap((item) => !bar && !collapsed && state.openIds.includes(item.id)
411
- ? [item, ...(item.children ?? [])]
412
- : [item]);
62
+ const visible = visibleItems(items, !bar && !collapsed, state.openIds);
413
63
  const activeId = visible.some((item) => item.id === state.active.subItem?.id)
414
64
  ? state.active.subItem?.id
415
65
  : state.active.item?.id;
416
66
  const profileActive = profile !== undefined && profileIsActive(profile, pathname);
417
- const compactScroll = useCompactBarScroll(bar ? (profileActive ? items.length : items.findIndex((item) => item.id === activeId)) : -1);
67
+ const compactScroll = useCompactBarScroll(bar ? compactSelectedIndex(items, activeId, profileActive) : -1);
418
68
  const roving = useRovingMenu({
419
69
  active: true,
420
70
  options: [
421
- ...visible.map((item) => ({
422
- selected: item.id === activeId,
423
- })),
71
+ ...visible.map((item) => ({ selected: item.id === activeId })),
424
72
  ...(profile === undefined ? [] : [{ selected: profileActive }]),
425
73
  ],
426
74
  });
427
- const renderBackground = (backgroundProps = {}) => (_jsxs(View, { ...backgroundProps, role: "navigation", accessibilityLabel: label, testID: "primary-navigation", dataSet: { layout: bar ? 'bar' : 'rail', collapsed: String(collapsed) }, style: [
428
- bar ? styles.bar : styles.rail,
429
- {
430
- backgroundColor: theme.background,
431
- borderColor: theme.border,
432
- paddingBottom: insets.bottom ?? 0,
433
- ...(bar
434
- ? {
435
- minHeight: barHeight,
436
- }
437
- : {}),
438
- paddingLeft: insets.left ?? 0,
439
- paddingRight: insets.right ?? 0,
440
- },
441
- !bar && {
75
+ return {
76
+ props,
77
+ insets,
78
+ bar,
79
+ collapsed,
80
+ fontScale: dimensions.fontScale,
81
+ barHeight,
82
+ state,
83
+ id,
84
+ visible,
85
+ roving,
86
+ compactScroll,
87
+ slots,
88
+ setSlotHeight,
89
+ profileOpen,
90
+ setProfileOpen,
91
+ profileTriggerRef,
92
+ profileMenuId,
93
+ };
94
+ }
95
+ function SlotsView({ model }) {
96
+ const { bar, slots, props } = model;
97
+ return (_jsxs(View, { testID: "navigation-slots", style: [
98
+ styles.slots,
99
+ bar && styles.barSlots,
100
+ { padding: props.theme.spacing, gap: props.theme.spacing },
101
+ ], onLayout: (event) => {
102
+ if (bar)
103
+ model.setSlotHeight(event.nativeEvent.layout.height);
104
+ }, children: [!slots.hasBrand ? null : (_jsx(View, { testID: "navigation-brand", style: [styles.slot, bar && styles.barBrand], children: slots.brand })), !slots.hasAccessory ? null : (_jsx(View, { testID: "navigation-accessory", style: styles.slot, children: slots.accessory }))] }));
105
+ }
106
+ function CollapseToggle({ model }) {
107
+ const { collapsed, props } = model;
108
+ return (_jsx(Target, { label: collapsed ? props.expandLabel : props.collapseLabel, expanded: !collapsed, collapsed: true, theme: props.theme, onPress: () => {
109
+ model.state.setCollapsed(!collapsed);
110
+ }, children: _jsx(Decorative, { children: _jsx(Text, { style: { ...textStyle(props.theme), color: props.theme.text }, children: collapsed ? '›' : '‹' }) }) }));
111
+ }
112
+ function SectionChildren({ model, item, groupId, }) {
113
+ const { state, roving, visible, props } = model;
114
+ return (_jsx(View, { nativeID: groupId, role: "group", accessibilityLabel: item.label, style: [styles.children, { borderColor: props.theme.border }], children: (item.children ?? []).map((child) => {
115
+ const selected = state.active.subItem?.id === child.id;
116
+ return (_jsx(Target, { label: child.label, selected: selected, href: child.href, theme: props.theme, roving: roving.itemProps(visible.indexOf(child)), onPress: (event) => {
117
+ follow(child.href, event, props.onNavigate);
118
+ }, children: child.icon === undefined ? null : (_jsx(Decorative, { children: child.icon({ active: selected, size: CHILD_ICON_SIZE }) })) }, child.id));
119
+ }) }));
120
+ }
121
+ function sectionHref(item, cycle, pathname) {
122
+ return cycle ? nextSectionHref(item, pathname) : item.href;
123
+ }
124
+ function NavigationSection({ model, item, index, }) {
125
+ const { bar, collapsed, state, props } = model;
126
+ const active = state.active.item?.id === item.id;
127
+ const group = !bar && (item.children ?? []).length > 0;
128
+ const open = !collapsed && state.openIds.includes(item.id);
129
+ const href = sectionHref(item, bar && active, props.pathname);
130
+ const groupId = `${model.id}-${String(index)}`;
131
+ return (_jsxs(View, { style: bar ? styles.barSection : undefined, testID: `navigation-section-${item.id}`, onLayout: bar ? model.compactScroll.itemLayout(index) : undefined, children: [_jsx(Target, { label: item.label, selected: active, ancestor: active && !bar && !collapsed && state.active.subItem !== null, theme: props.theme, bar: bar, collapsed: collapsed, roving: model.roving.itemProps(model.visible.indexOf(item)), ...(group ? { expanded: open, controls: groupId } : { href }), onPress: (event) => {
132
+ if (group)
133
+ state.toggleGroup(item.id);
134
+ else
135
+ follow(href, event, props.onNavigate);
136
+ }, children: _jsx(Decorative, { style: bar ? compactVisualStyle(model.fontScale) : undefined, children: item.icon({ active, size: NAVIGATION_BAR_DIMENSIONS.icon }) }) }), group && open ? _jsx(SectionChildren, { model: model, item: item, groupId: groupId }) : null] }));
137
+ }
138
+ function ProfileTarget({ model, profile, }) {
139
+ const { bar, props } = model;
140
+ return (_jsx(Profile, { profile: profile, open: model.profileOpen, onOpen: () => {
141
+ model.setProfileOpen(true);
142
+ }, triggerRef: model.profileTriggerRef, id: model.profileMenuId, theme: props.theme, bar: bar, collapsed: model.collapsed, pathname: props.pathname, onNavigate: props.onNavigate, roving: model.roving.itemProps(model.visible.length), ...(bar ? { onLayout: model.compactScroll.itemLayout(props.items.length) } : {}) }));
143
+ }
144
+ function surfaceStyle(model) {
145
+ const { bar, collapsed, insets, props } = model;
146
+ return {
147
+ backgroundColor: props.theme.background,
148
+ borderColor: props.theme.border,
149
+ paddingBottom: insets.bottom ?? 0,
150
+ ...(bar ? { minHeight: model.barHeight } : {}),
151
+ paddingLeft: insets.left ?? 0,
152
+ paddingRight: insets.right ?? 0,
153
+ ...(bar
154
+ ? {}
155
+ : {
442
156
  width: collapsed ? NAVIGATION_DIMENSIONS.collapsedRail : NAVIGATION_DIMENSIONS.rail,
443
157
  paddingTop: insets.top ?? 0,
444
- },
445
- props.style,
446
- ], children: [hasSlots ? (_jsxs(View, { testID: "navigation-slots", style: [
447
- styles.slots,
448
- bar && styles.barSlots,
449
- { padding: theme.spacing, gap: theme.spacing },
450
- ], onLayout: (event) => {
451
- if (bar)
452
- setSlotHeight(event.nativeEvent.layout.height);
453
- }, children: [!hasBrand ? null : (_jsx(View, { testID: "navigation-brand", style: [styles.slot, bar && styles.barBrand], children: brand })), !hasAccessory ? null : (_jsx(View, { testID: "navigation-accessory", style: styles.slot, children: accessory }))] })) : null, !bar ? (_jsx(Target, { label: collapsed ? expandLabel : collapseLabel, expanded: !collapsed, collapsed: true, theme: theme, onPress: () => {
454
- state.setCollapsed(!collapsed);
455
- }, children: _jsx(Decorative, { children: _jsx(Text, { style: { ...textStyle(theme), color: theme.text }, children: collapsed ? '›' : '‹' }) }) })) : null, _jsxs(ScrollView, { ...(bar ? compactScroll.scrollProps : {}), testID: "navigation-items", horizontal: bar, scrollEnabled: true, showsHorizontalScrollIndicator: false, style: bar
456
- ? [
457
- styles.barItems,
458
- {
459
- height: getNavigationBarHeight(dimensions.fontScale, 0, barMetrics(theme)) -
460
- NAVIGATION_BAR_DIMENSIONS.border,
461
- },
462
- ]
463
- : styles.railItems, contentContainerStyle: bar ? styles.barContent : undefined, children: [items.map((item, index) => {
464
- const active = state.active.item?.id === item.id;
465
- const children = item.children ?? [];
466
- const open = !collapsed && state.openIds.includes(item.id);
467
- const href = bar && active ? nextSectionHref(item, pathname) : item.href;
468
- return (_jsxs(View, { style: bar ? styles.barSection : undefined, testID: `navigation-section-${item.id}`, onLayout: bar ? compactScroll.itemLayout(index) : undefined, children: [_jsx(Target, { label: item.label, selected: active, ancestor: active && !bar && !collapsed && state.active.subItem !== null, theme: theme, bar: bar, collapsed: collapsed, roving: roving.itemProps(visible.indexOf(item)), ...(!bar && children.length > 0
469
- ? { expanded: open, controls: `${id}-${String(index)}` }
470
- : { href }), onPress: (event) => {
471
- if (!bar && children.length > 0)
472
- state.toggleGroup(item.id);
473
- else
474
- follow(href, event, onNavigate);
475
- }, children: _jsx(Decorative, { style: bar ? compactVisualStyle(dimensions.fontScale) : undefined, children: item.icon({ active, size: NAVIGATION_BAR_DIMENSIONS.icon }) }) }), !bar && open && children.length > 0 ? (_jsx(View, { nativeID: `${id}-${String(index)}`, role: "group", accessibilityLabel: item.label, style: [styles.children, { borderColor: theme.border }], children: children.map((child) => {
476
- const selected = state.active.subItem?.id === child.id;
477
- return (_jsx(Target, { label: child.label, selected: selected, href: child.href, theme: theme, roving: roving.itemProps(visible.indexOf(child)), onPress: (event) => {
478
- follow(child.href, event, onNavigate);
479
- }, children: child.icon === undefined ? null : (_jsx(Decorative, { children: child.icon({ active: selected, size: 20 }) })) }, child.id));
480
- }) })) : null] }, item.id));
481
- }), bar && profile !== undefined ? (_jsx(Profile, { profile: profile, open: profileOpen, onOpen: () => {
482
- setProfileOpen(true);
483
- }, triggerRef: profileTriggerRef, id: profileMenuId, theme: theme, bar: true, collapsed: false, pathname: pathname, onNavigate: onNavigate, roving: roving.itemProps(visible.length), onLayout: compactScroll.itemLayout(items.length) })) : null] }), !bar && profile !== undefined ? (_jsx(Profile, { profile: profile, open: profileOpen, onOpen: () => {
484
- setProfileOpen(true);
485
- }, triggerRef: profileTriggerRef, id: profileMenuId, theme: theme, bar: false, collapsed: collapsed, pathname: pathname, onNavigate: onNavigate, roving: roving.itemProps(visible.length) })) : null] }));
158
+ }),
159
+ };
160
+ }
161
+ function itemsStyle(model) {
162
+ if (!model.bar)
163
+ return styles.railItems;
164
+ const height = getNavigationBarHeight(model.fontScale, 0, barMetrics(model.props.theme)) -
165
+ NAVIGATION_BAR_DIMENSIONS.border;
166
+ return [styles.barItems, { height }];
167
+ }
168
+ function NavigationSurface({ model, backgroundProps, }) {
169
+ const { bar, collapsed, props, slots } = model;
170
+ const profile = props.profile;
171
+ return (_jsxs(View, { ...backgroundProps, role: "navigation", accessibilityLabel: props.label, testID: "primary-navigation", dataSet: { layout: bar ? 'bar' : 'rail', collapsed: String(collapsed) }, style: [bar ? styles.bar : styles.rail, surfaceStyle(model), props.style], children: [slots.hasBrand || slots.hasAccessory ? _jsx(SlotsView, { model: model }) : null, !bar ? _jsx(CollapseToggle, { model: model }) : null, _jsxs(ScrollView, { ...(bar ? model.compactScroll.scrollProps : {}), testID: "navigation-items", horizontal: bar, scrollEnabled: true, showsHorizontalScrollIndicator: false, style: itemsStyle(model), contentContainerStyle: bar ? styles.barContent : undefined, children: [props.items.map((item, index) => (_jsx(NavigationSection, { model: model, item: item, index: index }, item.id))), bar && profile !== undefined ? _jsx(ProfileTarget, { model: model, profile: profile }) : null] }), !bar && profile !== undefined ? _jsx(ProfileTarget, { model: model, profile: profile }) : null] }));
172
+ }
173
+ export function AppNavigation(props) {
174
+ const model = useAppNavigationModel(props);
175
+ const renderBackground = (backgroundProps = {}) => (_jsx(NavigationSurface, { model: model, backgroundProps: backgroundProps }));
176
+ const profile = props.profile;
486
177
  if (profile?.menu === undefined)
487
178
  return renderBackground();
488
- return (_jsx(Menu, { renderBackground: renderBackground, entries: profile.menu, closeLabel: closeLabel, pathname: pathname, visible: profileOpen, id: profileMenuId, label: profile.label, subtitle: profile.subtitle, triggerRef: profileTriggerRef, theme: theme, insets: insets, onClose: () => {
489
- setProfileOpen(false);
490
- }, onNavigate: onNavigate }));
491
- }
492
- export function SectionPicker({ replace = false, item, pathname, onNavigate, theme, closeLabel, insets, }) {
493
- const [open, setOpen] = useState(false);
494
- const triggerRef = useRef(null);
495
- const id = useId();
496
- const active = resolveActiveNavigation([item], pathname).subItem ?? item.children?.[0];
497
- const entries = item.children ?? [];
498
- useEffect(() => {
499
- setOpen(false);
500
- }, [pathname]);
501
- if (entries.length === 0)
502
- return null;
503
- const renderBackground = (backgroundProps) => (_jsx(View, { ...backgroundProps, role: "navigation", accessibilityLabel: item.label, children: _jsxs(FocusablePressable, { theme: theme, unfocusedBorderColor: theme.border, ref: triggerRef, ...(Platform.OS === 'web'
504
- ? { 'aria-controls': id, 'aria-haspopup': 'menu' }
505
- : {}), accessibilityRole: "button", accessibilityLabel: `${item.label}, ${active?.label ?? ''}`, accessibilityState: { expanded: open }, onPress: () => {
506
- setOpen(true);
507
- }, style: [
508
- styles.picker,
509
- {
510
- backgroundColor: theme.background,
511
- borderColor: theme.border,
512
- borderRadius: theme.radius,
513
- padding: theme.spacing,
514
- },
515
- ], children: [_jsxs(View, { children: [_jsx(Text, { style: { ...textStyle(theme), color: theme.muted }, children: item.label }), _jsx(Text, { style: { ...textStyle(theme), color: theme.text }, children: active?.label })] }), _jsx(Decorative, { children: _jsx(Text, { style: { ...textStyle(theme), color: theme.text }, children: "\u2304" }) })] }) }));
516
- return (_jsx(Menu, { renderBackground: renderBackground, entries: entries, closeLabel: closeLabel, pathname: pathname, label: item.label, visible: open, id: id, triggerRef: triggerRef, theme: theme, insets: insets, onClose: () => {
517
- setOpen(false);
518
- }, onNavigate: (href) => {
519
- announce(entries.find((entry) => entry.href === href)?.label ?? item.label);
520
- if (replace)
521
- onNavigate(href, { replace: true });
522
- else
523
- onNavigate(href);
524
- } }));
179
+ return (_jsx(Menu, { renderBackground: renderBackground, entries: profile.menu, closeLabel: props.closeLabel, pathname: props.pathname, visible: model.profileOpen, id: model.profileMenuId, label: profile.label, subtitle: profile.subtitle, triggerRef: model.profileTriggerRef, theme: props.theme, insets: model.insets, onClose: () => {
180
+ model.setProfileOpen(false);
181
+ }, onNavigate: props.onNavigate }));
525
182
  }
526
- const styles = StyleSheet.create({
527
- labelGroup: { minWidth: 0, flexShrink: 1 },
528
- menuProfile: { padding: 8 },
529
- target: {
530
- padding: NAVIGATION_BAR_DIMENSIONS.targetPadding,
531
- },
532
- railTarget: { flexDirection: 'row', alignItems: 'center' },
533
- collapsedTarget: { justifyContent: 'center' },
534
- barTarget: { flexDirection: 'column', alignItems: 'center', justifyContent: 'center', flex: 1 },
535
- bar: {
536
- flexDirection: 'column',
537
- minHeight: NAVIGATION_DIMENSIONS.nativeBar,
538
- borderTopWidth: NAVIGATION_BAR_DIMENSIONS.border,
539
- width: '100%',
540
- },
541
- rail: { borderRightWidth: 1, height: '100%' },
542
- slots: { flexShrink: 0 },
543
- barSlots: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' },
544
- slot: { minWidth: 0, flexShrink: 1 },
545
- barBrand: { flexShrink: 0 },
546
- railItems: { flex: 1 },
547
- barItems: { width: '100%', flexGrow: 0, flexShrink: 0 },
548
- barContent: { flexDirection: 'row', flexGrow: 1, minWidth: '100%' },
549
- barSection: { flex: 1, minWidth: NAVIGATION_DIMENSIONS.target },
550
- children: { marginLeft: 20, paddingLeft: 8, borderLeftWidth: 1 },
551
- profile: { position: 'relative', marginTop: 'auto', paddingTop: 8, borderTopWidth: 1 },
552
- avatar: {
553
- width: NAVIGATION_DIMENSIONS.avatar,
554
- height: NAVIGATION_DIMENSIONS.avatar,
555
- borderRadius: NAVIGATION_DIMENSIONS.avatar / 2,
556
- alignItems: 'center',
557
- justifyContent: 'center',
558
- },
559
- modal: { flex: 1, justifyContent: 'flex-end' },
560
- menu: { borderWidth: 1, padding: 8, maxHeight: '80%' },
561
- menuItem: {
562
- justifyContent: 'center',
563
- padding: 8,
564
- },
565
- picker: {
566
- flexDirection: 'row',
567
- alignItems: 'center',
568
- justifyContent: 'space-between',
569
- borderWidth: 1,
570
- width: '100%',
571
- },
572
- });
183
+ // A local binding keeps the export a plain property, so Jest can spy on it like before the split.
184
+ export const SectionPicker = Picker;
573
185
  //# sourceMappingURL=native.js.map