@thekestongroup/ui 0.2.0 → 0.3.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 (207) hide show
  1. package/README.md +90 -32
  2. package/dist/components/Age/Age.vue.d.ts +19 -0
  3. package/dist/components/AppShell/AppShell.vue.d.ts +108 -0
  4. package/dist/components/Avatar/Avatar.vue.d.ts +34 -0
  5. package/dist/components/AvatarGroup/AvatarGroup.vue.d.ts +25 -0
  6. package/dist/components/Badge/Badge.vue.d.ts +41 -0
  7. package/dist/components/Breadcrumb/Breadcrumb.vue.d.ts +39 -0
  8. package/dist/components/Button/Button.vue.d.ts +69 -0
  9. package/dist/components/Calendar/Calendar.vue.d.ts +34 -0
  10. package/dist/components/Card/Card.vue.d.ts +56 -0
  11. package/dist/components/Checkbox/Checkbox.vue.d.ts +40 -0
  12. package/dist/components/Checklist/Checklist.vue.d.ts +18 -0
  13. package/dist/components/Chip/Chip.vue.d.ts +44 -0
  14. package/dist/components/Divider/Divider.vue.d.ts +36 -0
  15. package/dist/components/{ToastItem.vue.d.ts → FormField/FieldMessage.vue.d.ts} +10 -2
  16. package/dist/components/FormField/FormField.vue.d.ts +61 -0
  17. package/dist/components/Header/Header.vue.d.ts +104 -0
  18. package/dist/components/InfoTip/InfoTip.vue.d.ts +20 -0
  19. package/dist/components/Input/Input.vue.d.ts +56 -0
  20. package/dist/components/InputCode/InputCode.vue.d.ts +53 -0
  21. package/dist/components/InputDate/InputDate.vue.d.ts +49 -0
  22. package/dist/components/InputNumber/InputNumber.vue.d.ts +44 -0
  23. package/dist/components/InputPassword/InputPassword.vue.d.ts +51 -0
  24. package/dist/components/InputRichText/InputRichText.vue.d.ts +44 -0
  25. package/dist/components/Link/Link.vue.d.ts +39 -0
  26. package/dist/components/Notification/Notification.vue.d.ts +62 -0
  27. package/dist/components/Notification/NotificationPane.vue.d.ts +38 -0
  28. package/dist/components/Notification/Toaster.vue.d.ts +51 -0
  29. package/dist/components/Notification/notification.samples.d.ts +3 -0
  30. package/dist/components/Notification/notifications.d.ts +115 -0
  31. package/dist/components/PageHeader/PageHeader.vue.d.ts +43 -0
  32. package/dist/components/Popover/Popover.vue.d.ts +83 -0
  33. package/dist/components/Popover/popover.d.ts +16 -0
  34. package/dist/components/Progress/Progress.vue.d.ts +26 -0
  35. package/dist/components/RadioGroup/RadioGroup.vue.d.ts +45 -0
  36. package/dist/components/RelativeTime/RelativeTime.vue.d.ts +22 -0
  37. package/dist/components/Select/Select.vue.d.ts +110 -0
  38. package/dist/components/Sidebar/Sidebar.vue.d.ts +106 -0
  39. package/dist/components/Sidebar/sidebar.d.ts +10 -0
  40. package/dist/components/Slideover/Slideover.vue.d.ts +125 -0
  41. package/dist/components/Spinner/Spinner.vue.d.ts +30 -0
  42. package/dist/components/StatTile/StatTile.vue.d.ts +46 -0
  43. package/dist/components/SummaryList/SummaryList.vue.d.ts +62 -0
  44. package/dist/components/Switch/Switch.vue.d.ts +39 -0
  45. package/dist/components/Table/Table.vue.d.ts +82 -0
  46. package/dist/components/Table/table.d.ts +36 -0
  47. package/dist/components/Tooltip/Tooltip.vue.d.ts +46 -0
  48. package/dist/index.d.ts +89 -25
  49. package/dist/keston-ui.js +4963 -14785
  50. package/dist/lib/cn.d.ts +4 -0
  51. package/dist/lib/dates.d.ts +27 -0
  52. package/dist/lib/initials.d.ts +2 -0
  53. package/dist/lib/parseDate.d.ts +44 -0
  54. package/dist/lib/useNow.d.ts +3 -0
  55. package/package.json +53 -24
  56. package/src/components/.gitkeep +0 -0
  57. package/src/components/Age/Age.stories.ts +24 -0
  58. package/src/components/Age/Age.vue +56 -0
  59. package/src/components/AppShell/AppShell.stories.ts +124 -0
  60. package/src/components/AppShell/AppShell.vue +212 -0
  61. package/src/components/Avatar/Avatar.stories.ts +56 -0
  62. package/src/components/Avatar/Avatar.vue +118 -0
  63. package/src/components/AvatarGroup/AvatarGroup.stories.ts +47 -0
  64. package/src/components/AvatarGroup/AvatarGroup.vue +97 -0
  65. package/src/components/Badge/Badge.stories.ts +92 -0
  66. package/src/components/Badge/Badge.vue +67 -0
  67. package/src/components/Breadcrumb/Breadcrumb.stories.ts +58 -0
  68. package/src/components/Breadcrumb/Breadcrumb.vue +109 -0
  69. package/src/components/Button/Button.mdx +79 -0
  70. package/src/components/Button/Button.stories.ts +207 -0
  71. package/src/components/Button/Button.vue +266 -0
  72. package/src/components/Calendar/Calendar.stories.ts +51 -0
  73. package/src/components/Calendar/Calendar.vue +364 -0
  74. package/src/components/Card/Card.stories.ts +109 -0
  75. package/src/components/Card/Card.vue +115 -0
  76. package/src/components/Checkbox/Checkbox.stories.ts +102 -0
  77. package/src/components/Checkbox/Checkbox.vue +102 -0
  78. package/src/components/Checklist/Checklist.stories.ts +67 -0
  79. package/src/components/Checklist/Checklist.vue +38 -0
  80. package/src/components/Chip/Chip.stories.ts +71 -0
  81. package/src/components/Chip/Chip.vue +86 -0
  82. package/src/components/Divider/Divider.stories.ts +57 -0
  83. package/src/components/Divider/Divider.vue +58 -0
  84. package/src/components/FormField/FieldMessage.vue +81 -0
  85. package/src/components/FormField/FormField.stories.ts +55 -0
  86. package/src/components/FormField/FormField.vue +160 -0
  87. package/src/components/Header/Header.stories.ts +168 -0
  88. package/src/components/Header/Header.vue +232 -0
  89. package/src/components/InfoTip/InfoTip.stories.ts +63 -0
  90. package/src/components/InfoTip/InfoTip.vue +50 -0
  91. package/src/components/Input/Input.stories.ts +206 -0
  92. package/src/components/Input/Input.vue +90 -0
  93. package/src/components/InputCode/InputCode.stories.ts +85 -0
  94. package/src/components/InputCode/InputCode.vue +145 -0
  95. package/src/components/InputDate/InputDate.stories.ts +58 -0
  96. package/src/components/InputDate/InputDate.vue +258 -0
  97. package/src/components/InputNumber/InputNumber.stories.ts +63 -0
  98. package/src/components/InputNumber/InputNumber.vue +109 -0
  99. package/src/components/InputPassword/InputPassword.stories.ts +105 -0
  100. package/src/components/InputPassword/InputPassword.vue +132 -0
  101. package/src/components/InputRichText/InputRichText.stories.ts +100 -0
  102. package/src/components/InputRichText/InputRichText.vue +194 -0
  103. package/src/components/Link/Link.stories.ts +73 -0
  104. package/src/components/Link/Link.vue +56 -0
  105. package/src/components/Notification/Notification.stories.ts +196 -0
  106. package/src/components/Notification/Notification.vue +136 -0
  107. package/src/components/Notification/NotificationPane.vue +187 -0
  108. package/src/components/Notification/Toaster.vue +446 -0
  109. package/src/components/Notification/notification.samples.ts +45 -0
  110. package/src/components/Notification/notifications.test.ts +99 -0
  111. package/src/components/Notification/notifications.ts +172 -0
  112. package/src/components/PageHeader/PageHeader.stories.ts +79 -0
  113. package/src/components/PageHeader/PageHeader.vue +66 -0
  114. package/src/components/Popover/Popover.stories.ts +135 -0
  115. package/src/components/Popover/Popover.vue +192 -0
  116. package/src/components/Popover/popover.ts +20 -0
  117. package/src/components/Progress/Progress.stories.ts +74 -0
  118. package/src/components/Progress/Progress.vue +75 -0
  119. package/src/components/RadioGroup/RadioGroup.stories.ts +90 -0
  120. package/src/components/RadioGroup/RadioGroup.vue +132 -0
  121. package/src/components/RelativeTime/RelativeTime.stories.ts +74 -0
  122. package/src/components/RelativeTime/RelativeTime.vue +51 -0
  123. package/src/components/Select/Select.mdx +202 -0
  124. package/src/components/Select/Select.stories.ts +223 -0
  125. package/src/components/Select/Select.vue +630 -0
  126. package/src/components/Sidebar/Sidebar.stories.ts +192 -0
  127. package/src/components/Sidebar/Sidebar.vue +382 -0
  128. package/src/components/Sidebar/sidebar.ts +20 -0
  129. package/src/components/Slideover/Slideover.stories.ts +225 -0
  130. package/src/components/Slideover/Slideover.vue +365 -0
  131. package/src/components/Spinner/Spinner.stories.ts +79 -0
  132. package/src/components/Spinner/Spinner.vue +97 -0
  133. package/src/components/StatTile/StatTile.stories.ts +75 -0
  134. package/src/components/StatTile/StatTile.vue +89 -0
  135. package/src/components/SummaryList/SummaryList.stories.ts +103 -0
  136. package/src/components/SummaryList/SummaryList.vue +127 -0
  137. package/src/components/Switch/Switch.stories.ts +100 -0
  138. package/src/components/Switch/Switch.vue +96 -0
  139. package/src/components/Table/Table.stories.ts +206 -0
  140. package/src/components/Table/Table.vue +220 -0
  141. package/src/components/Table/table.test.ts +65 -0
  142. package/src/components/Table/table.ts +70 -0
  143. package/src/components/Tooltip/Tooltip.stories.ts +57 -0
  144. package/src/components/Tooltip/Tooltip.vue +86 -0
  145. package/src/examples/AppPages.stories.ts +500 -0
  146. package/src/examples/FormLayout.stories.ts +185 -0
  147. package/src/examples/KestonIdPages.stories.ts +415 -0
  148. package/src/examples/SchoolPages.stories.ts +665 -0
  149. package/src/index.ts +114 -36
  150. package/src/lib/cn.ts +4 -0
  151. package/src/lib/dates.test.ts +68 -0
  152. package/src/lib/dates.ts +86 -0
  153. package/src/lib/initials.ts +8 -0
  154. package/src/lib/parseDate.test.ts +331 -0
  155. package/src/lib/parseDate.ts +561 -0
  156. package/src/lib/useNow.ts +20 -0
  157. package/src/styles/theme.css +101 -0
  158. package/dist/components/AppLauncher.vue.d.ts +0 -10
  159. package/dist/components/Avatar.vue.d.ts +0 -16
  160. package/dist/components/Button.vue.d.ts +0 -27
  161. package/dist/components/Checkbox.vue.d.ts +0 -18
  162. package/dist/components/CloseButton.vue.d.ts +0 -6
  163. package/dist/components/Dialog.vue.d.ts +0 -23
  164. package/dist/components/Drawer.vue.d.ts +0 -34
  165. package/dist/components/DropdownMenu.vue.d.ts +0 -21
  166. package/dist/components/DropdownMenuItem.vue.d.ts +0 -19
  167. package/dist/components/DropdownMenuSeparator.vue.d.ts +0 -6
  168. package/dist/components/Input.vue.d.ts +0 -14
  169. package/dist/components/Label.vue.d.ts +0 -16
  170. package/dist/components/NotificationsMenu.vue.d.ts +0 -3
  171. package/dist/components/Popover.vue.d.ts +0 -23
  172. package/dist/components/ProfileSwitcher.vue.d.ts +0 -24
  173. package/dist/components/Select.vue.d.ts +0 -28
  174. package/dist/components/Separator.vue.d.ts +0 -11
  175. package/dist/components/ToastRenderer.vue.d.ts +0 -3
  176. package/dist/components/Toaster.vue.d.ts +0 -3
  177. package/dist/components/Tooltip.vue.d.ts +0 -23
  178. package/dist/components/TopBar.vue.d.ts +0 -34
  179. package/dist/favicon-old.svg +0 -1
  180. package/dist/favicon.svg +0 -1
  181. package/dist/keston-ui.cjs +0 -15
  182. package/dist/lib/toast.d.ts +0 -29
  183. package/dist/lib/types.d.ts +0 -5
  184. package/src/components/AppLauncher.vue +0 -63
  185. package/src/components/Avatar.vue +0 -51
  186. package/src/components/Button.vue +0 -54
  187. package/src/components/Checkbox.vue +0 -47
  188. package/src/components/CloseButton.vue +0 -24
  189. package/src/components/Dialog.vue +0 -65
  190. package/src/components/Drawer.vue +0 -202
  191. package/src/components/DropdownMenu.vue +0 -41
  192. package/src/components/DropdownMenuItem.vue +0 -28
  193. package/src/components/DropdownMenuSeparator.vue +0 -12
  194. package/src/components/Input.vue +0 -21
  195. package/src/components/Label.vue +0 -15
  196. package/src/components/NotificationsMenu.vue +0 -115
  197. package/src/components/Popover.vue +0 -46
  198. package/src/components/ProfileSwitcher.vue +0 -113
  199. package/src/components/Select.vue +0 -97
  200. package/src/components/Separator.vue +0 -30
  201. package/src/components/ToastItem.vue +0 -158
  202. package/src/components/ToastRenderer.vue +0 -113
  203. package/src/components/Toaster.vue +0 -11
  204. package/src/components/Tooltip.vue +0 -48
  205. package/src/components/TopBar.vue +0 -52
  206. package/src/lib/toast.ts +0 -95
  207. package/src/lib/types.ts +0 -5
