@baukit/navigation 0.7.4 → 0.9.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/src/web.tsx CHANGED
@@ -26,6 +26,7 @@ import {
26
26
  resolveActiveMenuEntry,
27
27
  validateNavigation,
28
28
  type NavigationItem,
29
+ type NavigationSlots,
29
30
  type NavigationProfile,
30
31
  type NavigationProfileMenuEntry,
31
32
  } from './index.js';
@@ -80,6 +81,7 @@ interface MenuProps {
80
81
  readonly entries: readonly NavigationProfileMenuEntry[];
81
82
  readonly id: string;
82
83
  readonly label: string;
84
+ readonly closeLabel: string;
83
85
  readonly triggerRef: RefObject<HTMLButtonElement | null>;
84
86
  readonly onClose: () => void;
85
87
  readonly onNavigate: Navigate | undefined;
@@ -93,6 +95,7 @@ function Menu({
93
95
  entries,
94
96
  id,
95
97
  label,
98
+ closeLabel,
96
99
  triggerRef,
97
100
  onClose,
98
101
  onNavigate,
@@ -103,12 +106,13 @@ function Menu({
103
106
  }: MenuProps) {
104
107
  const activeEntry = resolveActiveMenuEntry(entries, pathname);
105
108
  const containerRef = useRef<HTMLDivElement>(null);
109
+ const menuRef = useRef<HTMLDivElement>(null);
106
110
  const roving = useRovingMenu({
107
111
  active: true,
108
112
  options: entries.map((entry) => ({ disabled: entry.disabled })),
109
113
  });
110
114
  useEffect(() => {
111
- const menu = containerRef.current;
115
+ const menu = menuRef.current;
112
116
  const first = menu?.querySelector<HTMLElement>('[role="menuitem"]:not([aria-disabled="true"])');
113
117
  (first ?? menu)?.focus();
114
118
  }, []);
@@ -135,10 +139,6 @@ function Menu({
135
139
  <div
136
140
  className="bk-navigation-menu"
137
141
  style={maxHeight === undefined ? undefined : { maxHeight }}
138
- id={id}
139
- role="menu"
140
- aria-label={subtitle === undefined ? label : `${label}, ${subtitle}`}
141
- tabIndex={-1}
142
142
  ref={containerRef}
143
143
  onKeyDown={(event) => {
144
144
  if (event.key === 'Escape') {
@@ -153,61 +153,76 @@ function Menu({
153
153
  }
154
154
  }}
155
155
  >
156
- {subtitle === undefined ? null : (
157
- <div className="bk-navigation-menu-profile" aria-hidden="true">
158
- <span className="bk-navigation-label">{label}</span>
159
- <span className="bk-navigation-subtitle">{subtitle}</span>
156
+ <div
157
+ className="bk-navigation-menu-content"
158
+ id={id}
159
+ role="menu"
160
+ aria-label={subtitle === undefined ? label : `${label}, ${subtitle}`}
161
+ tabIndex={-1}
162
+ ref={menuRef}
163
+ >
164
+ {subtitle === undefined ? null : (
165
+ <div className="bk-navigation-menu-profile" aria-hidden="true">
166
+ <span className="bk-navigation-label">{label}</span>
167
+ <span className="bk-navigation-subtitle">{subtitle}</span>
168
+ </div>
169
+ )}
170
+ <div className="bk-navigation-menu-items">
171
+ {entries.map((entry, index) => {
172
+ const props = {
173
+ ...roving.itemProps(index),
174
+ className: 'bk-navigation-item',
175
+ role: 'menuitem',
176
+ 'aria-disabled': entry.disabled === true ? true : undefined,
177
+ 'data-active': entry === activeEntry ? 'page' : undefined,
178
+ 'data-tone': entry.tone,
179
+ } as const;
180
+ return entry.href !== undefined ? (
181
+ <Link
182
+ {...props}
183
+ renderLink={renderLink}
184
+ href={entry.href}
185
+ key={entry.id}
186
+ aria-current={entry === activeEntry ? 'page' : undefined}
187
+ onClick={(event) => {
188
+ if (entry.disabled === true) {
189
+ event.preventDefault();
190
+ return;
191
+ }
192
+ if (!isNavigationClick(event)) return;
193
+ closeAndRestore();
194
+ follow(entry.href, event, onNavigate);
195
+ }}
196
+ onKeyDown={(event) => {
197
+ props.onKeyDown(event);
198
+ if (event.key === ' ' && entry.disabled !== true) {
199
+ event.preventDefault();
200
+ event.currentTarget.click();
201
+ }
202
+ }}
203
+ >
204
+ {entry.label}
205
+ </Link>
206
+ ) : (
207
+ <button
208
+ {...props}
209
+ type="button"
210
+ disabled={entry.disabled}
211
+ key={entry.id}
212
+ onClick={() => {
213
+ closeAndRestore();
214
+ entry.onSelect();
215
+ }}
216
+ >
217
+ {entry.label}
218
+ </button>
219
+ );
220
+ })}
160
221
  </div>
161
- )}
162
- {entries.map((entry, index) => {
163
- const props = {
164
- ...roving.itemProps(index),
165
- className: 'bk-navigation-item',
166
- role: 'menuitem',
167
- 'aria-disabled': entry.disabled === true ? true : undefined,
168
- 'data-active': entry === activeEntry ? 'page' : undefined,
169
- } as const;
170
- return entry.href !== undefined ? (
171
- <Link
172
- {...props}
173
- renderLink={renderLink}
174
- href={entry.href}
175
- key={entry.id}
176
- aria-current={entry === activeEntry ? 'page' : undefined}
177
- onClick={(event) => {
178
- if (entry.disabled === true) {
179
- event.preventDefault();
180
- return;
181
- }
182
- if (!isNavigationClick(event)) return;
183
- closeAndRestore();
184
- follow(entry.href, event, onNavigate);
185
- }}
186
- onKeyDown={(event) => {
187
- props.onKeyDown(event);
188
- if (event.key === ' ' && entry.disabled !== true) {
189
- event.preventDefault();
190
- event.currentTarget.click();
191
- }
192
- }}
193
- >
194
- {entry.label}
195
- </Link>
196
- ) : (
197
- <button
198
- {...props}
199
- type="button"
200
- disabled={entry.disabled}
201
- key={entry.id}
202
- onClick={() => {
203
- closeAndRestore();
204
- entry.onSelect();
205
- }}
206
- >
207
- {entry.label}
208
- </button>
209
- );
210
- })}
222
+ </div>
223
+ <button type="button" className="bk-navigation-menu-close" onClick={closeAndRestore}>
224
+ {closeLabel}
225
+ </button>
211
226
  </div>
212
227
  );
213
228
  }
@@ -245,6 +260,7 @@ function Avatar({
245
260
  }
246
261
 
247
262
  interface ProfileProps {
263
+ readonly closeLabel: string;
248
264
  readonly profile: NavigationProfile;
249
265
  readonly pathname: string;
250
266
  readonly collapsed: boolean;
@@ -253,6 +269,7 @@ interface ProfileProps {
253
269
  readonly renderLink: NavigationLinkRenderer | undefined;
254
270
  }
255
271
  function Profile({
272
+ closeLabel,
256
273
  profile,
257
274
  pathname,
258
275
  collapsed,
@@ -325,6 +342,7 @@ function Profile({
325
342
  )}
326
343
  {open && profile.menu !== undefined ? (
327
344
  <Menu
345
+ closeLabel={closeLabel}
328
346
  entries={profile.menu}
329
347
  id={id}
330
348
  label={profile.label}
@@ -342,7 +360,8 @@ function Profile({
342
360
  );
343
361
  }
344
362
 
345
- export interface AppNavigationProps extends CollapseProps {
363
+ export interface AppNavigationProps extends CollapseProps, NavigationSlots {
364
+ readonly closeLabel: string;
346
365
  readonly items: readonly NavigationItem<NavigationIcon>[];
347
366
  readonly profile?: NavigationProfile;
348
367
  readonly pathname: string;
@@ -366,6 +385,25 @@ export function AppNavigation(props: AppNavigationProps) {
366
385
  const collapsed = layout === 'rail' && state.collapsed;
367
386
  const { reducedMotion, resolved } = useReducedMotionPreference();
368
387
  const id = useId();
388
+ const navigationRef = useRef<HTMLElement>(null);
389
+ useLayoutEffect(() => {
390
+ const navigation = navigationRef.current;
391
+ if (layout !== 'bar' || navigation === null || props.onBarHeightChange === undefined) return;
392
+ const measure = () => {
393
+ props.onBarHeightChange?.(navigation.getBoundingClientRect().height);
394
+ };
395
+ measure();
396
+ const observer = new ResizeObserver(measure);
397
+ observer.observe(navigation);
398
+ return () => {
399
+ observer.disconnect();
400
+ };
401
+ }, [layout, props.onBarHeightChange]);
402
+ const slotState = { layout, collapsed };
403
+ const brand = props.renderBrand?.(slotState);
404
+ const accessory = props.renderAccessory?.(slotState);
405
+ const hasBrand = brand != null && typeof brand !== 'boolean';
406
+ const hasAccessory = accessory != null && typeof accessory !== 'boolean';
369
407
  const visible = items.flatMap((item) =>
370
408
  layout === 'rail' && !collapsed && state.openIds.includes(item.id)
371
409
  ? [item, ...(item.children ?? [])]
@@ -385,6 +423,14 @@ export function AppNavigation(props: AppNavigationProps) {
385
423
  });
386
424
  return (
387
425
  <nav
426
+ ref={navigationRef}
427
+ style={
428
+ layout === 'bar'
429
+ ? {
430
+ gridTemplateColumns: `repeat(${String(Math.max(1, items.length + (profile === undefined ? 0 : 1)))}, minmax(${String(NAVIGATION_DIMENSIONS.target)}px, 1fr))`,
431
+ }
432
+ : undefined
433
+ }
388
434
  aria-label={label}
389
435
  className={`bk-navigation ${props.className ?? ''}`}
390
436
  data-testid="primary-navigation"
@@ -392,6 +438,12 @@ export function AppNavigation(props: AppNavigationProps) {
392
438
  data-collapsed={collapsed}
393
439
  data-motion={!resolved ? 'unresolved' : reducedMotion ? 'reduced' : 'standard'}
394
440
  >
441
+ {!hasBrand && !hasAccessory ? null : (
442
+ <div className="bk-navigation-slots">
443
+ {!hasBrand ? null : <div className="bk-navigation-brand">{brand}</div>}
444
+ {!hasAccessory ? null : <div className="bk-navigation-accessory">{accessory}</div>}
445
+ </div>
446
+ )}
395
447
  {layout === 'rail' ? (
396
448
  <button
397
449
  className="bk-navigation-toggle"
@@ -497,6 +549,7 @@ export function AppNavigation(props: AppNavigationProps) {
497
549
  </div>
498
550
  {profile === undefined ? null : (
499
551
  <Profile
552
+ closeLabel={props.closeLabel}
500
553
  profile={profile}
501
554
  pathname={pathname}
502
555
  collapsed={collapsed}
@@ -557,12 +610,19 @@ function useSectionMenuHeight(open: boolean, triggerRef: RefObject<HTMLButtonEle
557
610
  }
558
611
 
559
612
  export interface SectionPickerProps {
613
+ readonly closeLabel: string;
560
614
  readonly item: NavigationItem<NavigationIcon>;
561
615
  readonly pathname: string;
562
616
  readonly onNavigate?: Navigate;
563
617
  readonly renderLink?: NavigationLinkRenderer;
564
618
  }
565
- export function SectionPicker({ item, pathname, onNavigate, renderLink }: SectionPickerProps) {
619
+ export function SectionPicker({
620
+ item,
621
+ pathname,
622
+ onNavigate,
623
+ renderLink,
624
+ closeLabel,
625
+ }: SectionPickerProps) {
566
626
  const [open, setOpen] = useState(false);
567
627
  const triggerRef = useRef<HTMLButtonElement>(null);
568
628
  const maxHeight = useSectionMenuHeight(open, triggerRef);
@@ -593,6 +653,7 @@ export function SectionPicker({ item, pathname, onNavigate, renderLink }: Sectio
593
653
  </button>
594
654
  {open ? (
595
655
  <Menu
656
+ closeLabel={closeLabel}
596
657
  entries={entries}
597
658
  maxHeight={maxHeight}
598
659
  id={id}