@aleph-alpha/ui-library 6.3.0 → 6.3.2

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.
@@ -86,8 +86,9 @@ export const Default: Story = {
86
86
  };
87
87
 
88
88
  /**
89
- * Collapsing leaves the bar untouched: the toggle stays where it was, and only
90
- * the column beneath it narrows to the icon rail.
89
+ * Collapsing hands the toggle over: while the sidebar is expanded the control
90
+ * sits in the bar, and once the column narrows to the icon rail the bar gives
91
+ * it up and the rail carries its own trigger at the top instead.
91
92
  */
92
93
  export const TogglingTheSidebar: Story = {
93
94
  globals: { mediaBreakpoint: 'default' },
@@ -98,16 +99,19 @@ export const TogglingTheSidebar: Story = {
98
99
  },
99
100
  play: async ({ canvasElement, step }) => {
100
101
  // Scenario (sidebar layout): the toggle in the bar collapses the navigation
101
- // beneath it, by pointer and from the keyboard, and the bar stays put.
102
+ // beneath it, reappears at the top of the icon rail, and works from the
103
+ // keyboard in both homes.
102
104
  const canvas = within(canvasElement);
103
105
  const nav = canvas.getByRole('navigation', { name: 'Main' });
104
106
  const bar = canvas.getByRole('banner', { name: 'Top bar' });
105
- const toggle = canvas.getByRole('button', { name: 'Toggle sidebar' });
106
107
 
107
108
  await step(
108
- 'interaction: the toggle collapses the sidebar to the icon rail',
109
+ 'interaction: the toggle in the bar collapses the sidebar to the icon rail',
109
110
  async () => {
111
+ const toggle = canvas.getByRole('button', { name: 'Toggle sidebar' });
112
+
110
113
  await expect(nav).toHaveAttribute('data-state', 'expanded');
114
+ await expect(bar.contains(toggle)).toBe(true);
111
115
 
112
116
  await userEvent.click(toggle);
113
117
 
@@ -115,29 +119,60 @@ export const TogglingTheSidebar: Story = {
115
119
  expect(nav).toHaveAttribute('data-state', 'collapsed'),
116
120
  );
117
121
  await expect(nav).toHaveAttribute('data-collapsible', 'icon');
122
+ },
123
+ );
118
124
 
119
- // Only the column below the bar collapses: the toggle is still in the
120
- // bar, so it does not travel with the navigation it controls.
121
- await expect(bar.contains(toggle)).toBe(true);
122
- await expect(nav.contains(toggle)).toBe(false);
125
+ await step(
126
+ 'interaction: the collapsed rail carries the toggle, and the bar gives it up',
127
+ async () => {
128
+ // The control moved, so it is re-queried: the one in the bar unmounted
129
+ // with its `v-if`, and the rail rendered its own through `start-action`.
130
+ const railToggle = await canvas.findByRole('button', {
131
+ name: 'Toggle sidebar',
132
+ });
133
+
134
+ await expect(nav.contains(railToggle)).toBe(true);
135
+ await expect(bar.contains(railToggle)).toBe(false);
123
136
  },
124
137
  );
125
138
 
126
139
  await step(
127
- 'keyboard: the toggle is a tab stop and expands again on Enter (WCAG 2.1.1)',
140
+ 'screen-reader: the rail toggle announces the collapsed state (WCAG 4.1.2)',
128
141
  async () => {
129
- toggle.focus();
130
- await expect(toggle).toHaveFocus();
142
+ await expect(
143
+ canvas.getByRole('button', { name: 'Toggle sidebar' }),
144
+ ).toHaveAttribute('aria-expanded', 'false');
145
+ },
146
+ );
147
+
148
+ await step(
149
+ 'keyboard: the rail toggle is focusable and Enter expands again (WCAG 2.1.1)',
150
+ async () => {
151
+ const railToggle = canvas.getByRole('button', {
152
+ name: 'Toggle sidebar',
153
+ });
154
+
155
+ railToggle.focus();
156
+ await expect(railToggle).toHaveFocus();
131
157
 
132
158
  await userEvent.keyboard('{Enter}');
133
159
 
134
160
  await waitFor(() =>
135
161
  expect(nav).toHaveAttribute('data-state', 'expanded'),
136
162
  );
163
+
164
+ // And the control is back in the bar.
165
+ const barToggle = await canvas.findByRole('button', {
166
+ name: 'Toggle sidebar',
167
+ });
168
+ await expect(bar.contains(barToggle)).toBe(true);
137
169
  },
138
170
  );
139
171
 
140
172
  await step('keyboard: and Space collapses it again', async () => {
173
+ const barToggle = canvas.getByRole('button', { name: 'Toggle sidebar' });
174
+
175
+ barToggle.focus();
141
176
  await userEvent.keyboard(' ');
142
177
 
143
178
  await waitFor(() =>
@@ -90,7 +90,9 @@ const LAYOUT_ACTIONS = `
90
90
  * The bar: wordmark and sidebar toggle leading, layout switch and profile
91
91
  * trailing. The bar is a sibling of the sidebar rather than an ancestor, so the
92
92
  * toggle takes the same `open` ref the sidebar is bound to instead of reaching
93
- * for the sidebar context.
93
+ * for the sidebar context. It carries the toggle only while the sidebar is
94
+ * expanded — collapsed, the icon rail shows its own through `start-action`,
95
+ * so the control sits on the column it reopens.
94
96
  * @param layoutActions Whether the bar carries the layout switch.
95
97
  * @returns Template string for the bar.
96
98
  */
@@ -103,11 +105,10 @@ function topBar(layoutActions = true) {
103
105
  >
104
106
  <template #logo>${WORDMARK}</template>
105
107
 
106
- <template #sidebar-toggle>
108
+ <template v-if="open" #sidebar-toggle>
107
109
  <UiSidebarTrigger
108
110
  v-model:open="open"
109
111
  tooltip="Toggle sidebar"
110
- tooltip-side="bottom"
111
112
  />
112
113
  </template>
113
114
 
@@ -205,9 +206,11 @@ function menuGroup(label: string, items: string): string {
205
206
  }
206
207
 
207
208
  /**
208
- * The navigation column. No logo and no toggle — those live in the bar above it
209
- * now. The sidebar renders an unlabelled element, so the landmark is named here
210
- * rather than baked into the pattern.
209
+ * The navigation column. No logo, and the bar above carries the toggle while
210
+ * the column is expanded; collapsed, the rail takes it over through
211
+ * `start-action`, where the trigger reads the sidebar context it sits in. The
212
+ * sidebar renders an unlabelled element, so the landmark is named here rather
213
+ * than baked into the pattern.
211
214
  */
212
215
  const SIDEBAR = `
213
216
  <UiSidebar
@@ -217,6 +220,15 @@ const SIDEBAR = `
217
220
  role="navigation"
218
221
  aria-label="Main"
219
222
  >
223
+ <template #start-action>
224
+ <!-- The menu is a list, so the trigger needs a row of its own: a bare
225
+ button directly inside the <ul> is an axe violation (list). -->
226
+ <UiSidebarMenu>
227
+ <UiSidebarMenuItem>
228
+ <UiSidebarTrigger tooltip="Toggle sidebar" />
229
+ </UiSidebarMenuItem>
230
+ </UiSidebarMenu>
231
+ </template>
220
232
  <UiSidebarContent>
221
233
  <UiSidebarMenu class="px-2">
222
234
  <template v-for="item in primaryItems" :key="item.title">
@@ -743,11 +755,13 @@ export const templateSource = `<!-- one fade for the shell: the band spans both
743
755
  >
744
756
  <!-- full-width bar; its toggle drives the same ref the sidebar is bound to,
745
757
  which is what \`v-model:open\` on the trigger is for: the bar is the
746
- sidebar's sibling, so there is no context for it to reach -->
758
+ sidebar's sibling, so there is no context for it to reach. The bar only
759
+ carries it while the sidebar is expanded — collapsed, the rail shows its
760
+ own (see \`start-action\` below) -->
747
761
  <UiTopBar ref="topBarRef" class="shrink-0" leading-width="calc(var(--sidebar-width, 16rem) - 1rem)">
748
762
  <template #logo><AppWordmark /></template>
749
- <template #sidebar-toggle>
750
- <UiSidebarTrigger v-model:open="open" tooltip="Toggle sidebar" tooltip-side="bottom" />
763
+ <template v-if="open" #sidebar-toggle>
764
+ <UiSidebarTrigger v-model:open="open" tooltip="Toggle sidebar" />
751
765
  </template>
752
766
  <!-- the avatar and the card it opens are Patterns/UiProfileCard -->
753
767
  <template #profile><ProfileCard /></template>
@@ -758,6 +772,16 @@ export const templateSource = `<!-- one fade for the shell: the band spans both
758
772
  <!-- isolate: rows raise their own icons, which would otherwise escape the
759
773
  column and land over the shell's fade -->
760
774
  <UiSidebar v-model:open="open" collapsible="icon" class="isolate" role="navigation" aria-label="Main">
775
+ <!-- rendered only while the bound \`open\` is false: the collapsed rail
776
+ carries the toggle the bar gave up, and inside the sidebar the
777
+ trigger reads the context, so it takes no binding of its own -->
778
+ <template #start-action>
779
+ <UiSidebarMenu>
780
+ <UiSidebarMenuItem>
781
+ <UiSidebarTrigger tooltip="Toggle sidebar" />
782
+ </UiSidebarMenuItem>
783
+ </UiSidebarMenu>
784
+ </template>
761
785
  <UiSidebarContent>…</UiSidebarContent>
762
786
  </UiSidebar>
763
787
 
@@ -18,6 +18,7 @@ import UiSidebarMenuButton from './UiSidebarMenuButton.vue';
18
18
  import UiSidebarMenuItem from './UiSidebarMenuItem.vue';
19
19
  import UiSidebarPopoverMenu from './UiSidebarPopoverMenu.vue';
20
20
  import UiSidebarPopoverMenuItem from './UiSidebarPopoverMenuItem.vue';
21
+ import UiSidebarTrigger from './UiSidebarTrigger.vue';
21
22
  const meta: Meta<typeof UiSidebar> = {
22
23
  title: 'Patterns/UiSidebar/UiSidebar',
23
24
  component: UiSidebar,
@@ -42,7 +43,7 @@ const meta: Meta<typeof UiSidebar> = {
42
43
  docs: {
43
44
  description: {
44
45
  component:
45
- 'Application sidebar shell wrapping the core `Sidebar` primitive. It owns its own open state — nothing needs to wrap it — and accepts `v-model:open` when a control outside it should drive the collapse. On desktop it is an ordinary flex child, so the main content column beside it is a plain `<main class="flex-1">`. Controls adjust `side` and `collapsible` for the demo layout.',
46
+ 'Application sidebar shell wrapping the core `Sidebar` primitive. It owns its own open state — nothing needs to wrap it — and accepts `v-model:open` when a control outside it should drive the collapse. On desktop it is an ordinary flex child, so the main content column beside it is a plain `<main class="flex-1">`. While a bound `open` is `false`, the `start-action` slot renders at the top of the collapsed rail — the shell in **Patterns/UiLayout** uses it to hand the toggle from the top bar to the rail. Controls adjust `side` and `collapsible` for the demo layout.',
46
47
  },
47
48
  },
48
49
  },
@@ -504,6 +505,147 @@ export const ControlledOpenState: Story = {
504
505
  },
505
506
  };
506
507
 
508
+ /**
509
+ * The collapsed rail carrying its own toggle: content passed to `start-action`
510
+ * renders at the top of the sidebar only while the bound `open` is `false`,
511
+ * and leaves again the moment it expands. Here the control outside the sidebar
512
+ * mirrors a top bar — shown only while the sidebar is open, so exactly one
513
+ * toggle exists at a time, the way the shell in **Patterns/UiLayout** wires it.
514
+ */
515
+ export const CollapsedStartAction: Story = {
516
+ render: () => ({
517
+ components: {
518
+ UiIcon,
519
+ UiSidebar,
520
+ UiSidebarContent,
521
+ UiSidebarMenu,
522
+ UiSidebarMenuButton,
523
+ UiSidebarMenuItem,
524
+ UiSidebarTrigger,
525
+ },
526
+ setup() {
527
+ const open = ref(false);
528
+ const items = [
529
+ { title: 'Home', icon: 'home' },
530
+ { title: 'Collections', icon: 'folder' },
531
+ ];
532
+
533
+ return { items, open };
534
+ },
535
+ template: `
536
+ <div class="flex h-svh w-full overflow-hidden px-2 py-3">
537
+ <UiSidebar v-model:open="open" collapsible="icon">
538
+ <template #start-action>
539
+ <!-- The menu is a list, so the trigger needs a row of its own: a
540
+ bare button directly inside the <ul> is an axe violation. -->
541
+ <UiSidebarMenu>
542
+ <UiSidebarMenuItem>
543
+ <UiSidebarTrigger tooltip="Toggle sidebar" />
544
+ </UiSidebarMenuItem>
545
+ </UiSidebarMenu>
546
+ </template>
547
+ <UiSidebarContent>
548
+ <UiSidebarMenu class="px-2">
549
+ <UiSidebarMenuItem v-for="item in items" :key="item.title">
550
+ <UiSidebarMenuButton :tooltip="item.title" :aria-label="item.title">
551
+ <template #start-icon>
552
+ <UiIcon :name="item.icon" size="s" />
553
+ </template>
554
+ {{ item.title }}
555
+ </UiSidebarMenuButton>
556
+ </UiSidebarMenuItem>
557
+ </UiSidebarMenu>
558
+ </UiSidebarContent>
559
+ </UiSidebar>
560
+
561
+ <main class="bg-background flex min-h-0 min-w-0 flex-1 flex-col p-4">
562
+ <!-- Stands in for the top bar: the control lives out here only while
563
+ the sidebar is expanded, so the rail and the bar never show a
564
+ toggle at the same time. -->
565
+ <UiSidebarTrigger
566
+ v-if="open"
567
+ v-model:open="open"
568
+ tooltip="Toggle sidebar"
569
+ />
570
+ </main>
571
+ </div>
572
+ `,
573
+ }),
574
+ play: async ({ canvasElement, step }) => {
575
+ // Scenario (UiSidebar): the collapsed rail offers the way back out through
576
+ // its `start-action` slot, and the control trades places with the one
577
+ // outside as the state flips. Ends collapsed, where it started.
578
+ const canvas = within(canvasElement);
579
+ const sidebar = canvasElement.querySelector<HTMLElement>(
580
+ '[data-slot="sidebar"]',
581
+ );
582
+
583
+ await step(
584
+ 'interaction: the collapsed rail renders the start-action content',
585
+ async () => {
586
+ await expect(sidebar).toHaveAttribute('data-state', 'collapsed');
587
+
588
+ const railToggle = canvas.getByRole('button', {
589
+ name: 'Toggle sidebar',
590
+ });
591
+ await expect(sidebar?.contains(railToggle)).toBe(true);
592
+ },
593
+ );
594
+
595
+ await step(
596
+ 'screen-reader: the rail toggle announces the collapsed state (WCAG 4.1.2)',
597
+ async () => {
598
+ await expect(
599
+ canvas.getByRole('button', { name: 'Toggle sidebar' }),
600
+ ).toHaveAttribute('aria-expanded', 'false');
601
+ },
602
+ );
603
+
604
+ await step(
605
+ 'keyboard: Enter on the rail toggle expands the sidebar, and the slot leaves with it (WCAG 2.1.1)',
606
+ async () => {
607
+ const railToggle = canvas.getByRole('button', {
608
+ name: 'Toggle sidebar',
609
+ });
610
+
611
+ railToggle.focus();
612
+ await expect(railToggle).toHaveFocus();
613
+
614
+ await userEvent.keyboard('{Enter}');
615
+
616
+ await waitFor(() =>
617
+ expect(sidebar).toHaveAttribute('data-state', 'expanded'),
618
+ );
619
+
620
+ // The slot is gone, so the only toggle left is the one outside.
621
+ const outerToggle = await canvas.findByRole('button', {
622
+ name: 'Toggle sidebar',
623
+ });
624
+ await expect(sidebar?.contains(outerToggle)).toBe(false);
625
+ await expect(outerToggle).toHaveAttribute('aria-expanded', 'true');
626
+ },
627
+ );
628
+
629
+ await step(
630
+ 'interaction: collapsing from outside brings the slot back',
631
+ async () => {
632
+ await userEvent.click(
633
+ canvas.getByRole('button', { name: 'Toggle sidebar' }),
634
+ );
635
+
636
+ await waitFor(() =>
637
+ expect(sidebar).toHaveAttribute('data-state', 'collapsed'),
638
+ );
639
+
640
+ const railToggle = await canvas.findByRole('button', {
641
+ name: 'Toggle sidebar',
642
+ });
643
+ await expect(sidebar?.contains(railToggle)).toBe(true);
644
+ },
645
+ );
646
+ },
647
+ };
648
+
507
649
  /**
508
650
  * A draggable outer border. The handle is a real control rather than a hit
509
651
  * area, so the width is reachable without a pointer: arrows nudge it by
@@ -19,6 +19,19 @@ const props = withDefaults(defineProps<UiSidebarProps>(), {
19
19
 
20
20
  const emit = defineEmits<UiSidebarEmits>();
21
21
 
22
+ defineSlots<{
23
+ /** Sidebar body — content, groups, and footer. */
24
+ default(): unknown;
25
+ /**
26
+ * Rendered at the top of the sidebar only while a bound `open` is `false`,
27
+ * so the collapsed icon rail can carry its own control — typically a
28
+ * `UiSidebarTrigger` that expands it again. Requires the controlled API
29
+ * (`v-model:open`); an uncontrolled sidebar never renders this slot, since
30
+ * the wrapper cannot see the state the core manages internally.
31
+ */
32
+ 'start-action'(): unknown;
33
+ }>();
34
+
22
35
  // `open` is only forwarded when the consumer controls it; passing `undefined`
23
36
  // through would otherwise pin the sidebar into controlled mode with no value.
24
37
  const openProps = computed(() => ({
@@ -57,6 +70,10 @@ const resizeOptions = computed(() => {
57
70
  v-bind="{ ...openProps, ...$attrs }"
58
71
  @update:open="emit('update:open', $event)"
59
72
  >
73
+ <!-- `open === false` rather than `!open`: `open` is `undefined` when the
74
+ sidebar is uncontrolled, and the slot must not sit in an expanded
75
+ sidebar just because nothing is bound. -->
76
+ <slot v-if="props.open === false" name="start-action" />
60
77
  <slot />
61
78
  </Sidebar>
62
79
  </template>
@@ -44,7 +44,9 @@ const triggerClass = computed(() =>
44
44
  cn(
45
45
  'inline-flex rounded-sm p-2',
46
46
  'group-data-[collapsible=icon]:h-8 group-data-[collapsible=icon]:w-8 group-data-[collapsible=icon]:p-0',
47
- 'text-content-on-surface-primary',
47
+ // Muted at rest so the icon does not compete with the navigation it sits
48
+ // beside; hover and focus lift it back to primary.
49
+ 'text-content-on-surface-muted hover:text-content-on-surface-primary focus:text-content-on-surface-primary',
48
50
  attrs.class as string | undefined,
49
51
  ),
50
52
  );
@@ -241,6 +241,36 @@ describe('UiSidebar', () => {
241
241
  // viewport by the `test - Mobile Breakpoint` story, where the breakpoint is
242
242
  // the browser's rather than a mocked `matchMedia`.
243
243
 
244
+ test('renders the start-action slot only while a bound open is false', async () => {
245
+ const { queryByTestId, rerender } = render(UiSidebar, {
246
+ props: { open: false, collapsible: 'icon' },
247
+ slots: {
248
+ 'start-action': '<span data-testid="rail-action">Toggle</span>',
249
+ default: 'Content',
250
+ },
251
+ });
252
+
253
+ expect(queryByTestId('rail-action')).toBeInTheDocument();
254
+
255
+ await rerender({ open: true });
256
+
257
+ expect(queryByTestId('rail-action')).not.toBeInTheDocument();
258
+ });
259
+
260
+ test('never renders the start-action slot when open is not bound', () => {
261
+ // `open` stays undefined in uncontrolled mode, so the wrapper cannot tell
262
+ // collapsed from expanded — the slot must not show up in either state.
263
+ const { queryByTestId } = render(UiSidebar, {
264
+ props: { defaultOpen: false, collapsible: 'icon' },
265
+ slots: {
266
+ 'start-action': '<span data-testid="rail-action">Toggle</span>',
267
+ default: 'Content',
268
+ },
269
+ });
270
+
271
+ expect(queryByTestId('rail-action')).not.toBeInTheDocument();
272
+ });
273
+
244
274
  test('supports configurable mobile transition duration', () => {
245
275
  const { container } = render({
246
276
  components: { UiSidebar },
@@ -861,7 +861,7 @@ export const DefaultSlotWrapper: Story = {
861
861
  async () => {
862
862
  const banner = canvas.getByRole('banner', { name: 'Top bar' });
863
863
  await expect(banner).toBeVisible();
864
- await expect(banner).toHaveClass('px-4', 'py-2.5');
864
+ await expect(banner).toHaveClass('px-[0.875rem]', 'py-2.5');
865
865
  },
866
866
  );
867
867
 
@@ -106,10 +106,11 @@ const sidebarToggleSlot = computed(() => slotName('sidebar-toggle'));
106
106
  const layoutActionsSlot = computed(() => slotName('layout-actions'));
107
107
  const profileSlot = computed(() => slotName('profile'));
108
108
 
109
- /** `py-2.5` / `px-4` are the scale steps for the specified 10px / 16px padding. */
109
+ /** `py-2.5` is the scale step for the specified 10px padding; the 14px
110
+ * horizontal padding has no scale step, so it is written out. */
110
111
  const rootClass = computed(() =>
111
112
  cn(
112
- 'flex w-full items-center gap-4 px-4 py-2.5',
113
+ 'flex w-full items-center gap-4 px-[0.875rem] py-2.5',
113
114
  'bg-background-surface-primary text-content-on-surface-primary',
114
115
  attrs.class ?? '',
115
116
  ),
@@ -39,12 +39,12 @@ describe('UiTopBar', () => {
39
39
  vi.restoreAllMocks();
40
40
  });
41
41
 
42
- test('renders a named banner landmark with the tokenized 10px/16px padding', () => {
42
+ test('renders a named banner landmark with the specified 10px/14px padding', () => {
43
43
  const { getByRole } = render(UiTopBar);
44
44
 
45
45
  const banner = getByRole('banner', { name: 'Top bar' });
46
46
  expect(banner.tagName).toBe('HEADER');
47
- expect(banner).toHaveClass('px-4', 'py-2.5');
47
+ expect(banner).toHaveClass('px-[0.875rem]', 'py-2.5');
48
48
  expect(banner).toHaveAttribute('data-mode', 'desktop');
49
49
  });
50
50
 
@@ -97,7 +97,7 @@ describe('UiTopBar', () => {
97
97
  });
98
98
 
99
99
  const banner = getByRole('banner');
100
- expect(banner).toHaveClass('border-b', 'px-4');
100
+ expect(banner).toHaveClass('border-b', 'px-[0.875rem]');
101
101
  expect(banner).toHaveAttribute('data-testid', 'bar');
102
102
  expect(banner).toHaveAttribute('id', 'app-top-bar');
103
103
  });
@@ -119,7 +119,7 @@ describe('UiTopBar', () => {
119
119
  });
120
120
 
121
121
  expect(getByRole('banner', { name: 'Top bar' })).toHaveClass(
122
- 'px-4',
122
+ 'px-[0.875rem]',
123
123
  'py-2.5',
124
124
  );
125
125
  });