@@ -0,0 +1,630 @@
1
+ <script setup lang="ts">
2
+ import { computed, nextTick, onBeforeUnmount, onMounted, ref, useId, type Component, type HTMLAttributes } from 'vue'
3
+ import {
4
+ ComboboxAnchor,
5
+ ComboboxContent,
6
+ ComboboxEmpty,
7
+ ComboboxGroup,
8
+ ComboboxInput,
9
+ ComboboxItem,
10
+ ComboboxItemIndicator,
11
+ ComboboxLabel,
12
+ ComboboxPortal,
13
+ ComboboxRoot,
14
+ ComboboxSeparator,
15
+ ComboboxTrigger,
16
+ ComboboxViewport,
17
+ SelectContent,
18
+ SelectGroup,
19
+ SelectItem,
20
+ SelectItemIndicator,
21
+ SelectItemText,
22
+ SelectLabel,
23
+ SelectPortal,
24
+ SelectRoot,
25
+ SelectSeparator,
26
+ SelectTrigger,
27
+ SelectViewport
28
+ } from 'reka-ui'
29
+ import { Check, ChevronDown, ChevronLeft, ChevronRight } from '@lucide/vue'
30
+ import { cn } from '../../lib/cn'
31
+ import Chip from '../Chip/Chip.vue'
32
+ import FormField, { type FormFieldStatus } from '../FormField/FormField.vue'
33
+ import { popoverContent } from '../Popover/popover'
34
+
35
+ export type SelectValueType = string | number
36
+
37
+ export interface SelectOption {
38
+ /** Required. Shown in the list and, once chosen, in the field. */
39
+ label: string
40
+ /** What v-model is set to when chosen. Defaults to the label. Not used on options with children. */
41
+ value?: SelectValueType
42
+ /** Smaller grey text under the label, in the list only. Searchable lists match it too. */
43
+ description?: string
44
+ /** A Lucide (or any) icon component, shown before the label (and in the field once chosen). */
45
+ icon?: Component
46
+ disabled?: boolean
47
+ /**
48
+ * Nested options. Choosing this option opens them (with a back row to
49
+ * return) instead of choosing it, e.g. Classes › Year 8 › 11Y/En2 › Ben.
50
+ */
51
+ children?: (SelectOption | string)[]
52
+ }
53
+
54
+ export interface SelectGroupOptions {
55
+ /** Heading above the group's options. */
56
+ label?: string
57
+ /** A Lucide (or any) icon component, shown before the heading. */
58
+ icon?: Component
59
+ options: (SelectOption | string)[]
60
+ }
61
+
62
+ /**
63
+ * Choose one option from a list that opens below the field, e.g. a year
64
+ * group or a school. Looks like Input (same label, helper, status message and
65
+ * box).
66
+ *
67
+ * - Default: built on Reka UI's Select. The field is a button; arrow keys,
68
+ * Home / End and typing the start of an option move through the list, Enter
69
+ * / Space choose, Escape closes.
70
+ * - `searchable`: built on Reka UI's Combobox. The field is a text box;
71
+ * typing filters the options (anywhere in the label, ignoring case and
72
+ * accents), arrow keys move, Enter chooses. When it loses focus it shows
73
+ * the chosen option again.
74
+ *
75
+ * Options can be plain strings (label and value the same) or objects with a
76
+ * label and, optionally, a description and an icon; they can be grouped under
77
+ * headings, and nested (`children`): choosing an option with children opens
78
+ * them, with a back row at the top. → / ← also go in and back. Searchable
79
+ * lists search every level and show where each match is.
80
+ *
81
+ * Slots: leading (before the value, replacing the chosen option's icon).
82
+ */
83
+ export interface SelectProps {
84
+ /** Visible label. Always set it: it is also the field's accessible name. */
85
+ label: string
86
+ /** Options in a single list. Use `groups` for headings. */
87
+ options?: (SelectOption | string)[]
88
+ /** Groups of options, each with an optional heading. Shown after `options`. */
89
+ groups?: SelectGroupOptions[]
90
+ /** Type in the field to filter the options. Use for lists of more than about 10. */
91
+ searchable?: boolean
92
+ /**
93
+ * Choose any number of options. v-model is then an array, the list stays
94
+ * open while ticking, and the chosen options show as chips in the field
95
+ * (with × to remove them when searchable).
96
+ */
97
+ multiple?: boolean
98
+ /** Shown in the field when nothing is chosen. Default "Choose…", or "Search…" when searchable. */
99
+ placeholder?: string
100
+ /** Shown in the list when a search matches nothing. */
101
+ emptyText?: string
102
+ /** Extra guidance about what to choose. */
103
+ helper?: string
104
+ /** Show helper text under the label, or behind a `?` icon next to it. */
105
+ helperPlacement?: 'below' | 'icon'
106
+ /** Error message, e.g. from a Zod schema. Overrides `status` / `statusText`. */
107
+ errorText?: string
108
+ /** Colours the field and the status text. */
109
+ status?: FormFieldStatus
110
+ /** Message under the field. */
111
+ statusText?: string
112
+ /** Form field name: adds a hidden native field so the value is submitted with a form. */
113
+ name?: string
114
+ /** Must be chosen before a form can be submitted. */
115
+ required?: boolean
116
+ disabled?: boolean
117
+ class?: HTMLAttributes['class']
118
+ }
119
+
120
+ const props = withDefaults(defineProps<SelectProps>(), {
121
+ options: () => [],
122
+ groups: () => [],
123
+ emptyText: 'No matches',
124
+ helperPlacement: 'below'
125
+ })
126
+
127
+ defineSlots<{
128
+ leading?(): unknown
129
+ }>()
130
+
131
+ /** The chosen option's value, or undefined when nothing is chosen. */
132
+ const model = defineModel<SelectValueType | SelectValueType[] | undefined>()
133
+
134
+ // The chosen values as a list (empty, one, or several).
135
+ const values = computed<SelectValueType[]>(() =>
136
+ Array.isArray(model.value) ? model.value : model.value === undefined ? [] : [model.value]
137
+ )
138
+
139
+ // Reka's Select treats an empty value as "nothing chosen" (shows the
140
+ // placeholder); Combobox takes undefined.
141
+ const rootValue = computed(() => {
142
+ if (props.multiple) return values.value
143
+ return props.searchable ? model.value : (model.value ?? '')
144
+ })
145
+ function setValue(next: unknown) {
146
+ if (props.multiple) {
147
+ model.value = Array.isArray(next) ? (next as SelectValueType[]) : []
148
+ // Clear the search after each tick, ready for the next one. (Reka does
149
+ // this for single choices only.) The input event updates Reka's own copy
150
+ // of the search, so the list shows every option again.
151
+ if (props.searchable && search.value) {
152
+ search.value = ''
153
+ nextTick(() => searchInput.value?.$el?.dispatchEvent(new Event('input', { bubbles: true })))
154
+ }
155
+ return
156
+ }
157
+ model.value = next === '' || next === null ? undefined : (next as SelectValueType)
158
+ // Searchable: select the chosen label, so typing straight away replaces it
159
+ // (as when the field is first focused) rather than adding to it.
160
+ if (props.searchable) nextTick(() => searchInput.value?.$el?.select?.())
161
+ }
162
+
163
+ const placeholderText = computed(() => props.placeholder ?? (props.searchable ? 'Search…' : 'Choose…'))
164
+
165
+ const toOption = (option: SelectOption | string): SelectOption =>
166
+ typeof option === 'string' ? { label: option, value: option } : { ...option, value: option.value ?? option.label }
167
+
168
+ interface Group {
169
+ label?: string
170
+ icon?: Component
171
+ options: SelectOption[]
172
+ }
173
+
174
+ const allGroups = computed<Group[]>(() => [
175
+ ...(props.options.length ? [{ options: props.options }] : []),
176
+ ...props.groups
177
+ ].map((group) => ({ label: group.label, icon: group.icon, options: group.options.map(toOption) })))
178
+
179
+ // ── Nesting ─────────────────────────────────────────────────────────────────
180
+ // Every choosable option (no children), with the parents above it. Options
181
+ // are matched by value, so one that appears in several places (a student in
182
+ // two classes) is chosen once: ticked wherever it appears, one chip.
183
+ interface Leaf {
184
+ option: SelectOption
185
+ path: SelectOption[]
186
+ }
187
+ function collectLeaves(options: SelectOption[], path: SelectOption[], into: Leaf[]) {
188
+ for (const option of options) {
189
+ if (option.children?.length) collectLeaves(option.children.map(toOption), [...path, option], into)
190
+ else into.push({ option, path })
191
+ }
192
+ return into
193
+ }
194
+ const leaves = computed(() => allGroups.value.flatMap((group) => collectLeaves(group.options, [], [])))
195
+ const nested = computed(() => leaves.value.some((leaf) => leaf.path.length))
196
+
197
+ // The chosen option, for its icon and label in the field.
198
+ const selectedLeaf = computed(() =>
199
+ props.multiple ? undefined : leaves.value.find((leaf) => leaf.option.value === model.value)
200
+ )
201
+ const selected = computed(() => selectedLeaf.value?.option)
202
+
203
+ // Multiple: the chosen options, in the order they were chosen.
204
+ const chosen = computed(() =>
205
+ props.multiple
206
+ ? values.value.flatMap((value) => leaves.value.find((leaf) => leaf.option.value === value)?.option ?? [])
207
+ : []
208
+ )
209
+ function remove(value: SelectValueType) {
210
+ model.value = values.value.filter((v) => v !== value)
211
+ }
212
+ // Searchable: Backspace in an empty search box removes the last chip.
213
+ function onSearchBackspace(event: KeyboardEvent) {
214
+ if (!props.multiple || search.value || !values.value.length) return
215
+ event.preventDefault()
216
+ remove(values.value[values.value.length - 1]!)
217
+ }
218
+ const labelFor = (value: unknown) => leaves.value.find((leaf) => leaf.option.value === value)?.option.label ?? ''
219
+
220
+ // The parents opened so far; empty at the top level. Opening the list starts
221
+ // where the chosen option is, so people see it in place.
222
+ const trail = ref<SelectOption[]>([])
223
+ const current = computed(() => trail.value[trail.value.length - 1])
224
+
225
+ // Open or closed. Held here (not only in Reka) so a press anywhere in a
226
+ // searchable field can open it, not just on the text box itself.
227
+ const isOpen = ref(false)
228
+
229
+ function onOpenChange(open: boolean) {
230
+ isOpen.value = open
231
+ // Multiple choices can be on different levels, so those start at the top.
232
+ if (open) trail.value = selectedLeaf.value ? [...selectedLeaf.value.path] : []
233
+ // Closing hands focus back to the field. After the mouse (or touch), hide
234
+ // the focus ring, as if the field had been clicked; after the keyboard,
235
+ // keep it. Browsers can't tell, as Reka moves focus by script.
236
+ else pointerFocus.value = usedPointer
237
+ }
238
+
239
+ // A text box always counts as focus-visible, so the ring would show while
240
+ // the list is open after a click, then fade when an option is chosen. Treat a
241
+ // click like the plain Select: no ring until a key is pressed.
242
+ function onSearchFocus(event: FocusEvent) {
243
+ ;(event.target as HTMLInputElement).select()
244
+ pointerFocus.value = usedPointer
245
+ }
246
+
247
+ // ── Focus ring after closing ────────────────────────────────────────────────
248
+ // Whether the mouse / touch or the keyboard was used last, anywhere on the
249
+ // page (the list is outside this component, in a portal).
250
+ let usedPointer = false
251
+ const pointerFocus = ref(false)
252
+ const onPointer = () => (usedPointer = true)
253
+ const onKey = () => {
254
+ usedPointer = false
255
+ pointerFocus.value = false
256
+ }
257
+ onMounted(() => {
258
+ document.addEventListener('pointerdown', onPointer, true)
259
+ document.addEventListener('keydown', onKey, true)
260
+ // The chosen option and the back row are bold. Browsers only download a
261
+ // font weight once something on the page uses it, and show a stand-in
262
+ // until it arrives, so on a page with no other bold text the first option
263
+ // chosen would visibly jump as the real bold swapped in. Load it now.
264
+ document.fonts?.load(`700 1em ${getComputedStyle(document.body).fontFamily}`).catch(() => {})
265
+ })
266
+ onBeforeUnmount(() => {
267
+ document.removeEventListener('pointerdown', onPointer, true)
268
+ document.removeEventListener('keydown', onKey, true)
269
+ })
270
+
271
+ // Searchable: what has been typed. Group dividers are hidden while searching,
272
+ // as Reka hides groups with no matches and the lines would double up.
273
+ const search = ref('')
274
+ const searchInput = ref<{ $el?: HTMLInputElement }>()
275
+
276
+ // What the list shows: a row per option, grouped. Searching a nested list
277
+ // shows every matching option from every level, with its path.
278
+ interface Row {
279
+ key: string
280
+ option: SelectOption
281
+ parent: boolean
282
+ /** Where it is, e.g. "Classes › Year 8", when searching a nested list. */
283
+ path?: string
284
+ }
285
+ const rowsFor = (options: SelectOption[], prefix: string): Row[] =>
286
+ options.map((option, i) => {
287
+ const parent = !!option.children?.length
288
+ return { key: parent ? `${prefix}-parent-${i}` : `${prefix}-${String(option.value)}`, option, parent }
289
+ })
290
+
291
+ const shownGroups = computed<(Omit<Group, 'options'> & { rows: Row[] })[]>(() => {
292
+ if (props.searchable && search.value && nested.value) {
293
+ // The same option can be in several places (a student in two classes).
294
+ // It's one choice, so it's one row, listing every place it's in.
295
+ const rows = new Map<SelectValueType, Row>()
296
+ for (const leaf of leaves.value) {
297
+ const path = leaf.path.map((parent) => parent.label).join(' › ')
298
+ const row = rows.get(leaf.option.value!)
299
+ if (row) row.path = [row.path, path].filter(Boolean).join(' · ') || undefined
300
+ else rows.set(leaf.option.value!, { key: `leaf-${String(leaf.option.value)}`, option: leaf.option, parent: false, path: path || undefined })
301
+ }
302
+ return [{ rows: [...rows.values()] }]
303
+ }
304
+ if (current.value) {
305
+ return [{ rows: rowsFor(current.value.children!.map(toOption), `level-${trail.value.length}`) }]
306
+ }
307
+ return allGroups.value.map((group, g) => ({ label: group.label, icon: group.icon, rows: rowsFor(group.options, `group-${g}`) }))
308
+ })
309
+
310
+ const showBack = computed(() => !!current.value && !(props.searchable && search.value))
311
+
312
+ // Parent rows and the back row are Reka items (so arrow keys and typing reach
313
+ // them), but they navigate instead of being chosen. These values are objects,
314
+ // so they can never match a real value.
315
+ const BACK = { back: true }
316
+ const navValue = (row: Row) => ({ parent: row.key })
317
+
318
+ const listId = `${useId()}-list`
319
+
320
+ // After moving between levels, put the highlight on the first option (not
321
+ // the back row, which is one ↑ away), so keyboard users carry on into it.
322
+ // Going back highlights the option just left instead.
323
+ async function focusRow(from?: SelectOption) {
324
+ await nextTick()
325
+ const list = document.querySelector<HTMLElement>(`[data-list-id="${listId}"]`)
326
+ const fromKey = from && shownGroups.value.flatMap((group) => group.rows).find((row) => row.parent && row.option.label === from.label)?.key
327
+ const first =
328
+ (fromKey && list?.querySelector<HTMLElement>(`[data-parent-key="${fromKey}"]`)) ||
329
+ list?.querySelector<HTMLElement>('[role="option"]:not([data-disabled]):not([data-back])') ||
330
+ list?.querySelector<HTMLElement>('[role="option"][data-back]')
331
+ if (!first) return
332
+ if (props.searchable) first.dispatchEvent(new PointerEvent('pointermove', { bubbles: true }))
333
+ else first.focus()
334
+ }
335
+
336
+ function openParent(event: Event, option: SelectOption) {
337
+ event.preventDefault()
338
+ trail.value = [...trail.value, option]
339
+ focusRow()
340
+ }
341
+
342
+ function goBack(event?: Event) {
343
+ event?.preventDefault()
344
+ const from = current.value
345
+ trail.value = trail.value.slice(0, -1)
346
+ focusRow(from)
347
+ }
348
+
349
+ // → opens the highlighted parent, ← goes back a level. In a searchable list
350
+ // only while nothing is typed, so the arrows still move the text cursor.
351
+ function onListKeydown(event: KeyboardEvent) {
352
+ // Reka can pass the listener on twice; handle each key press once.
353
+ if (event.defaultPrevented || (props.searchable && search.value)) return
354
+ if (event.key === 'ArrowLeft' && trail.value.length) {
355
+ event.preventDefault()
356
+ goBack()
357
+ } else if (event.key === 'ArrowRight') {
358
+ const list = document.querySelector<HTMLElement>(`[data-list-id="${listId}"]`)
359
+ const highlighted = list?.querySelector<HTMLElement>('[data-highlighted][data-parent-key]')
360
+ const row = highlighted && shownGroups.value.flatMap((group) => group.rows).find((r) => r.key === highlighted.dataset.parentKey)
361
+ if (!row) return
362
+ event.preventDefault()
363
+ openParent(event, row.option)
364
+ }
365
+ }
366
+
367
+ // The list is the same for both; only Reka's parts differ.
368
+ const parts = computed(() =>
369
+ props.searchable
370
+ ? {
371
+ Root: ComboboxRoot,
372
+ Portal: ComboboxPortal,
373
+ Content: ComboboxContent,
374
+ Viewport: ComboboxViewport,
375
+ Group: ComboboxGroup,
376
+ Label: ComboboxLabel,
377
+ Separator: ComboboxSeparator,
378
+ Item: ComboboxItem,
379
+ ItemText: 'span',
380
+ Indicator: ComboboxItemIndicator
381
+ }
382
+ : {
383
+ Root: SelectRoot,
384
+ Portal: SelectPortal,
385
+ Content: SelectContent,
386
+ Viewport: SelectViewport,
387
+ Group: SelectGroup,
388
+ Label: SelectLabel,
389
+ Separator: SelectSeparator,
390
+ Item: SelectItem,
391
+ ItemText: SelectItemText,
392
+ Indicator: SelectItemIndicator
393
+ }
394
+ )
395
+
396
+ // The trigger (or the search box's anchor) fills FormField's box: negative
397
+ // margins cancel its padding, so the whole box opens the list. The box draws
398
+ // the border and focus ring.
399
+ const fill = '-mx-3 -my-2 flex min-w-0 flex-1 items-center gap-2 self-stretch px-3 py-2'
400
+
401
+ // The list is at least as wide as the field and no taller than the space
402
+ // below / above it; it scrolls inside.
403
+ const content = computed(() => cn(
404
+ popoverContent,
405
+ 'overflow-hidden',
406
+ props.searchable
407
+ ? 'max-h-(--reka-combobox-content-available-height) min-w-(--reka-combobox-trigger-width) max-w-[max(var(--reka-combobox-trigger-width),--spacing(80))]'
408
+ : 'max-h-(--reka-select-content-available-height) min-w-(--reka-select-trigger-width) max-w-[max(var(--reka-select-trigger-width),--spacing(80))]'
409
+ ))
410
+
411
+ // Compact rows: 32px tall with a label only (6px + 20px line + 6px), 8px
412
+ // either side, tinted like a ghost Button. A description adds lines below;
413
+ // the icon and tick stay level with the label (h-5 slots, top-aligned).
414
+ const item = [
415
+ 'relative flex min-h-8 cursor-pointer items-start gap-2 rounded px-2 py-1.5 text-sm outline-none select-none',
416
+ 'data-highlighted:bg-black/10',
417
+ 'data-disabled:cursor-not-allowed data-disabled:opacity-40'
418
+ ]
419
+
420
+ // Searchable lists filter on this: the label, description and path.
421
+ const searchText = (row: Row) =>
422
+ props.searchable ? [row.option.label, row.option.description, row.path].filter(Boolean).join(' ') : row.option.label
423
+
424
+ // Turns over while the list is open.
425
+ const chevron = 'transition-transform duration-150 in-data-[state=open]:rotate-180 motion-reduce:transition-none'
426
+ </script>
427
+
428
+ <template>
429
+ <FormField
430
+ :label="label"
431
+ :helper="helper"
432
+ :helper-placement="helperPlacement"
433
+ :error-text="errorText"
434
+ :status="status"
435
+ :status-text="statusText"
436
+ :disabled="disabled"
437
+ :class="props.class"
438
+ >
439
+ <template #default="{ id, describedBy, invalid }">
440
+ <component
441
+ :is="parts.Root"
442
+ :model-value="rootValue"
443
+ :name="name"
444
+ :required="required"
445
+ :disabled="disabled"
446
+ :multiple="multiple"
447
+ v-bind="searchable ? { openOnClick: true, class: 'contents' } : {}"
448
+ @update:model-value="setValue"
449
+ :open="isOpen"
450
+ @update:open="onOpenChange"
451
+ >
452
+ <!-- Searchable: a text box that filters the list. (Combobox's root
453
+ renders a <div>; `contents` above keeps this anchor a direct
454
+ flex child of the box, so it fills it.) -->
455
+ <!-- A press anywhere in the field opens the list: the padding, beside
456
+ the chips, or on the text box (Reka itself only opens for the
457
+ text box). Not on a chip's ×, which removes it instead. -->
458
+ <ComboboxAnchor
459
+ v-if="searchable"
460
+ :class="fill"
461
+ @pointerdown="pointerFocus = true"
462
+ @click="!disabled && !($event.target as Element).closest('button') && onOpenChange(true)"
463
+ >
464
+ <slot name="leading">
465
+ <component :is="selected.icon" v-if="selected?.icon" aria-hidden="true" />
466
+ </slot>
467
+ <!-- Multiple: a chip per chosen option, then the search box, wrapping
468
+ onto more lines as needed. × removes one (mouse); keyboard users
469
+ press Backspace in the empty box, or untick it in the list. -->
470
+ <span :class="multiple ? 'flex min-w-0 flex-1 flex-wrap items-center gap-1' : 'contents'">
471
+ <!-- pointerdown.stop keeps a press on × from opening the list. -->
472
+ <Chip
473
+ v-for="option in chosen"
474
+ :key="String(option.value)"
475
+ size="sm"
476
+ removable
477
+ :remove-label="`Remove ${option.label}`"
478
+ :remove-focusable="false"
479
+ :disabled="disabled"
480
+ @pointerdown.stop
481
+ @remove="remove(option.value!)"
482
+ >
483
+ <template v-if="option.icon" #leading><component :is="option.icon" aria-hidden="true" /></template>
484
+ {{ option.label }}
485
+ </Chip>
486
+ <ComboboxInput
487
+ :id="id"
488
+ ref="searchInput"
489
+ v-model="search"
490
+ :display-value="multiple ? () => '' : labelFor"
491
+ :placeholder="multiple && chosen.length ? '' : placeholderText"
492
+ :aria-invalid="invalid"
493
+ :aria-describedby="describedBy"
494
+ autocomplete="off"
495
+ :class="cn('h-5 min-w-0 flex-1 bg-transparent outline-none placeholder:text-black/55 disabled:cursor-not-allowed', multiple && 'min-w-16')"
496
+ :data-pointer-focus="pointerFocus || undefined"
497
+ @focus="onSearchFocus"
498
+ @blur="pointerFocus = false"
499
+ @keydown="($event.target as HTMLElement).ariaExpanded === 'true' && onListKeydown($event)"
500
+ @keydown.backspace="onSearchBackspace"
501
+ />
502
+ </span>
503
+ <ComboboxTrigger tabindex="-1" aria-label="Show options" class="flex cursor-pointer disabled:cursor-not-allowed">
504
+ <ChevronDown aria-hidden="true" :class="chevron" />
505
+ </ComboboxTrigger>
506
+ </ComboboxAnchor>
507
+
508
+ <!-- Default: a button showing the chosen option. -->
509
+ <SelectTrigger
510
+ v-else
511
+ :id="id"
512
+ :aria-invalid="invalid"
513
+ :aria-describedby="describedBy"
514
+ :data-pointer-focus="pointerFocus || undefined"
515
+ @blur="pointerFocus = false"
516
+ :class="cn(fill, 'cursor-pointer text-left outline-none disabled:cursor-not-allowed data-placeholder:text-black/55')"
517
+ >
518
+ <slot name="leading">
519
+ <component :is="selected.icon" v-if="selected?.icon" aria-hidden="true" />
520
+ </slot>
521
+ <!-- The chosen label, from the options themselves rather than Reka's
522
+ SelectValue, which only knows the options currently showing (a
523
+ nested choice isn't, once the list is back at the top). -->
524
+ <!-- Multiple: a chip per chosen option, wrapping onto more lines. -->
525
+ <span v-if="multiple && chosen.length" class="flex min-w-0 flex-1 flex-wrap gap-1">
526
+ <Chip v-for="option in chosen" :key="String(option.value)" size="sm">
527
+ <template v-if="option.icon" #leading><component :is="option.icon" aria-hidden="true" /></template>
528
+ {{ option.label }}
529
+ </Chip>
530
+ </span>
531
+ <span v-else class="h-5 min-w-0 flex-1 truncate">{{ selected?.label ?? placeholderText }}</span>
532
+ <ChevronDown aria-hidden="true" :class="chevron" />
533
+ </SelectTrigger>
534
+
535
+ <component :is="parts.Portal">
536
+ <component
537
+ :is="parts.Content"
538
+ position="popper"
539
+ :side-offset="6"
540
+ :class="content"
541
+ :data-list-id="listId"
542
+ @keydown="!searchable && onListKeydown($event)"
543
+ @mousedown="searchable && $event.preventDefault()"
544
+ >
545
+ <!-- Searchable: pressing in the list mustn't take focus from the
546
+ search box (the pressed option goes away as the list closes,
547
+ which would leave focus nowhere). -->
548
+ <!-- Reka hides the scrollbar (its own injected style); show a thin
549
+ one so long lists look scrollable. -->
550
+ <component
551
+ :is="parts.Viewport"
552
+ class="flex flex-col gap-0.5 p-1 [scrollbar-width:thin]! [&::-webkit-scrollbar]:block!"
553
+ >
554
+ <ComboboxEmpty v-if="searchable" class="px-2 py-1.5 text-sm text-black/65">
555
+ {{ emptyText }}
556
+ </ComboboxEmpty>
557
+
558
+ <!-- Inside a nested option: back to the level above. -->
559
+ <template v-if="showBack">
560
+ <component
561
+ :is="parts.Item"
562
+ :value="BACK"
563
+ data-back
564
+ :text-value="current!.label"
565
+ :class="cn(item, 'font-bold')"
566
+ @select="goBack"
567
+ >
568
+ <span class="flex h-5 shrink-0 items-center">
569
+ <ChevronLeft aria-hidden="true" class="size-4" />
570
+ </span>
571
+ <!-- Names the level being shown; pressing it goes up one. -->
572
+ <span class="min-w-0 flex-1 truncate"><span class="sr-only">Back from </span>{{ current!.label }}</span>
573
+ </component>
574
+ <component :is="parts.Separator" class="-mx-1 my-0.5 border-t border-black/15" />
575
+ </template>
576
+
577
+ <template v-for="(group, g) in shownGroups" :key="g">
578
+ <component
579
+ :is="parts.Separator"
580
+ v-if="g > 0 && !search"
581
+ class="-mx-1 my-0.5 border-t border-black/15"
582
+ />
583
+ <component :is="parts.Group" class="flex flex-col gap-0.5">
584
+ <component
585
+ :is="parts.Label"
586
+ v-if="group.label"
587
+ class="flex h-6 items-center gap-1.5 px-2 text-xs font-bold text-black/65 uppercase"
588
+ >
589
+ <component :is="group.icon" v-if="group.icon" aria-hidden="true" class="size-3.5 shrink-0" />
590
+ <span class="truncate">{{ group.label }}</span>
591
+ </component>
592
+ <component
593
+ :is="parts.Item"
594
+ v-for="row in group.rows"
595
+ :key="row.key"
596
+ :value="row.parent ? navValue(row) : row.option.value!"
597
+ :disabled="row.option.disabled"
598
+ :text-value="searchText(row)"
599
+ :data-parent-key="row.parent ? row.key : undefined"
600
+ :class="item"
601
+ @select="row.parent && openParent($event, row.option)"
602
+ >
603
+ <!-- No empty space for options without an icon: an indent
604
+ reads as nesting. Give every option an icon, or none. -->
605
+ <span v-if="row.option.icon" class="flex h-5 shrink-0 items-center">
606
+ <component :is="row.option.icon" aria-hidden="true" class="size-4 opacity-70" />
607
+ </span>
608
+ <span class="flex min-w-0 flex-1 flex-col">
609
+ <component :is="parts.ItemText" class="truncate in-data-[state=checked]:font-bold">
610
+ {{ row.option.label }}<span v-if="row.parent" class="sr-only">, more options</span>
611
+ </component>
612
+ <span v-if="row.path" class="text-xs text-black/65">{{ row.path }}</span>
613
+ <span v-if="row.option.description" class="text-xs text-black/65">{{ row.option.description }}</span>
614
+ </span>
615
+ <!-- Parents: an arrow into their options. Others: the tick's
616
+ space, reserved on every row so labels don't shift. -->
617
+ <span class="flex h-5 w-4 shrink-0 items-center justify-center">
618
+ <ChevronRight v-if="row.parent" aria-hidden="true" class="size-4 opacity-70" />
619
+ <component :is="parts.Indicator" v-else><Check class="size-4" /></component>
620
+ </span>
621
+ </component>
622
+ </component>
623
+ </template>
624
+ </component>
625
+ </component>
626
+ </component>
627
+ </component>
628
+ </template>
629
+ </FormField>
630
+ </template>