@xds/vue 1.1.0 → 1.2.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 (151) hide show
  1. package/dist/Breadcrumb.js +4 -0
  2. package/dist/Breadcrumb.vue.d.ts +6 -0
  3. package/dist/Breadcrumb.vue_vue_type_script_setup_true_lang.js +47 -0
  4. package/dist/Checkbox.vue_vue_type_script_setup_true_lang.js +11 -3
  5. package/dist/Combobox.js +4 -0
  6. package/dist/Combobox.vue.d.ts +22 -0
  7. package/dist/Combobox.vue_vue_type_script_setup_true_lang.js +140 -0
  8. package/dist/Link.js +4 -0
  9. package/dist/Link.vue.d.ts +27 -0
  10. package/dist/Link.vue_vue_type_script_setup_true_lang.js +62 -0
  11. package/dist/Pagination.js +4 -0
  12. package/dist/Pagination.vue.d.ts +9 -0
  13. package/dist/Pagination.vue_vue_type_script_setup_true_lang.js +74 -0
  14. package/dist/Popover.js +4 -0
  15. package/dist/Popover.vue.d.ts +26 -0
  16. package/dist/Popover.vue_vue_type_script_setup_true_lang.js +81 -0
  17. package/dist/Select.vue.d.ts +3 -1
  18. package/dist/Select.vue_vue_type_script_setup_true_lang.js +41 -17
  19. package/dist/Table.js +4 -0
  20. package/dist/Table.vue.d.ts +10 -0
  21. package/dist/Table.vue_vue_type_script_setup_true_lang.js +61 -0
  22. package/dist/TimePicker.js +4 -0
  23. package/dist/TimePicker.vue.d.ts +10 -0
  24. package/dist/TimePicker.vue_vue_type_script_setup_true_lang.js +168 -0
  25. package/dist/ToastQueue.js +4 -0
  26. package/dist/ToastQueue.vue.d.ts +9 -0
  27. package/dist/ToastQueue.vue_vue_type_script_setup_true_lang.js +42 -0
  28. package/dist/base.d.ts +13 -0
  29. package/dist/base.js +14 -0
  30. package/dist/index.d.ts +13 -4
  31. package/dist/index.js +10 -1
  32. package/dist/preview.d.ts +20 -3
  33. package/dist/preview.js +29 -6
  34. package/eject/files/Accordion.vue +89 -0
  35. package/eject/files/AccordionPanel.vue +47 -0
  36. package/eject/files/Avatar.vue +41 -0
  37. package/eject/files/AvatarStack.vue +46 -0
  38. package/eject/files/Badge.vue +29 -0
  39. package/eject/files/Banner.vue +65 -0
  40. package/eject/files/Breadcrumb.vue +43 -0
  41. package/eject/files/Button.vue +93 -0
  42. package/eject/files/Card.vue +85 -0
  43. package/eject/files/Checkbox.vue +104 -0
  44. package/eject/files/Chip.vue +32 -0
  45. package/eject/files/ChipBar.vue +25 -0
  46. package/eject/files/Combobox.vue +175 -0
  47. package/eject/files/CountBadge.vue +45 -0
  48. package/eject/files/DatePicker.vue +223 -0
  49. package/eject/files/DateRangePicker.vue +317 -0
  50. package/eject/files/Dialog.vue +105 -0
  51. package/eject/files/Divider.vue +15 -0
  52. package/eject/files/EmptyState.vue +28 -0
  53. package/eject/files/Icon.vue +34 -0
  54. package/eject/files/IconButton.vue +71 -0
  55. package/eject/files/Link.vue +48 -0
  56. package/eject/files/ListRow.vue +33 -0
  57. package/eject/files/MediaCard.vue +109 -0
  58. package/eject/files/Menu.vue +181 -0
  59. package/eject/files/Pagination.vue +88 -0
  60. package/eject/files/PlaceRow.vue +54 -0
  61. package/eject/files/Popover.vue +119 -0
  62. package/eject/files/ProgressBar.vue +34 -0
  63. package/eject/files/RadioGroup.vue +126 -0
  64. package/eject/files/SearchField.vue +129 -0
  65. package/eject/files/SectionHeader.vue +37 -0
  66. package/eject/files/SegmentedControl.vue +81 -0
  67. package/eject/files/Select.vue +269 -0
  68. package/eject/files/Sidebar.vue +101 -0
  69. package/eject/files/SidebarRailButton.vue +31 -0
  70. package/eject/files/Skeleton.vue +27 -0
  71. package/eject/files/Slider.vue +126 -0
  72. package/eject/files/Spinner.vue +36 -0
  73. package/eject/files/Switch.vue +88 -0
  74. package/eject/files/Table.vue +77 -0
  75. package/eject/files/Tabs.vue +72 -0
  76. package/eject/files/Text.vue +22 -0
  77. package/eject/files/TextField.vue +190 -0
  78. package/eject/files/Thumbnail.vue +62 -0
  79. package/eject/files/TimePicker.vue +224 -0
  80. package/eject/files/Toast.vue +83 -0
  81. package/eject/files/ToastQueue.vue +54 -0
  82. package/eject/files/Tooltip.vue +28 -0
  83. package/eject/files/TooltipOverlay.vue +86 -0
  84. package/eject/files/accordion.css +89 -0
  85. package/eject/files/accordion.ts +67 -0
  86. package/eject/files/avatar-stack.ts +57 -0
  87. package/eject/files/avatar.ts +42 -0
  88. package/eject/files/badge.ts +30 -0
  89. package/eject/files/banner.ts +71 -0
  90. package/eject/files/base.css +38 -0
  91. package/eject/files/breadcrumb.css +51 -0
  92. package/eject/files/breadcrumb.ts +42 -0
  93. package/eject/files/button.css +241 -0
  94. package/eject/files/button.ts +106 -0
  95. package/eject/files/card.ts +58 -0
  96. package/eject/files/checkbox.ts +52 -0
  97. package/eject/files/chip-bar.ts +16 -0
  98. package/eject/files/chip.ts +35 -0
  99. package/eject/files/combobox.css +269 -0
  100. package/eject/files/combobox.ts +85 -0
  101. package/eject/files/count-badge.ts +32 -0
  102. package/eject/files/date-picker.css +375 -0
  103. package/eject/files/date-picker.ts +280 -0
  104. package/eject/files/date-range-picker.ts +338 -0
  105. package/eject/files/dialog.css +139 -0
  106. package/eject/files/dialog.ts +15 -0
  107. package/eject/files/divider.ts +5 -0
  108. package/eject/files/empty-state.ts +22 -0
  109. package/eject/files/icon.ts +36 -0
  110. package/eject/files/link.css +64 -0
  111. package/eject/files/link.ts +63 -0
  112. package/eject/files/list-row.ts +66 -0
  113. package/eject/files/media-card.ts +66 -0
  114. package/eject/files/menu.css +192 -0
  115. package/eject/files/menu.ts +84 -0
  116. package/eject/files/overlay.css +40 -0
  117. package/eject/files/pagination.css +106 -0
  118. package/eject/files/pagination.ts +68 -0
  119. package/eject/files/parts.ts +17 -0
  120. package/eject/files/popover.css +101 -0
  121. package/eject/files/popover.ts +37 -0
  122. package/eject/files/progress-bar.ts +26 -0
  123. package/eject/files/radio-group.css +184 -0
  124. package/eject/files/radio-group.ts +30 -0
  125. package/eject/files/section-header.ts +32 -0
  126. package/eject/files/segmented-control.ts +33 -0
  127. package/eject/files/select.css +347 -0
  128. package/eject/files/select.ts +77 -0
  129. package/eject/files/sidebar.ts +75 -0
  130. package/eject/files/skeleton.ts +51 -0
  131. package/eject/files/slider.css +160 -0
  132. package/eject/files/slider.ts +80 -0
  133. package/eject/files/spinner.ts +22 -0
  134. package/eject/files/switch.ts +45 -0
  135. package/eject/files/table.css +107 -0
  136. package/eject/files/table.ts +84 -0
  137. package/eject/files/tabs.css +120 -0
  138. package/eject/files/tabs.ts +37 -0
  139. package/eject/files/text-field.css +225 -0
  140. package/eject/files/text-field.ts +64 -0
  141. package/eject/files/text.ts +38 -0
  142. package/eject/files/thumbnail.ts +51 -0
  143. package/eject/files/time-picker.ts +113 -0
  144. package/eject/files/toast-queue.ts +64 -0
  145. package/eject/files/toast.ts +31 -0
  146. package/eject/files/tooltip.css +111 -0
  147. package/eject/files/tooltip.ts +21 -0
  148. package/eject/manifest.json +1122 -0
  149. package/package.json +11 -4
  150. package/styles.css +1066 -168
  151. package/tailwind.css +1295 -218
