@backmatter/studio 0.0.22 → 0.0.24

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.
@@ -0,0 +1,542 @@
1
+ // packages/studio/src/pages/BlockLibrary.tsx
2
+ // Browse the site's blocks by group, like a component library: a grid of
3
+ // group covers, then one group's blocks beside a list of every group. Search
4
+ // spans all blocks. With `onPick` a card adds the block; without it the
5
+ // library is read-only and lists each block's fields.
6
+ import { useMemo, useState } from "react";
7
+ import { Dialog } from "@base-ui/react/dialog";
8
+ import * as stylex from "@stylexjs/stylex";
9
+ import type { BlockSnapshot } from "../api";
10
+ import { useStudioDefinition } from "../define-studio";
11
+ import { fieldLabel } from "../fields/field-label";
12
+ import { Button } from "../ui/Button";
13
+ import { Input } from "../ui/Input";
14
+ import { cx, isTransitioning } from "../ui/popup";
15
+ import { colors, radius, space, type, z } from "../styles/tokens.stylex";
16
+ import { BlockThumbnail } from "./BlockPicker";
17
+ import { blockPreviewSrc } from "./block-picker";
18
+ import { groupBlocks, searchBlocks, type BlockGroupView } from "./block-groups";
19
+
20
+ const hover = "@media (hover: hover)";
21
+ const narrow = "@media (max-width: 640px)";
22
+
23
+ function countLabel(n: number) {
24
+ return n === 1 ? "1 block" : `${n} blocks`;
25
+ }
26
+
27
+ function GroupCover({ group, siteUrl }: { group: BlockGroupView; siteUrl?: string | null }) {
28
+ const [failed, setFailed] = useState(false);
29
+ const src = blockPreviewSrc(group.coverSrc, siteUrl);
30
+ if (src && !failed) {
31
+ return (
32
+ <span aria-hidden {...stylex.props(styles.cover)}>
33
+ <img
34
+ src={src}
35
+ alt=""
36
+ loading="lazy"
37
+ decoding="async"
38
+ draggable={false}
39
+ onError={() => setFailed(true)}
40
+ {...stylex.props(styles.coverImage)}
41
+ />
42
+ </span>
43
+ );
44
+ }
45
+ return <BlockThumbnail block={group.cover} siteUrl={siteUrl} />;
46
+ }
47
+
48
+ function BlockCard({
49
+ block,
50
+ siteUrl,
51
+ onPick,
52
+ }: {
53
+ block: BlockSnapshot;
54
+ siteUrl?: string | null;
55
+ onPick?: (type: string) => void;
56
+ }) {
57
+ const definition = useStudioDefinition();
58
+ const label = definition.blocks?.[block.name]?.label ?? block.name;
59
+ const fields = Object.entries(block.fields ?? {});
60
+ const body = (
61
+ <>
62
+ <BlockThumbnail block={block} siteUrl={siteUrl} />
63
+ <span {...stylex.props(styles.cardText)}>
64
+ <span {...stylex.props(styles.cardName)}>{label}</span>
65
+ {block.description ? (
66
+ <span {...stylex.props(styles.cardDescription)}>{block.description}</span>
67
+ ) : null}
68
+ {!onPick && fields.length > 0 ? (
69
+ <span {...stylex.props(styles.cardMeta)}>
70
+ {fields.map(([name, field]) => fieldLabel(name, field)).join(" · ")}
71
+ </span>
72
+ ) : null}
73
+ </span>
74
+ </>
75
+ );
76
+ if (!onPick) {
77
+ return (
78
+ <div data-block-card={block.name} {...stylex.props(styles.card)}>
79
+ {body}
80
+ </div>
81
+ );
82
+ }
83
+ return (
84
+ <button
85
+ type="button"
86
+ data-block-card={block.name}
87
+ onClick={() => onPick(block.name)}
88
+ {...stylex.props(styles.card, styles.cardButton)}
89
+ >
90
+ {body}
91
+ </button>
92
+ );
93
+ }
94
+
95
+ export function BlockLibrary({
96
+ catalog,
97
+ siteUrl,
98
+ onPick,
99
+ autoFocusSearch,
100
+ }: {
101
+ catalog: BlockSnapshot[];
102
+ siteUrl?: string | null;
103
+ onPick?: (type: string) => void;
104
+ autoFocusSearch?: boolean;
105
+ }) {
106
+ const definition = useStudioDefinition();
107
+ const groups = useMemo(
108
+ () => groupBlocks(catalog, definition.blockGroups),
109
+ [catalog, definition.blockGroups],
110
+ );
111
+ const [openGroup, setOpenGroup] = useState<string | null>(null);
112
+ const [query, setQuery] = useState("");
113
+ const labelOf = (b: BlockSnapshot) => definition.blocks?.[b.name]?.label ?? b.name;
114
+ const hits = query.trim() ? searchBlocks(groups, query, labelOf) : null;
115
+ const current = groups.find((g) => g.name === openGroup) ?? null;
116
+
117
+ const grid = (blocks: BlockSnapshot[]) => (
118
+ <div {...stylex.props(styles.blockGrid)}>
119
+ {blocks.map((block) => (
120
+ <BlockCard key={block.name} block={block} siteUrl={siteUrl} onPick={onPick} />
121
+ ))}
122
+ </div>
123
+ );
124
+
125
+ let main;
126
+ if (hits) {
127
+ main = hits.length ? (
128
+ grid(hits)
129
+ ) : (
130
+ <p {...stylex.props(styles.empty)}>No blocks match “{query.trim()}”.</p>
131
+ );
132
+ } else if (current) {
133
+ main = (
134
+ <div {...stylex.props(styles.groupDetail)}>
135
+ <div {...stylex.props(styles.groupHeading)}>
136
+ <h3 {...stylex.props(styles.groupTitle)}>{current.label}</h3>
137
+ {current.description ? (
138
+ <p {...stylex.props(styles.groupDescription)}>{current.description}</p>
139
+ ) : null}
140
+ </div>
141
+ {grid(current.blocks)}
142
+ </div>
143
+ );
144
+ } else {
145
+ main = (
146
+ <div {...stylex.props(styles.groupGrid)}>
147
+ {groups.map((group) => (
148
+ <button
149
+ key={group.name}
150
+ type="button"
151
+ data-block-group={group.name}
152
+ onClick={() => setOpenGroup(group.name)}
153
+ {...stylex.props(styles.card, styles.cardButton)}
154
+ >
155
+ <GroupCover group={group} siteUrl={siteUrl} />
156
+ <span {...stylex.props(styles.cardText)}>
157
+ <span {...stylex.props(styles.cardName)}>{group.label}</span>
158
+ <span {...stylex.props(styles.cardCount)}>{countLabel(group.blocks.length)}</span>
159
+ </span>
160
+ </button>
161
+ ))}
162
+ </div>
163
+ );
164
+ }
165
+
166
+ return (
167
+ <div {...stylex.props(styles.root)}>
168
+ <div {...stylex.props(styles.toolbar)}>
169
+ {current && !hits ? (
170
+ <button
171
+ type="button"
172
+ onClick={() => setOpenGroup(null)}
173
+ {...stylex.props(styles.crumb)}
174
+ >
175
+ ← All groups
176
+ </button>
177
+ ) : (
178
+ <span {...stylex.props(styles.crumbLabel)}>
179
+ {hits ? countLabel(hits.length) : `${groups.length} groups · ${countLabel(catalog.length)}`}
180
+ </span>
181
+ )}
182
+ <div {...stylex.props(styles.search)}>
183
+ <Input
184
+ type="search"
185
+ placeholder="Search blocks"
186
+ aria-label="Search blocks"
187
+ value={query}
188
+ autoFocus={autoFocusSearch}
189
+ onChange={(e) => setQuery(e.currentTarget.value)}
190
+ />
191
+ </div>
192
+ </div>
193
+ <div {...stylex.props(styles.body, current && !hits && styles.bodySplit)}>
194
+ {current && !hits ? (
195
+ <nav aria-label="Block groups" {...stylex.props(styles.nav)}>
196
+ {groups.map((group) => (
197
+ <button
198
+ key={group.name}
199
+ type="button"
200
+ aria-current={group.name === current.name ? "true" : undefined}
201
+ onClick={() => setOpenGroup(group.name)}
202
+ {...stylex.props(
203
+ styles.navItem,
204
+ group.name === current.name && styles.navItemActive,
205
+ )}
206
+ >
207
+ <span {...stylex.props(styles.navLabel)}>{group.label}</span>
208
+ <span {...stylex.props(styles.navCount)}>{group.blocks.length}</span>
209
+ </button>
210
+ ))}
211
+ </nav>
212
+ ) : null}
213
+ <div {...stylex.props(styles.main)}>{main}</div>
214
+ </div>
215
+ </div>
216
+ );
217
+ }
218
+
219
+ /** "Add block" as a dialog that opens on the block groups. */
220
+ export function BlockLibraryDialog({
221
+ catalog,
222
+ siteUrl,
223
+ disabled,
224
+ onPick,
225
+ }: {
226
+ catalog: BlockSnapshot[];
227
+ siteUrl?: string | null;
228
+ disabled?: boolean;
229
+ onPick: (type: string) => void;
230
+ }) {
231
+ const [open, setOpen] = useState(false);
232
+ return (
233
+ <Dialog.Root open={open} onOpenChange={setOpen}>
234
+ <Dialog.Trigger
235
+ disabled={disabled}
236
+ render={
237
+ <Button variant="secondary" disabled={disabled}>
238
+ Add block
239
+ </Button>
240
+ }
241
+ />
242
+ <Dialog.Portal>
243
+ <Dialog.Backdrop
244
+ className={(state) =>
245
+ cx(dialog.backdrop, isTransitioning(state.transitionStatus) && dialog.hidden)
246
+ }
247
+ />
248
+ <Dialog.Popup
249
+ aria-label="Add block"
250
+ className={(state) =>
251
+ cx(dialog.popup, isTransitioning(state.transitionStatus) && dialog.hidden)
252
+ }
253
+ >
254
+ <div {...stylex.props(dialog.header)}>
255
+ <Dialog.Title className={cx(dialog.title)}>Add block</Dialog.Title>
256
+ <Dialog.Close render={<Button variant="ghost" size="sm">Close</Button>} />
257
+ </div>
258
+ {open ? (
259
+ <BlockLibrary
260
+ catalog={catalog}
261
+ siteUrl={siteUrl}
262
+ autoFocusSearch
263
+ onPick={(type) => {
264
+ onPick(type);
265
+ setOpen(false);
266
+ }}
267
+ />
268
+ ) : null}
269
+ </Dialog.Popup>
270
+ </Dialog.Portal>
271
+ </Dialog.Root>
272
+ );
273
+ }
274
+
275
+ const dialog = stylex.create({
276
+ backdrop: {
277
+ position: "fixed",
278
+ inset: 0,
279
+ zIndex: z.palette,
280
+ backgroundColor: colors.overlay,
281
+ transitionProperty: "opacity",
282
+ transitionDuration: "120ms",
283
+ "@media (prefers-reduced-motion: reduce)": { transitionDuration: "0ms" },
284
+ },
285
+ popup: {
286
+ position: "fixed",
287
+ inset: 0,
288
+ margin: "auto",
289
+ zIndex: z.palette,
290
+ boxSizing: "border-box",
291
+ display: "flex",
292
+ flexDirection: "column",
293
+ width: "min(1080px, calc(100vw - 32px))",
294
+ height: "min(760px, calc(100vh - 32px))",
295
+ overflow: "hidden",
296
+ borderRadius: radius.md,
297
+ borderWidth: 1,
298
+ borderStyle: "solid",
299
+ borderColor: colors.border,
300
+ backgroundColor: colors.bgApp,
301
+ color: colors.text,
302
+ boxShadow: "0 16px 48px rgba(24, 24, 27, 0.2)",
303
+ outline: "none",
304
+ transitionProperty: "opacity, transform",
305
+ transitionDuration: "120ms",
306
+ transitionTimingFunction: "ease-out",
307
+ "@media (prefers-reduced-motion: reduce)": { transitionDuration: "0ms" },
308
+ },
309
+ hidden: {
310
+ opacity: 0,
311
+ },
312
+ header: {
313
+ display: "flex",
314
+ alignItems: "center",
315
+ justifyContent: "space-between",
316
+ gap: space.lg,
317
+ paddingBlock: space.md,
318
+ paddingInlineStart: space.xl,
319
+ paddingInlineEnd: space.md,
320
+ borderBottomWidth: 1,
321
+ borderBottomStyle: "solid",
322
+ borderBottomColor: colors.border,
323
+ backgroundColor: colors.bgSurface,
324
+ },
325
+ title: {
326
+ margin: 0,
327
+ fontSize: type.lg,
328
+ fontWeight: 600,
329
+ },
330
+ });
331
+
332
+ const styles = stylex.create({
333
+ root: {
334
+ display: "flex",
335
+ flexDirection: "column",
336
+ minHeight: 0,
337
+ flex: 1,
338
+ },
339
+ toolbar: {
340
+ display: "flex",
341
+ alignItems: "center",
342
+ justifyContent: "space-between",
343
+ gap: space.lg,
344
+ paddingBlock: space.lg,
345
+ paddingInline: space.xl,
346
+ borderBottomWidth: 1,
347
+ borderBottomStyle: "solid",
348
+ borderBottomColor: colors.border,
349
+ },
350
+ crumb: {
351
+ appearance: "none",
352
+ border: "none",
353
+ background: "none",
354
+ padding: 0,
355
+ color: colors.textMuted,
356
+ fontSize: type.md,
357
+ cursor: "pointer",
358
+ [hover]: { ":hover": { color: colors.text } },
359
+ },
360
+ crumbLabel: {
361
+ color: colors.textMuted,
362
+ fontFamily: type.mono,
363
+ fontSize: type.xs,
364
+ letterSpacing: "0.06em",
365
+ textTransform: "uppercase",
366
+ },
367
+ search: {
368
+ width: "16rem",
369
+ maxWidth: "50%",
370
+ },
371
+ body: {
372
+ display: "flex",
373
+ flex: 1,
374
+ minHeight: 0,
375
+ },
376
+ bodySplit: {
377
+ display: "grid",
378
+ gridTemplateColumns: {
379
+ default: "11rem minmax(0, 1fr)",
380
+ [narrow]: "minmax(0, 1fr)",
381
+ },
382
+ },
383
+ nav: {
384
+ display: { default: "flex", [narrow]: "none" },
385
+ flexDirection: "column",
386
+ gap: "1px",
387
+ padding: space.md,
388
+ overflowY: "auto",
389
+ borderRightWidth: 1,
390
+ borderRightStyle: "solid",
391
+ borderRightColor: colors.border,
392
+ },
393
+ navItem: {
394
+ appearance: "none",
395
+ border: "none",
396
+ display: "flex",
397
+ alignItems: "baseline",
398
+ justifyContent: "space-between",
399
+ gap: space.md,
400
+ paddingBlock: space.sm,
401
+ paddingInline: space.md,
402
+ borderRadius: radius.sm,
403
+ backgroundColor: "transparent",
404
+ color: colors.textMuted,
405
+ fontSize: type.md,
406
+ textAlign: "left",
407
+ cursor: "pointer",
408
+ [hover]: { ":hover": { backgroundColor: colors.bgHover, color: colors.text } },
409
+ },
410
+ navItemActive: {
411
+ backgroundColor: colors.bgActive,
412
+ color: colors.text,
413
+ fontWeight: 500,
414
+ },
415
+ navLabel: {
416
+ overflow: "hidden",
417
+ textOverflow: "ellipsis",
418
+ whiteSpace: "nowrap",
419
+ },
420
+ navCount: {
421
+ fontFamily: type.mono,
422
+ fontSize: type.xs,
423
+ color: colors.textMuted,
424
+ },
425
+ main: {
426
+ flex: 1,
427
+ minWidth: 0,
428
+ overflowY: "auto",
429
+ padding: space.xl,
430
+ },
431
+ groupGrid: {
432
+ display: "grid",
433
+ gridTemplateColumns: "repeat(auto-fill, minmax(13rem, 1fr))",
434
+ gap: space.xl,
435
+ },
436
+ blockGrid: {
437
+ display: "grid",
438
+ gridTemplateColumns: "repeat(auto-fill, minmax(16rem, 1fr))",
439
+ gap: space.xl,
440
+ },
441
+ groupDetail: {
442
+ display: "flex",
443
+ flexDirection: "column",
444
+ gap: space.xl,
445
+ },
446
+ groupHeading: {
447
+ display: "flex",
448
+ flexDirection: "column",
449
+ gap: space.xs,
450
+ },
451
+ groupTitle: {
452
+ margin: 0,
453
+ fontSize: type.display,
454
+ fontWeight: 600,
455
+ letterSpacing: "-0.01em",
456
+ },
457
+ groupDescription: {
458
+ margin: 0,
459
+ maxWidth: "40rem",
460
+ color: colors.textMuted,
461
+ fontSize: type.md,
462
+ lineHeight: 1.45,
463
+ },
464
+ card: {
465
+ display: "flex",
466
+ flexDirection: "column",
467
+ gap: space.md,
468
+ minWidth: 0,
469
+ padding: space.md,
470
+ borderWidth: 1,
471
+ borderStyle: "solid",
472
+ borderColor: colors.border,
473
+ borderRadius: radius.md,
474
+ backgroundColor: colors.bgSurface,
475
+ color: colors.text,
476
+ font: "inherit",
477
+ textAlign: "left",
478
+ },
479
+ cardButton: {
480
+ appearance: "none",
481
+ cursor: "pointer",
482
+ transitionProperty: "border-color, background-color",
483
+ transitionDuration: "120ms",
484
+ outline: "none",
485
+ [hover]: { ":hover": { borderColor: colors.textMuted } },
486
+ ":focus-visible": { borderColor: colors.focus, backgroundColor: colors.bgHover },
487
+ },
488
+ cover: {
489
+ position: "relative",
490
+ display: "block",
491
+ aspectRatio: "16 / 10",
492
+ overflow: "hidden",
493
+ borderRadius: radius.sm,
494
+ backgroundColor: colors.bgPanel,
495
+ },
496
+ coverImage: {
497
+ display: "block",
498
+ width: "100%",
499
+ height: "100%",
500
+ objectFit: "cover",
501
+ objectPosition: "top center",
502
+ },
503
+ cardText: {
504
+ display: "flex",
505
+ flexDirection: "column",
506
+ gap: "2px",
507
+ minWidth: 0,
508
+ },
509
+ cardName: {
510
+ fontSize: type.lg,
511
+ fontWeight: 500,
512
+ overflow: "hidden",
513
+ textOverflow: "ellipsis",
514
+ whiteSpace: "nowrap",
515
+ },
516
+ cardCount: {
517
+ color: colors.textMuted,
518
+ fontFamily: type.mono,
519
+ fontSize: type.xs,
520
+ },
521
+ cardDescription: {
522
+ display: "-webkit-box",
523
+ WebkitBoxOrient: "vertical",
524
+ WebkitLineClamp: 2,
525
+ overflow: "hidden",
526
+ color: colors.textMuted,
527
+ fontSize: type.sm,
528
+ lineHeight: 1.35,
529
+ },
530
+ cardMeta: {
531
+ marginTop: space.xs,
532
+ color: colors.textMuted,
533
+ fontFamily: type.mono,
534
+ fontSize: type.xs,
535
+ lineHeight: 1.5,
536
+ },
537
+ empty: {
538
+ margin: 0,
539
+ color: colors.textMuted,
540
+ fontSize: type.md,
541
+ },
542
+ });
@@ -0,0 +1,57 @@
1
+ // packages/studio/src/pages/BlocksLibraryPage.tsx
2
+ // Every block the site defines, by group, with screenshots and fields: the
3
+ // place to see what the page builder offers before editing a page.
4
+ import { useOutletContext } from "react-router";
5
+ import * as stylex from "@stylexjs/stylex";
6
+ import type { WorkspaceOutletContext } from "../workspace/types";
7
+ import { Text } from "../ui/Text";
8
+ import { colors, space, type } from "../styles/tokens.stylex";
9
+ import { BlockLibrary } from "./BlockLibrary";
10
+ import { primarySiteUrl } from "./preview-url";
11
+
12
+ export function BlocksLibraryPage() {
13
+ const { workspace, blocks } = useOutletContext<WorkspaceOutletContext>();
14
+ const siteUrl = primarySiteUrl(workspace.siteUrls ?? []);
15
+ return (
16
+ <div {...stylex.props(styles.page)}>
17
+ <header {...stylex.props(styles.header)}>
18
+ <h1 {...stylex.props(styles.title)}>Blocks</h1>
19
+ </header>
20
+ {blocks.length === 0 ? (
21
+ <div {...stylex.props(styles.empty)}>
22
+ <Text tone="muted" size="sm">
23
+ No blocks in the schema. Add `defineBlock` files to the site’s blocks folder.
24
+ </Text>
25
+ </div>
26
+ ) : (
27
+ <BlockLibrary catalog={blocks} siteUrl={siteUrl} />
28
+ )}
29
+ </div>
30
+ );
31
+ }
32
+
33
+ const styles = stylex.create({
34
+ page: {
35
+ display: "flex",
36
+ flexDirection: "column",
37
+ height: "100%",
38
+ minHeight: 0,
39
+ backgroundColor: colors.bgApp,
40
+ },
41
+ header: {
42
+ paddingBlock: space.lg,
43
+ paddingInline: space.xl,
44
+ borderBottomWidth: 1,
45
+ borderBottomStyle: "solid",
46
+ borderBottomColor: colors.border,
47
+ backgroundColor: colors.bgSurface,
48
+ },
49
+ title: {
50
+ margin: 0,
51
+ fontSize: type.lg,
52
+ fontWeight: 600,
53
+ },
54
+ empty: {
55
+ padding: space.xl,
56
+ },
57
+ });