@noix-dev/brix-vue 0.0.0-stage → 0.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 (255) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +74 -2
  3. package/package.json +117 -3
  4. package/src/components/address-field/AddressField.vue +337 -0
  5. package/src/components/address-field/index.ts +1 -0
  6. package/src/components/appearance-picker/AppearancePicker.vue +502 -0
  7. package/src/components/appearance-picker/AppearanceSegment.vue +175 -0
  8. package/src/components/appearance-picker/index.ts +44 -0
  9. package/src/components/autofill/AutoFillDialog.vue +264 -0
  10. package/src/components/autofill/index.ts +1 -0
  11. package/src/components/badge/Badge.vue +23 -0
  12. package/src/components/badge/index.ts +106 -0
  13. package/src/components/build-stamp/BuildStamp.vue +62 -0
  14. package/src/components/build-stamp/index.ts +1 -0
  15. package/src/components/button/Button.vue +31 -0
  16. package/src/components/button/index.ts +122 -0
  17. package/src/components/card/Card.vue +29 -0
  18. package/src/components/card/CardAction.vue +17 -0
  19. package/src/components/card/CardContent.vue +14 -0
  20. package/src/components/card/CardDescription.vue +14 -0
  21. package/src/components/card/CardFooter.vue +22 -0
  22. package/src/components/card/CardHeader.vue +22 -0
  23. package/src/components/card/CardTitle.vue +29 -0
  24. package/src/components/card/index.ts +47 -0
  25. package/src/components/chart/BarChart.vue +123 -0
  26. package/src/components/chart/StackedBarChart.vue +149 -0
  27. package/src/components/chart/appearanceRevision.ts +22 -0
  28. package/src/components/chart/chartColor.ts +59 -0
  29. package/src/components/chart/index.ts +6 -0
  30. package/src/components/checkbox/Checkbox.vue +61 -0
  31. package/src/components/checkbox/index.ts +1 -0
  32. package/src/components/code-editor/CodeEditor.vue +352 -0
  33. package/src/components/code-editor/index.ts +9 -0
  34. package/src/components/code-editor/syntaxTheme.ts +110 -0
  35. package/src/components/colour-picker/ColourPicker.vue +421 -0
  36. package/src/components/colour-picker/index.ts +1 -0
  37. package/src/components/combobox/Combobox.vue +337 -0
  38. package/src/components/combobox/index.ts +1 -0
  39. package/src/components/comments/CommentComposer.vue +302 -0
  40. package/src/components/comments/CommentMessage.vue +259 -0
  41. package/src/components/comments/CommentThread.vue +250 -0
  42. package/src/components/comments/commentBody.ts +185 -0
  43. package/src/components/comments/commentLevels.ts +139 -0
  44. package/src/components/comments/commentNode.ts +35 -0
  45. package/src/components/comments/composer.ts +20 -0
  46. package/src/components/comments/index.ts +29 -0
  47. package/src/components/confirm-dialog/ConfirmDialog.vue +153 -0
  48. package/src/components/confirm-dialog/index.ts +1 -0
  49. package/src/components/containers/Box.vue +42 -0
  50. package/src/components/containers/Section.vue +88 -0
  51. package/src/components/containers/SectionAction.vue +17 -0
  52. package/src/components/containers/SectionContent.vue +14 -0
  53. package/src/components/containers/SectionDescription.vue +14 -0
  54. package/src/components/containers/SectionHeader.vue +22 -0
  55. package/src/components/containers/SectionTitle.vue +17 -0
  56. package/src/components/containers/index.ts +9 -0
  57. package/src/components/data-table/ColumnMenu.vue +241 -0
  58. package/src/components/data-table/DataTable.vue +447 -0
  59. package/src/components/data-table/ExpandCaret.vue +34 -0
  60. package/src/components/data-table/SortableHead.vue +184 -0
  61. package/src/components/data-table/TableLoadingRow.vue +13 -0
  62. package/src/components/data-table/TableProgressRow.vue +26 -0
  63. package/src/components/data-table/columns.ts +13 -0
  64. package/src/components/data-table/index.ts +3 -0
  65. package/src/components/data-table/pinned.ts +19 -0
  66. package/src/components/data-table/rowActivation.ts +14 -0
  67. package/src/components/data-table/sort.ts +13 -0
  68. package/src/components/data-table/types.ts +23 -0
  69. package/src/components/date-picker/CalendarPanel.vue +432 -0
  70. package/src/components/date-picker/DateField.vue +293 -0
  71. package/src/components/date-picker/DatePicker.vue +147 -0
  72. package/src/components/date-picker/UntilDatePicker.vue +96 -0
  73. package/src/components/date-picker/dateFieldGuard.ts +87 -0
  74. package/src/components/date-picker/index.ts +4 -0
  75. package/src/components/date-picker/useDateField.ts +37 -0
  76. package/src/components/date-range-picker/DateRangePicker.vue +247 -0
  77. package/src/components/date-range-picker/index.ts +1 -0
  78. package/src/components/date-time-picker/DateTimePicker.vue +291 -0
  79. package/src/components/date-time-picker/index.ts +1 -0
  80. package/src/components/dialog/Dialog.vue +250 -0
  81. package/src/components/dialog/DialogSurface.vue +214 -0
  82. package/src/components/dialog/dialogEdges.ts +65 -0
  83. package/src/components/dialog/dialogSizes.ts +35 -0
  84. package/src/components/dialog/index.ts +12 -0
  85. package/src/components/dialog/toastHost.ts +18 -0
  86. package/src/components/dropdown/Dropdown.vue +101 -0
  87. package/src/components/dropdown/DropdownItems.vue +154 -0
  88. package/src/components/dropdown/dropdown.ts +64 -0
  89. package/src/components/dropdown/index.ts +2 -0
  90. package/src/components/file-preview/FilePreview.vue +78 -0
  91. package/src/components/file-preview/index.ts +1 -0
  92. package/src/components/file-upload/FileUpload.vue +234 -0
  93. package/src/components/file-upload/index.ts +1 -0
  94. package/src/components/filter-check-menu/FilterCheckMenu.vue +562 -0
  95. package/src/components/filter-check-menu/filterCheckOption.ts +10 -0
  96. package/src/components/filter-check-menu/index.ts +2 -0
  97. package/src/components/form-field/FormField.vue +74 -0
  98. package/src/components/form-field/index.ts +2 -0
  99. package/src/components/form-field/types.ts +5 -0
  100. package/src/components/frames/FrameCentred.vue +137 -0
  101. package/src/components/frames/FrameSidebar.vue +113 -0
  102. package/src/components/frames/FrameSplit.vue +109 -0
  103. package/src/components/frames/index.ts +3 -0
  104. package/src/components/icon/BellIcon.vue +27 -0
  105. package/src/components/icon/BoldIcon.vue +31 -0
  106. package/src/components/icon/CalendarDaysIcon.vue +33 -0
  107. package/src/components/icon/CheckIcon.vue +31 -0
  108. package/src/components/icon/ChevronDownIcon.vue +30 -0
  109. package/src/components/icon/ChevronLeftIcon.vue +31 -0
  110. package/src/components/icon/ChevronRightIcon.vue +31 -0
  111. package/src/components/icon/ChevronUpIcon.vue +30 -0
  112. package/src/components/icon/CircleAlertIcon.vue +29 -0
  113. package/src/components/icon/ClockIcon.vue +31 -0
  114. package/src/components/icon/Columns3Icon.vue +32 -0
  115. package/src/components/icon/FlipHorizontalIcon.vue +29 -0
  116. package/src/components/icon/ItalicIcon.vue +28 -0
  117. package/src/components/icon/LinkIcon.vue +27 -0
  118. package/src/components/icon/ListIcon.vue +29 -0
  119. package/src/components/icon/ListOrderedIcon.vue +28 -0
  120. package/src/components/icon/PenLineIcon.vue +31 -0
  121. package/src/components/icon/PlusIcon.vue +28 -0
  122. package/src/components/icon/RotateCcwIcon.vue +28 -0
  123. package/src/components/icon/RotateCwIcon.vue +28 -0
  124. package/src/components/icon/SearchIcon.vue +31 -0
  125. package/src/components/icon/StarIcon.vue +29 -0
  126. package/src/components/icon/TriangleAlertIcon.vue +29 -0
  127. package/src/components/icon/UnderlineIcon.vue +27 -0
  128. package/src/components/icon/Undo2Icon.vue +28 -0
  129. package/src/components/icon/XIcon.vue +31 -0
  130. package/src/components/icon/ZoomInIcon.vue +30 -0
  131. package/src/components/icon/ZoomOutIcon.vue +29 -0
  132. package/src/components/icon/index.ts +37 -0
  133. package/src/components/icon-picker/IconPicker.vue +560 -0
  134. package/src/components/icon-picker/index.ts +1 -0
  135. package/src/components/image-crop-dialog/CropRatioRow.vue +132 -0
  136. package/src/components/image-crop-dialog/CropVariantStrip.vue +277 -0
  137. package/src/components/image-crop-dialog/ImageCropDialog.vue +1541 -0
  138. package/src/components/image-crop-dialog/index.ts +16 -0
  139. package/src/components/image-upload/ImageUpload.vue +345 -0
  140. package/src/components/image-upload/index.ts +2 -0
  141. package/src/components/input/Input.vue +48 -0
  142. package/src/components/input/index.ts +1 -0
  143. package/src/components/keycap/Keycap.vue +49 -0
  144. package/src/components/keycap/index.ts +1 -0
  145. package/src/components/label/Label.vue +26 -0
  146. package/src/components/label/index.ts +1 -0
  147. package/src/components/menu/MenuCheckboxItem.vue +63 -0
  148. package/src/components/menu/MenuContent.vue +45 -0
  149. package/src/components/menu/MenuGroup.vue +22 -0
  150. package/src/components/menu/MenuItem.vue +34 -0
  151. package/src/components/menu/MenuLabel.vue +22 -0
  152. package/src/components/menu/MenuRadioGroup.vue +22 -0
  153. package/src/components/menu/MenuRadioItem.vue +50 -0
  154. package/src/components/menu/MenuRoot.vue +25 -0
  155. package/src/components/menu/MenuSeparator.vue +27 -0
  156. package/src/components/menu/MenuSub.vue +29 -0
  157. package/src/components/menu/MenuSubContent.vue +48 -0
  158. package/src/components/menu/MenuSubTrigger.vue +49 -0
  159. package/src/components/menu/MenuTrigger.vue +22 -0
  160. package/src/components/menu/index.ts +20 -0
  161. package/src/components/menu/menuClasses.ts +74 -0
  162. package/src/components/morph-select/MorphSelect.vue +150 -0
  163. package/src/components/morph-select/index.ts +1 -0
  164. package/src/components/nav/NavFlyout.vue +343 -0
  165. package/src/components/nav/NavGroup.vue +306 -0
  166. package/src/components/nav/NavItem.vue +243 -0
  167. package/src/components/nav/NavNodes.ts +66 -0
  168. package/src/components/nav/NavSection.vue +105 -0
  169. package/src/components/nav/NavSidebar.vue +137 -0
  170. package/src/components/nav/index.ts +5 -0
  171. package/src/components/nav/navChain.ts +112 -0
  172. package/src/components/nav/navClasses.ts +229 -0
  173. package/src/components/nav/navNode.ts +10 -0
  174. package/src/components/nav/navRegistry.ts +24 -0
  175. package/src/components/notification-list/NotificationList.vue +314 -0
  176. package/src/components/notification-list/index.ts +2 -0
  177. package/src/components/notification-list/notifications.ts +21 -0
  178. package/src/components/pagination/Pagination.vue +296 -0
  179. package/src/components/pagination/index.ts +1 -0
  180. package/src/components/pin-field/PinField.vue +332 -0
  181. package/src/components/pin-field/index.ts +1 -0
  182. package/src/components/popover-panel/PopoverPanel.vue +115 -0
  183. package/src/components/popover-panel/index.ts +5 -0
  184. package/src/components/rating/Rating.vue +201 -0
  185. package/src/components/rating/index.ts +1 -0
  186. package/src/components/record-avatar/RecordAvatar.vue +76 -0
  187. package/src/components/record-avatar/index.ts +1 -0
  188. package/src/components/result-window/ResultWindow.vue +399 -0
  189. package/src/components/result-window/index.ts +2 -0
  190. package/src/components/result-window/results.ts +75 -0
  191. package/src/components/rich-editor/RichEditor.vue +370 -0
  192. package/src/components/rich-editor/RichEditorLinkDialog.vue +133 -0
  193. package/src/components/rich-editor/index.ts +8 -0
  194. package/src/components/row-actions/RowActions.vue +262 -0
  195. package/src/components/row-actions/index.ts +2 -0
  196. package/src/components/row-actions/rowActions.ts +44 -0
  197. package/src/components/search-select/SearchSelect.vue +599 -0
  198. package/src/components/search-select/index.ts +2 -0
  199. package/src/components/search-select/types.ts +14 -0
  200. package/src/components/segmented-choice/SegmentedChoice.vue +136 -0
  201. package/src/components/segmented-choice/index.ts +2 -0
  202. package/src/components/segmented-choice/segmentedOption.ts +6 -0
  203. package/src/components/select/Select.vue +18 -0
  204. package/src/components/select/SelectContent.vue +73 -0
  205. package/src/components/select/SelectGroup.vue +21 -0
  206. package/src/components/select/SelectItem.vue +47 -0
  207. package/src/components/select/SelectItemText.vue +12 -0
  208. package/src/components/select/SelectLabel.vue +17 -0
  209. package/src/components/select/SelectScrollDownButton.vue +36 -0
  210. package/src/components/select/SelectScrollUpButton.vue +36 -0
  211. package/src/components/select/SelectSeparator.vue +19 -0
  212. package/src/components/select/SelectTrigger.vue +52 -0
  213. package/src/components/select/SelectValue.vue +12 -0
  214. package/src/components/select/index.ts +11 -0
  215. package/src/components/session/IdleTimer.vue +71 -0
  216. package/src/components/session/SessionDialogs.vue +177 -0
  217. package/src/components/session/index.ts +2 -0
  218. package/src/components/split-button/SplitButton.vue +122 -0
  219. package/src/components/split-button/index.ts +2 -0
  220. package/src/components/split-button/splitButtonItem.ts +7 -0
  221. package/src/components/switch/Switch.vue +60 -0
  222. package/src/components/switch/index.ts +1 -0
  223. package/src/components/table/Table.vue +51 -0
  224. package/src/components/table/TableBody.vue +14 -0
  225. package/src/components/table/TableCaption.vue +17 -0
  226. package/src/components/table/TableCell.vue +22 -0
  227. package/src/components/table/TableEmpty.vue +72 -0
  228. package/src/components/table/TableFooter.vue +17 -0
  229. package/src/components/table/TableHead.vue +22 -0
  230. package/src/components/table/TableHeader.vue +14 -0
  231. package/src/components/table/TableRow.vue +22 -0
  232. package/src/components/table/index.ts +9 -0
  233. package/src/components/tabs/Tabs.vue +173 -0
  234. package/src/components/tabs/index.ts +2 -0
  235. package/src/components/tabs/tabItem.ts +12 -0
  236. package/src/components/textarea/LimitedTextarea.vue +81 -0
  237. package/src/components/textarea/TextLimitCounter.vue +107 -0
  238. package/src/components/textarea/Textarea.vue +42 -0
  239. package/src/components/textarea/index.ts +4 -0
  240. package/src/components/textarea/textLimit.ts +43 -0
  241. package/src/components/time-picker/TimePicker.vue +522 -0
  242. package/src/components/time-picker/index.ts +1 -0
  243. package/src/components/toast/ToastHost.vue +276 -0
  244. package/src/components/toast/index.ts +1 -0
  245. package/src/components/update-notice/UpdateNotice.vue +103 -0
  246. package/src/components/update-notice/index.ts +1 -0
  247. package/src/components/user-menu/UserMenu.vue +154 -0
  248. package/src/components/user-menu/index.ts +1 -0
  249. package/src/index.ts +182 -0
  250. package/src/lib/cn.ts +21 -0
  251. package/src/lib/panelFocus.ts +65 -0
  252. package/src/lib/sessionMonitor.ts +390 -0
  253. package/src/lib/slotContent.ts +17 -0
  254. package/src/lib/updateCheck.ts +133 -0
  255. package/src/styles.css +82 -0
