@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,271 @@
1
+ /* Form and newsletter blocks. Layout and plain fields; the look around them belongs to the site. */
2
+ /* In the `brim` layer: any CSS of the site wins over it, whatever its selectors. */
3
+ @layer brim {
4
+ /* As wide as its place: the width is the site's, or the owner's in a blocks field. */
5
+ .brim-form {
6
+ display: block;
7
+ }
8
+
9
+ .brim-form form {
10
+ display: flex;
11
+ flex-direction: column;
12
+ gap: 1rem;
13
+ }
14
+
15
+ /* A step, button or message out of turn stays hidden whatever display the site's classes give it. */
16
+ .brim-form [hidden] {
17
+ display: none !important;
18
+ }
19
+
20
+ .brim-form__title {
21
+ margin: 0;
22
+ }
23
+
24
+ .brim-form__info,
25
+ .brim-form__disclaimer {
26
+ margin: 0;
27
+ white-space: pre-line;
28
+ }
29
+
30
+ .brim-form__disclaimer {
31
+ font-size: 0.875em;
32
+ opacity: 0.75;
33
+ }
34
+
35
+ /* Fields take the whole row; two halves share one where the form is wide enough. */
36
+ .brim-form {
37
+ container-type: inline-size;
38
+ }
39
+
40
+ .brim-form__fields {
41
+ display: grid;
42
+ grid-template-columns: repeat(2, minmax(0, 1fr));
43
+ gap: 1rem;
44
+ }
45
+
46
+ .brim-form__field {
47
+ display: flex;
48
+ grid-column: 1 / -1;
49
+ flex-direction: column;
50
+ gap: 0.375rem;
51
+ }
52
+
53
+ @container (min-width: 32rem) {
54
+ .brim-form__field[data-width="half"] {
55
+ grid-column: auto;
56
+ }
57
+ }
58
+
59
+ .brim-form__help {
60
+ margin: 0;
61
+ font-size: 0.875em;
62
+ opacity: 0.75;
63
+ }
64
+
65
+ /* A form asked in steps. Without the script every step shows, under its own title. */
66
+ .brim-form__step {
67
+ display: flex;
68
+ flex-direction: column;
69
+ gap: 1rem;
70
+ min-width: 0;
71
+ margin: 0;
72
+ padding: 0;
73
+ border: 0;
74
+ }
75
+
76
+ .brim-form__step-title {
77
+ padding: 0;
78
+ font-weight: 600;
79
+ }
80
+
81
+ .brim-form__step-intro {
82
+ margin: 0;
83
+ }
84
+
85
+ .brim-form__tabs,
86
+ .brim-form__progress {
87
+ display: none;
88
+ }
89
+
90
+ .brim-form[data-stepping] .brim-form__tabs {
91
+ display: flex;
92
+ flex-wrap: wrap;
93
+ gap: 0.5rem;
94
+ }
95
+
96
+ .brim-form[data-stepping] .brim-form__step-title {
97
+ position: absolute;
98
+ width: 1px;
99
+ height: 1px;
100
+ overflow: hidden;
101
+ clip-path: inset(50%);
102
+ }
103
+
104
+ .brim-form__tab {
105
+ display: inline-flex;
106
+ align-items: center;
107
+ gap: 0.5rem;
108
+ padding: 0.5rem 0.75rem;
109
+ border: 1px solid var(--brim-theme-light2, #e4e4e7);
110
+ border-radius: var(--brim-theme-radius, 0.5rem);
111
+ background: none;
112
+ color: inherit;
113
+ font: inherit;
114
+ cursor: pointer;
115
+ }
116
+
117
+ .brim-form__tab[aria-selected="true"] {
118
+ border-color: currentColor;
119
+ font-weight: 600;
120
+ }
121
+
122
+ .brim-form__tab-number {
123
+ font-variant-numeric: tabular-nums;
124
+ opacity: 0.6;
125
+ }
126
+
127
+ .brim-form[data-stepping] .brim-form__progress {
128
+ display: block;
129
+ height: 3px;
130
+ overflow: hidden;
131
+ background: var(--brim-theme-light2, #e4e4e7);
132
+ }
133
+
134
+ .brim-form__progress span {
135
+ display: block;
136
+ width: var(--brim-form-progress, 0%);
137
+ height: 100%;
138
+ background: var(--brim-theme-primary, #1e1e1d);
139
+ transition: width 0.3s ease;
140
+ }
141
+
142
+ .brim-form :is(input:not([type="checkbox"], [type="radio"]), textarea, select) {
143
+ width: 100%;
144
+ padding: 0.65rem 0.75rem;
145
+ border: 1px solid var(--brim-theme-light2, #e4e4e7);
146
+ border-radius: var(--brim-theme-radius, 0.5rem);
147
+ background: var(--brim-theme-background, #ffffff);
148
+ color: inherit;
149
+ font: inherit;
150
+ }
151
+
152
+ .brim-form :is(input, textarea, select):focus-visible {
153
+ outline: 2px solid var(--brim-theme-primary, #1e1e1d);
154
+ outline-offset: 1px;
155
+ }
156
+
157
+ .brim-form__check {
158
+ display: flex;
159
+ align-items: flex-start;
160
+ gap: 0.5rem;
161
+ }
162
+
163
+ .brim-form__choices {
164
+ display: flex;
165
+ flex-direction: column;
166
+ gap: 0.375rem;
167
+ }
168
+
169
+ /* Choices as cards: a grid of boxes, each a whole label to click. The radio stays in the page, for the keyboard,
170
+ screen readers and the form's check, out of sight. The look of a card is the site's. */
171
+ .brim-form__choices[data-display="cards"] {
172
+ display: grid;
173
+ grid-template-columns: repeat(auto-fill, minmax(min(100%, 9rem), 1fr));
174
+ gap: 0.5rem;
175
+ }
176
+
177
+ .brim-form__choice {
178
+ position: relative;
179
+ display: flex;
180
+ flex-direction: column;
181
+ align-items: flex-start;
182
+ gap: 0.375rem;
183
+ padding: 0.75rem;
184
+ border: 1px solid var(--brim-theme-light2, #e4e4e7);
185
+ border-radius: var(--brim-theme-radius, 0.5rem);
186
+ cursor: pointer;
187
+ }
188
+
189
+ .brim-form__choice input {
190
+ position: absolute;
191
+ inset: 0;
192
+ width: 100%;
193
+ height: 100%;
194
+ margin: 0;
195
+ opacity: 0;
196
+ cursor: pointer;
197
+ }
198
+
199
+ .brim-form__choice:has(input:checked) {
200
+ border-color: currentColor;
201
+ }
202
+
203
+ .brim-form__choice:has(input:focus-visible) {
204
+ outline: 2px solid var(--brim-theme-primary, #1e1e1d);
205
+ outline-offset: 1px;
206
+ }
207
+
208
+ /* A choice with a photo: the photo fills the card's top, edge to edge. */
209
+ .brim-form__choice-image {
210
+ display: block;
211
+ align-self: stretch;
212
+ margin: -0.75rem -0.75rem 0;
213
+ overflow: hidden;
214
+ border-radius: inherit;
215
+ border-bottom-left-radius: 0;
216
+ border-bottom-right-radius: 0;
217
+ }
218
+
219
+ .brim-form__choice-image img {
220
+ display: block;
221
+ width: 100%;
222
+ aspect-ratio: 4 / 3;
223
+ object-fit: cover;
224
+ }
225
+
226
+ .brim-form__choice-icon img {
227
+ display: block;
228
+ width: 1.75rem;
229
+ height: 1.75rem;
230
+ object-fit: contain;
231
+ }
232
+
233
+ /* Kept in the page for scripts to find, out of sight for people. */
234
+ .brim-form__trap {
235
+ position: absolute;
236
+ left: -9999px;
237
+ width: 1px;
238
+ height: 1px;
239
+ overflow: hidden;
240
+ }
241
+
242
+ .brim-form__actions {
243
+ display: flex;
244
+ flex-wrap: wrap;
245
+ gap: 0.75rem;
246
+ }
247
+
248
+ .brim-form[data-stepped] .brim-form__actions :is(.brim-form__next, .brim-form__submit) {
249
+ margin-left: auto;
250
+ }
251
+
252
+ .brim-form__error {
253
+ margin: 0;
254
+ color: #b42318;
255
+ }
256
+
257
+ .brim-form__success {
258
+ margin: 0;
259
+ white-space: pre-line;
260
+ }
261
+
262
+ .brim-newsletter__row {
263
+ display: flex;
264
+ flex-wrap: wrap;
265
+ gap: 0.5rem;
266
+ }
267
+
268
+ .brim-newsletter__row input {
269
+ flex: 1 1 14rem;
270
+ }
271
+ }
@@ -0,0 +1,126 @@
1
+ ---
2
+ import { defaultScope } from "../../runtime/request-state.ts";
3
+ import { imageSources } from "../../runtime/media.ts";
4
+ import "./gallery.css";
5
+ import "./lightbox.css";
6
+ import type { Item } from "../../content/items.ts";
7
+ import { type ItemBinding, boundValue } from "../../content/item-fields.ts";
8
+ import type { NodeScope } from "../../runtime/context.ts";
9
+ import { type GalleryValue, type StartingImage, readImages, toSaved } from "../images.ts";
10
+ import { imageGallery } from "../schemas/image-gallery.ts";
11
+
12
+ /** The slideshow's buttons, in English unless the site says otherwise. */
13
+ const LABELS = { gallery: "Gallery", close: "Close", previous: "Previous picture", next: "Next picture" };
14
+
15
+ /**
16
+ * Images the owner adds, orders and describes in the side panel. The gallery keeps them and the sizes visitors
17
+ * download (a `srcset` the CDN serves); their layout is the site's. Draw it with a function, any shape at all:
18
+ *
19
+ * <Gallery nodeId="team-photos" images={photos} sizes="(max-width: 768px) 100vw, 50vw">
20
+ * {({ images }) => images.map((image) => <img {...image.img} class="photo" loading="lazy" />)}
21
+ * </Gallery>
22
+ *
23
+ * With `item` and `field` it shows an item's `gallery` field, and what the owner changes is saved into the item.
24
+ * Without a function, Brim draws a plain grid of `columns`.
25
+ *
26
+ * A picture that is not a link opens over the whole screen, as a slideshow of the gallery; `image.img` carries what
27
+ * makes it open. `lightbox={false}` leaves it out, and so does the owner's switch in the side panel. `labels` says
28
+ * its buttons in the page's language.
29
+ */
30
+ interface Props {
31
+ nodeId: string;
32
+ scope?: NodeScope;
33
+ /** Starting images: `{ src, alt, caption?, link? }`. */
34
+ images?: StartingImage[];
35
+ item?: Item;
36
+ field?: string;
37
+ /** How wide each image is drawn, for the browser's choice of copy; by default from `columns`. */
38
+ sizes?: string;
39
+ columns?: number;
40
+ gap?: number;
41
+ /** Whether a click opens a picture over the whole screen; true until the owner switches it off. */
42
+ lightbox?: boolean;
43
+ /** What the slideshow's buttons are called for a screen reader, in the page's language. */
44
+ labels?: Partial<typeof LABELS>;
45
+ as?: "div" | "section" | "ul" | "figure";
46
+ [attribute: string]: unknown;
47
+ }
48
+
49
+ const {
50
+ nodeId,
51
+ scope = defaultScope(),
52
+ images: starting,
53
+ item,
54
+ field,
55
+ sizes,
56
+ columns,
57
+ gap,
58
+ lightbox,
59
+ labels,
60
+ as: Tag = "div",
61
+ // Given once, through class:list: spread with the other attributes it would be written twice.
62
+ class: className,
63
+ ...attributes
64
+ } = Astro.props;
65
+ const binding: ItemBinding | undefined = item && field ? { item, field } : undefined;
66
+ const settings = Astro.locals.brim.settings(imageGallery, nodeId, scope, {
67
+ ...(starting && { images: toSaved(starting) }),
68
+ ...(columns !== undefined && { columns }),
69
+ ...(gap !== undefined && { gap }),
70
+ ...(lightbox !== undefined && { lightbox }),
71
+ });
72
+ // The item's gallery wins over anything stored with the element: the item is where it is saved.
73
+ if (binding) settings.images = toSaved((boundValue(binding) as StartingImage[] | undefined) ?? []);
74
+ const count = Math.max(1, Math.min(6, Number(settings.columns) || 3));
75
+ const drawnAt = sizes ?? `(max-width: 640px) 100vw, ${Math.round(100 / count)}vw`;
76
+ const opensPictures = settings.lightbox !== false;
77
+ const images = readImages(settings.images, (src) => imageSources(src, drawnAt)).map((image, index) => ({
78
+ ...image,
79
+ img: {
80
+ ...image.img,
81
+ ...(opensPictures && !image.link && { "data-lightbox": index, "data-caption": image.caption }),
82
+ },
83
+ }));
84
+ const words = { ...LABELS, ...labels };
85
+ const opening =
86
+ opensPictures && images.some((image) => !image.link)
87
+ ? {
88
+ "data-lightbox-gallery": "",
89
+ "data-lightbox-label": words.gallery,
90
+ "data-lightbox-close": words.close,
91
+ "data-lightbox-previous": words.previous,
92
+ "data-lightbox-next": words.next,
93
+ }
94
+ : undefined;
95
+ const drawn = Astro.slots.has("default");
96
+ const content = drawn ? await Astro.slots.render("default", [{ images } satisfies GalleryValue]) : undefined;
97
+ ---
98
+
99
+ <Tag
100
+ {...attributes}
101
+ class:list={[!drawn && "brim-gallery", className as string | undefined]}
102
+ style={drawn ? undefined : `--brim-gallery-columns:${count};--brim-gallery-gap:${Number(settings.gap) || 0}px`}
103
+ data-brim-drawn={drawn && Astro.locals.brim.editing ? "" : undefined}
104
+ {...opening}
105
+ {...Astro.locals.brim.marker("image-gallery", nodeId, scope, settings, binding)}
106
+ >
107
+ {drawn ? (
108
+ <Fragment set:html={content} />
109
+ ) : (
110
+ images.map((image) => {
111
+ const Frame = image.link ? "a" : "figure";
112
+ return (
113
+ <Frame class="brim-gallery__item" href={image.link}>
114
+ <img {...image.img} loading="lazy" decoding="async" />
115
+ {image.caption && <span class="brim-gallery__caption">{image.caption}</span>}
116
+ </Frame>
117
+ );
118
+ })
119
+ )}
120
+ {images.length === 0 && Astro.locals.brim.editing && <p>No images yet. Select the gallery to add some.</p>}
121
+ </Tag>
122
+
123
+ {/* Only a page with a gallery that opens gets the script. */}
124
+ {opening && <script>
125
+ import "./lightbox.client.ts";
126
+ </script>}
@@ -0,0 +1,43 @@
1
+ /* The grid Brim draws for a gallery the site does not draw itself: `columns` wide, fewer on smaller screens. */
2
+ /* In the `brim` layer: any CSS of the site wins over it, whatever its selectors. */
3
+ @layer brim {
4
+ .brim-gallery {
5
+ --brim-gallery-current-columns: var(--brim-gallery-columns, 3);
6
+
7
+ display: grid;
8
+ grid-template-columns: repeat(var(--brim-gallery-current-columns), minmax(0, 1fr));
9
+ gap: var(--brim-gallery-gap, 16px);
10
+ }
11
+
12
+ .brim-gallery__item {
13
+ display: flex;
14
+ flex-direction: column;
15
+ gap: 0.5rem;
16
+ margin: 0;
17
+ color: inherit;
18
+ text-decoration: none;
19
+ }
20
+
21
+ .brim-gallery__item img {
22
+ display: block;
23
+ width: 100%;
24
+ height: auto;
25
+ border-radius: var(--brim-theme-radius, 0.5rem);
26
+ }
27
+
28
+ .brim-gallery__caption {
29
+ font-size: 0.875em;
30
+ }
31
+
32
+ @media (max-width: 768px) {
33
+ .brim-gallery {
34
+ --brim-gallery-current-columns: min(var(--brim-gallery-columns, 3), 2);
35
+ }
36
+ }
37
+
38
+ @media (max-width: 480px) {
39
+ .brim-gallery {
40
+ --brim-gallery-current-columns: 1;
41
+ }
42
+ }
43
+ }
@@ -0,0 +1,178 @@
1
+ /**
2
+ * A gallery's pictures over the whole screen, as a slideshow: buttons, keys (arrows, Home, End; Escape closes) and a
3
+ * swipe go from one to the next. A gallery marks itself `data-lightbox-gallery` and its pictures `data-lightbox`
4
+ * (`image.img` carries it, in the site's markup too); the slides are built from those <img>s when one is opened, so
5
+ * the gallery's markup stays the site's. A picture inside a link or a button keeps doing what that does. Nothing opens
6
+ * in the editor, where a click selects.
7
+ */
8
+ import { inEditor } from "../in-editor.client.ts";
9
+
10
+ const GALLERY = "[data-lightbox-gallery]";
11
+ const PICTURE = "img[data-lightbox]";
12
+
13
+ const ICONS = {
14
+ close: "M6 6l12 12M18 6L6 18",
15
+ previous: "M15 5l-7 7 7 7",
16
+ next: "M9 5l7 7-7 7",
17
+ };
18
+
19
+ const opens = (picture: Element): boolean => picture.closest("a[href], button") === null;
20
+
21
+ /** A gallery's pictures in order, each once: a thumbnail of one shown larger is the same picture. */
22
+ function picturesOf(gallery: Element): HTMLImageElement[] {
23
+ const pictures = new Map<string, HTMLImageElement>();
24
+ for (const picture of gallery.querySelectorAll<HTMLImageElement>(PICTURE)) {
25
+ const position = picture.dataset.lightbox ?? "";
26
+ if (opens(picture) && !pictures.has(position)) pictures.set(position, picture);
27
+ }
28
+ return [...pictures.values()];
29
+ }
30
+
31
+ function button(name: keyof typeof ICONS, label: string): HTMLButtonElement {
32
+ const element = document.createElement("button");
33
+ element.type = "button";
34
+ element.className = `brim-lightbox__button brim-lightbox__${name}`;
35
+ element.setAttribute("aria-label", label);
36
+ element.innerHTML = `<svg viewBox="0 0 24 24" width="24" height="24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="${ICONS[name]}"/></svg>`;
37
+ return element;
38
+ }
39
+
40
+ function slideOf(picture: HTMLImageElement, shown: boolean): HTMLElement {
41
+ const figure = document.createElement("figure");
42
+ figure.className = "brim-lightbox__slide";
43
+ const image = document.createElement("img");
44
+ image.className = "brim-lightbox__image";
45
+ image.alt = picture.alt;
46
+ image.decoding = "async";
47
+ // Before the address: a picture far from the one opened is downloaded only as the slideshow nears it.
48
+ image.loading = shown ? "eager" : "lazy";
49
+ image.sizes = "100vw";
50
+ if (picture.srcset) image.srcset = picture.srcset;
51
+ image.src = picture.src;
52
+ figure.append(image);
53
+ const caption = picture.dataset.caption;
54
+ if (caption) {
55
+ const text = document.createElement("figcaption");
56
+ text.className = "brim-lightbox__caption";
57
+ text.textContent = caption;
58
+ figure.append(text);
59
+ }
60
+ return figure;
61
+ }
62
+
63
+ function open(gallery: HTMLElement, opened: HTMLImageElement): void {
64
+ const pictures = picturesOf(gallery);
65
+ const start = Math.max(
66
+ 0,
67
+ pictures.findIndex((picture) => picture.dataset.lightbox === opened.dataset.lightbox),
68
+ );
69
+ const { lightboxLabel = "", lightboxClose = "", lightboxPrevious = "", lightboxNext = "" } = gallery.dataset;
70
+
71
+ const dialog = document.createElement("dialog");
72
+ dialog.className = "brim-lightbox";
73
+ dialog.setAttribute("aria-label", lightboxLabel);
74
+ const track = document.createElement("div");
75
+ track.className = "brim-lightbox__track";
76
+ const slides = pictures.map((picture, index) => slideOf(picture, index === start));
77
+ track.append(...slides);
78
+ const count = document.createElement("p");
79
+ count.className = "brim-lightbox__count";
80
+ count.setAttribute("aria-live", "polite");
81
+ const previous = button("previous", lightboxPrevious);
82
+ const next = button("next", lightboxNext);
83
+ const close = button("close", lightboxClose);
84
+ dialog.append(track, close);
85
+ if (slides.length > 1) dialog.append(count, previous, next);
86
+
87
+ let shown = start;
88
+ const mark = (index: number): void => {
89
+ shown = index;
90
+ count.textContent = `${index + 1} / ${slides.length}`;
91
+ };
92
+ const goTo = (index: number, behavior: ScrollBehavior): void => {
93
+ track.scrollTo({ left: slides[index]?.offsetLeft ?? 0, behavior });
94
+ mark(index);
95
+ };
96
+ const still = matchMedia("(prefers-reduced-motion: reduce)");
97
+ const step = (by: 1 | -1): void => {
98
+ const index = (shown + by + slides.length) % slides.length;
99
+ // Round from the last picture to the first at once, not back past every one in between.
100
+ const jumps = Math.abs(index - shown) > 1 || still.matches;
101
+ goTo(index, jumps ? "instant" : "smooth");
102
+ };
103
+ const rightToLeft = getComputedStyle(gallery).direction === "rtl";
104
+
105
+ previous.addEventListener("click", () => step(-1));
106
+ next.addEventListener("click", () => step(1));
107
+ close.addEventListener("click", () => dialog.close());
108
+ // The room around a picture closes it.
109
+ track.addEventListener("click", (event) => {
110
+ if (!(event.target instanceof HTMLImageElement)) dialog.close();
111
+ });
112
+ dialog.addEventListener("keydown", (event) => {
113
+ if (event.key === "ArrowLeft") step(rightToLeft ? 1 : -1);
114
+ else if (event.key === "ArrowRight") step(rightToLeft ? -1 : 1);
115
+ else if (event.key === "Home") goTo(0, "instant");
116
+ else if (event.key === "End") goTo(slides.length - 1, "instant");
117
+ else return;
118
+ event.preventDefault();
119
+ });
120
+
121
+ // A swipe is the track's own scrolling: which picture shows is read once it comes to rest.
122
+ let settle: number | undefined;
123
+ track.addEventListener(
124
+ "scroll",
125
+ () => {
126
+ window.clearTimeout(settle);
127
+ settle = window.setTimeout(() => {
128
+ // The track scrolls the other way, below zero, on a page read from right to left.
129
+ if (track.clientWidth > 0) mark(Math.round(Math.abs(track.scrollLeft) / track.clientWidth));
130
+ }, 120);
131
+ },
132
+ { passive: true },
133
+ );
134
+ const onResize = (): void => goTo(shown, "instant");
135
+ window.addEventListener("resize", onResize);
136
+
137
+ dialog.addEventListener("close", () => {
138
+ window.clearTimeout(settle);
139
+ window.removeEventListener("resize", onResize);
140
+ dialog.remove();
141
+ opened.focus({ preventScroll: true });
142
+ });
143
+
144
+ document.body.append(dialog);
145
+ dialog.showModal();
146
+ goTo(start, "instant");
147
+ }
148
+
149
+ /** Opens the picture clicked or pressed; false when it is not one that opens. */
150
+ function openFrom(target: EventTarget | null): boolean {
151
+ if (!(target instanceof HTMLImageElement) || !target.matches(PICTURE) || !opens(target)) return false;
152
+ const gallery = target.closest<HTMLElement>(GALLERY);
153
+ if (!gallery) return false;
154
+ open(gallery, target);
155
+ return true;
156
+ }
157
+
158
+ /** The pictures are reached with Tab and said to be buttons, only where the script that opens them runs. */
159
+ function prepare(): void {
160
+ for (const picture of document.querySelectorAll<HTMLImageElement>(`${GALLERY} ${PICTURE}`)) {
161
+ if (!opens(picture) || picture.getAttribute("role") === "button") continue;
162
+ picture.tabIndex = 0;
163
+ picture.setAttribute("role", "button");
164
+ picture.setAttribute("aria-haspopup", "dialog");
165
+ }
166
+ }
167
+
168
+ if (!inEditor()) {
169
+ prepare();
170
+ // A site with Astro's client router draws its next page without loading this again.
171
+ document.addEventListener("astro:page-load", prepare);
172
+ document.addEventListener("click", (event) => {
173
+ openFrom(event.target);
174
+ });
175
+ document.addEventListener("keydown", (event) => {
176
+ if ((event.key === "Enter" || event.key === " ") && openFrom(event.target)) event.preventDefault();
177
+ });
178
+ }