gooee 0.48.0 → 0.51.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 (185) hide show
  1. package/package.json +4 -2
  2. package/scripts/add.js +28 -6
  3. package/src/components/Accordion/Accordion.vue +62 -0
  4. package/src/components/Accordion/AccordionItem.vue +45 -0
  5. package/src/components/Accordion/accordionContext.ts +6 -0
  6. package/src/components/Alert/Alert.tsx +7 -27
  7. package/src/components/Alert/Alert.vue +59 -0
  8. package/src/components/AlertDialog/AlertDialog.vue +126 -0
  9. package/src/components/AnimatedNumber/AnimatedNumber.vue +36 -0
  10. package/src/components/AspectRatio/AspectRatio.vue +17 -0
  11. package/src/components/Avatar/Avatar.vue +56 -0
  12. package/src/components/AvatarGroup/AvatarGroup.vue +42 -0
  13. package/src/components/Badge/Badge.vue +32 -0
  14. package/src/components/Breadcrumb/Breadcrumb.vue +58 -0
  15. package/src/components/Button/Button.vue +130 -0
  16. package/src/components/ButtonGroup/ButtonGroup.tsx +4 -21
  17. package/src/components/ButtonGroup/ButtonGroup.vue +33 -0
  18. package/src/components/Card/Card.vue +34 -0
  19. package/src/components/Center/Center.vue +32 -0
  20. package/src/components/Checkbox/Checkbox.tsx +8 -25
  21. package/src/components/Checkbox/Checkbox.vue +108 -0
  22. package/src/components/CheckboxGroup/CheckboxGroup.vue +76 -0
  23. package/src/components/Cluster/Cluster.vue +34 -0
  24. package/src/components/CodeBlock/CodeBlock.vue +45 -0
  25. package/src/components/Collapsible/Collapsible.vue +59 -0
  26. package/src/components/Container/Container.vue +22 -0
  27. package/src/components/CopyButton/CopyButton.tsx +3 -49
  28. package/src/components/CopyButton/CopyButton.vue +116 -0
  29. package/src/components/Dial/Dial.tsx +12 -120
  30. package/src/components/Dial/Dial.vue +228 -0
  31. package/src/components/Dialog/Dialog.vue +139 -0
  32. package/src/components/Divider/Divider.vue +27 -0
  33. package/src/components/DropdownMenu/DropdownMenu.tsx +11 -46
  34. package/src/components/DropdownMenu/DropdownMenu.vue +185 -0
  35. package/src/components/EmptyState/EmptyState.vue +33 -0
  36. package/src/components/Fab/Fab.tsx +4 -14
  37. package/src/components/Fab/Fab.vue +115 -0
  38. package/src/components/Fieldset/Fieldset.vue +42 -0
  39. package/src/components/FormField/FormField.tsx +2 -17
  40. package/src/components/FormField/FormField.vue +64 -0
  41. package/src/components/Grid/Grid.vue +43 -0
  42. package/src/components/HoldToConfirm/HoldToConfirm.tsx +13 -142
  43. package/src/components/HoldToConfirm/HoldToConfirm.vue +281 -0
  44. package/src/components/IconButton/IconButton.vue +25 -0
  45. package/src/components/Input/Input.vue +112 -0
  46. package/src/components/InputGroup/InputGroup.vue +102 -0
  47. package/src/components/Kbd/Kbd.tsx +2 -12
  48. package/src/components/Kbd/Kbd.vue +21 -0
  49. package/src/components/Link/Link.vue +38 -0
  50. package/src/components/LivePill/LivePill.tsx +12 -40
  51. package/src/components/LivePill/LivePill.vue +152 -0
  52. package/src/components/Meter/Meter.vue +88 -0
  53. package/src/components/MorphButton/MorphButton.tsx +11 -67
  54. package/src/components/MorphButton/MorphButton.vue +120 -0
  55. package/src/components/NumberInput/NumberInput.tsx +11 -48
  56. package/src/components/NumberInput/NumberInput.vue +184 -0
  57. package/src/components/Pagination/Pagination.tsx +2 -15
  58. package/src/components/Pagination/Pagination.vue +87 -0
  59. package/src/components/Pagination/range.ts +15 -0
  60. package/src/components/PasswordInput/PasswordInput.vue +53 -0
  61. package/src/components/PickerWheel/DateWheel.vue +48 -0
  62. package/src/components/PickerWheel/PickerWheel.tsx +15 -129
  63. package/src/components/PickerWheel/PickerWheel.vue +77 -0
  64. package/src/components/PickerWheel/TimeWheel.vue +50 -0
  65. package/src/components/PickerWheel/WheelColumn.vue +70 -0
  66. package/src/components/Popover/Popover.vue +108 -0
  67. package/src/components/ProgressBar/ProgressBar.vue +48 -0
  68. package/src/components/Prose/Prose.vue +18 -0
  69. package/src/components/Radio/Radio.vue +49 -0
  70. package/src/components/Radio/RadioGroup.vue +69 -0
  71. package/src/components/Retime/Retime.tsx +7 -40
  72. package/src/components/Retime/Retime.vue +64 -0
  73. package/src/components/RulerScrubber/RulerScrubber.tsx +9 -38
  74. package/src/components/RulerScrubber/RulerScrubber.vue +166 -0
  75. package/src/components/ScrollArea/ScrollArea.tsx +17 -129
  76. package/src/components/ScrollArea/ScrollArea.vue +58 -0
  77. package/src/components/SearchInput/SearchInput.tsx +2 -15
  78. package/src/components/SearchInput/SearchInput.vue +70 -0
  79. package/src/components/SegmentedControl/SegmentedControl.tsx +7 -51
  80. package/src/components/SegmentedControl/SegmentedControl.vue +111 -0
  81. package/src/components/Select/Select.tsx +16 -47
  82. package/src/components/Select/Select.vue +236 -0
  83. package/src/components/Sheet/Sheet.tsx +52 -231
  84. package/src/components/Sheet/Sheet.vue +249 -0
  85. package/src/components/Skeleton/Skeleton.vue +38 -0
  86. package/src/components/Slider/Slider.tsx +25 -84
  87. package/src/components/Slider/Slider.vue +113 -0
  88. package/src/components/Spinner/Spinner.vue +30 -0
  89. package/src/components/Split/Split.vue +34 -0
  90. package/src/components/SplitFlap/SplitFlap.tsx +11 -175
  91. package/src/components/SplitFlap/SplitFlap.vue +52 -0
  92. package/src/components/Stack/Stack.vue +22 -0
  93. package/src/components/SwipeDeck/SwipeDeck.tsx +30 -194
  94. package/src/components/SwipeDeck/SwipeDeck.vue +168 -0
  95. package/src/components/Switch/Switch.tsx +6 -75
  96. package/src/components/Switch/Switch.vue +66 -0
  97. package/src/components/Table/Table.module.css +12 -3
  98. package/src/components/Table/Table.tsx +8 -59
  99. package/src/components/Table/Table.vue +274 -0
  100. package/src/components/TableOfContents/TableOfContents.vue +90 -0
  101. package/src/components/Tabs/Tab.vue +37 -0
  102. package/src/components/Tabs/TabList.vue +47 -0
  103. package/src/components/Tabs/TabPanel.vue +26 -0
  104. package/src/components/Tabs/Tabs.module.css +2 -2
  105. package/src/components/Tabs/Tabs.tsx +11 -84
  106. package/src/components/Tabs/Tabs.vue +33 -0
  107. package/src/components/Tabs/tabsContext.ts +15 -0
  108. package/src/components/Tag/Tag.tsx +6 -26
  109. package/src/components/Tag/Tag.vue +63 -0
  110. package/src/components/Textarea/Textarea.tsx +2 -22
  111. package/src/components/Textarea/Textarea.vue +120 -0
  112. package/src/components/ThemeToggle/ThemeToggle.tsx +10 -84
  113. package/src/components/ThemeToggle/ThemeToggle.vue +90 -0
  114. package/src/components/Toast/Toast.tsx +34 -264
  115. package/src/components/Toast/Toast.vue +138 -0
  116. package/src/components/Toast/ToastItem.vue +108 -0
  117. package/src/components/Toggle/Toggle.vue +52 -0
  118. package/src/components/Tooltip/Tooltip.tsx +17 -48
  119. package/src/components/Tooltip/Tooltip.vue +95 -0
  120. package/src/components/VisuallyHidden/VisuallyHidden.vue +17 -0
  121. package/src/internal/FieldMessage.vue +41 -0
  122. package/src/internal/Roll.vue +20 -0
  123. package/src/internal/fling.ts +29 -106
  124. package/src/internal/focus.ts +4 -79
  125. package/src/internal/icons.module.css +39 -27
  126. package/src/internal/layers.ts +7 -61
  127. package/src/internal/liquid.ts +26 -213
  128. package/src/internal/liquidMatter.ts +9 -181
  129. package/src/internal/liquidMore.ts +6 -669
  130. package/src/internal/motion/alert.ts +17 -0
  131. package/src/internal/motion/buttonGroup.ts +21 -0
  132. package/src/internal/motion/checkbox.ts +39 -0
  133. package/src/internal/motion/collapseOut.ts +26 -0
  134. package/src/internal/motion/copyButton.ts +48 -0
  135. package/src/internal/motion/dial.ts +137 -0
  136. package/src/internal/motion/fab.ts +14 -0
  137. package/src/internal/motion/fling.ts +147 -0
  138. package/src/internal/motion/floating.ts +58 -0
  139. package/src/internal/motion/focus.ts +88 -0
  140. package/src/internal/motion/formField.ts +19 -0
  141. package/src/internal/motion/holdToConfirm.ts +147 -0
  142. package/src/internal/motion/index.ts +20 -0
  143. package/src/internal/motion/keys.ts +42 -0
  144. package/src/internal/motion/layers.ts +78 -0
  145. package/src/internal/motion/liquid.ts +241 -0
  146. package/src/internal/motion/liquidMatter.ts +198 -0
  147. package/src/internal/motion/liquidMore.ts +676 -0
  148. package/src/internal/motion/listbox.ts +69 -0
  149. package/src/internal/motion/livePill.ts +71 -0
  150. package/src/internal/motion/menuKeys.ts +58 -0
  151. package/src/internal/motion/morphButton.ts +71 -0
  152. package/src/internal/motion/numberInput.ts +57 -0
  153. package/src/internal/motion/pickerWheel.ts +166 -0
  154. package/src/internal/motion/retime.ts +45 -0
  155. package/src/internal/motion/ruler.ts +57 -0
  156. package/src/internal/motion/scrollArea.ts +133 -0
  157. package/src/internal/motion/scrollFrame.ts +152 -0
  158. package/src/internal/motion/scrubLook.ts +15 -0
  159. package/src/internal/motion/scrubber.ts +154 -0
  160. package/src/internal/motion/segmented.ts +63 -0
  161. package/src/internal/motion/sheetDetents.ts +271 -0
  162. package/src/internal/motion/slider.ts +108 -0
  163. package/src/internal/motion/softPress.ts +334 -0
  164. package/src/internal/motion/splitFlap.ts +205 -0
  165. package/src/internal/motion/swipeDeck.ts +236 -0
  166. package/src/internal/motion/switchThumb.ts +83 -0
  167. package/src/internal/motion/table.ts +77 -0
  168. package/src/internal/motion/tabs.ts +105 -0
  169. package/src/internal/motion/textarea.ts +22 -0
  170. package/src/internal/motion/themeIcon.ts +91 -0
  171. package/src/internal/motion/themeReveal.ts +71 -0
  172. package/src/internal/motion/toast.ts +291 -0
  173. package/src/internal/motion/tooltip.ts +58 -0
  174. package/src/internal/scrollFrame.ts +4 -145
  175. package/src/internal/softPress.ts +27 -289
  176. package/src/internal/themeReveal.ts +3 -61
  177. package/src/internal/useFloating.ts +9 -49
  178. package/src/internal/useScrub.ts +15 -120
  179. package/src/internal/vue/trigger.ts +18 -0
  180. package/src/internal/vue/useFloating.ts +38 -0
  181. package/src/internal/vue/usePresence.ts +21 -0
  182. package/src/internal/vue/useScrub.ts +40 -0
  183. package/src/internal/vueIcons.ts +134 -0
  184. package/src/tokens.css +5 -0
  185. package/src/internal/relativeTime.ts +0 -26
