@brimsites/brimjs 0.0.0-stage → 1.0.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 (238) hide show
  1. package/LICENSE +110 -0
  2. package/README.md +22 -2
  3. package/bin/brim.js +5 -0
  4. package/dist/cli.js +3802 -0
  5. package/editor-dist/editor.css +1 -0
  6. package/editor-dist/editor.js +91 -0
  7. package/package.json +71 -3
  8. package/skills/brim-convert/SKILL.md +256 -0
  9. package/skills/brim-hosting/SKILL.md +221 -0
  10. package/src/astro-shim.d.ts +5 -0
  11. package/src/auth/accounts.ts +341 -0
  12. package/src/auth/password.ts +34 -0
  13. package/src/auth/permissions.ts +44 -0
  14. package/src/blocks/accordion/Accordion.astro +48 -0
  15. package/src/blocks/accordion/accordion.css +63 -0
  16. package/src/blocks/analytics/Analytics.astro +45 -0
  17. package/src/blocks/analytics/ConsentBanner.astro +41 -0
  18. package/src/blocks/analytics/analytics.client.ts +125 -0
  19. package/src/blocks/analytics/analytics.css +51 -0
  20. package/src/blocks/analytics/consent.ts +51 -0
  21. package/src/blocks/button/Button.astro +64 -0
  22. package/src/blocks/button/button.css +100 -0
  23. package/src/blocks/carousel/Carousel.astro +140 -0
  24. package/src/blocks/carousel/carousel.client.ts +153 -0
  25. package/src/blocks/carousel/carousel.css +181 -0
  26. package/src/blocks/data/icons.ts +285 -0
  27. package/src/blocks/data/image-shapes.ts +91 -0
  28. package/src/blocks/data/social-icons.ts +41 -0
  29. package/src/blocks/data/social-networks.ts +256 -0
  30. package/src/blocks/divider/Divider.astro +27 -0
  31. package/src/blocks/divider/divider.css +17 -0
  32. package/src/blocks/form/Captcha.astro +27 -0
  33. package/src/blocks/form/Form.astro +250 -0
  34. package/src/blocks/form/FormField.astro +138 -0
  35. package/src/blocks/form/Newsletter.astro +80 -0
  36. package/src/blocks/form/form.client.ts +339 -0
  37. package/src/blocks/form/form.css +271 -0
  38. package/src/blocks/gallery/ImageGallery.astro +126 -0
  39. package/src/blocks/gallery/gallery.css +43 -0
  40. package/src/blocks/gallery/lightbox.client.ts +178 -0
  41. package/src/blocks/gallery/lightbox.css +134 -0
  42. package/src/blocks/icon/Icon.astro +41 -0
  43. package/src/blocks/icon/icon.css +16 -0
  44. package/src/blocks/image/Image.astro +158 -0
  45. package/src/blocks/image/image.css +42 -0
  46. package/src/blocks/images.ts +64 -0
  47. package/src/blocks/in-editor.client.ts +8 -0
  48. package/src/blocks/listing/Listing.astro +288 -0
  49. package/src/blocks/listing/ListingCards.astro +48 -0
  50. package/src/blocks/listing/listing.css +136 -0
  51. package/src/blocks/listing/query.ts +363 -0
  52. package/src/blocks/map/Map.astro +75 -0
  53. package/src/blocks/map/map.css +32 -0
  54. package/src/blocks/schemas/accordion.ts +118 -0
  55. package/src/blocks/schemas/button.ts +78 -0
  56. package/src/blocks/schemas/carousel.ts +133 -0
  57. package/src/blocks/schemas/divider.ts +29 -0
  58. package/src/blocks/schemas/form-field-types.ts +29 -0
  59. package/src/blocks/schemas/form.ts +350 -0
  60. package/src/blocks/schemas/icon.ts +24 -0
  61. package/src/blocks/schemas/image-gallery.ts +54 -0
  62. package/src/blocks/schemas/image.ts +87 -0
  63. package/src/blocks/schemas/index.ts +33 -0
  64. package/src/blocks/schemas/listing.ts +14 -0
  65. package/src/blocks/schemas/map.ts +81 -0
  66. package/src/blocks/schemas/newsletter.ts +324 -0
  67. package/src/blocks/schemas/social-media.ts +348 -0
  68. package/src/blocks/schemas/text.ts +151 -0
  69. package/src/blocks/schemas/video.ts +57 -0
  70. package/src/blocks/search/Search.astro +147 -0
  71. package/src/blocks/search/search.css +69 -0
  72. package/src/blocks/search/state.ts +28 -0
  73. package/src/blocks/settings.ts +94 -0
  74. package/src/blocks/social/SocialMedia.astro +102 -0
  75. package/src/blocks/social/social-links.ts +16 -0
  76. package/src/blocks/social/social.css +31 -0
  77. package/src/blocks/theme.ts +35 -0
  78. package/src/blocks/types.ts +52 -0
  79. package/src/blocks/video/Video.astro +90 -0
  80. package/src/blocks/video/video-source.ts +77 -0
  81. package/src/blocks/video/video.client.ts +43 -0
  82. package/src/blocks/video/video.css +29 -0
  83. package/src/check.ts +1119 -0
  84. package/src/cli.ts +493 -0
  85. package/src/components/Blocks.astro +68 -0
  86. package/src/components/Field.astro +61 -0
  87. package/src/components/Footer.astro +36 -0
  88. package/src/components/Link.astro +18 -0
  89. package/src/components/MobileMenu.astro +77 -0
  90. package/src/components/Navbar.astro +42 -0
  91. package/src/components/RichText.astro +32 -0
  92. package/src/components/Seo.astro +202 -0
  93. package/src/components/Setting.astro +46 -0
  94. package/src/components/Shared.astro +17 -0
  95. package/src/components/Text.astro +91 -0
  96. package/src/components/blocks.css +86 -0
  97. package/src/components/index.ts +27 -0
  98. package/src/components/mobile-menu.client.ts +21 -0
  99. package/src/components/mobile-menu.css +52 -0
  100. package/src/components/text.css +48 -0
  101. package/src/config.ts +413 -0
  102. package/src/content/block-layout.ts +63 -0
  103. package/src/content/block-values.ts +48 -0
  104. package/src/content/content-check.ts +174 -0
  105. package/src/content/content-types.ts +408 -0
  106. package/src/content/drafts.ts +115 -0
  107. package/src/content/feed.ts +237 -0
  108. package/src/content/field-types.ts +107 -0
  109. package/src/content/history.ts +140 -0
  110. package/src/content/ids.ts +50 -0
  111. package/src/content/image-size.ts +212 -0
  112. package/src/content/image-sizes.ts +126 -0
  113. package/src/content/item-fields.ts +110 -0
  114. package/src/content/item-head.ts +126 -0
  115. package/src/content/item-image.ts +26 -0
  116. package/src/content/item-path.ts +122 -0
  117. package/src/content/items.ts +580 -0
  118. package/src/content/language-scopes.ts +40 -0
  119. package/src/content/media.ts +271 -0
  120. package/src/content/menus.ts +51 -0
  121. package/src/content/page-templates.ts +52 -0
  122. package/src/content/redirects.ts +112 -0
  123. package/src/content/references.ts +165 -0
  124. package/src/content/rich-text.ts +63 -0
  125. package/src/content/search.ts +167 -0
  126. package/src/content/seed.ts +229 -0
  127. package/src/content/site-settings.ts +111 -0
  128. package/src/content/sitemap.ts +143 -0
  129. package/src/content/store.ts +225 -0
  130. package/src/content/tags.ts +67 -0
  131. package/src/content/text-default.ts +139 -0
  132. package/src/content/types.ts +137 -0
  133. package/src/content/validate.ts +253 -0
  134. package/src/content/validation-error.ts +2 -0
  135. package/src/db/cached.ts +140 -0
  136. package/src/db/database.ts +15 -0
  137. package/src/db/libsql.ts +29 -0
  138. package/src/db/schema.ts +189 -0
  139. package/src/db/url.ts +44 -0
  140. package/src/editor/client/BlockSettings.tsx +1063 -0
  141. package/src/editor/client/Chrome.tsx +284 -0
  142. package/src/editor/client/ContentBrowser.tsx +1299 -0
  143. package/src/editor/client/Editor.tsx +1756 -0
  144. package/src/editor/client/HistoryPanel.tsx +114 -0
  145. package/src/editor/client/ImagesWidget.tsx +113 -0
  146. package/src/editor/client/Inspector.tsx +127 -0
  147. package/src/editor/client/ItemForm.tsx +535 -0
  148. package/src/editor/client/LinkWidget.tsx +440 -0
  149. package/src/editor/client/ListingPanel.tsx +99 -0
  150. package/src/editor/client/Login.tsx +338 -0
  151. package/src/editor/client/Menu.tsx +64 -0
  152. package/src/editor/client/PageSettings.tsx +270 -0
  153. package/src/editor/client/RedirectsSettings.tsx +130 -0
  154. package/src/editor/client/Resize.tsx +204 -0
  155. package/src/editor/client/Sidebar.tsx +51 -0
  156. package/src/editor/client/SiteMap.tsx +568 -0
  157. package/src/editor/client/SiteSettings.tsx +409 -0
  158. package/src/editor/client/TextTools.tsx +391 -0
  159. package/src/editor/client/Toolbar.tsx +449 -0
  160. package/src/editor/client/VideoUpload.tsx +251 -0
  161. package/src/editor/client/api.ts +417 -0
  162. package/src/editor/client/brand.ts +20 -0
  163. package/src/editor/client/connection.ts +98 -0
  164. package/src/editor/client/context.ts +19 -0
  165. package/src/editor/client/css.d.ts +2 -0
  166. package/src/editor/client/editor.css +4656 -0
  167. package/src/editor/client/fields.tsx +812 -0
  168. package/src/editor/client/icons.tsx +174 -0
  169. package/src/editor/client/keys.ts +15 -0
  170. package/src/editor/client/kinds.ts +289 -0
  171. package/src/editor/client/languages.ts +32 -0
  172. package/src/editor/client/main.tsx +100 -0
  173. package/src/editor/client/responses.ts +26 -0
  174. package/src/editor/client/rich-text.ts +326 -0
  175. package/src/editor/client/site-tree.ts +239 -0
  176. package/src/editor/client/theme.ts +55 -0
  177. package/src/editor/client/tsconfig.json +18 -0
  178. package/src/editor/client/tus.ts +137 -0
  179. package/src/editor/client/use-rect.ts +50 -0
  180. package/src/editor/client/widgets.tsx +1218 -0
  181. package/src/forms/captcha.ts +83 -0
  182. package/src/forms/define.ts +166 -0
  183. package/src/forms/fields.ts +197 -0
  184. package/src/forms/file-types.ts +108 -0
  185. package/src/forms/files.ts +216 -0
  186. package/src/forms/form-page.ts +9 -0
  187. package/src/forms/handler.ts +622 -0
  188. package/src/forms/spec.ts +89 -0
  189. package/src/forms/store.ts +96 -0
  190. package/src/forms/work.ts +50 -0
  191. package/src/icons/library.ts +144 -0
  192. package/src/icons/load.ts +63 -0
  193. package/src/index.ts +99 -0
  194. package/src/init.ts +257 -0
  195. package/src/integration.ts +469 -0
  196. package/src/mail/mailer.ts +197 -0
  197. package/src/mail/notify.ts +272 -0
  198. package/src/mail/settings.ts +173 -0
  199. package/src/mail/templates.ts +191 -0
  200. package/src/mcp/page-nodes.ts +209 -0
  201. package/src/mcp/server.ts +111 -0
  202. package/src/mcp/tools.ts +637 -0
  203. package/src/runtime/TemplatePage.astro +39 -0
  204. package/src/runtime/TemplatePageStatic.astro +13 -0
  205. package/src/runtime/admin-route.ts +80 -0
  206. package/src/runtime/connect.ts +53 -0
  207. package/src/runtime/content.ts +188 -0
  208. package/src/runtime/context.ts +131 -0
  209. package/src/runtime/form-files.ts +18 -0
  210. package/src/runtime/forms-route.ts +33 -0
  211. package/src/runtime/forms.ts +28 -0
  212. package/src/runtime/icons.ts +5 -0
  213. package/src/runtime/media.ts +175 -0
  214. package/src/runtime/middleware.ts +186 -0
  215. package/src/runtime/nav.ts +13 -0
  216. package/src/runtime/query-reads.ts +49 -0
  217. package/src/runtime/query-store.ts +16 -0
  218. package/src/runtime/request-state.ts +69 -0
  219. package/src/runtime/robots.ts +29 -0
  220. package/src/runtime/rss-static.ts +16 -0
  221. package/src/runtime/rss.ts +29 -0
  222. package/src/runtime/server.ts +29 -0
  223. package/src/runtime/sitemap.ts +34 -0
  224. package/src/runtime/template-paths.ts +34 -0
  225. package/src/runtime/uploads-route.ts +13 -0
  226. package/src/seed-cli.ts +253 -0
  227. package/src/server/auth.ts +31 -0
  228. package/src/server/fetch-image.ts +219 -0
  229. package/src/server/handler.ts +2106 -0
  230. package/src/server/http.ts +162 -0
  231. package/src/server/locales.ts +73 -0
  232. package/src/server/node.ts +57 -0
  233. package/src/server/query-params.ts +249 -0
  234. package/src/server/rate-limit.ts +72 -0
  235. package/src/server/video-host.ts +162 -0
  236. package/src/starter.ts +70 -0
  237. package/src/version.ts +4 -0
  238. package/src/virtual.d.ts +65 -0
