@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,352 @@
1
+ <script setup lang="ts">
2
+ import type { Extension } from '@codemirror/state';
3
+ import type { EditorView } from '@codemirror/view';
4
+ import type { HTMLAttributes } from 'vue';
5
+ import { onBeforeUnmount, onMounted, ref, watch } from 'vue';
6
+ import { cn } from '../../lib/cn';
7
+
8
+ /**
9
+ * A code editor on the eight syntax roles: a text surface with line numbers, undo history,
10
+ * bracket matching and highlighting, two-way bound to `modelValue`.
11
+ *
12
+ * **THE DOOR.** This family carries the editor library as optional peer dependencies, so it is
13
+ * not on the root barrel (ADR 0014): import it from `@noix-dev/brix-vue/code-editor`, and install
14
+ * `codemirror`, `@codemirror/state`, `@codemirror/view`, `@codemirror/language` and
15
+ * `@lezer/highlight` beside it. A consumer who never renders an editor installs none of them.
16
+ *
17
+ * **GRAMMARS ARE THE CONSUMER'S.** `extensions` is where a language goes
18
+ * (`@codemirror/lang-html`, `@codemirror/lang-javascript`, anything else), which is what keeps
19
+ * every grammar out of this package's dependencies. Left out, the editor still edits — it simply
20
+ * highlights nothing, because nothing has parsed the text.
21
+ *
22
+ * **COLOUR.** Every colour here is a role, and none of them is read, resolved or subscribed to.
23
+ * The library generates a stylesheet from the highlight style and from the theme below, so a
24
+ * `var(--syntax-tag)` goes into it verbatim and the cascade resolves it: a theme, mode or accent
25
+ * change repaints the editor with nothing told about it. `syntaxTheme.ts` says why that is the
26
+ * opposite of what a chart's canvas needs.
27
+ *
28
+ * **Known limit: with no document there is no editor, only its box.** The guard sits one level up
29
+ * from the library, on the host element, the way `chart/BarChart.vue` keeps the canvas behind one:
30
+ * nothing constructs an `EditorView` where there is no DOM to own. A server-rendered page will
31
+ * therefore log a hydration mismatch when the client adds the editor, and a consumer who minds
32
+ * should wrap this in a client-only boundary. Server rendering is not otherwise supported.
33
+ *
34
+ * **What is live and what is read once, and the asymmetry is a decision.** `modelValue`,
35
+ * `minHeight`, `readonly` and `contentAttributes` are live: the last two swap through a
36
+ * compartment, the library's own idiom for a configuration slot, because a prop whose updates
37
+ * were silently ignored leaves a consumer unable to tell a bug from a misunderstanding.
38
+ * `extensions` is read when the view is built and a later change does not reconfigure it:
39
+ * changing the language of a live editor is a remount in every editor there is, and the prop
40
+ * exists so a consumer chooses a language once. Remount with a `key` to change it.
41
+ *
42
+ * **`update:modelValue` says the document changed, not that a person typed.** An external value
43
+ * re-seeds the document and is echoed straight back out. It terminates, because the watch
44
+ * compares the document before it dispatches, and under `v-model` the echo writes the same string
45
+ * the parent just wrote — but a parent that reads the event as "the user edited this" has to
46
+ * compare for itself.
47
+ *
48
+ * `RichEditor` chose the opposite on both halves, and the difference is a decision on each side
49
+ * and not an oversight on either: it re-seeds an external value with `emitUpdate: false` and says
50
+ * nothing back, and it applies a changed write-back mid-keystroke where this one suppresses it.
51
+ */
52
+
53
+ const props = withDefaults(
54
+ defineProps<{
55
+ modelValue: string;
56
+ /** The consumer's own extensions — a language grammar, a keymap, a lint source. */
57
+ extensions?: Extension[];
58
+ /** The least room the box takes, so a short document still reads as an editor. */
59
+ minHeight?: string;
60
+ readonly?: boolean;
61
+ /**
62
+ * Attributes for the content element itself — the one the library creates, which no
63
+ * template here can reach. It is how a field names the control: `id`, `aria-label`,
64
+ * `aria-describedby`. Live: a change is applied to the running editor.
65
+ *
66
+ * It exists so a consumer wires a field WITHOUT importing the editor library. The same
67
+ * thing is reachable through `extensions` as `EditorView.contentAttributes.of(…)`, and
68
+ * that is how the playground's page used to do it — which made the page import
69
+ * `@codemirror/view` to name a field, an import no consumer of this prop needs.
70
+ */
71
+ contentAttributes?: Record<string, string>;
72
+ class?: HTMLAttributes['class'];
73
+ }>(),
74
+ {
75
+ extensions: () => [],
76
+ minHeight: '320px',
77
+ readonly: false,
78
+ contentAttributes: undefined,
79
+ class: undefined,
80
+ },
81
+ );
82
+
83
+ const emit = defineEmits<{ (e: 'update:modelValue', value: string): void }>();
84
+
85
+ const host = ref<HTMLElement | null>(null);
86
+
87
+ // Without a document there is no DOM for the library to own, so the host element below is not
88
+ // rendered and nothing constructs a view. The guard is here, one level up from the code that
89
+ // would throw, rather than inside `onMounted`.
90
+ const hasDocument = typeof document !== 'undefined';
91
+
92
+ // Narrowed where used; the library's own types come in as types only, so nothing of it is in
93
+ // this module's import graph.
94
+ let view: EditorView | null = null;
95
+ // True while the update listener is propagating an edit of the editor's own, so the watch below
96
+ // does not re-seed the document from the value that edit just produced. An identical write-back
97
+ // is already a no-op — the watch compares the document first — so what this covers is a parent
98
+ // that writes back something it has CHANGED, a trim or a normalisation: re-seeding mid-keystroke
99
+ // maps the caret to the start of the replacement, and the next character lands in front of the
100
+ // last. `CodeEditor.test.ts` measures it by where that character goes.
101
+ let emitting = false;
102
+ // Set once the view exists, because the compartments they swap exist only then. The watches on
103
+ // `readonly` and `contentAttributes` are registered at setup, where Vue collects them, and do
104
+ // nothing until these are set.
105
+ let applyReadonly: ((readonly: boolean) => void) | null = null;
106
+ let applyContentAttributes: ((attributes: Record<string, string>) => void) | null = null;
107
+ // True from the moment the component starts going away. The import below is a chunk of the
108
+ // editor library, and a mount followed by a navigation is an ordinary gesture, so the component
109
+ // can be gone before it arrives — see the check after the await.
110
+ let disposed = false;
111
+
112
+ onMounted(async () => {
113
+ if (!host.value) return;
114
+
115
+ // The library is imported here rather than at module scope, so this module's own graph holds
116
+ // none of it. `codemirror` bundles `basicSetup` — line numbers, undo history, the default
117
+ // keymap, bracket matching and a fallback highlight style — and re-exports `EditorView`;
118
+ // `./syntaxTheme` comes the same way, because it reaches the library for `HighlightStyle`.
119
+ const [{ EditorView: View, basicSetup }, { Compartment, EditorState }, { syntaxHighlighting }] =
120
+ await Promise.all([
121
+ import('codemirror'),
122
+ import('@codemirror/state'),
123
+ import('./syntaxTheme'),
124
+ ]);
125
+
126
+ // The component may have gone while that chunk was loading, in which case `onBeforeUnmount`
127
+ // has already run against a `view` that was still null. Building one now would leave a
128
+ // detached editor — its listeners, its compartment and its injected sheet alive — with
129
+ // nothing left to destroy it, and with `host.value` already null it would not even be on the
130
+ // page. Both conditions are checked: the flag is the component's own word for it, and the
131
+ // null host is what Vue leaves behind.
132
+ if (disposed || !host.value) return;
133
+
134
+ // A compartment is a slot in the configuration that can be swapped without rebuilding the
135
+ // view, which is what makes `readonly` a live prop. `readOnly` is the state's own flag, which
136
+ // commands and extensions read; `editable` is what takes the content element out of
137
+ // `contenteditable`. A reader needs both: without the first, a keymap can still edit the
138
+ // document; without the second, the caret still lands in it.
139
+ const editability = new Compartment();
140
+ const editableFor = (readonly: boolean): Extension => [
141
+ EditorState.readOnly.of(readonly),
142
+ View.editable.of(!readonly),
143
+ ];
144
+
145
+ // The consumer's attributes for the content element, in a compartment of their own for the
146
+ // reason `readonly` has one: a field that renames its control mid-life is obeyed. The facet
147
+ // combines, so an extension of the consumer's that sets its own attributes is not displaced.
148
+ const attributes = new Compartment();
149
+
150
+ /*
151
+ * The chrome — the gutter, the active line, the selection and the caret — is not syntax: it
152
+ * is the editor's own furniture, and it takes existing roles rather than new ones. The ink at
153
+ * a share for the gutter, `--code-selection` for the selection, the ink itself for the caret.
154
+ * The active line paints nothing behind its text, which is decided below.
155
+ *
156
+ * That replaces the four hand-written dark-mode overrides the ported source needed. Those
157
+ * existed because its colours were fixed for a light page, so the dark mode had to restate
158
+ * every one of them; a share of a role has nothing to restate.
159
+ *
160
+ * `dark: true` is a statement about this surface rather than about the page: `--code-surface`
161
+ * is each theme's own dark card in both modes (the owner's decision, ADR 0016), so the editor
162
+ * is a dark editor under a light page too, and the library's `&dark` fallbacks are the ones
163
+ * that belong under these rules.
164
+ *
165
+ * THE ACTIVE LINE HAS NO BACKGROUND, and that is a decision rather than an omission. The
166
+ * contract measures the eight syntax roles against `--code-surface`, and the owner approved
167
+ * the colours against that ground. A wash under the text is a second ground the gate never
168
+ * sees: the dimmest token, the comment, clears 4.57 on the bare surface and falls below 4.5
169
+ * on an 8% wash of the ink, and measured down to 3% graphite is still short, so no share
170
+ * holds. Changing an approved colour is not this component's call, and an editor that paints
171
+ * a ground the gate does not measure is the canvas failure of the previous slice in a
172
+ * different place. The cue is the gutter, whose active number is painted in the full ink.
173
+ * The library's own dark fallback paints a faint wash here, so it is set to `transparent`
174
+ * rather than left out.
175
+ *
176
+ * THE GUTTER IS A CONTRACT ENTRY: its numbers are text, so the 55% below is measured at
177
+ * 4.5:1 by the `--code-foreground` indicator in `contract.json`.
178
+ *
179
+ * THE SELECTION IS `--code-selection`, A ROLE OF ITS OWN, not `--primary` and not a ramp
180
+ * rung. The surface is each theme's dark card in both modes, and `--primary` follows the
181
+ * page's mode: in light it is a dark fill on a dark card and the band all but vanishes. What
182
+ * the band needs is a colour that does not follow the mode, and a rung would supply that by
183
+ * coincidence while a role declares it. A rung is also the wrong thing to name where text is
184
+ * read over it, and the comment token is read over this band. The band stays under 3:1 and
185
+ * is recorded in `contract.json` rather than enforced: a share high enough to see as a band
186
+ * on every theme swallows that token.
187
+ *
188
+ * SELECTION IS THE ONE LONG SELECTOR, and the length is specificity rather than caution: the
189
+ * library's base theme writes the focused selection as
190
+ * `&dark.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground`, five class
191
+ * selectors deep, and a short rule loses to it however late it is mounted. Everything else it
192
+ * paints is two deep, which a theme rule ties and then wins, because a view theme's sheet is
193
+ * mounted after the base theme's. No `:deep()` block is needed for any of it.
194
+ */
195
+ const chrome = View.theme(
196
+ {
197
+ '&': {
198
+ height: '100%',
199
+ // The box carries the prop; the editor inherits it down the one element between.
200
+ minHeight: 'inherit',
201
+ backgroundColor: 'var(--code-surface)',
202
+ color: 'var(--code-foreground)',
203
+ fontSize: '13px',
204
+ },
205
+ // The box draws the focus ring for the whole family, so the editor draws none.
206
+ '&.cm-focused': { outline: 'none' },
207
+ '.cm-scroller': {
208
+ fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace',
209
+ lineHeight: '1.6',
210
+ },
211
+ '.cm-content': { caretColor: 'var(--code-foreground)' },
212
+ '.cm-cursor, .cm-dropCursor': { borderLeftColor: 'var(--code-foreground)' },
213
+ '.cm-gutters': {
214
+ backgroundColor: 'transparent',
215
+ border: 'none',
216
+ color: 'color-mix(in oklab, var(--code-foreground) 55%, transparent)',
217
+ },
218
+ '.cm-activeLine': { backgroundColor: 'transparent' },
219
+ '.cm-activeLineGutter': {
220
+ backgroundColor: 'transparent',
221
+ color: 'var(--code-foreground)',
222
+ },
223
+ [[
224
+ '.cm-selectionLayer .cm-selectionBackground',
225
+ '&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground',
226
+ '.cm-content ::selection',
227
+ ].join(', ')]: {
228
+ background: 'var(--code-selection)',
229
+ },
230
+ },
231
+ { dark: true },
232
+ );
233
+
234
+ const updateListener = View.updateListener.of((update) => {
235
+ if (!update.docChanged) return;
236
+ emitting = true;
237
+ emit('update:modelValue', update.state.doc.toString());
238
+ // Cleared on the next microtask, after the prop has flowed back through the watch.
239
+ queueMicrotask(() => {
240
+ emitting = false;
241
+ });
242
+ });
243
+
244
+ const state = EditorState.create({
245
+ doc: props.modelValue ?? '',
246
+ // Earlier is higher precedence, so brix's own highlight and chrome outrank whatever the
247
+ // prop carries: an extension that ships a theme of its own does not take the editor off
248
+ // the roles. A consumer who wants another look rebinds the roles.
249
+ extensions: [
250
+ basicSetup,
251
+ syntaxHighlighting(),
252
+ chrome,
253
+ View.lineWrapping,
254
+ updateListener,
255
+ editability.of(editableFor(props.readonly)),
256
+ attributes.of(View.contentAttributes.of(props.contentAttributes ?? {})),
257
+ ...props.extensions,
258
+ ],
259
+ });
260
+
261
+ // The state above read both props after the await, so a change that arrived while the imports
262
+ // were settling is already in it and needs no catching up here.
263
+ view = new View({ state, parent: host.value });
264
+ applyReadonly = (readonly) => {
265
+ view?.dispatch({ effects: editability.reconfigure(editableFor(readonly)) });
266
+ };
267
+ applyContentAttributes = (next) => {
268
+ view?.dispatch({
269
+ effects: attributes.reconfigure(View.contentAttributes.of(next)),
270
+ });
271
+ };
272
+ });
273
+
274
+ // An external value — a document loaded, a parent resetting the field — re-seeds the editor,
275
+ // unless the change came from this editor's own update listener. The re-seed is itself a
276
+ // document change, so it is echoed out through that listener; `RichEditor` re-seeds silently
277
+ // (`emitUpdate: false`) and applies a changed write-back where this one skips it. Both are
278
+ // deliberate, each in its own file.
279
+ watch(
280
+ () => props.modelValue,
281
+ (value) => {
282
+ if (!view || emitting) return;
283
+ const current = view.state.doc.toString();
284
+ if (current === (value ?? '')) return;
285
+ view.dispatch({ changes: { from: 0, to: current.length, insert: value ?? '' } });
286
+ },
287
+ );
288
+
289
+ watch(
290
+ () => props.readonly,
291
+ (readonly) => applyReadonly?.(readonly),
292
+ );
293
+
294
+ // The getter is the serialised attributes rather than the object: a parent that rebuilds the
295
+ // literal on every render — which is what `FormField`'s slot does on every keystroke — would
296
+ // otherwise reconfigure the running view on each one.
297
+ watch(
298
+ () => JSON.stringify(props.contentAttributes ?? {}),
299
+ () => applyContentAttributes?.(props.contentAttributes ?? {}),
300
+ );
301
+
302
+ onBeforeUnmount(() => {
303
+ disposed = true;
304
+ view?.destroy();
305
+ view = null;
306
+ applyReadonly = null;
307
+ applyContentAttributes = null;
308
+ });
309
+
310
+ /**
311
+ * Insert `text` over the current selection and refocus — the one reason to hold a reference.
312
+ *
313
+ * It does nothing before the view exists, which is until the library's chunk has loaded a tick
314
+ * or two after mount, and nothing after the component is unmounted. A caller that needs to know
315
+ * the text landed should read `modelValue` back rather than assume the call took.
316
+ */
317
+ function insertAtCursor(text: string): void {
318
+ if (!view) return;
319
+ const { from, to } = view.state.selection.main;
320
+ view.dispatch({
321
+ changes: { from, to, insert: text },
322
+ selection: { anchor: from + text.length },
323
+ });
324
+ view.focus();
325
+ }
326
+
327
+ defineExpose({ insertAtCursor });
328
+ </script>
329
+
330
+ <template>
331
+ <div
332
+ data-slot="code-editor"
333
+ :style="{ minHeight: props.minHeight }"
334
+ :class="
335
+ cn(
336
+ // The box, and the focus recipe for the whole family: the editor itself draws no
337
+ // outline, so the ring here is the only focus cue. `focus-within`, because what
338
+ // takes focus is the library's own content element inside.
339
+ // The resting edge is `--muted-foreground`, which is CLAUDE.md's rule for an
340
+ // active text field, rather than the approved mockup's `--input`: that edge is a
341
+ // contrast ratio of at most 1.82 over the ground it is drawn on, under the 3:1
342
+ // an indicator needs, where this one is at least 4.73. `docs/design/README.md`
343
+ // records the deviation and carries every figure, in one place.
344
+ 'border-muted-foreground focus-within:border-ring focus-within:ring-ring focus-within:ring-offset-background overflow-hidden rounded-lg border outline-none focus-within:ring-2 focus-within:ring-offset-2',
345
+ props.class,
346
+ )
347
+ "
348
+ >
349
+ <!-- The guard: with no document nothing builds an editor, and the box renders alone. -->
350
+ <div v-if="hasDocument" ref="host" class="[min-height:inherit]" />
351
+ </div>
352
+ </template>
@@ -0,0 +1,9 @@
1
+ export { default as CodeEditor } from './CodeEditor.vue';
2
+ // The eight syntax roles and the extension built from them leave through this door too: an
3
+ // application that renders an editor and colours a legend, a diff or a token chip to match it
4
+ // otherwise retypes the eight names one package out, and `syntaxHighlighting` lets a consumer
5
+ // composing its own editor paint with the same roles rather than with a copy of the mapping.
6
+ // Everything here arrives with the editor's optional peers, because this module reaches them —
7
+ // `syntaxTheme.ts` says why that is unlike `SERIES_ROLES` and why no module layout changes it.
8
+ export { SYNTAX_ROLES, syntaxHighlighting } from './syntaxTheme';
9
+ export type { SyntaxRoleNames } from './syntaxTheme';
@@ -0,0 +1,110 @@
1
+ import type { Extension } from '@codemirror/state';
2
+ import type { Tag } from '@lezer/highlight';
3
+ import { HighlightStyle, syntaxHighlighting as highlightWith } from '@codemirror/language';
4
+ import { tags } from '@lezer/highlight';
5
+
6
+ /**
7
+ * The eight syntax roles, in the order the token contract declares them. The contract gates
8
+ * exactly these — 4.5:1 against `--code-surface` and 0.08 ΔEok apart under four simulated
9
+ * visions — so a ninth group of marks has no role to default to.
10
+ *
11
+ * They leave through the family's door so an application that renders an editor can colour a
12
+ * legend, a diff or a token chip to match it without retyping the eight names one package out.
13
+ *
14
+ * **That is a narrower claim than `SERIES_ROLES` makes, and the difference is the door, not this
15
+ * module.** `SERIES_ROLES` reaches nothing but a workspace package, so a consumer can read the
16
+ * five names with no charting library installed. Anything imported from `@noix-dev/brix-vue/code-editor`
17
+ * arrives through an entry point that also exports `syntaxHighlighting`, which reaches
18
+ * `@codemirror/language` — so the names come with the editor's optional peers whatever module
19
+ * they are declared in, and moving them to a library-free file of their own would not change it.
20
+ * A consumer who wants them without an editor has no door here, and inventing one would be new
21
+ * public API that ADR 0014 reserves for a family carrying an optional peer.
22
+ */
23
+ export const SYNTAX_ROLES = [
24
+ '--syntax-tag',
25
+ '--syntax-attribute',
26
+ '--syntax-string',
27
+ '--syntax-keyword',
28
+ '--syntax-number',
29
+ '--syntax-comment',
30
+ '--syntax-meta',
31
+ '--syntax-invalid',
32
+ ] as const;
33
+
34
+ /**
35
+ * Eight role names, one per entry of `SYNTAX_ROLES` and in that order. Each is a custom-property
36
+ * name, so a bare colour written where a name belongs is a compile error and not `var(#fff)`.
37
+ */
38
+ export type SyntaxRoleNames = readonly [
39
+ tag: `--${string}`,
40
+ attribute: `--${string}`,
41
+ string: `--${string}`,
42
+ keyword: `--${string}`,
43
+ number: `--${string}`,
44
+ comment: `--${string}`,
45
+ meta: `--${string}`,
46
+ invalid: `--${string}`,
47
+ ];
48
+
49
+ export interface SyntaxGroup {
50
+ /** The Lezer tags a grammar hands over for this group of marks. */
51
+ readonly tags: readonly Tag[];
52
+ /** Which entry of `SYNTAX_ROLES` paints them. */
53
+ readonly role: (typeof SYNTAX_ROLES)[number];
54
+ /** Set only where the group carries a second cue beside its colour. */
55
+ readonly fontStyle?: 'italic';
56
+ }
57
+
58
+ /**
59
+ * The eight groups of marks, each with the tags a grammar reports for it. A group's position is
60
+ * its position in `SYNTAX_ROLES`, which is what lets a caller hand over its own eight names.
61
+ *
62
+ * A tag carries its children with it: `tags.keyword` also paints a grammar's
63
+ * `definitionKeyword` or `controlKeyword`, because those are defined with it as their parent.
64
+ */
65
+ export const SYNTAX_GROUPS: readonly SyntaxGroup[] = [
66
+ { role: '--syntax-tag', tags: [tags.tagName, tags.angleBracket] },
67
+ { role: '--syntax-attribute', tags: [tags.attributeName] },
68
+ { role: '--syntax-string', tags: [tags.attributeValue, tags.string] },
69
+ { role: '--syntax-keyword', tags: [tags.propertyName, tags.keyword] },
70
+ { role: '--syntax-number', tags: [tags.number, tags.atom, tags.unit] },
71
+ // The one group that recedes: it is held to the lowest contrast band of the eight, and the
72
+ // italic is the second cue that keeps it tellable from the colours around it.
73
+ { role: '--syntax-comment', tags: [tags.comment], fontStyle: 'italic' },
74
+ { role: '--syntax-meta', tags: [tags.meta, tags.processingInstruction] },
75
+ { role: '--syntax-invalid', tags: [tags.invalid] },
76
+ ];
77
+
78
+ /**
79
+ * The highlight style the editor paints with: one rule per group, each colour handed over as the
80
+ * string `var(--syntax-…)` rather than as a value.
81
+ *
82
+ * That is the whole reason this family needs no colour maths and no subscription. The library
83
+ * GENERATES A STYLESHEET from these rules, so the custom property goes into it verbatim and the
84
+ * cascade resolves it on the rendered token — a theme change, a mode change or an accent change
85
+ * repaints the editor with nothing told about it. A chart's canvas cannot take a colour from CSS
86
+ * and so has to read each role and convert it (`chart/chartColor.ts`); this is the opposite case,
87
+ * and copying that machinery here would be machinery for a problem this family does not have.
88
+ *
89
+ * `roles` names the eight custom properties, in `SYNTAX_ROLES` order, so an application with a
90
+ * palette of its own can paint with its own names.
91
+ */
92
+ export function syntaxHighlighting(roles: SyntaxRoleNames = SYNTAX_ROLES): Extension {
93
+ if (roles.length !== SYNTAX_GROUPS.length) {
94
+ // The tuple type says this already; the check is for a caller whose toolchain does not.
95
+ // A short list would paint one group `var(undefined)`, which is a token that silently
96
+ // looks like plain code rather than an error anybody sees.
97
+ throw new Error(
98
+ `the syntax theme takes ${SYNTAX_GROUPS.length} role names, one per group, ` +
99
+ `and was given ${roles.length}`,
100
+ );
101
+ }
102
+ const style = HighlightStyle.define(
103
+ SYNTAX_GROUPS.map((group, index) => ({
104
+ tag: group.tags,
105
+ color: `var(${roles[index]})`,
106
+ ...(group.fontStyle === undefined ? {} : { fontStyle: group.fontStyle }),
107
+ })),
108
+ );
109
+ return highlightWith(style);
110
+ }