@@ -0,0 +1,111 @@
1
+ <script setup lang="ts">
2
+ import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch, type Component } from 'vue'
3
+ import { createFlow } from '../../internal/motion/liquidMatter'
4
+ import { measureSegments, segmentInk, segmentKey } from '../../internal/motion/segmented'
5
+ import styles from './SegmentedControl.module.css'
6
+
7
+ // The Vue SegmentedControl: the same look (SegmentedControl.module.css), flowing pill and keys as the React one.
8
+
9
+ export type SegmentOption = { value: string; label: string; icon?: Component; disabled?: boolean; ariaLabel?: string }
10
+
11
+ const props = withDefaults(
12
+ defineProps<{
13
+ /** Accessible name for the group. */
14
+ label: string
15
+ /** The segments, in order. */
16
+ options: SegmentOption[]
17
+ /** Height and text size of the segments. */
18
+ size?: 'sm' | 'md'
19
+ /** Stretches the control to fill its container, with equal segments. */
20
+ fullWidth?: boolean
21
+ }>(),
22
+ { size: 'md' },
23
+ )
24
+
25
+ /** The selected value. Use v-model, or leave it out and the first option starts selected. */
26
+ const model = defineModel<string>()
27
+ const local = ref(props.options[0]?.value ?? '')
28
+ const value = computed({
29
+ get: () => model.value ?? local.value,
30
+ set: (next: string) => {
31
+ local.value = next
32
+ model.value = next
33
+ },
34
+ })
35
+
36
+ const root = ref<HTMLElement | null>(null)
37
+ const items = ref<Array<HTMLElement | null>>([])
38
+ const pillEl = ref<HTMLElement | null>(null)
39
+ const pill = ref<{ x: number; w: number } | null>(null)
40
+ const ready = ref(false)
41
+ const index = computed(() => props.options.findIndex((o) => o.value === value.value))
42
+ // With no match, the first enabled segment takes the tab stop.
43
+ const tabStop = computed(() => (index.value >= 0 && !props.options[index.value].disabled ? index.value : props.options.findIndex((o) => !o.disabled)))
44
+
45
+ const measure = () => {
46
+ const place = measureSegments(root.value, items.value[index.value])
47
+ if (place) pill.value = place
48
+ }
49
+ let ro: ResizeObserver | null = null
50
+ onMounted(() => {
51
+ measure()
52
+ ro = new ResizeObserver(measure)
53
+ if (root.value) ro.observe(root.value)
54
+ requestAnimationFrame(() => (ready.value = true))
55
+ })
56
+ watch([index, () => props.options.length], measure, { flush: 'post' })
57
+
58
+ // The pill flows to the new choice (motion/liquidMatter.ts); the labels under it take the chosen colour.
59
+ const flow = createFlow(
60
+ () => root.value,
61
+ () => pillEl.value,
62
+ { cls: styles.liquid, ink: segmentInk(() => root.value, () => items.value, styles.ink) },
63
+ )
64
+ watch(
65
+ () => [index.value, pill.value?.x, pill.value?.w, ready.value] as const,
66
+ () => nextTick(() => flow.update(pill.value, index.value, ready.value)),
67
+ { flush: 'post', immediate: true },
68
+ )
69
+ onBeforeUnmount(() => {
70
+ ro?.disconnect()
71
+ flow.destroy()
72
+ })
73
+
74
+ const onKeyDown = (e: KeyboardEvent) => {
75
+ const next = segmentKey(e.key, tabStop.value, props.options.map((o) => !!o.disabled))
76
+ if (next < 0) return
77
+ e.preventDefault()
78
+ value.value = props.options[next].value
79
+ items.value[next]?.focus()
80
+ }
81
+ </script>
82
+
83
+ <template>
84
+ <div ref="root" role="radiogroup" :aria-label="label" :class="[styles.root, styles[size], fullWidth && styles.full]" @keydown="onKeyDown">
85
+ <span
86
+ v-if="pill"
87
+ ref="pillEl"
88
+ :class="styles.pill"
89
+ :data-ready="ready || undefined"
90
+ :style="{ transform: `translateX(${pill.x}px)`, width: `${pill.w}px` }"
91
+ aria-hidden="true"
92
+ />
93
+ <button
94
+ v-for="(o, i) in options"
95
+ :key="o.value"
96
+ :ref="(el) => (items[i] = el as HTMLElement | null)"
97
+ type="button"
98
+ role="radio"
99
+ :aria-checked="o.value === value"
100
+ :aria-label="o.ariaLabel"
101
+ :tabindex="i === tabStop ? 0 : -1"
102
+ :disabled="o.disabled"
103
+ :class="styles.item"
104
+ :data-on="o.value === value || undefined"
105
+ @click="value = o.value"
106
+ >
107
+ <span v-if="o.icon" :class="styles.icon"><component :is="o.icon" /></span>
108
+ <span v-if="o.label" :class="styles.text">{{ o.label }}</span>
109
+ </button>
110
+ </div>
111
+ </template>
@@ -10,6 +10,7 @@ import { Portal } from '../../internal/Portal'
10
10
  import { useControllable } from '../../internal/useControllable'