@@ -0,0 +1,391 @@
1
+ import { type ReactNode, useEffect, useLayoutEffect, useRef, useState } from "react";
2
+ import {
3
+ AlignCenterIcon,
4
+ AlignJustifyIcon,
5
+ AlignLeftIcon,
6
+ AlignRightIcon,
7
+ BoldIcon,
8
+ BulletListIcon,
9
+ ChevronDownIcon,
10
+ ClearFormatIcon,
11
+ HighlightIcon,
12
+ ItalicIcon,
13
+ LinkIcon,
14
+ NumberedListIcon,
15
+ StrikeIcon,
16
+ SubscriptIcon,
17
+ SuperscriptIcon,
18
+ TextColorIcon,
19
+ UnderlineIcon,
20
+ } from "./icons.tsx";
21
+ import { useBrandColors } from "./brand.ts";
22
+ import { type TextAlign, alignOf } from "./rich-text.ts";
23
+ import { LinkWidget } from "./LinkWidget.tsx";
24
+ import { isComposing } from "./keys.ts";
25
+
26
+ /** Room kept between a popover and the edge of the page being edited. */
27
+ const EDGE = 8;
28
+
29
+ /** `text` is one element (a heading, a paragraph); `richtext` is flowing content with several blocks. */
30
+ export type TextKind = "text" | "richtext";
31
+
32
+ const MARKS = [
33
+ { command: "bold", label: "Bold", Icon: BoldIcon },
34
+ { command: "italic", label: "Italic", Icon: ItalicIcon },
35
+ { command: "underline", label: "Underline", Icon: UnderlineIcon },
36
+ { command: "strikeThrough", label: "Strikethrough", Icon: StrikeIcon },
37
+ { command: "subscript", label: "Subscript", Icon: SubscriptIcon },
38
+ { command: "superscript", label: "Superscript", Icon: SuperscriptIcon },
39
+ ] as const;
40
+
41
+ const ALIGNMENTS = [
42
+ { align: "left", command: "justifyLeft", label: "Align left", Icon: AlignLeftIcon },
43
+ { align: "center", command: "justifyCenter", label: "Align center", Icon: AlignCenterIcon },
44
+ { align: "right", command: "justifyRight", label: "Align right", Icon: AlignRightIcon },
45
+ { align: "justify", command: "justifyFull", label: "Justify", Icon: AlignJustifyIcon },
46
+ ] as const;
47
+
48
+ const TEXT_STYLES = [
49
+ { tag: "p", label: "Paragraph" },
50
+ { tag: "h1", label: "Heading 1" },
51
+ { tag: "h2", label: "Heading 2" },
52
+ { tag: "h3", label: "Heading 3" },
53
+ { tag: "h4", label: "Heading 4" },
54
+ { tag: "h5", label: "Heading 5" },
55
+ { tag: "h6", label: "Heading 6" },
56
+ { tag: "blockquote", label: "Quote" },
57
+ ];
58
+
59
+ /** Content colors offered to the owner. These are choices for the site's text, not editor colors. */
60
+ const PALETTE = [
61
+ "#000000",
62
+ "#434343",
63
+ "#666666",
64
+ "#999999",
65
+ "#cccccc",
66
+ "#ffffff",
67
+ "#d92d20",
68
+ "#f79009",
69
+ "#fdb022",
70
+ "#12b76a",
71
+ "#1570ef",
72
+ "#7a5af8",
73
+ "#b42318",
74
+ "#b54708",
75
+ "#93370d",
76
+ "#027a48",
77
+ "#175cd3",
78
+ "#5925dc",
79
+ ];
80
+
81
+ type Popover = "style" | "color" | "highlight" | "link" | null;
82
+
83
+ /** Formatting controls for the text being edited in place. They act on the selection inside the page frame. */
84
+ export function TextTools({
85
+ element,
86
+ kind,
87
+ onElement,
88
+ }: {
89
+ element: HTMLElement;
90
+ kind: TextKind;
91
+ /** Changes which element a single text is (paragraph, heading…). */
92
+ onElement: (tag: string) => void;
93
+ }): ReactNode {
94
+ const page = element.ownerDocument;
95
+ const [, refresh] = useState(0);
96
+ const [popover, setPopover] = useState<Popover>(null);
97
+ const [href, setHref] = useState("");
98
+ // The link popover is wider than the button it opens from: it is moved back inside the page being edited.
99
+ const linkPopover = useRef<HTMLDivElement>(null);
100
+ useLayoutEffect(() => {
101
+ const box = linkPopover.current;
102
+ const area = box?.closest(".brim-chrome")?.getBoundingClientRect();
103
+ if (!box || !area) return;
104
+ box.style.transform = "";
105
+ const rect = box.getBoundingClientRect();
106
+ const overflow = Math.max(0, rect.right - (area.right - EDGE)) - Math.max(0, area.left + EDGE - rect.left);
107
+ if (overflow !== 0) box.style.transform = `translateX(${-overflow}px)`;
108
+ }, [popover]);
109
+ // Popovers with inputs move focus out of the page, so the selection is remembered and put back.
110
+ const selection = useRef<Range | null>(null);
111
+
112
+ useEffect(() => {
113
+ const update = (): void => refresh((count) => count + 1);
114
+ page.addEventListener("selectionchange", update);
115
+ return () => page.removeEventListener("selectionchange", update);
116
+ }, [page]);
117
+
118
+ function run(command: string, value?: string): void {
119
+ element.focus();
120
+ if (selection.current) {
121
+ const current = page.getSelection();
122
+ current?.removeAllRanges();
123
+ current?.addRange(selection.current);
124
+ selection.current = null;
125
+ }
126
+ page.execCommand(command, false, value);
127
+ setPopover(null);
128
+ refresh((count) => count + 1);
129
+ }
130
+
131
+ function toggle(next: Popover): void {
132
+ const current = page.getSelection();
133
+ selection.current = current && current.rangeCount > 0 ? current.getRangeAt(0).cloneRange() : null;
134
+ if (next === "link") setHref(currentLink(page)?.getAttribute("href") ?? "");
135
+ setPopover(popover === next ? null : next);
136
+ }
137
+
138
+ function align(next: TextAlign, command: string): void {
139
+ if (kind === "richtext") return run(command);
140
+ // A single text has no blocks to align; the alignment belongs to the element itself.
141
+ element.style.textAlign = alignOf(element) === next ? "" : next;
142
+ refresh((count) => count + 1);
143
+ }
144
+
145
+ function chooseStyle(tag: string): void {
146
+ setPopover(null);
147
+ if (kind === "richtext") run("formatBlock", tag);
148
+ else if (tag !== element.tagName.toLowerCase()) onElement(tag);
149
+ }
150
+
151
+ /** From the link widget: a link for the selected words, or none when it is cleared. */
152
+ function applyLink(link: string): void {
153
+ if (link.trim() === "") run("unlink");
154
+ else run("createLink", link.trim());
155
+ }
156
+
157
+ const currentTag =
158
+ kind === "richtext" ? page.queryCommandValue("formatBlock").toLowerCase() : element.tagName.toLowerCase();
159
+ const currentStyle = TEXT_STYLES.find((style) => style.tag === currentTag)?.label ?? "Text";
160
+ const currentAlign = kind === "richtext" ? undefined : alignOf(element);
161
+
162
+ return (
163
+ <>
164
+ <div className="brim-toolbar-anchor">
165
+ <button
166
+ type="button"
167
+ className="brim-element__toolbar-btn brim-element__toolbar-btn--label"
168
+ title="Text style"
169
+ aria-haspopup="menu"
170
+ aria-expanded={popover === "style"}
171
+ onMouseDown={keepSelection}
172
+ onClick={() => toggle("style")}
173
+ >
174
+ {currentStyle}
175
+ <ChevronDownIcon />
176
+ </button>
177
+ {popover === "style" && (
178
+ <div className="brim-toolbar-popover brim-toolbar-menu" role="menu">
179
+ {TEXT_STYLES.map((style) => (
180
+ <button
181
+ key={style.tag}
182
+ type="button"
183
+ role="menuitemradio"
184
+ aria-checked={style.tag === currentTag}
185
+ className="field-select-option"
186
+ onMouseDown={keepSelection}
187
+ onClick={() => chooseStyle(style.tag)}
188
+ >
189
+ {style.label}
190
+ </button>
191
+ ))}
192
+ </div>
193
+ )}
194
+ </div>
195
+ <Divider />
196
+
197
+ {MARKS.map(({ command, label, Icon }) => (
198
+ <ToolButton key={command} label={label} active={page.queryCommandState(command)} onClick={() => run(command)}>
199
+ <Icon />
200
+ </ToolButton>
201
+ ))}
202
+ <Divider />
203
+
204
+ <ColorTool
205
+ label="Text color"
206
+ open={popover === "color"}
207
+ onToggle={() => toggle("color")}
208
+ onPick={(color) => run("foreColor", color)}
209
+ icon={<TextColorIcon />}
210
+ />
211
+ <ColorTool
212
+ label="Text background color"
213
+ open={popover === "highlight"}
214
+ onToggle={() => toggle("highlight")}
215
+ onPick={(color) => run("hiliteColor", color)}
216
+ onClear={() => run("hiliteColor", "transparent")}
217
+ icon={<HighlightIcon />}
218
+ />
219
+ <Divider />
220
+
221
+ {ALIGNMENTS.map(({ align: value, command, label, Icon }) => (
222
+ <ToolButton
223
+ key={value}
224
+ label={label}
225
+ active={kind === "richtext" ? page.queryCommandState(command) : currentAlign === value}
226
+ onClick={() => align(value, command)}
227
+ >
228
+ <Icon />
229
+ </ToolButton>
230
+ ))}
231
+ {kind === "richtext" && (
232
+ <>
233
+ <Divider />
234
+ <ToolButton
235
+ label="Bulleted list"
236
+ active={page.queryCommandState("insertUnorderedList")}
237
+ onClick={() => run("insertUnorderedList")}
238
+ >
239
+ <BulletListIcon />
240
+ </ToolButton>
241
+ <ToolButton
242
+ label="Numbered list"
243
+ active={page.queryCommandState("insertOrderedList")}
244
+ onClick={() => run("insertOrderedList")}
245
+ >
246
+ <NumberedListIcon />
247
+ </ToolButton>
248
+ </>
249
+ )}
250
+ <Divider />
251
+
252
+ <div className="brim-toolbar-anchor">
253
+ <ToolButton
254
+ label="Link"
255
+ active={currentLink(page) !== null || popover === "link"}
256
+ onClick={() => toggle("link")}
257
+ >
258
+ <LinkIcon />
259
+ </ToolButton>
260
+ {popover === "link" && (
261
+ // The editor's one link widget: type a link, or browse the site, a part of a page, an email or a phone.
262
+ <div
263
+ ref={linkPopover}
264
+ className="brim-toolbar-popover toolbar-link-popover"
265
+ onKeyDown={(event) => event.key === "Escape" && !isComposing(event) && setPopover(null)}
266
+ >
267
+ <LinkWidget label="Link" value={href} autoFocus onChange={applyLink} />
268
+ </div>
269
+ )}
270
+ </div>
271
+ <ToolButton label="Remove formatting" active={false} onClick={() => run("removeFormat")}>
272
+ <ClearFormatIcon />
273
+ </ToolButton>
274
+ <Divider />
275
+ </>
276
+ );
277
+ }
278
+
279
+ function ColorTool({
280
+ label,
281
+ icon,
282
+ open,
283
+ onToggle,
284
+ onPick,
285
+ onClear,
286
+ }: {
287
+ label: string;
288
+ icon: ReactNode;
289
+ open: boolean;
290
+ onToggle: () => void;
291
+ onPick: (color: string) => void;
292
+ onClear?: () => void;
293
+ }) {
294
+ const brand = useBrandColors();
295
+ return (
296
+ <div className="brim-toolbar-anchor">
297
+ <ToolButton label={label} active={open} onClick={onToggle}>
298
+ {icon}
299
+ </ToolButton>
300
+ {open && (
301
+ <div className="brim-toolbar-popover brim-color-popover">
302
+ {brand.length > 0 && (
303
+ <>
304
+ <p className="brim-color-group">Brand</p>
305
+ <div className="brim-color-swatches brim-color-swatches--brand">
306
+ {brand.map((color) => (
307
+ <button
308
+ key={color.value + color.name}
309
+ type="button"
310
+ className="brim-color-swatch"
311
+ title={`${color.name} · ${color.value}`}
312
+ aria-label={`${color.name} (${color.value})`}
313
+ style={{ background: color.value }}
314
+ onMouseDown={keepSelection}
315
+ onClick={() => onPick(color.value)}
316
+ />
317
+ ))}
318
+ </div>
319
+ <p className="brim-color-group">Other colors</p>
320
+ </>
321
+ )}
322
+ <div className="brim-color-swatches">
323
+ {PALETTE.map((color) => (
324
+ <button
325
+ key={color}
326
+ type="button"
327
+ className="brim-color-swatch"
328
+ title={color}
329
+ aria-label={color}
330
+ style={{ background: color }}
331
+ onMouseDown={keepSelection}
332
+ onClick={() => onPick(color)}
333
+ />
334
+ ))}
335
+ </div>
336
+ <label className="brim-color-custom">
337
+ <span>Custom color</span>
338
+ <input type="color" onChange={(event) => onPick(event.target.value)} />
339
+ </label>
340
+ {onClear && (
341
+ <button type="button" className="field-select-option" onMouseDown={keepSelection} onClick={onClear}>
342
+ None (remove background)
343
+ </button>
344
+ )}
345
+ </div>
346
+ )}
347
+ </div>
348
+ );
349
+ }
350
+
351
+ function currentLink(page: Document): HTMLAnchorElement | null {
352
+ const node = page.getSelection()?.anchorNode;
353
+ // Not `instanceof`: the selection lives in the page frame's realm.
354
+ const element = node?.nodeType === Node.ELEMENT_NODE ? (node as Element) : node?.parentElement;
355
+ return element?.closest("a") ?? null;
356
+ }
357
+
358
+ /** Keeps focus, and so the text selection, inside the page frame. */
359
+ function keepSelection(event: { preventDefault: () => void }): void {
360
+ event.preventDefault();
361
+ }
362
+
363
+ function Divider() {
364
+ return <span className="brim-element__toolbar-divider" aria-hidden="true" />;
365
+ }
366
+
367
+ function ToolButton({
368
+ label,
369
+ active,
370
+ onClick,
371
+ children,
372
+ }: {
373
+ label: string;
374
+ active: boolean;
375
+ onClick: () => void;
376
+ children: ReactNode;
377
+ }) {
378
+ return (
379
+ <button
380
+ type="button"
381
+ className={`brim-element__toolbar-btn${active ? " brim-element__toolbar-btn--active" : ""}`}
382
+ title={label}
383
+ aria-label={label}
384
+ aria-pressed={active}
385
+ onMouseDown={keepSelection}
386
+ onClick={onClick}
387
+ >
388
+ {children}
389
+ </button>
390
+ );
391
+ }