@@ -0,0 +1,421 @@
1
+ <script setup lang="ts">
2
+ import { PALETTE, normaliseHex, useUiLabels } from '@noix-dev/brix-core';
3
+ import { computed, nextTick, ref, useAttrs, useId, useTemplateRef, watch } from 'vue';
4
+ import type { HTMLAttributes } from 'vue';
5
+ import { cn } from '../../lib/cn';
6
+ import { ChevronDownIcon, ChevronUpIcon, XIcon } from '../icon';
7
+ import { Label } from '../label';
8
+ import { PopoverPanel } from '../popover-panel';
9
+
10
+ /**
11
+ * A colour field: a round swatch and the hex beside it, and a panel of fifty swatches, a
12
+ * disclosure for typing a hex of one's own, and an entry for no colour at all. The model is
13
+ * `'#RRGGBB'` and `''` for none. It replaces the native colour input, which cannot be empty:
14
+ * it always holds a colour, so "no colour chosen" and "black" are the same value there, and a
15
+ * form that must tell them apart has no way to. Here empty is a real value and the field says
16
+ * so ("No colour", dashed swatch), and it is not black.
17
+ *
18
+ * THE PANEL is the `PopoverPanel` shell, named by its trigger. The trigger is the whole field,
19
+ * and its own `aria-label` composes the action with the field's label ("Choose a colour,
20
+ * Category colour"), so two pickers on one form open panels that are announced differently. It
21
+ * is the trigger's own `aria-label`, not an `aria-labelledby` pointing at the label: the panel
22
+ * reaches the trigger by `aria-labelledby`, and a name computed through one reference ignores
23
+ * the second. The value the field shows is attached to the trigger as its description.
24
+ *
25
+ * KEYBOARD. The grid is ONE tab stop: the listbox itself takes focus when the panel opens and
26
+ * names the active swatch with `aria-activedescendant`, and no swatch is focusable. Fifty tab
27
+ * stops would be fifty presses to get past the field, and a roving tabindex would still put
28
+ * focus on one of fifty buttons; the listbox owns focus, the swatches are only options.
29
+ * Arrows move one swatch (Left and Right) or one row (Up and Down), Home and End go to the
30
+ * ends, and Enter or Space picks the active one. Moving picks nothing.
31
+ *
32
+ * A COLOUR THE PALETTE DOES NOT HOLD (typed by someone, or stored by an older version) is shown
33
+ * as chosen, through the custom row: the row is open when the panel is, and its field carries
34
+ * the hex. No swatch is marked, because none is it. A stored colour is compared in its
35
+ * normalised form, so `#0d4f4f` marks the same swatch as `#0D4F4F`.
36
+ *
37
+ * THE CUSTOM ROW commits on Enter, and only on Enter: `#abc` is a whole colour on the way to
38
+ * `#abcdef`, so committing as typed would change the model under the fingers, and leaving the
39
+ * field commits nothing either, because a blur is not a decision (a click on blank space in the
40
+ * panel, or on a button in a browser that does not focus buttons, blurs it with the panel still
41
+ * open). The draft lives as long as the panel is open and nothing that happens meanwhile touches
42
+ * it; closing the panel any other way (Escape, a click outside) discards it, and the next
43
+ * opening starts from the held colour. Text that is not a hex is marked invalid on Enter and
44
+ * is not committed.
45
+ *
46
+ * THE ACTIVE SWATCH'S RING IS NOT GATED on `group-focus-visible:`, and the two sibling pickers
47
+ * agree on that. MEASURED in a browser: after the panel's programmatic `list.focus()` the listbox
48
+ * holds focus and the active cell does NOT match `:focus-visible` (a programmatic `.focus()` sets
49
+ * none, CLAUDE.md "Measuring this system in a browser"), so the gate would draw nothing, while
50
+ * `aria-activedescendant` still announced each swatch and no test would notice. Ungated, the ring
51
+ * is drawn (`0 0 0 2px` of the background, then `0 0 0 4px` of `--foreground`). It shows whenever
52
+ * a swatch is active, and one always is (`active` is a valid index from the first render), so the
53
+ * ring stays on that swatch while focus sits on Custom, the hex field or No colour: it marks where
54
+ * the grid's cursor is, not where focus is, and the cursor does not leave the grid.
55
+ *
56
+ * THE GLYPHS are slots (ADR 0013): `chevron-icon` (the field's), `expand-icon` and
57
+ * `collapse-icon` (the custom row's disclosure, closed and open) and `clear-icon` (the "No
58
+ * colour" entry).
59
+ *
60
+ * CONTRAST, MEASURED with `packages/tokens/src/contrast.ts` over the six theme and mode pairs
61
+ * (kingfisher light and dark, graphite light and dark, noix light and dark), on the `--popover`
62
+ * each sits on. The playground's walk reads text only, and a swatch carries none:
63
+ *
64
+ * --ring on --popover (the chosen swatch's outline) 9.34 12.11 9.65 6.46 3.28 7.51
65
+ * --foreground on --popover (the active swatch's ring) 19.79 17.16 17.80 14.11 17.39 14.59
66
+ * --muted-foreground on --popover (the custom row's edge) 4.73 6.91 5.60 7.20 5.54 7.61
67
+ *
68
+ * The custom row's field draws `border-muted-foreground`, the resting edge of every field in the
69
+ * catalogue; it drew `--input` until a review measured that at 1.26 1.57 1.28 1.50 1.26 1.66 over
70
+ * the same ground, an order of magnitude under the 3:1 an edge needs. A swatch's own edge is
71
+ * `--foreground/15`, which over the popover reads 1.40 1.55 1.37 1.53 1.36 1.52: it is decoration
72
+ * on a filled swatch, whose colour is what tells swatches apart, and is recorded rather than
73
+ * raised.
74
+ *
75
+ * WORDING comes from the labels contract (`colourPickerChoose`, `colourPickerNone`,
76
+ * `colourPickerCustom`); `labels` overrides member by member and the rest keep the contract's.
77
+ *
78
+ * Attributes (`id`, `name`, `data-testid`, `aria-*`) land on the trigger; `class` and `style` on
79
+ * the outer element. The outer element, its label and the field box carry the `form-field`,
80
+ * `label` and `input` slots, so a page's density settings reach this field as they reach every
81
+ * other.
82
+ */
83
+ defineOptions({ inheritAttrs: false });
84
+
85
+ interface ColourPickerLabels {
86
+ colourPickerChoose: string;
87
+ colourPickerNone: string;
88
+ colourPickerCustom: string;
89
+ }
90
+
91
+ const props = withDefaults(
92
+ defineProps<{
93
+ /** `'#RRGGBB'`; `''` for no colour. */
94
+ modelValue?: string;
95
+ label?: string;
96
+ required?: boolean;
97
+ disabled?: boolean;
98
+ /** The error message. It marks the field invalid and is announced with it. */
99
+ error?: string;
100
+ /** A line under the field, announced with it. */
101
+ hint?: string;
102
+ /** Overrides the three words below member by member; the rest keep the contract's. */
103
+ labels?: Partial<ColourPickerLabels>;
104
+ class?: HTMLAttributes['class'];
105
+ }>(),
106
+ {
107
+ modelValue: '',
108
+ label: undefined,
109
+ required: false,
110
+ disabled: false,
111
+ error: undefined,
112
+ hint: undefined,
113
+ labels: undefined,
114
+ class: undefined,
115
+ },
116
+ );
117
+
118
+ const emit = defineEmits<{ (e: 'update:modelValue', value: string): void }>();
119
+
120
+ const attrs = useAttrs();
121
+ const contract = useUiLabels();
122
+
123
+ const text = computed<ColourPickerLabels>(() => ({
124
+ colourPickerChoose: contract.value.colourPickerChoose,
125
+ colourPickerNone: contract.value.colourPickerNone,
126
+ colourPickerCustom: contract.value.colourPickerCustom,
127
+ // Members given as `undefined` are dropped, not spread: they would blank the contract's word.
128
+ ...Object.fromEntries(Object.entries(props.labels ?? {}).filter(([, v]) => v !== undefined)),
129
+ }));
130
+
131
+ /**
132
+ * The trigger's name is the panel's name (the shell names the panel after it), so on a form
133
+ * with two of these it carries the field as well as the action. It is the trigger's own
134
+ * `aria-label`, not an `aria-labelledby` pointing at the label: see the docblock.
135
+ */
136
+ const triggerName = computed(() =>
137
+ props.label
138
+ ? `${text.value.colourPickerChoose}, ${props.label}`
139
+ : text.value.colourPickerChoose,
140
+ );
141
+
142
+ const COLUMNS = 10;
143
+
144
+ const fallbackId = useId();
145
+ const fieldId = computed(() => (attrs.id as string | undefined) ?? fallbackId);
146
+ const hintId = `${useId()}-hint`;
147
+ const errorId = `${useId()}-error`;
148
+ const valueId = `${useId()}-value`;
149
+ const listId = useId();
150
+ const customId = useId();
151
+
152
+ /** The model in its normalised form; null when it is empty or not a colour at all. */
153
+ const colour = computed(() => normaliseHex(props.modelValue ?? ''));
154
+ /** What the field shows: the normalised hex, or whatever is stored when that is not a hex. */
155
+ const shown = computed(() => colour.value ?? props.modelValue ?? '');
156
+ const isEmpty = computed(() => shown.value.trim() === '');
157
+ const isCustom = computed(() => colour.value !== null && !PALETTE.includes(colour.value));
158
+
159
+ // --- The panel. ---
160
+
161
+ const open = ref(false);
162
+ const customOpen = ref(false);
163
+ const active = ref(0);
164
+ const list = useTemplateRef<HTMLElement>('list');
165
+
166
+ const typed = ref(colour.value ?? '');
167
+ const typedInvalid = ref(false);
168
+ watch(colour, (current) => {
169
+ typed.value = current ?? '';
170
+ typedInvalid.value = false;
171
+ });
172
+
173
+ watch(open, async (isOpen) => {
174
+ if (!isOpen) {
175
+ return;
176
+ }
177
+ customOpen.value = isCustom.value;
178
+ typed.value = colour.value ?? '';
179
+ typedInvalid.value = false;
180
+ const chosen = colour.value ? PALETTE.indexOf(colour.value) : -1;
181
+ active.value = chosen >= 0 ? chosen : 0;
182
+ await nextTick();
183
+ list.value?.focus();
184
+ });
185
+
186
+ function pick(next: string): void {
187
+ // Clearing is judged against what is stored, not against what parses: a stored value that is
188
+ // not a hex (`red`) has no colour, yet it is not empty, and "No colour" must still replace it.
189
+ const changed = next === '' ? (props.modelValue ?? '') !== '' : next !== colour.value;
190
+ if (changed) {
191
+ emit('update:modelValue', next);
192
+ }
193
+ open.value = false;
194
+ }
195
+
196
+ function onListKeydown(event: KeyboardEvent): void {
197
+ const last = PALETTE.length - 1;
198
+ switch (event.key) {
199
+ case 'ArrowLeft':
200
+ active.value = Math.max(active.value - 1, 0);
201
+ break;
202
+ case 'ArrowRight':
203
+ active.value = Math.min(active.value + 1, last);
204
+ break;
205
+ case 'ArrowUp':
206
+ active.value = Math.max(active.value - COLUMNS, 0);
207
+ break;
208
+ case 'ArrowDown':
209
+ active.value = Math.min(active.value + COLUMNS, last);
210
+ break;
211
+ case 'Home':
212
+ active.value = 0;
213
+ break;
214
+ case 'End':
215
+ active.value = last;
216
+ break;
217
+ case 'Enter':
218
+ case ' ':
219
+ pick(PALETTE[active.value]!);
220
+ break;
221
+ default:
222
+ return;
223
+ }
224
+ event.preventDefault();
225
+ }
226
+
227
+ function commitTyped(): boolean {
228
+ const normal = normaliseHex(typed.value);
229
+ if (normal === null) {
230
+ typedInvalid.value = true;
231
+ return false;
232
+ }
233
+ typedInvalid.value = false;
234
+ typed.value = normal;
235
+ return true;
236
+ }
237
+
238
+ function onCustomKeydown(event: KeyboardEvent): void {
239
+ if (event.key === 'Enter') {
240
+ event.preventDefault();
241
+ if (commitTyped()) {
242
+ pick(typed.value);
243
+ }
244
+ }
245
+ }
246
+
247
+ const triggerAttrs = computed(() =>
248
+ Object.fromEntries(Object.entries(attrs).filter(([key]) => key !== 'style' && key !== 'class')),
249
+ );
250
+ const describedBy = computed(
251
+ () =>
252
+ [
253
+ triggerAttrs.value['aria-describedby'],
254
+ valueId,
255
+ props.hint ? hintId : null,
256
+ props.error ? errorId : null,
257
+ ]
258
+ .filter(Boolean)
259
+ .join(' ') || undefined,
260
+ );
261
+
262
+ const DOT = 'size-4 flex-none rounded-full border border-foreground/15';
263
+ </script>
264
+
265
+ <template>
266
+ <div
267
+ data-slot="form-field"
268
+ :class="cn('grid gap-1.5', props.class)"
269
+ :style="attrs.style as never"
270
+ >
271
+ <Label v-if="props.label" :for="fieldId">
272
+ {{ props.label }}
273
+ <span v-if="props.required" aria-hidden="true" class="text-destructive">*</span>
274
+ </Label>
275
+ <!-- The field box is the `input` slot: it is what a page's density settings size. -->
276
+ <div
277
+ data-slot="input"
278
+ :class="
279
+ cn(
280
+ 'border-muted-foreground dark:bg-input/30 relative flex h-9 w-full min-w-0 items-center rounded-lg border bg-transparent transition-colors',
281
+ 'focus-within:border-ring focus-within:ring-ring focus-within:ring-offset-background focus-within:ring-2 focus-within:ring-offset-2',
282
+ 'has-[button[aria-invalid=true]]:border-destructive has-[button[aria-invalid=true]]:ring-destructive/20 dark:has-[button[aria-invalid=true]]:ring-destructive/40 has-[button[aria-invalid=true]]:ring-3',
283
+ props.disabled && 'bg-input/50 dark:bg-input/80 cursor-not-allowed opacity-50',
284
+ )
285
+ "
286
+ >
287
+ <PopoverPanel v-model:open="open" :disabled="props.disabled">
288
+ <template #trigger>
289
+ <button
290
+ v-bind="triggerAttrs"
291
+ :id="fieldId"
292
+ type="button"
293
+ :aria-label="triggerName"
294
+ :aria-required="props.required || undefined"
295
+ :aria-describedby="describedBy"
296
+ :aria-invalid="
297
+ props.error ? 'true' : (triggerAttrs['aria-invalid'] as never)
298
+ "
299
+ class="flex h-full w-full min-w-0 items-center gap-2 rounded-lg bg-transparent px-2.5 text-left text-base outline-none disabled:pointer-events-none md:text-sm"
300
+ >
301
+ <span
302
+ data-testid="colour-dot"
303
+ :class="cn(DOT, isEmpty && 'border-muted-foreground border-dashed')"
304
+ :style="colour ? { backgroundColor: colour } : undefined"
305
+ />
306
+ <span
307
+ :id="valueId"
308
+ :class="
309
+ cn(
310
+ 'flex-1 truncate tabular-nums',
311
+ isEmpty && 'text-muted-foreground',
312
+ )
313
+ "
314
+ >
315
+ {{ isEmpty ? text.colourPickerNone : shown }}
316
+ </span>
317
+ <slot name="chevron-icon"
318
+ ><ChevronDownIcon class="text-muted-foreground"
319
+ /></slot>
320
+ </button>
321
+ </template>
322
+ <template #panel>
323
+ <div class="w-[262px] max-w-full">
324
+ <div
325
+ :id="listId"
326
+ ref="list"
327
+ role="listbox"
328
+ tabindex="0"
329
+ :aria-label="text.colourPickerChoose"
330
+ :aria-activedescendant="`${listId}-${active}`"
331
+ class="grid grid-cols-10 gap-1.5 rounded-sm p-1 outline-none focus-visible:ring-ring focus-visible:ring-2 focus-visible:ring-inset"
332
+ @keydown="onListKeydown"
333
+ >
334
+ <div
335
+ v-for="(swatch, index) in PALETTE"
336
+ :id="`${listId}-${index}`"
337
+ :key="swatch"
338
+ role="option"
339
+ :aria-label="swatch"
340
+ :aria-selected="colour === swatch"
341
+ :data-active="index === active || undefined"
342
+ :style="{ backgroundColor: swatch }"
343
+ class="border-foreground/15 size-5 cursor-pointer rounded-full border aria-selected:outline-ring aria-selected:outline-2 aria-selected:outline-offset-2 data-active:ring-2 data-active:ring-foreground data-active:ring-offset-2 data-active:ring-offset-popover"
344
+ @click="pick(swatch)"
345
+ />
346
+ </div>
347
+ <button
348
+ type="button"
349
+ :aria-expanded="customOpen"
350
+ :aria-controls="customId"
351
+ class="text-muted-foreground hover:bg-muted hover:text-foreground mt-2 flex w-full items-center gap-2 rounded-[calc(var(--radius)-6px)] px-2 py-1.5 text-left text-sm transition-colors outline-none focus-visible:ring-ring focus-visible:ring-2 focus-visible:ring-inset"
352
+ @click="customOpen = !customOpen"
353
+ >
354
+ <span class="flex-1">{{ text.colourPickerCustom }}</span>
355
+ <slot v-if="customOpen" name="collapse-icon"
356
+ ><ChevronUpIcon class="size-3"
357
+ /></slot>
358
+ <slot v-else name="expand-icon"
359
+ ><ChevronDownIcon class="size-3"
360
+ /></slot>
361
+ </button>
362
+ <div
363
+ v-if="customOpen"
364
+ :id="customId"
365
+ class="border-muted-foreground bg-background mx-0.5 mt-1 flex items-center gap-2 rounded-[calc(var(--radius)-6px)] border px-2 py-1.5"
366
+ >
367
+ <span
368
+ :class="DOT"
369
+ :style="
370
+ normaliseHex(typed)
371
+ ? { backgroundColor: normaliseHex(typed)! }
372
+ : undefined
373
+ "
374
+ />
375
+ <input
376
+ v-model="typed"
377
+ type="text"
378
+ spellcheck="false"
379
+ autocomplete="off"
380
+ :aria-label="text.colourPickerCustom"
381
+ :aria-invalid="typedInvalid ? 'true' : undefined"
382
+ :class="
383
+ cn(
384
+ 'min-w-0 flex-1 bg-transparent text-sm uppercase tabular-nums outline-none',
385
+ typedInvalid && 'text-destructive',
386
+ )
387
+ "
388
+ @input="typedInvalid = false"
389
+ @keydown="onCustomKeydown"
390
+ />
391
+ </div>
392
+ <button
393
+ type="button"
394
+ :aria-pressed="isEmpty"
395
+ class="text-muted-foreground hover:bg-muted hover:text-foreground mt-2 flex w-full items-center gap-2 rounded-[calc(var(--radius)-6px)] px-2 py-1.5 text-left text-sm transition-colors outline-none focus-visible:ring-ring focus-visible:ring-2 focus-visible:ring-inset aria-pressed:text-foreground aria-pressed:font-semibold"
396
+ @click="pick('')"
397
+ >
398
+ <span
399
+ class="border-muted-foreground grid size-5 flex-none place-items-center rounded-full border border-dashed"
400
+ >
401
+ <slot name="clear-icon"><XIcon class="size-3" /></slot>
402
+ </span>
403
+ {{ text.colourPickerNone }}
404
+ </button>
405
+ </div>
406
+ </template>
407
+ </PopoverPanel>
408
+ </div>
409
+ <p
410
+ v-if="props.hint"
411
+ :id="hintId"
412
+ data-testid="colour-picker-hint"
413
+ class="text-muted-foreground text-xs"
414
+ >
415
+ {{ props.hint }}
416
+ </p>
417
+ <p v-if="props.error" :id="errorId" role="alert" class="text-destructive text-sm">
418
+ {{ props.error }}
419
+ </p>
420
+ </div>
421
+ </template>
@@ -0,0 +1 @@
1
+ export { default as ColourPicker } from './ColourPicker.vue';