@@ -0,0 +1,126 @@
1
+ <!--
2
+ Ejected from `@xds/vue` 1.2.0 by `xds eject`. This copy is the app's own: change it
3
+ as you need. `xds eject --list` says which ejected files differ from what was copied.
4
+ -->
5
+ <script setup lang="ts" generic="Value extends string = string">
6
+ import { formDefault, keepChecked, useControllable, useFormReset } from "@xds/vue/base";
7
+ import { computed, useId, useTemplateRef } from "vue";
8
+ // One choice among a few, applied later with the form it belongs to. For a choice that takes
9
+ // effect at once among two to four options, a SegmentedControl is the usual pick.
10
+ //
11
+ // The chosen option has a dot and a thicker ring, so the choice does not depend on color. The
12
+ // options are native radio buttons, hidden and drawn beside, in a group named by the legend:
13
+ // the arrow keys move between them, and the error is tied to the group and announced.
14
+ import type { RadioGroupBaseProps } from "./radio-group.ts";
15
+
16
+ export type { RadioOption } from "./radio-group.ts";
17
+
18
+ export type RadioGroupProps<Value extends string = string> = RadioGroupBaseProps<Value>;
19
+
20
+ // `v-model:value`, written out rather than with defineModel: a choice is never undefined, and
21
+ // defineModel would say that it can be. See useControllable too.
22
+ const {
23
+ value: given,
24
+ defaultValue,
25
+ label,
26
+ options,
27
+ disabled = false,
28
+ error,
29
+ name,
30
+ } = defineProps<RadioGroupProps<Value>>();
31
+ const emit = defineEmits<{ "update:value": [value: Value] }>();
32
+
33
+ const id = useId();
34
+ const labelId = `${id}-label`;
35
+ const errorId = `${id}-error`;
36
+ const invalid = computed(() => error !== undefined);
37
+ const group = useTemplateRef("group");
38
+ const { value, set, controlled, reset } = useControllable<Value | undefined>(
39
+ "value",
40
+ () => given,
41
+ () => defaultValue,
42
+ (next) => {
43
+ if (next !== undefined) emit("update:value", next);
44
+ },
45
+ );
46
+ useFormReset(() => group.value, controlled, reset);
47
+
48
+ // The description is inside the pressable row, so the row's text would name the radio
49
+ // together with it. The label alone names it; the description describes it.
50
+ const describedBy = (index: number, description: string | undefined) =>
51
+ [description ? `${id}-${index}-description` : undefined, invalid.value ? errorId : undefined]
52
+ .filter(Boolean)
53
+ .join(" ") || undefined;
54
+
55
+ function choose(option: Value) {
56
+ set(option);
57
+ // The browser moved the dot already; where the owner kept the old value, it goes back, on
58
+ // every radio of the group.
59
+ for (const input of group.value?.querySelectorAll("input") ?? []) {
60
+ keepChecked(input, () => input.value === value.value);
61
+ }
62
+ }
63
+ </script>
64
+
65
+ <template>
66
+ <div
67
+ ref="group"
68
+ role="radiogroup"
69
+ :aria-labelledby="labelId"
70
+ :aria-describedby="invalid ? errorId : undefined"
71
+ :aria-invalid="invalid ? 'true' : undefined"
72
+ :aria-disabled="disabled ? 'true' : undefined"
73
+ aria-orientation="vertical"
74
+ class="{{prefix}}-radio-group"
75
+ >
76
+ <span :id="labelId" class="{{prefix}}-radio-group__label">{{ label }}</span>
77
+ <div class="{{prefix}}-radio-group__options">
78
+ <label
79
+ v-for="(option, index) in options"
80
+ :key="option.value"
81
+ class="{{prefix}}-radio-group__option"
82
+ >
83
+ <!-- aria-invalid is on each radio as well as the group, so it is heard on the radio
84
+ that has focus. Radios group by name, which is what makes the arrow keys move
85
+ between them. -->
86
+ <input
87
+ type="radio"
88
+ class="{{prefix}}-radio-group__input sr-only"
89
+ :name="name ?? id"
90
+ :value="option.value"
91
+ :checked="option.value === value"
92
+ v-bind="formDefault({ defaultChecked: option.value === value })"
93
+ :disabled="disabled"
94
+ :aria-labelledby="`${id}-${index}`"
95
+ :aria-describedby="describedBy(index, option.description)"
96
+ :aria-invalid="invalid ? 'true' : undefined"
97
+ @change="choose(option.value)"
98
+ />
99
+ <span class="{{prefix}}-radio-group__row">
100
+ <span class="{{prefix}}-radio-group__circle-line">
101
+ <span class="{{prefix}}-radio-group__circle"
102
+ ><span class="{{prefix}}-radio-group__dot"
103
+ /></span>
104
+ </span>
105
+ <span class="{{prefix}}-radio-group__words">
106
+ <span :id="`${id}-${index}`" class="{{prefix}}-radio-group__text">{{
107
+ option.label
108
+ }}</span>
109
+ <span
110
+ v-if="option.description"
111
+ :id="`${id}-${index}-description`"
112
+ class="{{prefix}}-radio-group__description"
113
+ >
114
+ {{ option.description }}
115
+ </span>
116
+ </span>
117
+ </span>
118
+ </label>
119
+ </div>
120
+ <!-- The region is there before the error is, so that the error is announced when it
121
+ appears. Empty, it takes no room. -->
122
+ <div aria-live="polite">
123
+ <span v-if="invalid" :id="errorId" class="{{prefix}}-radio-group__error">{{ error }}</span>
124
+ </div>
125
+ </div>
126
+ </template>
@@ -0,0 +1,129 @@
1
+ <!--
2
+ Ejected from `@xds/vue` 1.2.0 by `xds eject`. This copy is the app's own: change it
3
+ as you need. `xds eject --list` says which ejected files differ from what was copied.
4
+ -->
5
+ <script setup lang="ts">
6
+ import {
7
+ focusable,
8
+ formDefault,
9
+ keepValue,
10
+ useControllable,
11
+ useFormReset,
12
+ useLabels,
13
+ } from "@xds/vue/base";
14
+ import { computed, getCurrentInstance, useTemplateRef } from "vue";
15
+ import Icon from "./Icon.vue";
16
+ // A text field for a query: TextField's box as a pill, with a search icon in front and, while
17
+ // there is a value, a control that clears it. Escape clears too. A read-only field has neither.
18
+ //
19
+ // Use it to filter or search what is on the screen. For a value that is saved with a form,
20
+ // use a TextField, which has a visible label.
21
+ import { classes, rootPart } from "./parts.ts";
22
+ import type { SearchFieldBaseProps } from "./text-field.ts";
23
+
24
+ export type SearchFieldProps = SearchFieldBaseProps;
25
+
26
+ // `v-model:value`, written out rather than with defineModel: see useControllable.
27
+ const {
28
+ value: given,
29
+ defaultValue = "",
30
+ readOnly = false,
31
+ label,
32
+ placeholder,
33
+ disabled = false,
34
+ clearLabel,
35
+ } = defineProps<SearchFieldProps>();
36
+ const emit = defineEmits<{
37
+ "update:value": [value: string];
38
+ /** The query, on Enter or on the search key of a software keyboard. */
39
+ submit: [value: string];
40
+ // On the input, which the root holds.
41
+ focus: [event: FocusEvent];
42
+ blur: [event: FocusEvent];
43
+ }>();
44
+ const labels = useLabels();
45
+ const input = useTemplateRef<HTMLInputElement>("input");
46
+ const { value, set, controlled, reset } = useControllable(
47
+ "value",
48
+ () => given,
49
+ () => defaultValue,
50
+ (next) => emit("update:value", next),
51
+ );
52
+ useFormReset(() => input.value, controlled, reset);
53
+ defineExpose(focusable(() => input.value));
54
+ // Whether there is a query that Escape and the clear control can clear.
55
+ const clearable = computed(() => value.value !== "" && !disabled && !readOnly);
56
+ const instance = getCurrentInstance();
57
+
58
+ function edit(event: Event) {
59
+ const field = event.currentTarget as HTMLInputElement;
60
+ set(field.value);
61
+ if (!(event as InputEvent).isComposing) keepValue(field, () => value.value);
62
+ }
63
+ function settle(event: CompositionEvent) {
64
+ keepValue(event.currentTarget as HTMLInputElement, () => value.value);
65
+ }
66
+ function key(event: KeyboardEvent) {
67
+ // While a composition is open, Enter and Escape belong to the input method.
68
+ if (event.isComposing) return;
69
+ if (event.key === "Enter") {
70
+ if (instance?.vnode.props?.onSubmit) event.preventDefault();
71
+ emit("submit", value.value);
72
+ }
73
+ if (event.key === "Escape" && clearable.value) {
74
+ event.stopPropagation();
75
+ set("");
76
+ }
77
+ }
78
+ function clear() {
79
+ set("");
80
+ // The control is about to disappear, and focus would be lost with it.
81
+ input.value?.focus();
82
+ }
83
+ </script>
84
+
85
+ <template>
86
+ <div :class="classes('{{prefix}}-text-field', rootPart('SearchField'))">
87
+ <div
88
+ class="{{prefix}}-text-field__control {{prefix}}-text-field__control--pill {{prefix}}-text-field__group"
89
+ >
90
+ <span class="{{prefix}}-text-field__slot {{prefix}}-text-field__slot--leading"
91
+ ><Icon name="search" size="sm"
92
+ /></span>
93
+ <!-- Escape clears only while there is something to clear; an empty field lets Escape
94
+ through to whatever it sits in, such as a dialog. -->
95
+ <input
96
+ ref="input"
97
+ type="text"
98
+ role="searchbox"
99
+ class="{{prefix}}-text-field__input"
100
+ :aria-label="label"
101
+ enterkeyhint="search"
102
+ :placeholder="placeholder"
103
+ :value="value"
104
+ v-bind="formDefault({ defaultValue: value })"
105
+ :disabled="disabled"
106
+ :readonly="readOnly"
107
+ :data-uses-escape="clearable ? '' : undefined"
108
+ @input="edit"
109
+ @compositionend="settle"
110
+ @keydown="key"
111
+ @focus="emit('focus', $event)"
112
+ @blur="emit('blur', $event)"
113
+ />
114
+ <span
115
+ v-if="clearable"
116
+ class="{{prefix}}-text-field__slot {{prefix}}-text-field__slot--trailing"
117
+ >
118
+ <button
119
+ type="button"
120
+ :aria-label="clearLabel ?? labels.clear"
121
+ class="-me-1 flex size-xds-target-min cursor-pointer items-center justify-center rounded-xds-full hover:text-xds-field-text focus-visible:outline-(length:--xds-focus-width) focus-visible:outline-xds-border-focus focus-visible:outline-solid"
122
+ @click="clear"
123
+ >
124
+ <Icon name="circle-x" size="sm" />
125
+ </button>
126
+ </span>
127
+ </div>
128
+ </div>
129
+ </template>
@@ -0,0 +1,37 @@
1
+ <!--
2
+ Ejected from `@xds/vue` 1.2.0 by `xds eject`. This copy is the app's own: change it
3
+ as you need. `xds eject --list` says which ejected files differ from what was copied.
4
+ -->
5
+ <script setup lang="ts">
6
+ import Button from "./Button.vue";
7
+ import Icon from "./Icon.vue";
8
+ // The title row of a group of content. The title is a heading at `level`; the action is a quiet
9
+ // button with a chevron at its end edge, which points the other way in a right-to-left layout.
10
+ import { classes, rootPart } from "./parts.ts";
11
+ import { SECTION_HEADER_PARTS, type SectionHeaderBaseProps } from "./section-header.ts";
12
+
13
+ export type SectionHeaderProps = SectionHeaderBaseProps & {
14
+ /** A link-like action at the end, such as "See all". */
15
+ action?: { label: string; onPress: () => void };
16
+ };
17
+
18
+ const { title, description, icon, level = 2, action } = defineProps<SectionHeaderProps>();
19
+ </script>
20
+
21
+ <template>
22
+ <div :class="classes(rootPart('SectionHeader'), SECTION_HEADER_PARTS.root)">
23
+ <div :class="SECTION_HEADER_PARTS.lead">
24
+ <span v-if="icon" :class="SECTION_HEADER_PARTS.icon"><Icon :name="icon" size="md" /></span>
25
+ <div :class="SECTION_HEADER_PARTS.text">
26
+ <component :is="`h${level}`" :class="SECTION_HEADER_PARTS.title">{{ title }}</component>
27
+ <p v-if="description" :class="SECTION_HEADER_PARTS.description">{{ description }}</p>
28
+ </div>
29
+ </div>
30
+ <span v-if="action" :class="SECTION_HEADER_PARTS.action">
31
+ <Button variant="ghost" size="sm" @press="action.onPress()">
32
+ {{ action.label
33
+ }}<Icon name="chevron-right" size="sm" :class="SECTION_HEADER_PARTS.chevron" />
34
+ </Button>
35
+ </span>
36
+ </div>
37
+ </template>
@@ -0,0 +1,81 @@
1
+ <!--
2
+ Ejected from `@xds/vue` 1.2.0 by `xds eject`. This copy is the app's own: change it
3
+ as you need. `xds eject --list` says which ejected files differ from what was copied.
4
+ -->
5
+ <script setup lang="ts" generic="Value extends string">
6
+ import { formDefault, keepChecked, useControllable } from "@xds/vue/base";
7
+ import { useId, useTemplateRef } from "vue";
8
+ // A choice between a few views or modes, all visible at once. The selected segment is outlined
9
+ // as well as filled, so the choice does not depend on telling two greys apart.
10
+ //
11
+ // It is a group of native radio buttons, so the arrow keys move between segments.
12
+ import { classes, rootPart } from "./parts.ts";
13
+ import {
14
+ type SegmentedControlBaseProps,
15
+ segmentedControlClass,
16
+ segmentedControlOptionClass,
17
+ segmentedControlSegmentClass,
18
+ } from "./segmented-control.ts";
19
+
20
+ // `aria-label` is declared as `ariaLabel`: Vue keeps a prop under its camelized name, and a
21
+ // template still writes `aria-label`.
22
+ export type SegmentedControlProps<Value extends string> = Omit<
23
+ SegmentedControlBaseProps<Value>,
24
+ "aria-label"
25
+ > & { ariaLabel: string };
26
+
27
+ // `v-model:value`, written out rather than with defineModel: see useControllable.
28
+ const {
29
+ value: given,
30
+ defaultValue,
31
+ options,
32
+ ariaLabel,
33
+ fullWidth = false,
34
+ } = defineProps<SegmentedControlProps<Value>>();
35
+ const emit = defineEmits<{ "update:value": [value: Value] }>();
36
+ const { value, set } = useControllable(
37
+ "value",
38
+ () => given,
39
+ // Without a default, the first segment: one segment is always chosen.
40
+ () => defaultValue ?? (options[0]?.value as Value),
41
+ (next) => emit("update:value", next),
42
+ );
43
+
44
+ const name = useId();
45
+ const group = useTemplateRef("group");
46
+
47
+ function choose(option: Value) {
48
+ set(option);
49
+ // The browser moved the dot already; where the owner kept the old value, it goes back, on
50
+ // every radio of the group.
51
+ for (const input of group.value?.querySelectorAll("input") ?? []) {
52
+ keepChecked(input, () => input.value === value.value);
53
+ }
54
+ }
55
+ </script>
56
+
57
+ <template>
58
+ <div
59
+ ref="group"
60
+ :aria-label="ariaLabel"
61
+ role="radiogroup"
62
+ :class="classes(rootPart('SegmentedControl'), segmentedControlClass(fullWidth))"
63
+ >
64
+ <label
65
+ v-for="option in options"
66
+ :key="option.value"
67
+ :class="segmentedControlOptionClass(fullWidth)"
68
+ >
69
+ <input
70
+ type="radio"
71
+ :name="name"
72
+ :value="option.value"
73
+ class="peer sr-only"
74
+ :checked="option.value === value"
75
+ v-bind="formDefault({ defaultChecked: option.value === value })"
76
+ @change="choose(option.value)"
77
+ />
78
+ <span :class="segmentedControlSegmentClass">{{ option.label }}</span>
79
+ </label>
80
+ </div>
81
+ </template>
@@ -0,0 +1,269 @@
1
+ <!--
2
+ Ejected from `@xds/vue` 1.2.0 by `xds eject`. This copy is the app's own: change it
3
+ as you need. `xds eject --list` says which ejected files differ from what was copied.
4
+ -->
5
+ <script setup lang="ts" generic="Value extends string = string">
6
+ // A field for choosing one option from a list too long to show at once. Closed, it looks like a
7
+ // TextField; pressed, it opens the list under itself with a Menu's surface, as wide as the
8
+ // field. Choosing an option sets the value and closes the list; a press outside or Escape
9
+ // closes it too.
10
+ //
11
+ // The field is a button that says it opens a list box and whether the list is open. In the list
12
+ // the arrow keys move, typing jumps to an option, and Enter chooses. The chosen option is
13
+ // selected for assistive technology and marked with a check.
14
+ //
15
+ // With `multiple`, any number of options can be chosen: choosing one adds it or takes it away,
16
+ // the list stays open, and `v-model:values` (`valuesChange`) gets them all. The list box says
17
+ // that several can be chosen.
18
+ import {
19
+ keepListKeys,
20
+ Portal,
21
+ readCssPx,
22
+ space,
23
+ underlayLayerStyle,
24
+ useControllable,
25
+ useModalPopover,
26
+ usePresence,
27
+ } from "@xds/vue/base";
28
+ import * as select from "@zag-js/select";
29
+ import { normalizeProps, useMachine } from "@zag-js/vue";
30
+ import { computed, useId, useTemplateRef } from "vue";
31
+ import Icon from "./Icon.vue";
32
+ import {
33
+ orderedValues,
34
+ type SelectBaseProps,
35
+ type SelectOption,
36
+ selectedLabels,
37
+ selectOptionIds,
38
+ } from "./select.ts";
39
+
40
+ export type { SelectOption } from "./select.ts";
41
+
42
+ export type SelectProps<Value extends string = string> = SelectBaseProps<Value>;
43
+
44
+ type KeyHandler = (event: KeyboardEvent) => void;
45
+
46
+ // `v-model:value`, written out rather than with defineModel: a choice is never undefined, and
47
+ // defineModel would say that it can be. See useControllable too.
48
+ const {
49
+ value: given,
50
+ defaultValue,
51
+ label,
52
+ options,
53
+ placeholder,
54
+ description,
55
+ error,
56
+ name,
57
+ disabled = false,
58
+ multiple = false,
59
+ values: givenValues,
60
+ defaultValues,
61
+ } = defineProps<SelectProps<Value>>();
62
+ const emit = defineEmits<{
63
+ "update:value": [value: Value];
64
+ /** The chosen options of a select with `multiple`, in the options' order. */
65
+ "update:values": [values: Value[]];
66
+ valuesChange: [values: Value[]];
67
+ }>();
68
+ const { value, set } = useControllable<Value | undefined>(
69
+ "value",
70
+ () => given,
71
+ () => defaultValue,
72
+ (next) => {
73
+ if (next !== undefined) emit("update:value", next);
74
+ },
75
+ );
76
+
77
+ const { value: values, set: setValues } = useControllable<readonly Value[]>(
78
+ "values",
79
+ () => givenValues,
80
+ () => defaultValues ?? [],
81
+ (next) => {
82
+ emit("update:values", [...next]);
83
+ emit("valuesChange", [...next]);
84
+ },
85
+ );
86
+
87
+ const id = useId();
88
+ const valueId = `${id}-value`;
89
+ const descriptionId = `${id}-description`;
90
+ const errorId = `${id}-error`;
91
+ const invalid = computed(() => error !== undefined);
92
+ const collection = computed(() =>
93
+ select.collection<SelectOption<Value>>({
94
+ items: [...options],
95
+ itemToValue: (option) => option.value,
96
+ itemToString: (option) => option.label,
97
+ }),
98
+ );
99
+ const service = useMachine(
100
+ select.machine,
101
+ computed(() => ({
102
+ id,
103
+ collection: collection.value,
104
+ value: multiple ? [...values.value] : value.value === undefined ? [] : [value.value],
105
+ onValueChange: (details: select.ValueChangeDetails<SelectOption<Value>>) => {
106
+ if (multiple) {
107
+ setValues(orderedValues(options, details.value as Value[]));
108
+ return;
109
+ }
110
+ const next = details.value[0];
111
+ if (next !== undefined) set(next as Value);
112
+ },
113
+ multiple,
114
+ // Several are chosen one after another, so the list stays until the person closes it.
115
+ closeOnSelect: !multiple,
116
+ disabled,
117
+ invalid: invalid.value,
118
+ name,
119
+ positioning: {
120
+ placement: "bottom-start" as const,
121
+ gutter: readCssPx(null, "--xds-space-1", space[1]),
122
+ sameWidth: true,
123
+ // The list keeps this far from the window's edges.
124
+ overflowPadding: readCssPx(null, "--xds-space-3", space[3]),
125
+ },
126
+ })),
127
+ );
128
+ const api = computed(() => select.connect(service, normalizeProps));
129
+ // Zag's key handlers read whether a search by typing is under way when the field renders, and
130
+ // a search ends on a timer, which renders nothing: long after a search, Space would still
131
+ // search for a space instead of opening the list or choosing. A handler taken from the machine
132
+ // when the key comes reads it then.
133
+ const current = () => select.connect(service, normalizeProps);
134
+ // A span: the field is a button, which a <label> cannot point at. A press on it still focuses
135
+ // the field.
136
+ const labelProps = computed(() => {
137
+ const { for: _, ...rest } = api.value.getLabelProps();
138
+ return rest;
139
+ });
140
+ // A button that opens a list box, rather than Zag's combobox: named by the chosen option and
141
+ // then the label, and described by the help or the error. The error is said there, so the
142
+ // button itself does not claim to be invalid or required.
143
+ const triggerProps = computed(() => {
144
+ const {
145
+ role: _role,
146
+ "aria-invalid": _invalid,
147
+ "aria-required": _required,
148
+ onKeydown: _onKeydown,
149
+ ...rest
150
+ } = api.value.getTriggerProps();
151
+ return {
152
+ ...rest,
153
+ // Home and End leave a closed field to the page; Zag would choose the first or the last
154
+ // option.
155
+ onKeydown: (event: KeyboardEvent) => {
156
+ if (!api.value.open && (event.key === "Home" || event.key === "End")) return;
157
+ (current().getTriggerProps().onKeydown as KeyHandler | undefined)?.(event);
158
+ },
159
+ };
160
+ });
161
+ const positioner = useTemplateRef<HTMLDivElement>("positioner");
162
+ const surface = useTemplateRef<HTMLDivElement>("surface");
163
+ useModalPopover(
164
+ () => api.value.open,
165
+ () => positioner.value,
166
+ );
167
+ const present = usePresence(
168
+ () => api.value.open,
169
+ () => [surface.value],
170
+ );
171
+ const content = computed(() => {
172
+ const { onKeydown: _onKeydown, ...rest } = api.value.getContentProps();
173
+ return {
174
+ ...rest,
175
+ onKeydown: (event: KeyboardEvent) => {
176
+ keepListKeys(event, surface.value, ".{{prefix}}-select__item:not([data-disabled])", (next) =>
177
+ api.value.setHighlightedValue(next),
178
+ );
179
+ (current().getContentProps().onKeydown as KeyHandler | undefined)?.(event);
180
+ },
181
+ };
182
+ });
183
+ </script>
184
+
185
+ <template>
186
+ <div v-bind="api.getRootProps()" class="{{prefix}}-select">
187
+ <span v-bind="labelProps" class="{{prefix}}-select__label">{{ label }}</span>
188
+ <button
189
+ v-bind="triggerProps"
190
+ :aria-labelledby="`${valueId} ${labelProps.id}`"
191
+ :aria-describedby="invalid ? errorId : description ? descriptionId : undefined"
192
+ class="{{prefix}}-select__trigger"
193
+ >
194
+ <!-- The chosen option's label alone: the list's rows also hold a description and a
195
+ check, which do not belong in the field. -->
196
+ <span v-bind="api.getValueTextProps()" :id="valueId" class="{{prefix}}-select__value">
197
+ {{ (multiple ? selectedLabels(options, values) : api.valueAsString) || placeholder }}
198
+ </span>
199
+ <Icon name="chevrons-up-down" size="sm" class="text-xds-field-placeholder" />
200
+ </button>
201
+ <!-- One line under the box: the help, or the error in its place. The error stays mounted as
202
+ a live region so assistive technology announces it when it appears. -->
203
+ <div class="{{prefix}}-select__footer">
204
+ <span
205
+ v-if="!invalid && description"
206
+ :id="descriptionId"
207
+ class="{{prefix}}-select__description"
208
+ >
209
+ {{ description }}
210
+ </span>
211
+ <span :id="errorId" class="{{prefix}}-select__error" aria-live="polite">{{ error }}</span>
212
+ </div>
213
+ <!-- What a form submits; in a select of one, the first option stands for no choice. Zag's
214
+ props hide it from assistive technology, which uses the trigger. -->
215
+ <select v-bind="api.getHiddenSelectProps()">
216
+ <option v-if="!multiple" value="" />
217
+ <option v-for="option in options" :key="option.value" :value="option.value">
218
+ {{ option.label }}
219
+ </option>
220
+ </select>
221
+ <Portal>
222
+ <!-- Takes a press outside, which then only closes the list. The focus stays in the list
223
+ until the list hands it back to the field. -->
224
+ <div
225
+ v-if="api.open"
226
+ class="{{prefix}}-select__underlay"
227
+ :style="underlayLayerStyle(api.getPositionerProps().style)"
228
+ @mousedown.prevent
229
+ />
230
+ <div ref="positioner" v-bind="api.getPositionerProps()" class="{{prefix}}-select__positioner">
231
+ <!-- Shown until its exit animation ends; Zag would hide it as it closes. -->
232
+ <div v-bind="content" ref="surface" :hidden="!present" class="{{prefix}}-select__content">
233
+ <div v-bind="api.getListProps()" class="{{prefix}}-select__list">
234
+ <!-- Named by its label alone, as the field shows it; the help line describes it. -->
235
+ <div
236
+ v-for="(option, index) in collection.items"
237
+ :key="option.value"
238
+ v-bind="api.getItemProps({ item: option })"
239
+ :aria-labelledby="selectOptionIds(id, index).label"
240
+ :aria-describedby="
241
+ option.description ? selectOptionIds(id, index).description : undefined
242
+ "
243
+ class="{{prefix}}-select__item"
244
+ >
245
+ <span class="{{prefix}}-select__item-words">
246
+ <span
247
+ v-bind="api.getItemTextProps({ item: option })"
248
+ :id="selectOptionIds(id, index).label"
249
+ class="{{prefix}}-select__item-label"
250
+ >
251
+ {{ option.label }}
252
+ </span>
253
+ <span
254
+ v-if="option.description"
255
+ :id="selectOptionIds(id, index).description"
256
+ class="{{prefix}}-select__item-description"
257
+ >
258
+ {{ option.description }}
259
+ </span>
260
+ </span>
261
+ <!-- Unnamed, unlike a Menu's: the option itself reports that it is selected. -->
262
+ <Icon v-if="api.getItemState({ item: option }).selected" name="check" size="sm" />
263
+ </div>
264
+ </div>
265
+ </div>
266
+ </div>
267
+ </Portal>
268
+ </div>
269
+ </template>