@baukit/navigation 0.7.3 → 0.8.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/index.test.ts CHANGED
@@ -3,6 +3,8 @@ import { blendColors, contrastRatio, exampleTokens } from '@baukit/ui-tokens';
3
3
  import {
4
4
  getNavigationLayout,
5
5
  getNavigationBarHeight,
6
+ getNavigationBarVisualHeight,
7
+ NAVIGATION_BAR_DIMENSIONS,
6
8
  navigationReducer,
7
9
  navigationMatches,
8
10
  resolveActiveMenuEntry,
@@ -220,7 +222,51 @@ it('selects one most-specific menu route using its path length', () => {
220
222
  });
221
223
 
222
224
  it('reserves compact height for enlarged text and safe areas', () => {
223
- expect(getNavigationBarHeight(1, 24)).toBe(88);
224
- expect(getNavigationBarHeight(2, 24)).toBe(152);
225
- expect(getNavigationBarHeight(0.8)).toBe(64);
225
+ expect(getNavigationBarHeight(1, 24)).toBe(99);
226
+ expect(getNavigationBarHeight(1.3, 24)).toBe(107);
227
+ expect(getNavigationBarHeight(2, 24)).toBe(135);
228
+ expect(getNavigationBarHeight(0.8)).toBe(75);
229
+ });
230
+
231
+ it.each([1, 1.3, 2])('fits the profile and a full label at font scale %s', (fontScale) => {
232
+ const dimensions = NAVIGATION_BAR_DIMENSIONS;
233
+ const available =
234
+ getNavigationBarHeight(fontScale) -
235
+ dimensions.border -
236
+ 2 * (dimensions.targetPadding + dimensions.targetBorder) -
237
+ dimensions.spacing;
238
+ expect(available).toBeGreaterThanOrEqual(dimensions.avatar + 16 * fontScale);
239
+ expect(available).toBeGreaterThanOrEqual((dimensions.icon + 16) * fontScale);
240
+ });
241
+ it('reserves the configured label size, line height and spacing', () => {
242
+ expect(getNavigationBarHeight(1.3, 24, { spacing: 12, fontSize: 18 })).toBeCloseTo(125.3);
243
+ expect(getNavigationBarHeight(1.3, 24, { spacing: 12, lineHeight: 30 })).toBe(129.2);
244
+ });
245
+
246
+ it.each([
247
+ [0.8, 28],
248
+ [1, 28],
249
+ [1.3, 31.2],
250
+ [2, 48],
251
+ ])('shares one icon and avatar box at font scale %s', (fontScale, expected) => {
252
+ expect(getNavigationBarVisualHeight(fontScale)).toBeCloseTo(expected);
253
+ expect(getNavigationBarVisualHeight(fontScale)).toBeGreaterThanOrEqual(
254
+ NAVIGATION_BAR_DIMENSIONS.avatar,
255
+ );
256
+ expect(getNavigationBarVisualHeight(fontScale)).toBeGreaterThanOrEqual(
257
+ NAVIGATION_BAR_DIMENSIONS.icon * fontScale,
258
+ );
259
+ });
260
+
261
+ it.each(['light', 'dark'] as const)('keeps danger menu text readable in the %s theme', (scheme) => {
262
+ expect(
263
+ contrastRatio(
264
+ exampleTokens.color.status.danger[scheme],
265
+ exampleTokens.color.background.primary[scheme],
266
+ ),
267
+ ).toBeGreaterThanOrEqual(4.5);
268
+ });
269
+ it('includes measured slot height in compact height at both font scales', () => {
270
+ expect(getNavigationBarHeight(1, 24, { slotHeight: 40 })).toBe(139);
271
+ expect(getNavigationBarHeight(2, 24, { slotHeight: 64 })).toBe(199);
226
272
  });
package/src/index.ts CHANGED
@@ -18,6 +18,7 @@ export type NavigationProfileMenuEntry = {
18
18
  readonly id: string;
19
19
  readonly label: string;
20
20
  readonly disabled?: boolean;
21
+ readonly tone?: 'default' | 'danger';
21
22
  } & (
22
23
  | {
23
24
  readonly href: string;
@@ -54,19 +55,79 @@ export interface ActiveNavigation<Icon = unknown> {
54
55
  readonly subItem: NavigationSubItem<Icon> | null;
55
56
  }
56
57
 
57
- export const NAVIGATION_DIMENSIONS = {
58
+ export const NAVIGATION_BAR_DIMENSIONS = {
58
59
  avatar: 28,
60
+ icon: 24,
61
+ border: 1,
62
+ targetBorder: 3,
63
+ targetPadding: 8,
64
+ labelFontSize: 11,
65
+ labelLineHeight: 16,
66
+ spacing: 8,
67
+ } as const;
68
+
69
+ export interface NavigationSlotState {
70
+ readonly layout: 'bar' | 'rail';
71
+ readonly collapsed: boolean;
72
+ }
73
+
74
+ export interface NavigationSlots {
75
+ readonly renderBrand?: (state: NavigationSlotState) => ReactNode;
76
+ readonly renderAccessory?: (state: NavigationSlotState) => ReactNode;
77
+ readonly onBarHeightChange?: (height: number) => void;
78
+ }
79
+
80
+ export interface NavigationBarMetrics {
81
+ readonly slotHeight?: number;
82
+ readonly spacing?: number;
83
+ readonly fontSize?: number;
84
+ readonly lineHeight?: number;
85
+ }
86
+
87
+ export function getNavigationBarLineHeight(metrics: NavigationBarMetrics = {}): number {
88
+ return (
89
+ metrics.lineHeight ??
90
+ Math.ceil(
91
+ (metrics.fontSize ?? NAVIGATION_BAR_DIMENSIONS.labelFontSize) *
92
+ (NAVIGATION_BAR_DIMENSIONS.labelLineHeight / NAVIGATION_BAR_DIMENSIONS.labelFontSize),
93
+ )
94
+ );
95
+ }
96
+
97
+ export function getNavigationBarVisualHeight(fontScale = 1): number {
98
+ return Math.max(
99
+ NAVIGATION_BAR_DIMENSIONS.avatar,
100
+ NAVIGATION_BAR_DIMENSIONS.icon * Math.max(1, fontScale),
101
+ );
102
+ }
103
+
104
+ export function getNavigationBarHeight(
105
+ fontScale = 1,
106
+ bottomInset = 0,
107
+ metrics: NavigationBarMetrics = {},
108
+ ): number {
109
+ const scale = Math.max(1, fontScale);
110
+ return (
111
+ NAVIGATION_BAR_DIMENSIONS.border +
112
+ 2 * (NAVIGATION_BAR_DIMENSIONS.targetBorder + NAVIGATION_BAR_DIMENSIONS.targetPadding) +
113
+ getNavigationBarVisualHeight(scale) +
114
+ (metrics.spacing ?? NAVIGATION_BAR_DIMENSIONS.spacing) +
115
+ getNavigationBarLineHeight(metrics) * scale +
116
+ bottomInset +
117
+ (metrics.slotHeight ?? 0)
118
+ );
119
+ }
120
+
121
+ export const NAVIGATION_DIMENSIONS = {
122
+ avatar: NAVIGATION_BAR_DIMENSIONS.avatar,
59
123
  rail: 280,
60
124
  collapsedRail: 76,
61
125
  bar: 64,
126
+ nativeBar: getNavigationBarHeight(),
62
127
  target: 44,
63
128
  androidTarget: 48,
64
129
  } as const;
65
130
 
66
- export function getNavigationBarHeight(fontScale = 1, bottomInset = 0): number {
67
- return NAVIGATION_DIMENSIONS.bar * Math.max(1, fontScale) + bottomInset;
68
- }
69
-
70
131
  export function getNavigationLayout(width: number): 'bar' | 'rail' {
71
132
  return getLayoutMode(width, { medium: 600, expanded: 1024 }) === 'expanded' ? 'rail' : 'bar';
72
133
  }
@@ -10,6 +10,7 @@ const theme: NavigationTheme = {
10
10
  background: '#fff',
11
11
  text: '#111',
12
12
  muted: '#595959',
13
+ danger: '#b3261e',
13
14
  activeBackground: '#dbe9f8',
14
15
  activeText: '#005fcc',
15
16
  ancestorText: '#004799',
@@ -21,6 +22,7 @@ const theme: NavigationTheme = {
21
22
  const icon = () => <Text>●</Text>;
22
23
  let root: Root | undefined;
23
24
  let host: HTMLDivElement | undefined;
25
+
24
26
  afterEach(async () => {
25
27
  await act(() => {
26
28
  root?.unmount();
@@ -181,3 +183,46 @@ it('keeps a custom native avatar decorative with subtitle, typography and menu f
181
183
  await click(page.getByRole('button', { name: 'Close' }));
182
184
  await expect.element(trigger).toHaveFocus();
183
185
  });
186
+
187
+ it('preserves the compact brand width when accessory text wraps', async () => {
188
+ const heights = vi.fn<(height: number) => void>();
189
+ await mount(
190
+ <View style={{ height: 568, justifyContent: 'flex-end' }}>
191
+ <AppNavigation
192
+ label="Primary"
193
+ collapseLabel="Collapse"
194
+ expandLabel="Expand"
195
+ closeLabel="Close"
196
+ items={[{ id: 'home', label: 'Home', href: '/', icon }]}
197
+ width={320}
198
+ pathname="/"
199
+ theme={theme}
200
+ onNavigate={vi.fn()}
201
+ onBarHeightChange={heights}
202
+ renderBrand={() => <Text style={{ fontSize: 32 }}>Baukit</Text>}
203
+ renderAccessory={() => <Text style={{ fontSize: 32 }}>Workout active</Text>}
204
+ />
205
+ </View>,
206
+ );
207
+ const navigation = page.getByTestId('primary-navigation').element();
208
+ await act(async () => {
209
+ await new Promise<void>((resolve) => {
210
+ requestAnimationFrame(() =>
211
+ requestAnimationFrame(() => {
212
+ resolve();
213
+ }),
214
+ );
215
+ });
216
+ });
217
+ await expect
218
+ .poll(() => heights.mock.lastCall?.[0])
219
+ .toBe(navigation.getBoundingClientRect().height);
220
+ const brand = page.getByText('Baukit', { exact: true }).element();
221
+ const brandLines = document.createRange();
222
+ brandLines.selectNodeContents(brand);
223
+ expect(brandLines.getClientRects()).toHaveLength(1);
224
+ const accessory = page.getByTestId('navigation-accessory').element().getBoundingClientRect();
225
+ expect(accessory.left).toBeGreaterThanOrEqual(brand.getBoundingClientRect().right);
226
+ expect(accessory.right).toBeLessThanOrEqual(navigation.getBoundingClientRect().right);
227
+ expect(navigation.getBoundingClientRect().height).toBeGreaterThan(75);
228
+ });