@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,125 @@
1
+ import { CONSENT_KEY, type ConsentChoice, deletions, isStatisticsCookie, readChoice } from "./consent.ts";
2
+
3
+ /**
4
+ * Asks the visitor once, keeps the answer in their browser and loads Google Analytics only after they accept.
5
+ * Nothing is used for advertising. Taking consent back deletes the cookies, whatever domain they were set on, and
6
+ * stops the counting at once.
7
+ *
8
+ * Cookies are asked to stay on this host name alone (`cookie_domain: none`). A Google tag made as a Tag Manager
9
+ * container configures itself and ignores what the page asks, so its cookies land on the registrable domain unless the
10
+ * tag itself says otherwise; where the browser can tell, a warning says so (docs/seo-and-pages.md).
11
+ */
12
+ interface AnalyticsWindow extends Window {
13
+ dataLayer?: unknown[];
14
+ [disabled: `ga-disable-${string}`]: boolean | undefined;
15
+ }
16
+
17
+ const view = window as unknown as AnalyticsWindow;
18
+ /** The choice of this page when the browser keeps nothing (storage blocked). */
19
+ let unsaved: ConsentChoice | null = null;
20
+
21
+ function choice(): ConsentChoice | null {
22
+ try {
23
+ return readChoice(localStorage.getItem(CONSENT_KEY), Date.now()) ?? unsaved;
24
+ } catch {
25
+ return unsaved;
26
+ }
27
+ }
28
+
29
+ function keep(statistics: boolean): void {
30
+ unsaved = { statistics, at: Date.now() };
31
+ try {
32
+ localStorage.setItem(CONSENT_KEY, JSON.stringify(unsaved));
33
+ } catch {
34
+ // Kept for this page only.
35
+ }
36
+ }
37
+
38
+ /** The browser itself asks not to be followed: that is an answer, so the visitor is not asked again. */
39
+ const refusedByBrowser = (navigator as Navigator & { globalPrivacyControl?: boolean }).globalPrivacyControl === true;
40
+
41
+ // gtag.js reads what is pushed as `arguments`, not as an array.
42
+ function gtag(..._command: unknown[]): void {
43
+ view.dataLayer ??= [];
44
+ view.dataLayer.push(arguments);
45
+ }
46
+
47
+ function start(id: string): void {
48
+ view[`ga-disable-${id}`] = false;
49
+ if (document.querySelector("script[data-statistics]")) return;
50
+ gtag("consent", "default", {
51
+ ad_storage: "denied",
52
+ ad_user_data: "denied",
53
+ ad_personalization: "denied",
54
+ analytics_storage: "granted",
55
+ });
56
+ gtag("js", new Date());
57
+ gtag("config", id, {
58
+ cookie_domain: "none",
59
+ allow_google_signals: false,
60
+ allow_ad_personalization_signals: false,
61
+ });
62
+ warnOfSharedCookies();
63
+ const script = document.createElement("script");
64
+ script.async = true;
65
+ script.src = `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(id)}`;
66
+ script.dataset.statistics = "";
67
+ document.head.append(script);
68
+ }
69
+
70
+ function stop(id: string): void {
71
+ view[`ga-disable-${id}`] = true;
72
+ for (const cookie of document.cookie.split("; ")) {
73
+ const name = cookie.split("=")[0] ?? "";
74
+ if (!isStatisticsCookie(name)) continue;
75
+ for (const deletion of deletions(name, location.hostname)) document.cookie = deletion;
76
+ }
77
+ }
78
+
79
+ /** Said once in the console: the tag set its cookies on a domain, where the sites next to this one see them too. */
80
+ function warnOfSharedCookies(): void {
81
+ const store = (window as unknown as { cookieStore?: EventTarget }).cookieStore;
82
+ if (!store) return;
83
+ const heard = (event: Event) => {
84
+ const { changed = [] } = event as Event & { changed?: { name?: string; domain?: string | null }[] };
85
+ const shared = changed.find((cookie) => isStatisticsCookie(cookie.name ?? "") && cookie.domain);
86
+ if (!shared) return;
87
+ store.removeEventListener("change", heard);
88
+ console.warn(
89
+ `[brim] Google Analytics set ${shared.name} on ${shared.domain}, so other sites on that domain receive it. ` +
90
+ 'Set the tag\'s "cookie_domain" to "none" in Google Tag Manager to keep it on this host name.',
91
+ );
92
+ };
93
+ store.addEventListener("change", heard);
94
+ }
95
+
96
+ class BrimAnalytics extends HTMLElement {
97
+ connectedCallback(): void {
98
+ const id = this.dataset.id;
99
+ const banner = this.querySelector<HTMLElement>(".brim-consent");
100
+ if (!id || !banner) return;
101
+ const made = choice();
102
+ if (made?.statistics) start(id);
103
+ banner.hidden = made !== null || refusedByBrowser;
104
+
105
+ for (const button of banner.querySelectorAll<HTMLElement>("[data-consent]")) {
106
+ button.addEventListener("click", () => {
107
+ const accepted = button.dataset.consent === "accept";
108
+ keep(accepted);
109
+ banner.hidden = true;
110
+ if (accepted) start(id);
111
+ else stop(id);
112
+ });
113
+ }
114
+ for (const opener of document.querySelectorAll<HTMLElement>("[data-cookie-settings]")) {
115
+ opener.hidden = false;
116
+ opener.addEventListener("click", (event) => {
117
+ event.preventDefault();
118
+ banner.hidden = false;
119
+ banner.querySelector<HTMLElement>("[data-consent]")?.focus();
120
+ });
121
+ }
122
+ }
123
+ }
124
+
125
+ if (!customElements.get("brim-analytics")) customElements.define("brim-analytics", BrimAnalytics);
@@ -0,0 +1,51 @@
1
+ /* The banner's place and a plain look; the site's own CSS on these classes wins. */
2
+ /* In the `brim` layer: any CSS of the site wins over it, whatever its selectors. */
3
+ @layer brim {
4
+ .brim-consent {
5
+ position: fixed;
6
+ z-index: 1000;
7
+ right: 1rem;
8
+ bottom: 1rem;
9
+ left: 1rem;
10
+ max-width: 26rem;
11
+ padding: 1.25rem;
12
+ border: 1px solid color-mix(in srgb, currentColor 15%, transparent);
13
+ border-radius: 0.75rem;
14
+ background: Canvas;
15
+ color: CanvasText;
16
+ font-size: 0.9375rem;
17
+ line-height: 1.5;
18
+ }
19
+
20
+ .brim-consent[hidden] {
21
+ display: none;
22
+ }
23
+
24
+ .brim-consent__text {
25
+ margin: 0;
26
+ }
27
+
28
+ .brim-consent__actions {
29
+ display: grid;
30
+ grid-template-columns: 1fr 1fr;
31
+ gap: 0.75rem;
32
+ margin-top: 1rem;
33
+ }
34
+
35
+ /* Accepting and refusing look the same: neither is the easier one. */
36
+ .brim-consent__button {
37
+ min-height: 2.75rem;
38
+ padding: 0.5rem 1rem;
39
+ border: 1px solid currentColor;
40
+ border-radius: 0.5rem;
41
+ background: transparent;
42
+ color: inherit;
43
+ font: inherit;
44
+ cursor: pointer;
45
+ }
46
+
47
+ .brim-consent__button:focus-visible {
48
+ outline: 2px solid currentColor;
49
+ outline-offset: 2px;
50
+ }
51
+ }
@@ -0,0 +1,51 @@
1
+ /** A visitor's choice about statistics, kept in their browser. */
2
+ export interface ConsentChoice {
3
+ statistics: boolean;
4
+ /** When it was made, in milliseconds. */
5
+ at: number;
6
+ }
7
+
8
+ export const CONSENT_KEY = "brim-consent";
9
+ /** A choice is asked again after this long. */
10
+ export const CONSENT_MAX_AGE = 180 * 24 * 60 * 60 * 1000;
11
+
12
+ /** The choice as it was stored, or `null` when there is none, it is too old or it cannot be read. */
13
+ export function readChoice(raw: string | null, now: number): ConsentChoice | null {
14
+ try {
15
+ const value = JSON.parse(raw ?? "null") as Partial<ConsentChoice> | null;
16
+ if (typeof value?.statistics !== "boolean" || typeof value.at !== "number") return null;
17
+ return value.at > now || now - value.at >= CONSENT_MAX_AGE ? null : { statistics: value.statistics, at: value.at };
18
+ } catch {
19
+ return null;
20
+ }
21
+ }
22
+
23
+ /** The cookies Google Analytics keeps: `_ga` and one `_ga_<id>` per measurement id. */
24
+ export function isStatisticsCookie(name: string): boolean {
25
+ return name === "_ga" || name.startsWith("_ga_");
26
+ }
27
+
28
+ /**
29
+ * What deletes a cookie of this host name wherever it was set: on the host name alone, on the host name as a domain and
30
+ * on every domain above it. A cookie is only deleted with the domain it was set with, and that cannot be read back
31
+ * everywhere. A domain the browser does not accept (a public suffix) changes nothing.
32
+ */
33
+ export function deletions(name: string, host: string): string[] {
34
+ const labels = host.split(".");
35
+ const isAddress = host.includes(":") || /^[\d.]+$/.test(host);
36
+ const domains = isAddress ? [] : labels.slice(0, -1).map((_, i) => labels.slice(i).join("."));
37
+ return ["", ...domains.map((domain) => `; Domain=${domain}`)].map((domain) => `${name}=; Max-Age=0; Path=/${domain}`);
38
+ }
39
+
40
+ /**
41
+ * A Google Analytics 4 measurement id (`G-XXXXXXXXXX`), or `undefined` when it is empty. Anything else is refused:
42
+ * it goes into the page and into the address of a script.
43
+ */
44
+ export function measurementId(value: string | undefined): string | undefined {
45
+ const id = value?.trim().toUpperCase();
46
+ if (!id) return undefined;
47
+ if (!/^G-[A-Z0-9]{4,20}$/.test(id)) {
48
+ throw new Error(`BRIM_GOOGLE_ANALYTICS_ID "${value?.trim()}" must be a measurement id, like G-XXXXXXXXXX.`);
49
+ }
50
+ return id;
51
+ }
@@ -0,0 +1,64 @@
1
+ ---
2
+ import { defaultScope } from "../../runtime/request-state.ts";
3
+ import "./button.css";
4
+ import type { NodeScope } from "../../runtime/context.ts";
5
+ import { icons } from "../../runtime/icons.ts";
6
+ import { button } from "../schemas/button.ts";
7
+
8
+ /**
9
+ * A button or link styled as one of the site's button styles (`variant`, `size`), with an optional icon after its
10
+ * words. Every prop is a starting value the owner can change. Where it sits is the site's layout (or, in a `blocks`
11
+ * field, the block's position); `class` goes on the button.
12
+ */
13
+ interface Props {
14
+ nodeId: string;
15
+ scope?: NodeScope;
16
+ text?: string;
17
+ url?: string;
18
+ openInNewTab?: boolean;
19
+ variant?: "light-1" | "light-2" | "light-3" | "accent" | "block" | "dark-1" | "dark-2" | "ghost";
20
+ size?: "sm" | "md" | "lg";
21
+ showIcon?: boolean;
22
+ icon?: string;
23
+ class?: string;
24
+ }
25
+
26
+ const { nodeId, scope = defaultScope(), class: className, ...source } = Astro.props;
27
+ const settings = Astro.locals.brim.settings(button, nodeId, scope, source);
28
+ const { text, url, openInNewTab, variant, size, showIcon, icon } = settings;
29
+
30
+ const drawn = showIcon ? icons.find(icon) : undefined;
31
+ const iconPaths = drawn?.body;
32
+ // A pack's icon carries its own look; only Brim's outlines are drawn by the button.
33
+ const outline = drawn?.outline && {
34
+ fill: "none",
35
+ stroke: "currentColor",
36
+ "stroke-width": "2",
37
+ "stroke-linecap": "round",
38
+ "stroke-linejoin": "round",
39
+ };
40
+ const classes = ["brim-btn", `brim-btn--${variant}`, `brim-btn--${size}`, className];
41
+ const href = typeof url === "string" && url !== "" ? url : undefined;
42
+ const Element = href ? "a" : "button";
43
+ ---
44
+
45
+ <Element
46
+ class:list={classes}
47
+ href={href}
48
+ type={href ? undefined : "button"}
49
+ target={href && openInNewTab ? "_blank" : undefined}
50
+ rel={href && openInNewTab ? "noopener noreferrer" : undefined}
51
+ {...Astro.locals.brim.marker("button", nodeId, scope, settings)}
52
+ >
53
+ <span class="brim-btn__label">{text}</span>
54
+ {iconPaths && (
55
+ <svg
56
+ class="brim-btn__icon"
57
+ xmlns="http://www.w3.org/2000/svg"
58
+ viewBox={drawn?.viewBox}
59
+ {...outline}
60
+ aria-hidden="true"
61
+ set:html={iconPaths}
62
+ />
63
+ )}
64
+ </Element>
@@ -0,0 +1,100 @@
1
+ /*
2
+ * Button block. Deliberately small: shape, sizes and the named variants.
3
+ * Colors follow the site's theme tokens (--brim-theme-*), with neutral fallbacks.
4
+ */
5
+ /* In the `brim` layer: any CSS of the site wins over it, whatever its selectors. */
6
+ @layer brim {
7
+ .brim-btn {
8
+ --brim-btn-bg: var(--brim-theme-primary, #1e1e1d);
9
+ --brim-btn-text: var(--brim-theme-background, #ffffff);
10
+ --brim-btn-border: transparent;
11
+
12
+ display: inline-flex;
13
+ align-items: center;
14
+ justify-content: center;
15
+ gap: 0.5em;
16
+ padding: var(--brim-btn-padding-y) var(--brim-btn-padding-x);
17
+ border: 1px solid var(--brim-btn-border);
18
+ border-radius: var(--brim-theme-radius, 0.5rem);
19
+ background: var(--brim-btn-bg);
20
+ color: var(--brim-btn-text);
21
+ font: inherit;
22
+ font-weight: 600;
23
+ line-height: 1.2;
24
+ text-decoration: none;
25
+ cursor: pointer;
26
+ transition:
27
+ filter 0.15s,
28
+ background-color 0.15s;
29
+ }
30
+
31
+ .brim-btn:hover {
32
+ filter: brightness(0.92);
33
+ }
34
+
35
+ .brim-btn:focus-visible {
36
+ outline: 2px solid currentColor;
37
+ outline-offset: 2px;
38
+ }
39
+
40
+ .brim-btn--sm {
41
+ --brim-btn-padding-y: 0.4rem;
42
+ --brim-btn-padding-x: 0.85rem;
43
+ font-size: 0.875rem;
44
+ }
45
+
46
+ .brim-btn--md {
47
+ --brim-btn-padding-y: 0.65rem;
48
+ --brim-btn-padding-x: 1.25rem;
49
+ font-size: 1rem;
50
+ }
51
+
52
+ .brim-btn--lg {
53
+ --brim-btn-padding-y: 0.9rem;
54
+ --brim-btn-padding-x: 1.75rem;
55
+ font-size: 1.125rem;
56
+ }
57
+
58
+ .brim-btn--light-1 {
59
+ --brim-btn-bg: var(--brim-theme-background, #ffffff);
60
+ --brim-btn-text: var(--brim-theme-dark2, #0a0a0a);
61
+ --brim-btn-border: var(--brim-theme-light2, #e4e4e7);
62
+ }
63
+
64
+ .brim-btn--light-2 {
65
+ --brim-btn-bg: var(--brim-theme-light1, #f4f4f5);
66
+ --brim-btn-text: var(--brim-theme-dark2, #0a0a0a);
67
+ }
68
+
69
+ .brim-btn--light-3 {
70
+ --brim-btn-bg: var(--brim-theme-light2, #e4e4e7);
71
+ --brim-btn-text: var(--brim-theme-dark2, #0a0a0a);
72
+ }
73
+
74
+ .brim-btn--accent {
75
+ --brim-btn-bg: var(--brim-theme-accent, #2563eb);
76
+ --brim-btn-text: var(--brim-theme-accent-contrast, #ffffff);
77
+ }
78
+
79
+ .brim-btn--dark-1 {
80
+ --brim-btn-bg: var(--brim-theme-dark1, #3f3f46);
81
+ --brim-btn-text: var(--brim-theme-background, #ffffff);
82
+ }
83
+
84
+ .brim-btn--dark-2 {
85
+ --brim-btn-bg: var(--brim-theme-dark2, #0a0a0a);
86
+ --brim-btn-text: var(--brim-theme-background, #ffffff);
87
+ }
88
+
89
+ .brim-btn--ghost {
90
+ --brim-btn-bg: transparent;
91
+ --brim-btn-text: currentColor;
92
+ --brim-btn-border: currentColor;
93
+ }
94
+
95
+ .brim-btn__icon {
96
+ flex: none;
97
+ width: 1em;
98
+ height: 1em;
99
+ }
100
+ }
@@ -0,0 +1,140 @@
1
+ ---
2
+ import { defaultScope } from "../../runtime/request-state.ts";
3
+ import { imageSources } from "../../runtime/media.ts";
4
+ import "./carousel.css";
5
+ import type { Item } from "../../content/items.ts";
6
+ import { type ItemBinding, boundValue } from "../../content/item-fields.ts";
7
+ import type { NodeScope } from "../../runtime/context.ts";
8
+ import { type CarouselValue, type StartingImage, readImages, toSaved } from "../images.ts";
9
+ import { carousel } from "../schemas/carousel.ts";
10
+
11
+ /**
12
+ * Slides the owner adds, orders and describes in the side panel, and how they move: the carousel keeps the images,
13
+ * the sizes visitors download, and a small script for arrows, dots, autoplay and the slide it opens on. Its look is
14
+ * the site's: draw it with a function, marking the parts the script works with:
15
+ *
16
+ * <Carousel nodeId="product-photos" item={product} field="gallery" sizes="(max-width: 860px) 100vw, 50vw">
17
+ * {({ slides }) => <>
18
+ * <div class="photos" data-track>{slides.map((slide) => <img {...slide.img} />)}</div>
19
+ * <div class="thumbs">{slides.map((slide, index) => <button data-go={index}><img {...slide.img} sizes="120px" /></button>)}</div>
20
+ * <button data-previous aria-label="Previous">‹</button> <button data-next aria-label="Next">›</button>
21
+ * </>}
22
+ * </Carousel>
23
+ *
24
+ * `data-track` holds the slides (it scrolls); `data-previous` and `data-next` step; `data-go="2"` (or `data-dot`)
25
+ * goes to a slide and is marked `data-current` while it is shown, as the slide is `data-active`. `variant="continuous"`
26
+ * rolls the track by itself, for a strip of logos at their own widths. Slides the site's CSS lays one over the other
27
+ * (a hero whose picture changes) are marked `data-active` in turn, and the site's CSS draws the change, a fade or
28
+ * anything else: Brim keeps the slides, which one shows and when. With `item` and `field` it shows an item's
29
+ * `gallery` field and saves into it. Without a function, Brim draws a plain slider.
30
+ */
31
+ interface Props {
32
+ nodeId: string;
33
+ scope?: NodeScope;
34
+ /** Starting slides: `{ src, alt, caption?, link? }`. */
35
+ images?: StartingImage[];
36
+ item?: Item;
37
+ field?: string;
38
+ /** How wide a slide is drawn, for the browser's choice of copy; by default from the variant. */
39
+ sizes?: string;
40
+ /** `slider` snaps slide by slide, `center-peek` shows the neighbours, `continuous` rolls by itself. */
41
+ variant?: "slider" | "center-peek" | "continuous";
42
+ slidesPerView?: number;
43
+ gap?: number;
44
+ showArrows?: boolean;
45
+ showDots?: boolean;
46
+ /** The slide it opens on, from 1. */
47
+ start?: number;
48
+ autoplay?: boolean;
49
+ autoplaySpeed?: number;
50
+ /** Autoplay stops while the pointer is on the carousel; off for a backdrop the pointer rests on. */
51
+ pauseOnHover?: boolean;
52
+ loop?: boolean;
53
+ /** Seconds the rolling strip takes to go round once. */
54
+ continuousSpeed?: number;
55
+ [attribute: string]: unknown;
56
+ }
57
+
58
+ const { nodeId, scope = defaultScope(), images: starting, item, field, sizes, ...rest } = Astro.props;
59
+ const SETTINGS = new Set(Object.keys(carousel.schema.properties));
60
+ const source = Object.fromEntries(Object.entries(rest).filter(([name]) => SETTINGS.has(name)));
61
+ // `class` is given once, through class:list: spread with the other attributes it would be written twice.
62
+ const { class: className, ...attributes } = Object.fromEntries(
63
+ Object.entries(rest).filter(([name]) => !SETTINGS.has(name)),
64
+ );
65
+ const binding: ItemBinding | undefined = item && field ? { item, field } : undefined;
66
+ const settings = Astro.locals.brim.settings(carousel, nodeId, scope, {
67
+ ...source,
68
+ ...(starting && { images: toSaved(starting) }),
69
+ });
70
+ if (binding) settings.images = toSaved((boundValue(binding) as StartingImage[] | undefined) ?? []);
71
+
72
+ const variant = settings.variant === "center-peek" || settings.variant === "continuous" ? settings.variant : "slider";
73
+ const continuous = variant === "continuous";
74
+ const perView = Math.max(1, Math.min(6, Number(settings.slidesPerView) || 1));
75
+ const drawnAt =
76
+ sizes ??
77
+ (continuous
78
+ ? "320px"
79
+ : variant === "center-peek"
80
+ ? "80vw"
81
+ : `(max-width: 480px) 100vw, ${Math.round(100 / perView)}vw`);
82
+ const slides = readImages(settings.images, (src) => imageSources(src, drawnAt));
83
+ const drawn = Astro.slots.has("default");
84
+ const content = drawn ? await Astro.slots.render("default", [{ slides } satisfies CarouselValue]) : undefined;
85
+ const style = [
86
+ `--brim-carousel-per-view:${perView}`,
87
+ `--brim-carousel-gap:${Number(settings.gap) || 0}px`,
88
+ `--brim-carousel-duration:${Number(settings.continuousSpeed) || 24}s`,
89
+ ].join(";");
90
+ const start = Math.max(0, (Number(settings.start) || 1) - 1);
91
+ ---
92
+
93
+ <brim-carousel
94
+ {...attributes}
95
+ class:list={[!drawn && "brim-carousel", className as string | undefined]}
96
+ data-variant={variant}
97
+ data-start={start > 0 ? start : undefined}
98
+ data-autoplay={!continuous && settings.autoplay ? Number(settings.autoplaySpeed) || 4000 : undefined}
99
+ data-pause-on-hover={settings.pauseOnHover !== false ? "" : undefined}
100
+ data-loop={settings.loop !== false ? "" : undefined}
101
+ style={style}
102
+ data-brim-drawn={drawn && Astro.locals.brim.editing ? "" : undefined}
103
+ {...Astro.locals.brim.marker("carousel", nodeId, scope, settings, binding)}
104
+ >
105
+ {drawn ? (
106
+ <Fragment set:html={content} />
107
+ ) : (
108
+ <>
109
+ <div class="brim-carousel__track" data-track tabindex={continuous ? undefined : 0}>
110
+ {slides.map((slide, index) => {
111
+ const Slide = slide.link ? "a" : "div";
112
+ return (
113
+ <Slide class="brim-carousel__slide" href={slide.link}>
114
+ <img {...slide.img} loading={index === start ? "eager" : "lazy"} decoding="async" />
115
+ {slide.caption && <span class="brim-carousel__caption">{slide.caption}</span>}
116
+ </Slide>
117
+ );
118
+ })}
119
+ </div>
120
+ {!continuous && settings.showArrows !== false && slides.length > 1 && (
121
+ <>
122
+ <button type="button" class="brim-carousel__arrow" data-previous aria-label="Previous slide" />
123
+ <button type="button" class="brim-carousel__arrow" data-next aria-label="Next slide" />
124
+ </>
125
+ )}
126
+ {!continuous && settings.showDots && slides.length > 1 && (
127
+ <div class="brim-carousel__dots">
128
+ {slides.map((_, index) => (
129
+ <button type="button" data-go={index} aria-label={`Go to slide ${index + 1}`} />
130
+ ))}
131
+ </div>
132
+ )}
133
+ </>
134
+ )}
135
+ {slides.length === 0 && Astro.locals.brim.editing && <p>No slides yet. Select the carousel to add some.</p>}
136
+ </brim-carousel>
137
+
138
+ <script>
139
+ import "./carousel.client.ts";
140
+ </script>
@@ -0,0 +1,153 @@
1
+ /**
2
+ * What makes a carousel move, in the site's markup or Brim's: `[data-track]` holds the slides and scrolls;
3
+ * `[data-previous]` and `[data-next]` step; `[data-go]` (or `[data-dot]`) goes to a slide and is marked
4
+ * `data-current` while it shows, as the slide is marked `data-active`. `data-start` is the slide it opens on,
5
+ * `data-autoplay` the time per slide, and `data-variant="continuous"` rolls the track by itself, seamlessly.
6
+ * A track that does not scroll (slides the site's CSS lays one over the other) keeps the slide shown itself, so
7
+ * how one slide gives way to the next (a fade, a zoom) is the site's CSS on `[data-active]`. Autoplay stops under
8
+ * the pointer unless `data-pause-on-hover` is absent, and never plays in the editor, where the owner clicks what
9
+ * they see.
10
+ */
11
+
12
+ import { inEditor } from "../in-editor.client.ts";
13
+
14
+ class BrimCarousel extends HTMLElement {
15
+ #track: HTMLElement | null = null;
16
+ #timer: number | undefined;
17
+ /** The slide shown, for a track that does not scroll and so cannot say. */
18
+ #shown = 0;
19
+
20
+ connectedCallback(): void {
21
+ this.#track = this.querySelector<HTMLElement>("[data-track]");
22
+ if (!this.#track) return;
23
+
24
+ if (this.dataset.variant === "continuous") {
25
+ // A copy of the slides follows them, so the strip rolls round without a seam. It is only seen, never read.
26
+ for (const slide of [...this.#track.children]) {
27
+ const copy = slide.cloneNode(true) as HTMLElement;
28
+ copy.setAttribute("aria-hidden", "true");
29
+ copy.setAttribute("inert", "");
30
+ this.#track.append(copy);
31
+ }
32
+ return;
33
+ }
34
+
35
+ this.querySelector("[data-previous]")?.addEventListener("click", () => this.#step(-1));
36
+ this.querySelector("[data-next]")?.addEventListener("click", () => this.#step(1));
37
+ for (const button of this.querySelectorAll<HTMLElement>("[data-go], [data-dot]")) {
38
+ button.addEventListener("click", () => this.#goTo(Number(button.dataset.go ?? button.dataset.dot), "smooth"));
39
+ }
40
+ // Marked when the scroll comes to rest, so a smooth scroll to a slide does not mark the ones it passes.
41
+ let settle: number | undefined;
42
+ this.#track.addEventListener(
43
+ "scroll",
44
+ () => {
45
+ window.clearTimeout(settle);
46
+ settle = window.setTimeout(() => this.#markCurrent(), 80);
47
+ },
48
+ { passive: true },
49
+ );
50
+ const start = Number(this.dataset.start);
51
+ if (start > 0) this.#goTo(start, "instant");
52
+ this.#markCurrent();
53
+
54
+ const interval = Number(this.dataset.autoplay);
55
+ const reducedMotion = matchMedia("(prefers-reduced-motion: reduce)").matches;
56
+ if (interval > 0 && !reducedMotion && !inEditor()) {
57
+ this.#play(interval);
58
+ if (this.dataset.pauseOnHover !== undefined) {
59
+ this.addEventListener("pointerenter", () => this.#pause());
60
+ this.addEventListener("pointerleave", () => this.#play(interval));
61
+ }
62
+ this.addEventListener("focusin", () => this.#pause());
63
+ this.addEventListener("focusout", () => this.#play(interval));
64
+ }
65
+ }
66
+
67
+ disconnectedCallback(): void {
68
+ this.#pause();
69
+ }
70
+
71
+ /** Whether the slides sit side by side and the track scrolls, rather than lie where the site's CSS puts them. */
72
+ get #scrolls(): boolean {
73
+ const track = this.#track;
74
+ if (!track) return false;
75
+ // Slides the site lays one over the other in a track that does not scroll (a fading hero) are wider than it
76
+ // only when a picture's own width widens their cell: that is not a track to scroll.
77
+ if (["hidden", "clip", "visible"].includes(getComputedStyle(track).overflowX)) return false;
78
+ return track.scrollWidth > track.clientWidth + 1;
79
+ }
80
+
81
+ get #slides(): HTMLElement[] {
82
+ return [...(this.#track?.children ?? [])] as HTMLElement[];
83
+ }
84
+
85
+ /** Where the track scrolls to show a slide: at its start, or in its middle for a carousel that peeks. */
86
+ #leftOf(slide: HTMLElement): number {
87
+ const track = this.#track as HTMLElement;
88
+ const left = slide.offsetLeft - track.offsetLeft;
89
+ return this.dataset.variant === "center-peek" ? left - (track.clientWidth - slide.clientWidth) / 2 : left;
90
+ }
91
+
92
+ /** The slide nearest where the track has scrolled to. */
93
+ get #current(): number {
94
+ const track = this.#track;
95
+ if (!track) return 0;
96
+ if (!this.#scrolls) return this.#shown;
97
+ const distances = this.#slides.map((slide) => Math.abs(this.#leftOf(slide) - track.scrollLeft));
98
+ return distances.indexOf(Math.min(...distances));
99
+ }
100
+
101
+ #goTo(index: number, behavior: ScrollBehavior): void {
102
+ const slide = this.#slides[index];
103
+ if (!this.#track || !slide) return;
104
+ if (this.#scrolls) this.#track.scrollTo({ left: this.#leftOf(slide), behavior });
105
+ // The slide asked for is marked at once; the scroll that follows confirms it.
106
+ this.#markCurrent(index);
107
+ }
108
+
109
+ #step(direction: 1 | -1): void {
110
+ const last = this.#slides.length - 1;
111
+ const next = this.#current + direction;
112
+ const loops = this.dataset.loop !== undefined;
113
+ const track = this.#track;
114
+ const atEnd = track && this.#scrolls ? track.scrollLeft + track.clientWidth >= track.scrollWidth - 1 : false;
115
+ if (direction === 1 && (next > last || atEnd)) return loops ? this.#goTo(0, "smooth") : undefined;
116
+ if (next < 0) return loops ? this.#goTo(last, "smooth") : undefined;
117
+ this.#goTo(next, "smooth");
118
+ }
119
+
120
+ #markCurrent(current = this.#current): void {
121
+ this.#shown = current;
122
+ this.#slides.forEach((slide, index) => slide.toggleAttribute("data-active", index === current));
123
+ for (const button of this.querySelectorAll<HTMLElement>("[data-go], [data-dot]")) {
124
+ const shows = Number(button.dataset.go ?? button.dataset.dot) === current;
125
+ button.toggleAttribute("data-current", shows);
126
+ if (shows) button.setAttribute("aria-current", "true");
127
+ else button.removeAttribute("aria-current");
128
+ if (shows) this.#reveal(button);
129
+ }
130
+ }
131
+
132
+ /** Keeps the current thumbnail in sight when the thumbnails scroll sideways themselves. */
133
+ #reveal(button: HTMLElement): void {
134
+ const strip = button.parentElement;
135
+ if (!strip || strip === this.#track || strip.scrollWidth <= strip.clientWidth) return;
136
+ const left = button.offsetLeft - strip.offsetLeft;
137
+ if (left < strip.scrollLeft) strip.scrollTo({ left, behavior: "smooth" });
138
+ else if (left + button.offsetWidth > strip.scrollLeft + strip.clientWidth) {
139
+ strip.scrollTo({ left: left + button.offsetWidth - strip.clientWidth, behavior: "smooth" });
140
+ }
141
+ }
142
+
143
+ #play(interval: number): void {
144
+ this.#pause();
145
+ this.#timer = window.setInterval(() => this.#step(1), interval);
146
+ }
147
+
148
+ #pause(): void {
149
+ window.clearInterval(this.#timer);
150
+ }
151
+ }
152
+
153
+ if (!customElements.get("brim-carousel")) customElements.define("brim-carousel", BrimCarousel);