11
11
  import { useFloating } from '../../internal/useFloating'
12
12
  import { usePresence } from '../../internal/usePresence'
13
+ import { createListbox } from '../../internal/motion/listbox'
13
14
  import { FieldMessage } from '../../internal/FieldMessage'
14
15
  import styles from './Select.module.css'
15
16
 
@@ -76,7 +77,6 @@ export function Select({
76
77
  const triggerRef = useRef<HTMLButtonElement>(null)
77
78
  const listRef = useRef<HTMLDivElement>(null)
78
79
  const optionRefs = useRef<Array<HTMLDivElement | null>>([])
79
- const typeahead = useRef({ text: '', at: 0 })
80
80
  const { mounted, state } = usePresence(open, 'morph-out')
81
81
  const { style, side, ready } = useFloating(triggerRef, listRef, { open: mounted, side: 'bottom', align: 'start', offset: 6, matchWidth: true })
82
82
  // The list grows out of the field and folds back into it.
@@ -88,17 +88,12 @@ export function Select({
88
88
  const selectedIndex = options.findIndex((o) => o.value === value)
89
89
  const selected = selectedIndex >= 0 ? options[selectedIndex] : null
90
90
 
91
- const enabled = (i: number) => i >= 0 && i < options.length && !options[i].disabled
92
- const step = (from: number, dir: 1 | -1) => {
93
- let i = from
94
- for (let n = 0; n < options.length; n++) {
95
- i = (i + dir + options.length) % options.length
96
- if (enabled(i)) return i
97
- }
98
- return from
99
- }
100
- const first = () => step(-1, 1)
101
- const last = () => step(options.length, -1)
91
+ // Arrows, Home, End, Enter, Space, Tab and type-ahead (motion/listbox.ts).
92
+ const optionsRef = useRef(options)
93
+ optionsRef.current = options
94
+ const list = useRef<ReturnType<typeof createListbox>>(null)
95
+ list.current ??= createListbox(() => optionsRef.current)
96
+ const { enabled, first } = list.current
102
97
 
103
98
  const openList = (at?: number) => {
104
99
  if (disabled) return
@@ -129,41 +124,15 @@ export function Select({
129
124
  }, [open])
130
125
 
131
126
  const onKeyDown = (e: KeyboardEvent<HTMLButtonElement>) => {
132
- const k = e.key
133
- if (!open) {
134
- if (k === 'ArrowDown' || k === 'Enter' || k === ' ') {
135
- e.preventDefault()
136
- openList()
137
- } else if (k === 'ArrowUp') {
138
- e.preventDefault()
139
- openList(enabled(selectedIndex) ? selectedIndex : last())
140
- } else if (k.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) {
141
- openList()
142
- runTypeahead(k)
143
- }
144
- return
145
- }
146
- if (k === 'ArrowDown') { e.preventDefault(); setActive((a) => step(a, 1)) }
147
- else if (k === 'ArrowUp') { e.preventDefault(); setActive((a) => step(a, -1)) }
148
- else if (k === 'Home') { e.preventDefault(); setActive(first()) }
149
- else if (k === 'End') { e.preventDefault(); setActive(last()) }
150
- else if (k === 'Enter' || k === ' ') { e.preventDefault(); choose(active) }
151
- else if (k === 'Tab') { if (enabled(active)) setValue(options[active].value); setOpen(false) }
152
- else if (k.length === 1) runTypeahead(k)
153
- }
154
-
155
- const runTypeahead = (ch: string) => {
156
- const now = Date.now()
157
- const t = typeahead.current
158
- t.text = now - t.at > 600 ? ch.toLowerCase() : t.text + ch.toLowerCase()
159
- t.at = now
160
- const start = Math.max(active, 0)
161
- for (let n = 0; n < options.length; n++) {
162
- const i = (start + n + (t.text.length === 1 ? 1 : 0)) % options.length
163
- if (enabled(i) && options[i].label.toLowerCase().startsWith(t.text)) {
164
- setActive(i)
165
- return
166
- }
127
+ const r = list.current!.key(e, open, active, selectedIndex)
128
+ if (!r) return
129
+ if (r.prevent) e.preventDefault()
130
+ if (r.open) openList(r.active)
131
+ else if (r.active !== undefined) setActive(r.active)
132
+ if (r.choose) choose(active)
133
+ if (r.tab) {
134
+ if (enabled(active)) setValue(options[active].value)
135
+ setOpen(false)
167
136
  }
168
137
  }
169
138
 
@@ -0,0 +1,236 @@
1
+ <script setup lang="ts">
2
+ import { computed, nextTick, onBeforeUnmount, onMounted, ref, useId, watch } from 'vue'
3
+ import FieldMessage from '../../internal/FieldMessage.vue'
4
+ import { dismissable } from '../../internal/motion/layers'
5
+ import { createLiquid } from '../../internal/motion/liquid'
6
+ import { edgeFlow } from '../../internal/motion/liquidMore'
7
+ import { createListbox } from '../../internal/motion/listbox'
8
+ import { useFloating } from '../../internal/vue/useFloating'
9
+ import { usePresence } from '../../internal/vue/usePresence'
10
+ import { CheckIcon, ChevronDownIcon } from '../../internal/vueIcons'
11
+ import styles from './Select.module.css'
12
+
13
+ // The Vue Select: the same look (Select.module.css), list morph, focus edge and keys as the React one.
14
+
15
+ export type SelectOption = { value: string; label: string; disabled?: boolean; hint?: string }
16
+
17
+ const props = withDefaults(
18
+ defineProps<{
19
+ /** Visible name of the field, also read by screen readers. */
20
+ label: string
21
+ /** Keeps the label for screen readers only, for a list where a column heading already names the field. */
22
+ hideLabel?: boolean
23
+ /** Choices in the list, in order. */
24
+ options: SelectOption[]
25
+ /** Starting value when there is no v-model. */
26
+ defaultValue?: string | null
27
+ /** Shown when nothing is chosen yet. */
28
+ placeholder?: string
29
+ /** Hint under the field. Hidden while an error shows. */
30
+ help?: string
31
+ /** Error message under the field. Also marks the field invalid. */
32
+ error?: string
33
+ /** Shown in the open list when there are no options. */
34
+ emptyText?: string
35
+ /** Stops the list from opening. */
36
+ disabled?: boolean
37
+ /** Submits the selected value in a hidden field with this name. */
38
+ name?: string
39
+ /** Id of the button, so a label's `for` can point at it. Inner ids add a suffix to it. */
40
+ id?: string
41
+ }>(),
42
+ { hideLabel: false, defaultValue: null, placeholder: 'Choose one', emptyText: 'No options' },
43
+ )
44
+
45
+ /** The selected value. Use v-model, or leave it out and the field keeps its own. Null means nothing is chosen. */
46
+ const model = defineModel<string | null>()
47
+ const local = ref(props.defaultValue)
48
+ const value = computed({
49
+ get: () => (model.value === undefined ? local.value : model.value),
50
+ set: (next: string | null) => {
51
+ local.value = next
52
+ model.value = next
53
+ },
54
+ })
55
+
56
+ const auto = useId()
57
+ const baseId = computed(() => props.id ?? auto)
58
+ const labelId = computed(() => `${baseId.value}-label`)
59
+ const listId = computed(() => `${baseId.value}-list`)
60
+ const helpId = computed(() => `${baseId.value}-help`)
61
+ const errorId = computed(() => `${baseId.value}-error`)
62
+ const emptyId = computed(() => `${baseId.value}-empty`)
63
+ const triggerId = computed(() => props.id ?? `${auto}-trigger`)
64
+
65
+ const open = ref(false)
66
+ const active = ref(-1)
67
+ const trigger = ref<HTMLButtonElement | null>(null)
68
+ const listEl = ref<HTMLElement | null>(null)
69
+ const optionEls: Array<HTMLElement | null> = []
70
+ const { mounted, state } = usePresence(open, 'morph-out')
71
+ const { style, side, ready } = useFloating(trigger, listEl, mounted, { side: 'bottom', align: 'start', offset: 6, matchWidth: true })
72
+ // Vue, unlike React, adds no px to a number in a style.
73
+ const selectedIndex = computed(() => props.options.findIndex((o) => o.value === value.value))
74
+ const selected = computed(() => (selectedIndex.value >= 0 ? props.options[selectedIndex.value] : null))
75
+
76
+ // Arrows, Home, End, Enter, Space, Tab and type-ahead (motion/listbox.ts).
77
+ const list = createListbox(() => props.options)
78
+ const { enabled, first } = list
79
+
80
+ const openList = (at?: number) => {
81
+ if (props.disabled) return
82
+ open.value = true
83
+ active.value = at ?? (enabled(selectedIndex.value) ? selectedIndex.value : first())
84
+ }
85
+ const close = (focusTrigger = true) => {
86
+ open.value = false
87
+ if (focusTrigger) trigger.value?.focus()
88
+ }
89
+ const choose = (i: number) => {
90
+ if (!enabled(i)) return
91
+ value.value = props.options[i].value
92
+ close()
93
+ }
94
+
95
+ const onKeyDown = (e: KeyboardEvent) => {
96
+ const r = list.key(e, open.value, active.value, selectedIndex.value)
97
+ if (!r) return
98
+ if (r.prevent) e.preventDefault()
99
+ if (r.open) openList(r.active)
100
+ else if (r.active !== undefined) active.value = r.active
101
+ if (r.choose) choose(active.value)
102
+ if (r.tab) {
103
+ if (enabled(active.value)) value.value = props.options[active.value].value
104
+ open.value = false
105
+ }
106
+ }
107
+
108
+ // The list grows out of the field and folds back into it (motion/liquid.ts).
109
+ const liquid = createLiquid(
110
+ () => listEl.value,
111
+ () => trigger.value,
112
+ )
113
+ // Waits for the render that puts the list in its place, so the grow starts from the right shape.
114
+ watch(
115
+ [state, ready],
116
+ async () => {
117
+ if (!ready.value) return
118
+ await nextTick()
119
+ liquid.play(state.value)
120
+ },
121
+ { flush: 'post' },
122
+ )
123
+
124
+ // Esc and a press outside close the list; only Esc puts focus back on the field.
125
+ let undismiss = () => {}
126
+ watch(
127
+ open,
128
+ (now) => {
129
+ undismiss()
130
+ undismiss = now ? dismissable(() => [trigger.value, listEl.value], (reason) => close(reason === 'escape')) : () => {}
131
+ },
132
+ { flush: 'post' },
133
+ )
134
+
135
+ // One highlight glides between the options.
136
+ const hl = ref<{ top: number; height: number } | null>(null)
137
+ watch(
138
+ [active, open, mounted],
139
+ () => {
140
+ if (!open.value) return (hl.value = null)
141
+ const el = optionEls[active.value]
142
+ if (!el) return
143
+ hl.value = { top: el.offsetTop, height: el.offsetHeight }
144
+ el.scrollIntoView({ block: 'nearest' })
145
+ },
146
+ { flush: 'post' },
147
+ )
148
+
149
+ // Open or keyboard focused, the field's dark edge flows round it from where it was pressed (motion/liquidMore.ts).
150
+ let stopEdge = () => {}
151
+ onMounted(() => {
152
+ if (trigger.value) stopEdge = edgeFlow(trigger.value, trigger.value, ':is([data-open], :focus-visible)', styles.edge)
153
+ })
154
+ onBeforeUnmount(() => {
155
+ stopEdge()
156
+ undismiss()
157
+ liquid.destroy()
158
+ })
159
+
160
+ const empty = computed(() => props.options.length === 0)
161
+ const describedBy = computed(
162
+ () => [props.help && !props.error ? helpId.value : null, props.error ? errorId.value : null, open.value && empty.value ? emptyId.value : null].filter(Boolean).join(' ') || undefined,
163
+ )
164
+ </script>
165
+
166
+ <template>
167
+ <div :class="styles.root">
168
+ <span :id="labelId" :class="[styles.label, hideLabel && styles.hiddenLabel]" @click="trigger?.focus()">
169
+ {{ label }}
170
+ </span>
171
+ <button
172
+ :id="triggerId"
173
+ ref="trigger"
174
+ type="button"
175
+ role="combobox"
176
+ :class="styles.trigger"
177
+ :data-open="open || undefined"
178
+ :data-invalid="error ? true : undefined"
179
+ :aria-labelledby="`${labelId} ${triggerId}`"
180
+ aria-haspopup="listbox"
181
+ :aria-expanded="open"
182
+ :aria-controls="listId"
183
+ :aria-activedescendant="open && enabled(active) ? `${baseId}-opt-${active}` : undefined"
184
+ :aria-invalid="error ? true : undefined"
185
+ :aria-describedby="describedBy"
186
+ :disabled="disabled"
187
+ @click="open ? close() : openList()"
188
+ @keydown="onKeyDown"
189
+ >
190
+ <span :class="[styles.value, !selected && styles.placeholder]">
191
+ <span :key="selected?.value ?? 'none'" :class="styles.valueText" :title="selected ? selected.label : placeholder">
192
+ {{ selected ? selected.label : placeholder }}
193
+ </span>
194
+ </span>
195
+ <ChevronDownIcon :class="styles.chevron" />
196
+ </button>
197
+ <input v-if="name" type="hidden" :name="name" :value="value ?? ''" />
198
+ <FieldMessage :help-id="helpId" :error-id="errorId" :help="help" :error="error" />
199
+ <Teleport v-if="mounted" to="body">
200
+ <div
201
+ :id="listId"
202
+ ref="listEl"
203
+ role="listbox"
204
+ :aria-labelledby="labelId"
205
+ tabindex="-1"
206
+ :class="styles.list"
207
+ :data-state="state"
208
+ :data-side="side"
209
+ :style="style"
210
+ @mousedown.prevent
211
+ >
212
+ <span v-if="hl" :class="styles.highlight" :style="{ transform: `translateY(${hl.top}px)`, height: `${hl.height}px` }" aria-hidden="true" />
213
+ <div
214
+ v-for="(o, i) in options"
215
+ :id="`${baseId}-opt-${i}`"
216
+ :key="o.value"
217
+ :ref="(el) => (optionEls[i] = el as HTMLElement | null)"
218
+ role="option"
219
+ :aria-selected="o.value === value"
220
+ :aria-disabled="o.disabled || undefined"
221
+ :class="styles.option"
222
+ :data-active="i === active || undefined"
223
+ :style="{ '--i': i }"
224
+ @pointermove="enabled(i) && active !== i && (active = i)"
225
+ @click="choose(i)"
226
+ >
227
+ <span :class="styles.optionText">{{ o.label }}<span v-if="o.hint" :class="styles.hint">{{ o.hint }}</span></span>
228
+ <CheckIcon :class="styles.check" :data-on="o.value === value || undefined" />
229
+ </div>
230
+ <div v-if="empty" :id="emptyId" :class="styles.empty">
231
+ {{ emptyText }}
232
+ </div>
233
+ </div>
234
+ </Teleport>
235
+ </div>
236
+ </template>