@baukit/navigation 0.18.2 → 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.
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
@@ -0,0 +1,436 @@
1
+ import {
2
+ useCallback,
3
+ useEffect,
4
+ useLayoutEffect,
5
+ useRef,
6
+ useState,
7
+ type ReactNode,
8
+ type RefObject,
9
+ } from 'react';
10
+ import {
11
+ Modal,
12
+ Platform,
13
+ ScrollView,
14
+ Text,
15
+ View,
16
+ type GestureResponderEvent,
17
+ type ViewStyle,
18
+ } from 'react-native';
19
+ import {
20
+ asFocusTarget,
21
+ hostElement,
22
+ type OverlayBackgroundProps,
23
+ type RovingMenuKeyEvent,
24
+ useOverlayA11y,
25
+ useReducedMotionPreference,
26
+ useRovingMenu,
27
+ } from '@baukit/a11y-core';
28
+ import {
29
+ NAVIGATION_DIMENSIONS,
30
+ resolveActiveMenuEntry,
31
+ type NavigationProfileMenuEntry,
32
+ } from './index.js';
33
+ import {
34
+ Decorative,
35
+ FocusablePressable,
36
+ isModifiedPress,
37
+ webLink,
38
+ type RovingItemProps,
39
+ } from './native-primitives.js';
40
+ import { styles } from './native-styles.js';
41
+ import {
42
+ MENU_EDGE,
43
+ MENU_WIDTH,
44
+ menuModalPadding,
45
+ subtitleTextStyle,
46
+ textStyle,
47
+ type Navigate,
48
+ type NavigationInsets,
49
+ type NavigationTheme,
50
+ } from './native-theme.js';
51
+
52
+ export interface MenuProps {
53
+ readonly subtitle?: string | undefined;
54
+ readonly renderBackground: (props: OverlayBackgroundProps) => ReactNode;
55
+ readonly entries: readonly NavigationProfileMenuEntry[];
56
+ readonly label: string;
57
+ readonly closeLabel: string;
58
+ readonly pathname: string;
59
+ readonly visible: boolean;
60
+ readonly onClose: () => void;
61
+ readonly onNavigate: Navigate;
62
+ readonly triggerRef: RefObject<View | null>;
63
+ readonly theme: NavigationTheme;
64
+ readonly id: string;
65
+ readonly insets?: NavigationInsets | undefined;
66
+ }
67
+
68
+ type Selection = () => void;
69
+
70
+ /** Holds the chosen entry's action until the closing Modal is gone. */
71
+ function usePendingSelection(visible: boolean): {
72
+ readonly isPending: () => boolean;
73
+ readonly begin: (selection: Selection) => void;
74
+ readonly finish: () => void;
75
+ } {
76
+ const pending = useRef<Selection | null>(null);
77
+ const finish = useCallback(() => {
78
+ const selection = pending.current;
79
+ pending.current = null;
80
+ selection?.();
81
+ }, []);
82
+ useEffect(() => {
83
+ if (visible) {
84
+ pending.current = null;
85
+ return;
86
+ }
87
+ if (Platform.OS === 'ios' || pending.current === null) return;
88
+ // Android removes an unanimated Modal in the close commit and has no onDismiss event.
89
+ const frame = requestAnimationFrame(finish);
90
+ return () => {
91
+ cancelAnimationFrame(frame);
92
+ };
93
+ }, [visible, finish]);
94
+ useEffect(
95
+ () => () => {
96
+ pending.current = null;
97
+ },
98
+ [],
99
+ );
100
+ const isPending = useCallback(() => pending.current !== null, []);
101
+ const begin = useCallback((selection: Selection) => {
102
+ pending.current = selection;
103
+ }, []);
104
+ return { isPending, begin, finish };
105
+ }
106
+
107
+ interface Presentation {
108
+ readonly webPresented: boolean;
109
+ readonly deferFocus: (task: () => void) => { cancel: () => void };
110
+ readonly onShow: () => void;
111
+ }
112
+
113
+ /** Delays initial focus until the Modal reports it is on screen. */
114
+ function usePresentation(visible: boolean): Presentation {
115
+ const pendingFocus = useRef<(() => void) | null>(null);
116
+ const presented = useRef(false);
117
+ const [webPresented, setWebPresented] = useState(false);
118
+ if (!visible && webPresented) setWebPresented(false);
119
+ useEffect(() => {
120
+ if (!visible) presented.current = false;
121
+ }, [visible]);
122
+ const deferFocus = useCallback((task: () => void) => {
123
+ if (presented.current) task();
124
+ else pendingFocus.current = task;
125
+ return {
126
+ cancel: () => {
127
+ pendingFocus.current = null;
128
+ presented.current = false;
129
+ },
130
+ };
131
+ }, []);
132
+ const onShow = (): void => {
133
+ if (!visible) return;
134
+ presented.current = true;
135
+ const focus = pendingFocus.current;
136
+ pendingFocus.current = null;
137
+ focus?.();
138
+ if (Platform.OS === 'web') setWebPresented(true);
139
+ };
140
+ return { webPresented, deferFocus, onShow };
141
+ }
142
+
143
+ function menuPosition(trigger: HTMLElement): ViewStyle {
144
+ const rect = trigger.getBoundingClientRect();
145
+ const width = Math.min(MENU_WIDTH, window.innerWidth - 2 * MENU_EDGE);
146
+ const below = window.innerHeight - rect.bottom;
147
+ const above = rect.top >= below;
148
+ return {
149
+ position: 'absolute',
150
+ left: Math.max(MENU_EDGE, Math.min(rect.left, window.innerWidth - width - MENU_EDGE)),
151
+ width,
152
+ maxHeight: Math.max(NAVIGATION_DIMENSIONS.target, (above ? rect.top : below) - 2 * MENU_EDGE),
153
+ ...(above
154
+ ? { bottom: window.innerHeight - rect.top + MENU_EDGE }
155
+ : { top: rect.bottom + MENU_EDGE }),
156
+ };
157
+ }
158
+
159
+ function useWebMenuPosition(
160
+ visible: boolean,
161
+ triggerRef: RefObject<View | null>,
162
+ ): ViewStyle | undefined {
163
+ const [webPosition, setWebPosition] = useState<ViewStyle>();
164
+ useLayoutEffect(() => {
165
+ if (!visible || Platform.OS !== 'web') return;
166
+ function positionMenu(): void {
167
+ const trigger = hostElement(triggerRef);
168
+ if (trigger instanceof HTMLElement) setWebPosition(menuPosition(trigger));
169
+ }
170
+ positionMenu();
171
+ window.addEventListener('resize', positionMenu);
172
+ window.addEventListener('scroll', positionMenu, true);
173
+ return () => {
174
+ window.removeEventListener('resize', positionMenu);
175
+ window.removeEventListener('scroll', positionMenu, true);
176
+ };
177
+ }, [visible, triggerRef]);
178
+ return webPosition;
179
+ }
180
+
181
+ function containsTarget(host: object | null, target: Node): boolean {
182
+ return host instanceof Node && host.contains(target);
183
+ }
184
+
185
+ function useWebOutsidePress(
186
+ visible: boolean,
187
+ refs: { readonly container: RefObject<View | null>; readonly trigger: RefObject<View | null> },
188
+ onClose: () => void,
189
+ ): void {
190
+ const { container, trigger } = refs;
191
+ useEffect(() => {
192
+ if (!visible || Platform.OS !== 'web') return;
193
+ function outside(event: PointerEvent): void {
194
+ const target = event.target;
195
+ if (!(target instanceof Node)) return;
196
+ if (containsTarget(hostElement(container), target)) return;
197
+ if (containsTarget(hostElement(trigger), target)) return;
198
+ onClose();
199
+ }
200
+ document.addEventListener('pointerdown', outside);
201
+ return () => {
202
+ document.removeEventListener('pointerdown', outside);
203
+ };
204
+ }, [visible, container, trigger, onClose]);
205
+ }
206
+
207
+ function MenuHeader({
208
+ label,
209
+ subtitle,
210
+ theme,
211
+ }: {
212
+ readonly label: string;
213
+ readonly subtitle: string;
214
+ readonly theme: NavigationTheme;
215
+ }): ReactNode {
216
+ return (
217
+ <Decorative>
218
+ <View style={styles.menuProfile}>
219
+ <Text numberOfLines={1} style={{ ...textStyle(theme), color: theme.text }}>
220
+ {label}
221
+ </Text>
222
+ <Text numberOfLines={1} style={subtitleTextStyle(theme)}>
223
+ {subtitle}
224
+ </Text>
225
+ </View>
226
+ </Decorative>
227
+ );
228
+ }
229
+
230
+ function entryTextColor(
231
+ theme: NavigationTheme,
232
+ entry: NavigationProfileMenuEntry,
233
+ active: boolean,
234
+ ): string {
235
+ if (entry.tone === 'danger') return theme.danger;
236
+ return active ? theme.activeText : theme.text;
237
+ }
238
+
239
+ interface MenuEntryProps {
240
+ readonly entry: NavigationProfileMenuEntry;
241
+ readonly active: boolean;
242
+ readonly roving: RovingItemProps;
243
+ readonly theme: NavigationTheme;
244
+ readonly onSelect: (entry: NavigationProfileMenuEntry, event: GestureResponderEvent) => void;
245
+ }
246
+ function MenuEntry({ entry, active, roving, theme, onSelect }: MenuEntryProps): ReactNode {
247
+ return (
248
+ <FocusablePressable
249
+ theme={theme}
250
+ selected={active}
251
+ {...{ dataSet: { active: active ? 'page' : 'false', tone: entry.tone } }}
252
+ {...roving}
253
+ accessibilityRole="menuitem"
254
+ accessibilityLabel={entry.label}
255
+ accessibilityState={{ disabled: entry.disabled === true, selected: active }}
256
+ disabled={entry.disabled}
257
+ {...(entry.href === undefined ? {} : webLink(entry.href, active))}
258
+ onPress={(event) => {
259
+ onSelect(entry, event);
260
+ }}
261
+ style={[
262
+ styles.menuItem,
263
+ {
264
+ paddingHorizontal: theme.spacing,
265
+ backgroundColor:
266
+ active && entry.tone !== 'danger' ? theme.activeBackground : theme.background,
267
+ },
268
+ ]}
269
+ >
270
+ <Text style={{ color: entryTextColor(theme, entry, active), ...textStyle(theme, active) }}>
271
+ {entry.label}
272
+ </Text>
273
+ </FocusablePressable>
274
+ );
275
+ }
276
+
277
+ function selectionFor(entry: NavigationProfileMenuEntry, onNavigate: Navigate): Selection {
278
+ const href = entry.href;
279
+ if (href === undefined) return entry.onSelect;
280
+ return () => {
281
+ onNavigate(href);
282
+ };
283
+ }
284
+
285
+ function motionState(resolved: boolean, reducedMotion: boolean): string {
286
+ if (!resolved) return 'unresolved';
287
+ return reducedMotion ? 'reduced' : 'standard';
288
+ }
289
+
290
+ interface MenuPanelProps {
291
+ readonly menu: MenuProps;
292
+ readonly containerRef: RefObject<View | null>;
293
+ readonly closeRef: RefObject<View | null>;
294
+ readonly containerProps: object;
295
+ readonly roving: ReturnType<typeof useRovingMenu>;
296
+ readonly activeEntry: NavigationProfileMenuEntry | null;
297
+ readonly webPosition: ViewStyle | undefined;
298
+ readonly onCloseAndRestore: () => void;
299
+ readonly onSelect: MenuEntryProps['onSelect'];
300
+ }
301
+
302
+ function MenuPanel({
303
+ menu,
304
+ containerRef,
305
+ closeRef,
306
+ containerProps,
307
+ roving,
308
+ activeEntry,
309
+ webPosition,
310
+ onCloseAndRestore,
311
+ onSelect,
312
+ }: MenuPanelProps): ReactNode {
313
+ const { theme, subtitle, label, closeLabel } = menu;
314
+ const { reducedMotion, resolved } = useReducedMotionPreference();
315
+ return (
316
+ <View
317
+ {...containerProps}
318
+ {...{
319
+ onKeyDown: (event: RovingMenuKeyEvent) => {
320
+ if (event.nativeEvent.key === 'Escape') {
321
+ event.preventDefault();
322
+ onCloseAndRestore();
323
+ }
324
+ if (Platform.OS === 'web' && event.nativeEvent.key === 'Tab') onCloseAndRestore();
325
+ },
326
+ }}
327
+ ref={containerRef}
328
+ accessibilityViewIsModal={Platform.OS !== 'web'}
329
+ accessibilityLabel={subtitle === undefined ? label : `${label}, ${subtitle}`}
330
+ role="menu"
331
+ nativeID={menu.id}
332
+ testID="navigation-menu"
333
+ style={[
334
+ styles.menu,
335
+ {
336
+ backgroundColor: theme.background,
337
+ borderColor: theme.border,
338
+ borderRadius: theme.radius,
339
+ },
340
+ Platform.OS === 'web' && webPosition,
341
+ ]}
342
+ {...{ dataSet: { motion: motionState(resolved, reducedMotion) } }}
343
+ >
344
+ {subtitle === undefined ? null : (
345
+ <MenuHeader label={label} subtitle={subtitle} theme={theme} />
346
+ )}
347
+ <ScrollView>
348
+ {menu.entries.map((entry, index) => (
349
+ <MenuEntry
350
+ key={entry.id}
351
+ entry={entry}
352
+ active={entry === activeEntry}
353
+ roving={roving.itemProps(index)}
354
+ theme={theme}
355
+ onSelect={onSelect}
356
+ />
357
+ ))}
358
+ </ScrollView>
359
+ <FocusablePressable
360
+ theme={theme}
361
+ ref={closeRef}
362
+ accessibilityRole="button"
363
+ accessibilityLabel={closeLabel}
364
+ onPress={onCloseAndRestore}
365
+ style={styles.menuItem}
366
+ >
367
+ <Text style={{ ...textStyle(theme), color: theme.text }}>{closeLabel}</Text>
368
+ </FocusablePressable>
369
+ </View>
370
+ );
371
+ }
372
+
373
+ export function Menu(props: MenuProps): ReactNode {
374
+ const { entries, visible, onClose, triggerRef } = props;
375
+ const activeEntry = resolveActiveMenuEntry(entries, props.pathname);
376
+ const containerRef = useRef<View>(null);
377
+ const closeRef = useRef<View>(null);
378
+ const selection = usePendingSelection(visible);
379
+ const presentation = usePresentation(visible);
380
+ const roving = useRovingMenu({
381
+ active: visible,
382
+ options: entries.map((entry) => ({
383
+ disabled: entry.disabled,
384
+ selected: entry === activeEntry,
385
+ })),
386
+ });
387
+ const { containerProps, backgroundProps } = useOverlayA11y({
388
+ active: visible && (Platform.OS !== 'web' || presentation.webPresented),
389
+ deferFocus: presentation.deferFocus,
390
+ containerRef,
391
+ triggerRef,
392
+ initialFocusRef: roving.activeIndex === null ? closeRef : roving.initialFocusRef,
393
+ onEscape: onClose,
394
+ });
395
+ const webPosition = useWebMenuPosition(visible, triggerRef);
396
+ useWebOutsidePress(visible, { container: containerRef, trigger: triggerRef }, onClose);
397
+ function closeAndRestore(): void {
398
+ onClose();
399
+ if (Platform.OS === 'web') asFocusTarget(triggerRef)?.focus();
400
+ }
401
+ function select(entry: NavigationProfileMenuEntry, event: GestureResponderEvent): void {
402
+ if (isModifiedPress(event) || selection.isPending()) return;
403
+ event.preventDefault();
404
+ selection.begin(selectionFor(entry, props.onNavigate));
405
+ closeAndRestore();
406
+ }
407
+ return (
408
+ <>
409
+ {props.renderBackground(backgroundProps)}
410
+ <Modal
411
+ visible={visible}
412
+ transparent
413
+ animationType="none"
414
+ onRequestClose={onClose}
415
+ onDismiss={() => {
416
+ if (!visible) selection.finish();
417
+ }}
418
+ onShow={presentation.onShow}
419
+ >
420
+ <View style={[styles.modal, menuModalPadding(props.insets)]}>
421
+ <MenuPanel
422
+ menu={props}
423
+ containerRef={containerRef}
424
+ closeRef={closeRef}
425
+ containerProps={containerProps}
426
+ roving={roving}
427
+ activeEntry={activeEntry}
428
+ webPosition={webPosition}
429
+ onCloseAndRestore={closeAndRestore}
430
+ onSelect={select}
431
+ />
432
+ </View>
433
+ </Modal>
434
+ </>
435
+ );
436
+ }