@aleph-alpha/ui-library 1.25.1 → 1.25.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.
Files changed (31) hide show
  1. package/dist/system/components/UiBadge/index.d.ts +1 -1
  2. package/dist/system/components/UiProgress/index.d.ts +1 -1
  3. package/dist/system/components/UiSelect/UiSelect.vue.d.ts +6 -4
  4. package/dist/system/components/UiSelect/UiSelectContent.vue.d.ts +1 -0
  5. package/dist/system/components/UiSelect/UiSelectItem.vue.d.ts +8 -2
  6. package/dist/system/components/UiSelect/UiSelectTrigger.vue.d.ts +1 -0
  7. package/dist/system/components/UiSelect/UiSelectValue.vue.d.ts +3 -0
  8. package/dist/system/components/UiSelect/context.d.ts +28 -0
  9. package/dist/system/components/UiSelect/types.d.ts +32 -8
  10. package/dist/system/lib.js +16139 -15924
  11. package/package.json +4 -3
  12. package/src/components/UiBadge/index.ts +1 -1
  13. package/src/components/UiProgress/index.ts +1 -1
  14. package/src/components/UiSelect/UiSelect.stories.ts +137 -0
  15. package/src/components/UiSelect/UiSelect.vue +161 -6
  16. package/src/components/UiSelect/UiSelectContent.vue +22 -0
  17. package/src/components/UiSelect/UiSelectItem.vue +83 -1
  18. package/src/components/UiSelect/UiSelectTrigger.vue +51 -0
  19. package/src/components/UiSelect/UiSelectValue.vue +37 -1
  20. package/src/components/UiSelect/__tests__/UiSelect.test.ts +564 -0
  21. package/src/components/UiSelect/context.ts +41 -0
  22. package/src/components/UiSelect/types.ts +32 -8
  23. package/src/components/core/navigation-menu/NavigationMenuContent.vue +1 -1
  24. package/src/components/core/navigation-menu/NavigationMenuLink.vue +7 -1
  25. package/src/components/core/navigation-menu/NavigationMenuViewport.vue +1 -1
  26. package/src/components/core/navigation-menu/index.ts +10 -1
  27. package/src/patterns/UiSidebar/UiSidebarChatHistoryItem.vue +6 -1
  28. package/src/patterns/UiSidebar/UiSidebarMenuButton.stories.ts +117 -2
  29. package/src/patterns/UiSidebar/UiSidebarMenuButton.vue +4 -0
  30. package/src/patterns/UiSidebar/UiSidebarMenuItem.stories.ts +119 -1
  31. package/src/patterns/UiSidebar/UiSidebarMenuSubItem.vue +3 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aleph-alpha/ui-library",
3
- "version": "1.25.1",
3
+ "version": "1.25.2",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "main": "dist/system/lib.js",
@@ -72,11 +72,12 @@
72
72
  "vite-plugin-css-injected-by-js": "^3.5.2",
73
73
  "vite-plugin-dts": "^4.0.0",
74
74
  "vitest": "^3.0.0",
75
+ "vue-router": "4.6.4",
75
76
  "vue-tsc": "^2.2.12",
76
77
  "wait-on": "9.0.3",
77
- "@aleph-alpha/eslint-config-frontend": "0.5.0",
78
78
  "@aleph-alpha/prettier-config-frontend": "0.4.0",
79
- "@aleph-alpha/tsconfig-frontend": "0.5.0"
79
+ "@aleph-alpha/tsconfig-frontend": "0.5.0",
80
+ "@aleph-alpha/eslint-config-frontend": "0.5.0"
80
81
  },
81
82
  "peerDependencies": {
82
83
  "@unocss/preset-wind4": ">=66.0.0",
@@ -1,2 +1,2 @@
1
1
  export { default as UiBadge } from './UiBadge.vue';
2
- export type * from './types.ts';
2
+ export type * from './types';
@@ -1,2 +1,2 @@
1
1
  export { default as UiProgress } from './UiProgress.vue';
2
- export type * from './types.ts';
2
+ export type * from './types';
@@ -411,6 +411,143 @@ const value = ref('banana')
411
411
  </UiSelect>
412
412
  </template>`;
413
413
 
414
+ const multipleTemplateSource = `<script setup lang="ts">
415
+ import { ref } from 'vue'
416
+ import {
417
+ UiSelect,
418
+ UiSelectTrigger,
419
+ UiSelectContent,
420
+ UiSelectItem,
421
+ UiSelectValue,
422
+ } from '@aleph-alpha/ui-library'
423
+
424
+ const value = ref<string[]>([])
425
+ </script>
426
+
427
+ <template>
428
+ <!--
429
+ The trigger keeps a fixed width. Selected values render as a
430
+ comma-separated list and truncate with an ellipsis instead of
431
+ stretching the trigger when more items are selected.
432
+ -->
433
+ <UiSelect v-model="value" :multiple="true">
434
+ <UiSelectTrigger class="w-[220px]">
435
+ <UiSelectValue placeholder="Select fruits" />
436
+ </UiSelectTrigger>
437
+ <UiSelectContent>
438
+ <UiSelectItem value="apple">Apple</UiSelectItem>
439
+ <UiSelectItem value="banana">Banana</UiSelectItem>
440
+ <UiSelectItem value="orange">Orange</UiSelectItem>
441
+ <UiSelectItem value="grape">Grape</UiSelectItem>
442
+ <UiSelectItem value="mango">Mango</UiSelectItem>
443
+ </UiSelectContent>
444
+ </UiSelect>
445
+ </template>`;
446
+
447
+ /**
448
+ * Multi-select mode. Pass `multiple` to switch the trigger into a popover-backed
449
+ * listbox. The popover stays open between clicks so users can toggle several
450
+ * items, the selected values are emitted as an array via `update:modelValue`,
451
+ * and the trigger renders the selected items as a comma-separated list that
452
+ * truncates with an ellipsis when it exceeds the trigger's width.
453
+ */
454
+ export const Multiple: Story = {
455
+ render: () => ({
456
+ components: { UiSelect, UiSelectTrigger, UiSelectContent, UiSelectItem, UiSelectValue },
457
+ setup() {
458
+ const value = ref<string[]>([]);
459
+ return { value };
460
+ },
461
+ template: `
462
+ <div class="flex flex-col gap-3">
463
+ <UiSelect v-model="value" :multiple="true">
464
+ <UiSelectTrigger class="w-[220px]">
465
+ <UiSelectValue placeholder="Select fruits" />
466
+ </UiSelectTrigger>
467
+ <UiSelectContent>
468
+ <UiSelectItem value="apple">Apple</UiSelectItem>
469
+ <UiSelectItem value="banana">Banana</UiSelectItem>
470
+ <UiSelectItem value="orange">Orange</UiSelectItem>
471
+ <UiSelectItem value="grape">Grape</UiSelectItem>
472
+ <UiSelectItem value="mango">Mango</UiSelectItem>
473
+ </UiSelectContent>
474
+ </UiSelect>
475
+ <div class="text-xs text-content-on-surface-secondary">Selected: {{ value.join(', ') || '—' }}</div>
476
+ </div>
477
+ `,
478
+ }),
479
+ parameters: {
480
+ docs: {
481
+ source: {
482
+ code: multipleTemplateSource,
483
+ },
484
+ },
485
+ },
486
+ };
487
+
488
+ const multipleWithDefaultTemplateSource = `<script setup lang="ts">
489
+ import { ref } from 'vue'
490
+ import {
491
+ UiSelect,
492
+ UiSelectTrigger,
493
+ UiSelectContent,
494
+ UiSelectItem,
495
+ UiSelectValue,
496
+ } from '@aleph-alpha/ui-library'
497
+
498
+ const value = ref<string[]>(['banana', 'mango'])
499
+ </script>
500
+
501
+ <template>
502
+ <UiSelect v-model="value" :multiple="true">
503
+ <UiSelectTrigger class="w-[260px]">
504
+ <UiSelectValue placeholder="Select fruits" />
505
+ </UiSelectTrigger>
506
+ <UiSelectContent>
507
+ <UiSelectItem value="apple">Apple</UiSelectItem>
508
+ <UiSelectItem value="banana">Banana</UiSelectItem>
509
+ <UiSelectItem value="orange">Orange</UiSelectItem>
510
+ <UiSelectItem value="grape">Grape</UiSelectItem>
511
+ <UiSelectItem value="mango">Mango</UiSelectItem>
512
+ </UiSelectContent>
513
+ </UiSelect>
514
+ </template>`;
515
+
516
+ /**
517
+ * Multi-select with a pre-selected list of values. The trigger renders the
518
+ * matching item labels even before the popover is opened.
519
+ */
520
+ export const MultipleWithDefaultValue: Story = {
521
+ render: () => ({
522
+ components: { UiSelect, UiSelectTrigger, UiSelectContent, UiSelectItem, UiSelectValue },
523
+ setup() {
524
+ const value = ref<string[]>(['banana', 'mango']);
525
+ return { value };
526
+ },
527
+ template: `
528
+ <UiSelect v-model="value" :multiple="true">
529
+ <UiSelectTrigger class="w-[260px]">
530
+ <UiSelectValue placeholder="Select fruits" />
531
+ </UiSelectTrigger>
532
+ <UiSelectContent>
533
+ <UiSelectItem value="apple">Apple</UiSelectItem>
534
+ <UiSelectItem value="banana">Banana</UiSelectItem>
535
+ <UiSelectItem value="orange">Orange</UiSelectItem>
536
+ <UiSelectItem value="grape">Grape</UiSelectItem>
537
+ <UiSelectItem value="mango">Mango</UiSelectItem>
538
+ </UiSelectContent>
539
+ </UiSelect>
540
+ `,
541
+ }),
542
+ parameters: {
543
+ docs: {
544
+ source: {
545
+ code: multipleWithDefaultTemplateSource,
546
+ },
547
+ },
548
+ },
549
+ };
550
+
414
551
  /**
415
552
  * Select with a pre-selected default value.
416
553
  */
@@ -1,6 +1,10 @@
1
1
  <script setup lang="ts">
2
2
  import type { AcceptableValue } from 'reka-ui';
3
+ import { computed, ref } from 'vue';
3
4
  import { Select as ShadcnSelect } from '@/components/core/select';
5
+ import { Listbox as ShadcnListbox } from '@/components/core/listbox';
6
+ import { Popover as ShadcnPopover } from '@/components/core/popover';
7
+ import { provideUiSelectContext } from './context';
4
8
  import type { UiSelectProps, UiSelectEmits } from './types';
5
9
 
6
10
  defineOptions({
@@ -16,32 +20,183 @@
16
20
  required: false,
17
21
  name: undefined,
18
22
  dir: 'ltr',
23
+ multiple: false,
19
24
  });
20
25
 
21
26
  const emit = defineEmits<UiSelectEmits>();
22
27
 
23
- // reka-ui's AcceptableValue can be string | number | boolean | Record
24
- // Our API restricts to string only for type safety
25
- function handleModelValueUpdate(value: AcceptableValue) {
28
+ // -------- Single-select (default) --------
29
+
30
+ function handleSingleModelValueUpdate(value: AcceptableValue) {
26
31
  if (typeof value === 'string') {
27
32
  emit('update:modelValue', value);
28
33
  }
29
34
  }
35
+
36
+ const singleModelValue = computed(() =>
37
+ typeof props.modelValue === 'string' ? props.modelValue : undefined,
38
+ );
39
+ const singleDefaultValue = computed(() =>
40
+ typeof props.defaultValue === 'string' ? props.defaultValue : undefined,
41
+ );
42
+
43
+ // -------- Multi-select (popover + listbox) --------
44
+
45
+ function toArray(value: string | string[] | undefined): string[] {
46
+ if (Array.isArray(value)) return value;
47
+ if (typeof value === 'string' && value.length > 0) return [value];
48
+ return [];
49
+ }
50
+
51
+ // Multi-select is treated as **controlled** when the consumer passes
52
+ // `modelValue` (even an empty array); otherwise we manage state
53
+ // internally seeded from `defaultValue`. This mirrors how Vue
54
+ // components are conventionally controlled and prevents internal
55
+ // state from drifting away from the prop when the parent doesn't
56
+ // echo `update:modelValue` back.
57
+ const isMultiControlled = computed(() => props.modelValue !== undefined);
58
+
59
+ // Uncontrolled storage. Only mutated when `isMultiControlled` is false.
60
+ const internalMultiValue = ref<string[]>(toArray(props.defaultValue));
61
+
62
+ // Effective selection used by the listbox, the trigger label, the
63
+ // hidden form inputs, and the provided context. When controlled it
64
+ // tracks the prop directly so the UI never leads the parent state.
65
+ const multiSelectedValues = computed<string[]>(() =>
66
+ isMultiControlled.value ? toArray(props.modelValue) : internalMultiValue.value,
67
+ );
68
+
69
+ function normalizeMultiValue(value: AcceptableValue | AcceptableValue[]): string[] {
70
+ if (Array.isArray(value)) {
71
+ return value.filter((v): v is string => typeof v === 'string');
72
+ }
73
+ if (typeof value === 'string') {
74
+ return [value];
75
+ }
76
+ return [];
77
+ }
78
+
79
+ function handleMultiModelValueUpdate(value: AcceptableValue | AcceptableValue[]) {
80
+ const next = normalizeMultiValue(value);
81
+ // In controlled mode we **only emit** — the parent owns the value
82
+ // and is responsible for echoing it back via `modelValue`. Mutating
83
+ // `internalMultiValue` here would cause a brief desync where the
84
+ // UI moves but the prop hasn't.
85
+ if (!isMultiControlled.value) {
86
+ internalMultiValue.value = next;
87
+ }
88
+ emit('update:modelValue', next);
89
+ }
90
+
91
+ // Open state for the multi-select popover. Mirrors the controlled `open`
92
+ // prop when provided, otherwise stays uncontrolled internally.
93
+ const internalOpen = ref<boolean>(props.defaultOpen ?? false);
94
+ const popoverOpen = computed({
95
+ get() {
96
+ return props.open ?? internalOpen.value;
97
+ },
98
+ set(value: boolean) {
99
+ internalOpen.value = value;
100
+ emit('update:open', value);
101
+ },
102
+ });
103
+
104
+ // -------- Form integration (multi-select) --------
105
+
106
+ // Hidden `<input type="hidden">` values rendered for FormData
107
+ // submission. We only emit hidden inputs when `name` is set; without
108
+ // a name they wouldn't contribute to form data anyway.
109
+ const formHiddenValues = computed<string[]>(() =>
110
+ props.multiple && props.name ? multiSelectedValues.value : [],
111
+ );
112
+
113
+ // -------- Context provided to subcomponents --------
114
+
115
+ const itemLabels = ref<Map<string, string>>(new Map());
116
+
117
+ // Use getters instead of evaluating `props.multiple` once at setup
118
+ // time — descendants choose between the single-select and
119
+ // multi-select render paths via `ctx.mode`, and that decision must
120
+ // track the current prop value, not a stale snapshot. Without
121
+ // getters, toggling `:multiple` after mount would leave children
122
+ // rendering the original branch.
123
+ provideUiSelectContext({
124
+ get mode() {
125
+ return props.multiple ? 'multiple' : 'single';
126
+ },
127
+ get multiple() {
128
+ return props.multiple
129
+ ? {
130
+ selectedValues: multiSelectedValues,
131
+ itemLabels,
132
+ }
133
+ : undefined;
134
+ },
135
+ });
30
136
  </script>
31
137
 
32
138
  <template>
33
139
  <ShadcnSelect
34
- :model-value="props.modelValue"
35
- :default-value="props.defaultValue"
140
+ v-if="!props.multiple"
141
+ :model-value="singleModelValue"
142
+ :default-value="singleDefaultValue"
36
143
  :default-open="props.defaultOpen"
37
144
  :open="props.open"
38
145
  :disabled="props.disabled"
39
146
  :required="props.required"
40
147
  :name="props.name"
41
148
  :dir="props.dir"
42
- @update:model-value="handleModelValueUpdate"
149
+ @update:model-value="handleSingleModelValueUpdate"
43
150
  @update:open="emit('update:open', $event)"
44
151
  >
45
152
  <slot />
46
153
  </ShadcnSelect>
154
+
155
+ <ShadcnListbox
156
+ v-else
157
+ :model-value="multiSelectedValues"
158
+ :multiple="true"
159
+ :disabled="props.disabled"
160
+ :dir="props.dir"
161
+ selection-behavior="toggle"
162
+ @update:model-value="handleMultiModelValueUpdate"
163
+ >
164
+ <ShadcnPopover v-model:open="popoverOpen">
165
+ <slot />
166
+ </ShadcnPopover>
167
+
168
+ <!--
169
+ Form integration for multi-select.
170
+
171
+ Native `<select multiple name="...">` submits one form entry per
172
+ selected option (same key, repeated values). We mirror that by
173
+ rendering one hidden input per selected value when `name` is set
174
+ so multi-select participates in standard FormData / form POSTs.
175
+
176
+ For `required`, we render a visually-hidden but focusable text
177
+ input that participates in native form validation. We can't use
178
+ `display:none`/`visibility:hidden` (browsers skip validating
179
+ those), so we use the standard sr-only clip-rect trick. The
180
+ input intentionally has no `name` so it doesn't appear in
181
+ submitted form data — its only job is to fire constraint
182
+ validation on submit when no value is selected.
183
+ -->
184
+ <input
185
+ v-for="value in formHiddenValues"
186
+ :key="value"
187
+ type="hidden"
188
+ :name="props.name"
189
+ :value="value"
190
+ />
191
+ <input
192
+ v-if="props.required"
193
+ type="text"
194
+ :value="multiSelectedValues.length > 0 ? 'has-value' : ''"
195
+ :required="true"
196
+ :disabled="props.disabled"
197
+ tabindex="-1"
198
+ aria-hidden="true"
199
+ class="pointer-events-none absolute m-[-1px] size-px overflow-hidden border-0 p-0 whitespace-nowrap [clip:rect(0,0,0,0)]"
200
+ />
201
+ </ShadcnListbox>
47
202
  </template>
@@ -1,5 +1,8 @@
1
1
  <script setup lang="ts">
2
2
  import { SelectContent as ShadcnSelectContent } from '@/components/core/select';
3
+ import { PopoverContent as ShadcnPopoverContent } from '@/components/core/popover';
4
+ import { ListboxContent as ShadcnListboxContent } from '@/components/core/listbox';
5
+ import { useUiSelectContext } from './context';
3
6
  import type { UiSelectContentProps, UiSelectContentEmits } from './types';
4
7
 
5
8
  defineOptions({
@@ -14,10 +17,13 @@
14
17
  });
15
18
 
16
19
  const emit = defineEmits<UiSelectContentEmits>();
20
+
21
+ const ctx = useUiSelectContext();
17
22
  </script>
18
23
 
19
24
  <template>
20
25
  <ShadcnSelectContent
26
+ v-if="ctx.mode === 'single'"
21
27
  :side="props.side"
22
28
  :side-offset="props.sideOffset"
23
29
  :align="props.align"
@@ -27,4 +33,20 @@
27
33
  >
28
34
  <slot />
29
35
  </ShadcnSelectContent>
36
+
37
+ <ShadcnPopoverContent
38
+ v-else
39
+ data-slot="select-content"
40
+ :side="props.side"
41
+ :side-offset="props.sideOffset"
42
+ :align="props.align"
43
+ :force-mount="true"
44
+ class="min-w-(--reka-popper-anchor-width) w-(--reka-popper-anchor-width) max-w-(--reka-popper-available-width) p-1 data-[state=closed]:invisible data-[state=closed]:pointer-events-none"
45
+ @escape-key-down="emit('escapeKeyDown', $event)"
46
+ @pointer-down-outside="emit('pointerDownOutside', $event)"
47
+ >
48
+ <ShadcnListboxContent>
49
+ <slot />
50
+ </ShadcnListboxContent>
51
+ </ShadcnPopoverContent>
30
52
  </template>
@@ -1,5 +1,12 @@
1
1
  <script setup lang="ts">
2
+ import { onBeforeUnmount, onMounted, ref, watch } from 'vue';
2
3
  import { SelectItem as ShadcnSelectItem } from '@/components/core/select';
4
+ import {
5
+ ListboxItem as ShadcnListboxItem,
6
+ ListboxItemIndicator as ShadcnListboxItemIndicator,
7
+ } from '@/components/core/listbox';
8
+ import { UiIcon } from '@/components/UiIcon';
9
+ import { useUiSelectContext } from './context';
3
10
  import type { UiSelectItemProps } from './types';
4
11
 
5
12
  defineOptions({
@@ -10,10 +17,85 @@
10
17
  disabled: false,
11
18
  textValue: undefined,
12
19
  });
20
+
21
+ const ctx = useUiSelectContext();
22
+
23
+ // ----- Multi-select label registration -----
24
+ // We expose the rendered slot's text to the trigger via the shared
25
+ // `itemLabels` registry so `UiSelectValue` can render comma-separated
26
+ // labels for the selected values without consumers having to provide a
27
+ // separate label map.
28
+ const labelEl = ref<HTMLElement | null>(null);
29
+
30
+ function readLabel(): string {
31
+ return labelEl.value?.textContent?.trim() || props.textValue || props.value;
32
+ }
33
+
34
+ function syncLabel() {
35
+ if (ctx.mode !== 'multiple' || !ctx.multiple) return;
36
+ ctx.multiple.itemLabels.value.set(props.value, readLabel());
37
+ }
38
+
39
+ onMounted(syncLabel);
40
+
41
+ // When `props.value` changes, the entry registered under the OLD key
42
+ // becomes orphaned — `syncLabel()` only writes to the NEW key, so
43
+ // without an explicit delete the registry would accumulate stale
44
+ // mappings. That manifests two ways for consumers:
45
+ // 1. The trigger can render a stale label for a value that's no
46
+ // longer a real option (because `UiSelectValue` happily reads
47
+ // whatever the registry still has under that key).
48
+ // 2. For dynamic option lists (e.g. `<UiSelectItem v-for>` driven
49
+ // by remote data), the map grows unbounded.
50
+ // Use the watcher's `(next, prev)` signature to drop the previous
51
+ // key before re-registering under the new one.
52
+ watch(
53
+ () => props.value,
54
+ (next, prev) => {
55
+ if (ctx.mode !== 'multiple' || !ctx.multiple) return;
56
+ if (prev !== undefined && prev !== next) {
57
+ ctx.multiple.itemLabels.value.delete(prev);
58
+ }
59
+ ctx.multiple.itemLabels.value.set(next, readLabel());
60
+ },
61
+ );
62
+
63
+ // `textValue` only affects the label string under the same key,
64
+ // so a plain re-sync is sufficient (no stale key to clean up).
65
+ watch(() => props.textValue, syncLabel);
66
+
67
+ onBeforeUnmount(() => {
68
+ if (ctx.mode !== 'multiple' || !ctx.multiple) return;
69
+ ctx.multiple.itemLabels.value.delete(props.value);
70
+ });
13
71
  </script>
14
72
 
15
73
  <template>
16
- <ShadcnSelectItem :value="props.value" :disabled="props.disabled" :text-value="props.textValue">
74
+ <ShadcnSelectItem
75
+ v-if="ctx.mode === 'single'"
76
+ :value="props.value"
77
+ :disabled="props.disabled"
78
+ :text-value="props.textValue"
79
+ >
17
80
  <slot />
18
81
  </ShadcnSelectItem>
82
+
83
+ <ShadcnListboxItem
84
+ v-else
85
+ :value="props.value"
86
+ :disabled="props.disabled"
87
+ :text-value="props.textValue"
88
+ class="focus:bg-hover-default focus:text-foreground data-[highlighted]:bg-hover-default data-[highlighted]:text-foreground relative flex w-full cursor-default items-center gap-2 rounded-sm py-1.5 pr-8 pl-2 text-sm outline-hidden select-none data-[disabled]:pointer-events-none data-[disabled]:opacity-50"
89
+ >
90
+ <span ref="labelEl" class="flex items-center gap-2">
91
+ <slot />
92
+ </span>
93
+ <span class="absolute right-2 flex size-3.5 items-center justify-center">
94
+ <ShadcnListboxItemIndicator>
95
+ <slot name="indicator-icon">
96
+ <UiIcon name="check" :size="16" />
97
+ </slot>
98
+ </ShadcnListboxItemIndicator>
99
+ </span>
100
+ </ShadcnListboxItem>
19
101
  </template>
@@ -1,5 +1,10 @@
1
1
  <script setup lang="ts">
2
+ import { computed } from 'vue';
2
3
  import { SelectTrigger as ShadcnSelectTrigger } from '@/components/core/select';
4
+ import { PopoverTrigger as ShadcnPopoverTrigger } from '@/components/core/popover';
5
+ import { UiIcon } from '@/components/UiIcon';
6
+ import { cn } from '@/lib/utils';
7
+ import { useUiSelectContext } from './context';
3
8
  import type { UiSelectTriggerProps } from './types';
4
9
 
5
10
  defineOptions({
@@ -12,14 +17,47 @@
12
17
  size: 'default',
13
18
  });
14
19
 
20
+ const ctx = useUiSelectContext();
21
+
22
+ // Mirror reka-ui SelectTrigger's `data-placeholder` attribute on our
23
+ // popover-backed multi-select trigger. Single-select gets this from
24
+ // SelectTrigger itself (when no value is selected). Without it the
25
+ // trigger keeps `bg-accent` and looks "active"/blue even when empty,
26
+ // because the `data-[placeholder]:bg-background-input-default` rule
27
+ // in `multiTriggerClasses` never matches.
28
+ const isMultiPlaceholder = computed(
29
+ () => ctx.mode === 'multiple' && (ctx.multiple?.selectedValues.value.length ?? 0) === 0,
30
+ );
31
+
15
32
  // shadcn trigger styles target only SVG icons (`[&_svg]...`) but our icons are spans (`data-slot="icon"`).
16
33
  // Add equivalent sizing/behavior + rotate-on-open for the chevron.
17
34
  const uiIconTriggerClasses =
18
35
  '[&_[data-slot=icon]]:pointer-events-none [&_[data-slot=icon]]:shrink-0 [&_[data-slot=icon]]:size-4 [&_[data-slot=icon]]:transition-transform [&_[data-slot=icon]]:duration-200 [&[data-state=open]_[data-slot=icon]]:rotate-180';
36
+
37
+ // In multi-select mode, we render a Popover instead of reka-ui's Select, so
38
+ // we have to recreate the SelectTrigger button (incl. the styling that
39
+ // SelectTrigger.vue applies under the hood) and append the chevron icon
40
+ // ourselves to keep the visual parity with single-select.
41
+ //
42
+ // Two deliberate differences from single-select:
43
+ // - No `w-fit`: the trigger width must NOT grow with the selected
44
+ // labels. Consumers control the width via `min-w-*` / `w-*` classes
45
+ // (see stories). `max-w-full` keeps it from overflowing its parent.
46
+ // - No `whitespace-nowrap` on the trigger itself — we move single-line
47
+ // + ellipsis behavior down to the UiSelectValue span so it can
48
+ // truncate inside the flex row instead of forcing the button wider.
49
+ const multiTriggerClasses = computed(() =>
50
+ cn(
51
+ "border-input data-[placeholder]:text-muted-foreground data-[placeholder]:bg-background-input-default [&_svg:not([class*='text-'])]:text-muted-foreground focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive flex max-w-full items-center justify-between gap-2 rounded-md border bg-accent px-3 py-2 text-sm shadow-xs transition-[color,box-shadow] outline-none focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50 *:data-[slot=select-value]:min-w-0 *:data-[slot=select-value]:flex-1 *:data-[slot=select-value]:truncate [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
52
+ props.size === 'default' ? 'h-9' : 'h-8',
53
+ uiIconTriggerClasses,
54
+ ),
55
+ );
19
56
  </script>
20
57
 
21
58
  <template>
22
59
  <ShadcnSelectTrigger
60
+ v-if="ctx.mode === 'single'"
23
61
  :disabled="props.disabled"
24
62
  :size="props.size"
25
63
  :aria-label="props.ariaLabel"
@@ -27,4 +65,17 @@
27
65
  >
28
66
  <slot />
29
67
  </ShadcnSelectTrigger>
68
+
69
+ <ShadcnPopoverTrigger
70
+ v-else
71
+ data-slot="select-trigger"
72
+ :data-size="props.size"
73
+ :data-placeholder="isMultiPlaceholder ? '' : undefined"
74
+ :disabled="props.disabled"
75
+ :aria-label="props.ariaLabel"
76
+ :class="multiTriggerClasses"
77
+ >
78
+ <slot />
79
+ <UiIcon name="chevron-down" :size="16" class="opacity-50" />
80
+ </ShadcnPopoverTrigger>
30
81
  </template>
@@ -1,5 +1,7 @@
1
1
  <script setup lang="ts">
2
+ import { computed } from 'vue';
2
3
  import { SelectValue as ShadcnSelectValue } from '@/components/core/select';
4
+ import { useUiSelectContext } from './context';
3
5
  import type { UiSelectValueProps } from './types';
4
6
 
5
7
  defineOptions({
@@ -9,10 +11,44 @@
9
11
  const props = withDefaults(defineProps<UiSelectValueProps>(), {
10
12
  placeholder: undefined,
11
13
  });
14
+
15
+ const ctx = useUiSelectContext();
16
+
17
+ // Multi-select: derive the displayed text from the registry of item
18
+ // labels populated by `UiSelectItem`. We keep the selection order
19
+ // (i.e. the order returned by Listbox) for consistent output.
20
+ const displayText = computed(() => {
21
+ if (ctx.mode !== 'multiple' || !ctx.multiple) return '';
22
+ const labels = ctx.multiple.itemLabels.value;
23
+ return ctx.multiple.selectedValues.value.map((value) => labels.get(value) ?? value).join(', ');
24
+ });
25
+
26
+ const hasSelection = computed(
27
+ () => ctx.mode === 'multiple' && (ctx.multiple?.selectedValues.value.length ?? 0) > 0,
28
+ );
12
29
  </script>
13
30
 
14
31
  <template>
15
- <ShadcnSelectValue :placeholder="props.placeholder">
32
+ <ShadcnSelectValue v-if="ctx.mode === 'single'" :placeholder="props.placeholder">
16
33
  <slot />
17
34
  </ShadcnSelectValue>
35
+
36
+ <!--
37
+ Multi-select display: render as a block-level truncating span.
38
+ `truncate` (= `overflow:hidden; text-overflow:ellipsis; white-space:nowrap`)
39
+ works only when this element participates in the parent flex row with
40
+ `min-w-0` + `flex-1` (applied via `UiSelectTrigger` via the
41
+ `*:data-[slot=select-value]:*` selectors), which is why we avoid flex
42
+ layout on the span itself here.
43
+ -->
44
+ <span
45
+ v-else
46
+ data-slot="select-value"
47
+ :data-placeholder="hasSelection ? undefined : ''"
48
+ class="block truncate"
49
+ >
50
+ <slot v-if="$slots.default" :selected="ctx.multiple?.selectedValues.value ?? []" />
51
+ <template v-else-if="hasSelection">{{ displayText }}</template>
52
+ <template v-else>{{ props.placeholder }}</template>
53
+ </span>
18
54
  </template>