@backmatter/studio 0.0.21 → 0.0.23

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.
@@ -4,6 +4,7 @@ import * as stylex from "@stylexjs/stylex";
4
4
  import {
5
5
  documentLabel,
6
6
  internalSuffixError,
7
+ isBareAnchor,
7
8
  joinInternalLink,
8
9
  linkSide,
9
10
  matchesWhere,
@@ -12,7 +13,10 @@ import {
12
13
  } from "@backmatter/core";
13
14
  import type { SchemaField } from "../api";
14
15
  import { colors, radius, space, type } from "../styles/tokens.stylex";
15
- import { Input, Select } from "../ui/Input";
16
+ import { Input, Select, type SelectOption } from "../ui/Input";
17
+
18
+ /** Select value for "This page": a bare `#anchor` on whichever page shows the link. */
19
+ const THIS_PAGE = "#";
16
20
 
17
21
  type ModelRef = {
18
22
  type: string;
@@ -60,15 +64,22 @@ function internalSuffixState(value: string): {
60
64
  return { parts, showSuffix: true, selectValue: parts.path };
61
65
  }
62
66
 
67
+ function modelGroupLabel(type: string): string {
68
+ return type.charAt(0).toUpperCase() + type.slice(1);
69
+ }
70
+
71
+ /** Routes grouped by model in schema order, each titled with its route beneath. */
63
72
  function buildRouteOptions(
64
73
  field: SchemaField,
65
74
  models: ModelRef[],
66
75
  documentLists: RelationDocumentLists | undefined,
67
- ): Array<{ value: string; label: string }> {
76
+ ): SelectOption[] {
68
77
  if (!documentLists) return [];
69
- const options: Array<{ value: string; label: string }> = [];
78
+ const options: SelectOption[] = [];
70
79
  for (const model of routableTypes(field, models)) {
80
+ const group = modelGroupLabel(model.type);
71
81
  const docs = documentLists[model.type] ?? [];
82
+ const modelOptions: SelectOption[] = [];
72
83
  for (const doc of docs) {
73
84
  if (!matchesWhere({ slug: doc.slug, frontmatter: doc.frontmatter }, field.where)) {
74
85
  continue;
@@ -76,13 +87,19 @@ function buildRouteOptions(
76
87
  const route = routeFor(model, doc.slug);
77
88
  if (route === "") continue;
78
89
  const label = documentLabel(doc.frontmatter, model);
79
- options.push({
90
+ modelOptions.push({
80
91
  value: route,
81
- label: label ? `${label} ${route}` : route,
92
+ label: label || route,
93
+ description: label ? route : undefined,
94
+ group,
82
95
  });
83
96
  }
97
+ // Titles that open with a quote mark sort by their first letter.
98
+ modelOptions.sort((a, b) =>
99
+ a.label.localeCompare(b.label, undefined, { ignorePunctuation: true, sensitivity: "base" }),
100
+ );
101
+ options.push(...modelOptions);
84
102
  }
85
- options.sort((a, b) => a.label.localeCompare(b.label));
86
103
  return options;
87
104
  }
88
105
 
@@ -153,11 +170,15 @@ export function LinkFieldInput({
153
170
  linkSide(raw) === "external" ? "external" : "internal",
154
171
  );
155
172
  const [pendingClearOf, setPendingClearOf] = useState<string | null>(null);
173
+ const [thisPagePicked, setThisPagePicked] = useState(false);
174
+ const [openParts, setOpenParts] = useState({ query: false, anchor: false });
156
175
  const [lastSyncedRaw, setLastSyncedRaw] = useState(raw);
157
176
  if (raw !== lastSyncedRaw) {
158
177
  setLastSyncedRaw(raw);
159
178
  if (raw !== lastEmittedRawRef.current) {
160
179
  lastEmittedRawRef.current = null;
180
+ setThisPagePicked(false);
181
+ setOpenParts({ query: false, anchor: false });
161
182
  if (raw !== "") {
162
183
  setEditorSide(linkSide(raw) === "external" ? "external" : "internal");
163
184
  }
@@ -217,22 +238,32 @@ export function LinkFieldInput({
217
238
  );
218
239
  }
219
240
 
220
- const { parts, showSuffix, selectValue } = raw === "" ? {
241
+ const internal = raw === "" ? {
221
242
  parts: { path: "", query: "", anchor: "" },
222
243
  showSuffix: true,
223
244
  selectValue: "",
224
245
  } : internalSuffixState(raw);
225
- // With no route picked, the anchor is a bare `#anchor` on the page that
226
- // renders the link. A field limited to models or a filter can't take one.
246
+ const { parts, showSuffix } = internal;
247
+ // A bare anchor has no page to hold to a field's models or filter.
227
248
  const bareAnchorAllowed = !(field.models && field.models.length > 0) && !field.where;
249
+ const bare = raw !== "" && isBareAnchor(raw);
250
+ const thisPage = bare || (raw === "" && thisPagePicked && bareAnchorAllowed);
228
251
  const hasRoute = parts.path !== "";
229
- const queryEditable = showSuffix && hasRoute;
230
- const anchorEditable = showSuffix && (hasRoute || bareAnchorAllowed);
252
+ const selectValue = thisPage ? THIS_PAGE : internal.selectValue;
231
253
 
232
- const options = buildRouteOptions(field, models, documentLists);
233
- if (selectValue && !options.some((option) => option.value === selectValue)) {
254
+ const options: SelectOption[] = buildRouteOptions(field, models, documentLists);
255
+ if (selectValue && selectValue !== THIS_PAGE && !options.some((option) => option.value === selectValue)) {
234
256
  options.unshift({ value: selectValue, label: selectValue });
235
257
  }
258
+ if (bareAnchorAllowed || bare) {
259
+ options.unshift({ value: THIS_PAGE, label: "This page" });
260
+ }
261
+
262
+ const showAnchor =
263
+ showSuffix && (thisPage || (hasRoute && (openParts.anchor || parts.anchor !== "")));
264
+ const showQuery = showSuffix && hasRoute && (openParts.query || parts.query !== "");
265
+ const canAddAnchor = !readOnly && showSuffix && hasRoute && !showAnchor;
266
+ const canAddQuery = !readOnly && showSuffix && hasRoute && !showQuery;
236
267
 
237
268
  const internalPressed = editorSide !== "external";
238
269
 
@@ -257,42 +288,121 @@ export function LinkFieldInput({
257
288
  chip
258
289
  searchable
259
290
  clearable={!field.required}
260
- placeholder="Select a value"
291
+ placeholder="Choose a page"
261
292
  options={options}
262
293
  value={selectValue || null}
263
294
  disabled={readOnly}
264
295
  onValueChange={(next) => {
265
296
  if (readOnly) return;
266
297
  if (next == null) {
298
+ setThisPagePicked(false);
299
+ setOpenParts({ query: false, anchor: false });
267
300
  emitChange(undefined);
268
301
  return;
269
302
  }
303
+ if (next === THIS_PAGE) {
304
+ setThisPagePicked(true);
305
+ const anchor = showSuffix ? parts.anchor : "";
306
+ if (anchor !== "") {
307
+ emitChange(`#${anchor}`);
308
+ } else if (raw !== "") {
309
+ emitChange(undefined);
310
+ }
311
+ return;
312
+ }
313
+ setThisPagePicked(false);
270
314
  emitChange(joinInternalLink(next, showSuffix ? parts.query : "", showSuffix ? parts.anchor : ""));
271
315
  }}
272
316
  />
273
- <Input
274
- aria-label="Query"
275
- value={showSuffix ? parts.query : ""}
276
- readOnly={readOnly || !queryEditable}
277
- onChange={(event) => {
278
- if (readOnly || !queryEditable) return;
279
- emitChange(joinInternalLink(parts.path, event.target.value, parts.anchor));
280
- }}
281
- />
282
- <Input
283
- aria-label="Anchor"
284
- value={showSuffix ? parts.anchor : ""}
285
- readOnly={readOnly || !anchorEditable}
286
- onChange={(event) => {
287
- if (readOnly || !anchorEditable) return;
288
- const next = joinInternalLink(parts.path, parts.query, event.target.value);
289
- emitChange(next === "" ? undefined : next);
290
- }}
291
- />
317
+ {showQuery ? (
318
+ <LinkPart
319
+ label="Query"
320
+ prefix="?"
321
+ placeholder="key=value"
322
+ value={parts.query}
323
+ readOnly={readOnly}
324
+ onChange={(query) => {
325
+ if (readOnly) return;
326
+ emitChange(joinInternalLink(parts.path, query, parts.anchor));
327
+ }}
328
+ />
329
+ ) : null}
330
+ {showAnchor ? (
331
+ <LinkPart
332
+ label="Anchor"
333
+ prefix="#"
334
+ placeholder="section-id"
335
+ value={parts.anchor}
336
+ readOnly={readOnly}
337
+ onChange={(anchor) => {
338
+ if (readOnly) return;
339
+ const next = thisPage
340
+ ? joinInternalLink("", "", anchor)
341
+ : joinInternalLink(parts.path, parts.query, anchor);
342
+ emitChange(next === "" ? undefined : next);
343
+ }}
344
+ />
345
+ ) : null}
346
+ {canAddAnchor || canAddQuery ? (
347
+ <div {...stylex.props(styles.addRow)}>
348
+ {canAddAnchor ? (
349
+ <button
350
+ type="button"
351
+ {...stylex.props(styles.addButton)}
352
+ onClick={() => setOpenParts((open) => ({ ...open, anchor: true }))}
353
+ >
354
+ Add anchor
355
+ </button>
356
+ ) : null}
357
+ {canAddQuery ? (
358
+ <button
359
+ type="button"
360
+ {...stylex.props(styles.addButton)}
361
+ onClick={() => setOpenParts((open) => ({ ...open, query: true }))}
362
+ >
363
+ Add query
364
+ </button>
365
+ ) : null}
366
+ </div>
367
+ ) : null}
292
368
  </div>
293
369
  );
294
370
  }
295
371
 
372
+ function LinkPart({
373
+ label,
374
+ prefix,
375
+ placeholder,
376
+ value,
377
+ readOnly,
378
+ onChange,
379
+ }: {
380
+ label: "Query" | "Anchor";
381
+ prefix: string;
382
+ placeholder: string;
383
+ value: string;
384
+ readOnly?: boolean;
385
+ onChange: (value: string) => void;
386
+ }) {
387
+ return (
388
+ <label {...stylex.props(styles.partRow)}>
389
+ <span {...stylex.props(styles.partLabel)}>{label}</span>
390
+ <span {...stylex.props(styles.partInput)}>
391
+ <span aria-hidden {...stylex.props(styles.partPrefix)}>
392
+ {prefix}
393
+ </span>
394
+ <Input
395
+ aria-label={label}
396
+ placeholder={placeholder}
397
+ value={value}
398
+ readOnly={readOnly}
399
+ onChange={(event) => onChange(event.target.value)}
400
+ />
401
+ </span>
402
+ </label>
403
+ );
404
+ }
405
+
296
406
  const styles = stylex.create({
297
407
  stack: {
298
408
  display: "flex",
@@ -315,6 +425,45 @@ const styles = stylex.create({
315
425
  fontSize: type.sm,
316
426
  cursor: "pointer",
317
427
  },
428
+ partRow: {
429
+ display: "flex",
430
+ alignItems: "center",
431
+ gap: space.sm,
432
+ },
433
+ partLabel: {
434
+ flexShrink: 0,
435
+ width: "4rem",
436
+ fontFamily: type.sans,
437
+ fontSize: type.xs,
438
+ color: colors.textMuted,
439
+ },
440
+ partInput: {
441
+ display: "flex",
442
+ alignItems: "center",
443
+ gap: space.xs,
444
+ flex: 1,
445
+ minWidth: 0,
446
+ },
447
+ partPrefix: {
448
+ flexShrink: 0,
449
+ fontFamily: type.sans,
450
+ fontSize: type.sm,
451
+ color: colors.textMuted,
452
+ },
453
+ addRow: {
454
+ display: "flex",
455
+ gap: space.md,
456
+ },
457
+ addButton: {
458
+ padding: 0,
459
+ borderWidth: 0,
460
+ backgroundColor: "transparent",
461
+ color: colors.textMuted,
462
+ fontFamily: type.sans,
463
+ fontSize: type.xs,
464
+ cursor: "pointer",
465
+ textDecoration: { default: "none", ":hover": "underline" },
466
+ },
318
467
  sideButtonPressed: {
319
468
  borderColor: colors.text,
320
469
  fontWeight: 600,
@@ -6,7 +6,7 @@ import { isSortable, useSortable } from "@dnd-kit/react/sortable";
6
6
  import * as stylex from "@stylexjs/stylex";
7
7
  import type { BlockSnapshot, SchemaField } from "../api";
8
8
  import { useBlockConfig } from "../define-studio";
9
- import { FieldControl } from "../fields/FieldControl";
9
+ import { FieldControl, useIsSwitchField } from "../fields/FieldControl";
10
10
  import { fieldLabel } from "../fields/field-label";
11
11
  import { omitField } from "../fields/omit-field";
12
12
  import { Button } from "../ui/Button";
@@ -100,6 +100,7 @@ function BlockItem({
100
100
  const def = catalog.find((b) => b.name === block.type);
101
101
  const fieldEntries = Object.entries(def?.fields ?? {});
102
102
  const config = useBlockConfig(block.type);
103
+ const isSwitch = useIsSwitchField(models);
103
104
  const Preview = config?.preview;
104
105
 
105
106
  return (
@@ -109,7 +110,16 @@ function BlockItem({
109
110
  data-block-id={block.id}
110
111
  {...stylex.props(styles.item, isDragging && styles.itemDragging)}
111
112
  >
112
- <div {...stylex.props(styles.itemBar)}>
113
+ <div
114
+ data-block-bar=""
115
+ {...stylex.props(styles.itemBar)}
116
+ // The label button carries the keyboard and screen reader toggle;
117
+ // its click bubbles here, and so does a click on the row around it.
118
+ onClick={(event) => {
119
+ if ((event.target as Element).closest("[data-drag-handle], [data-block-actions]")) return;
120
+ onToggle();
121
+ }}
122
+ >
113
123
  <button
114
124
  ref={handleRef}
115
125
  type="button"
@@ -123,9 +133,9 @@ function BlockItem({
123
133
  </button>
124
134
  <button
125
135
  type="button"
136
+ data-block-toggle=""
126
137
  {...stylex.props(styles.toggle)}
127
138
  aria-expanded={open}
128
- onClick={onToggle}
129
139
  >
130
140
  {config?.icon ? (
131
141
  <span {...stylex.props(styles.icon)} aria-hidden>
@@ -136,7 +146,7 @@ function BlockItem({
136
146
  {config?.label ?? block.type}
137
147
  </Text>
138
148
  </button>
139
- <div {...stylex.props(styles.itemActions)}>
149
+ <div data-block-actions="" {...stylex.props(styles.itemActions)}>
140
150
  <Button
141
151
  variant="ghost"
142
152
  disabled={readOnly}
@@ -170,6 +180,7 @@ function BlockItem({
170
180
  <Field
171
181
  key={name}
172
182
  label={fieldLabel(name, field)}
183
+ inline={isSwitch(field)}
173
184
  hint={
174
185
  field.description ? (
175
186
  <span {...stylex.props(styles.fieldGuidance)}>{field.description}</span>
@@ -338,8 +349,16 @@ const styles = stylex.create({
338
349
  borderBottomStyle: "solid",
339
350
  borderBottomColor: colors.border,
340
351
  backgroundColor: colors.bgSurface,
352
+ // The list stays overflow-visible for menus and drag, so the end rows
353
+ // round their own corners to sit inside its border.
354
+ ":first-child": {
355
+ borderTopLeftRadius: `calc(${radius.md} - 1px)`,
356
+ borderTopRightRadius: `calc(${radius.md} - 1px)`,
357
+ },
341
358
  ":last-child": {
342
359
  borderBottomWidth: 0,
360
+ borderBottomLeftRadius: `calc(${radius.md} - 1px)`,
361
+ borderBottomRightRadius: `calc(${radius.md} - 1px)`,
343
362
  },
344
363
  },
345
364
  itemDragging: {
@@ -368,6 +387,16 @@ const styles = stylex.create({
368
387
  paddingInlineEnd: space.md,
369
388
  paddingInlineStart: space.xs,
370
389
  paddingBlock: space.sm,
390
+ // Follows the row's rounded end corners so the hover stays inside them.
391
+ borderRadius: "inherit",
392
+ cursor: "pointer",
393
+ transitionProperty: "background-color",
394
+ transitionDuration: "120ms",
395
+ [hover]: {
396
+ ":hover": {
397
+ backgroundColor: `color-mix(in srgb, ${colors.bgHover} 50%, transparent)`,
398
+ },
399
+ },
371
400
  },
372
401
  handle: {
373
402
  display: "inline-flex",
@@ -412,6 +441,7 @@ const styles = stylex.create({
412
441
  color: colors.text,
413
442
  font: "inherit",
414
443
  textAlign: "start",
444
+ cursor: "pointer",
415
445
  },
416
446
  itemActions: {
417
447
  display: "flex",
@@ -421,7 +451,7 @@ const styles = stylex.create({
421
451
  itemBody: {
422
452
  display: "flex",
423
453
  flexDirection: "column",
424
- gap: space.md,
454
+ gap: space.xl,
425
455
  paddingInline: space.md,
426
456
  paddingBottom: space.md,
427
457
  },
@@ -20,7 +20,7 @@ import {
20
20
  type SchemaPayload,
21
21
  type SchemaField,
22
22
  } from "../api";
23
- import { FieldControl } from "../fields/FieldControl";
23
+ import { FieldControl, useIsSwitchField } from "../fields/FieldControl";
24
24
  import { loadRelationDocuments } from "../fields/load-relation-documents";
25
25
  import { omitField } from "../fields/omit-field";
26
26
  import { linkDocumentTypes } from "../fields/link-targets";
@@ -198,6 +198,7 @@ export function DocEditorPage() {
198
198
  const [confirmRevert, setConfirmRevert] = useState(false);
199
199
  const [saveState, setSaveState] = useState<SaveState>("idle");
200
200
  const [previewKey, setPreviewKey] = useState(0);
201
+ const isSwitch = useIsSwitchField(models);
201
202
  const [previewLoading, setPreviewLoading] = useState(true);
202
203
  const [previewFrame, setPreviewFrame] = useState(0);
203
204
  const [previewOffer, setPreviewOffer] = useState<PreviewChannelRef | null>(null);
@@ -976,7 +977,7 @@ export function DocEditorPage() {
976
977
  </div>
977
978
  </div>
978
979
  {previewUrl ? (
979
- <PreviewViewport size={previewSize} dragging={dragging}>
980
+ <PreviewViewport size={previewSize} dragging={dragging} loading={previewLoading}>
980
981
  <iframe
981
982
  key={`${previewKey}-${previewUrl}`}
982
983
  ref={previewFrameRef}
@@ -1005,11 +1006,7 @@ export function DocEditorPage() {
1005
1006
  </Link>
1006
1007
  </div>
1007
1008
  ) : (
1008
- <div
1009
- {...stylex.props(styles.iframe)}
1010
- aria-busy="true"
1011
- aria-label="Preview waiting for checkout"
1012
- />
1009
+ <PreviewViewport size={previewSize} loading loadingLabel="Loading checkout…" />
1013
1010
  )}
1014
1011
  </aside>
1015
1012
 
@@ -1129,7 +1126,7 @@ export function DocEditorPage() {
1129
1126
  detailNames.map((name) => {
1130
1127
  const field = fieldDefs[name]!;
1131
1128
  return (
1132
- <Field key={name} label={fieldLabel(name)}>
1129
+ <Field key={name} label={fieldLabel(name)} inline={isSwitch(field)}>
1133
1130
  <FieldControl
1134
1131
  name={name}
1135
1132
  field={field}
@@ -1532,7 +1529,7 @@ const styles = stylex.create({
1532
1529
  form: {
1533
1530
  display: "flex",
1534
1531
  flexDirection: "column",
1535
- gap: space.lg,
1532
+ gap: space.xl,
1536
1533
  paddingBottom: space.xxl,
1537
1534
  },
1538
1535
  });
@@ -400,7 +400,7 @@ export function MediaFieldInput({
400
400
  <div {...stylex.props(styles.gallery)}>
401
401
  <MediaGalleryPage
402
402
  pick
403
- pickedUrls={multiple ? checkUrls : undefined}
403
+ pickedUrls={checkUrls}
404
404
  onToggleUrl={
405
405
  multiple
406
406
  ? (url) => {
@@ -69,6 +69,7 @@ export function MediaGalleryPage({
69
69
  const dragDepth = useRef(0);
70
70
  const scroller = useRef<HTMLDivElement>(null);
71
71
  const grid = useRef<HTMLDivElement>(null);
72
+ const shownPicked = useRef(false);
72
73
  const [columns, setColumns] = useState(1);
73
74
  const loadingPage = useRef(false);
74
75
  const request = useRef(0);
@@ -394,6 +395,15 @@ export function MediaGalleryPage({
394
395
  const showEmpty =
395
396
  settled && state.pending.length === 0 && state.loaded.length === 0 && state.total === 0;
396
397
 
398
+ // When the picker opens, bring the media already in use into view once.
399
+ useEffect(() => {
400
+ if (!pick || shownPicked.current || !pickedUrls?.length) return;
401
+ const tile = grid.current?.querySelector<HTMLElement>("button[aria-current='true']");
402
+ if (!tile) return;
403
+ shownPicked.current = true;
404
+ tile.scrollIntoView?.({ block: "nearest" });
405
+ });
406
+
397
407
  return (
398
408
  <div {...stylex.props(styles.page)}>
399
409
  <div {...stylex.props(styles.toolbar)}>
@@ -485,8 +495,8 @@ export function MediaGalleryPage({
485
495
  item={cell.item}
486
496
  menu={!pick}
487
497
  selected={
488
- onToggleUrl
489
- ? (pickedUrls?.includes(cell.item.url) ?? false)
498
+ pickedUrls
499
+ ? pickedUrls.includes(cell.item.url)
490
500
  : !pick && !sharedId && cell.item.id === selectedId
491
501
  }
492
502
  canDelete={canDelete}
@@ -584,7 +594,17 @@ function MediaHit({
584
594
  </button>
585
595
  );
586
596
  if (!menu) {
587
- if (!selected || !onUnpick) return image;
597
+ if (!selected) return image;
598
+ if (!onUnpick) {
599
+ return (
600
+ <>
601
+ {image}
602
+ <span data-media-in-use {...stylex.props(styles.inUse)}>
603
+ In use
604
+ </span>
605
+ </>
606
+ );
607
+ }
588
608
  return (
589
609
  <>
590
610
  {image}
@@ -780,6 +800,19 @@ const styles = stylex.create({
780
800
  picked: {
781
801
  cursor: "default",
782
802
  },
803
+ inUse: {
804
+ position: "absolute",
805
+ top: "12px",
806
+ left: "12px",
807
+ paddingInline: space.sm,
808
+ paddingBlock: "2px",
809
+ borderRadius: "999px",
810
+ backgroundColor: "rgba(24, 24, 27, 0.85)",
811
+ color: "#fafafa",
812
+ fontSize: type.xs,
813
+ fontWeight: 500,
814
+ pointerEvents: "none",
815
+ },
783
816
  unpick: {
784
817
  position: "absolute",
785
818
  top: "12px",
@@ -1,12 +1,14 @@
1
1
  // packages/studio/src/pages/PreviewSize.tsx
2
2
  import type { KeyboardEvent, ReactNode } from "react";
3
3
  import * as stylex from "@stylexjs/stylex";
4
+ import { Snake } from "loading-dev";
4
5
  import {
5
6
  IconImac,
6
7
  IconPhone,
7
8
  IconTablet,
8
9
  } from "@central-icons-react/round-outlined-radius-2-stroke-1.5";
9
- import { colors, radius } from "../styles/tokens.stylex";
10
+ import { colors, radius, space } from "../styles/tokens.stylex";
11
+ import { Text } from "../ui/Text";
10
12
  import { Tooltip } from "../ui/Tooltip";
11
13
  import {
12
14
  previewFrameWidth,
@@ -77,15 +79,22 @@ export function PreviewSizeSwitch({
77
79
  );
78
80
  }
79
81
 
80
- /** Centers a fixed-width preview and scrolls sideways when the pane is narrower. */
82
+ /**
83
+ * Centers a fixed-width preview and scrolls sideways when the pane is narrower.
84
+ * While `loading`, a loader covers the page until it has something to show.
85
+ */
81
86
  export function PreviewViewport({
82
87
  size,
83
88
  dragging = false,
89
+ loading = false,
90
+ loadingLabel = "Loading preview…",
84
91
  children,
85
92
  }: {
86
93
  size: PreviewSize;
87
94
  dragging?: boolean;
88
- children: ReactNode;
95
+ loading?: boolean;
96
+ loadingLabel?: string;
97
+ children?: ReactNode;
89
98
  }) {
90
99
  const width = previewFrameWidth(size);
91
100
  const viewport = stylex.props(styles.viewport, dragging && styles.dragging);
@@ -97,6 +106,14 @@ export function PreviewViewport({
97
106
  style={width === null ? slot.style : { ...slot.style, width }}
98
107
  >
99
108
  {children}
109
+ {loading ? (
110
+ <div role="status" {...stylex.props(styles.loader)}>
111
+ <span {...stylex.props(styles.loaderMark)} aria-hidden>
112
+ <Snake size={20} />
113
+ </span>
114
+ <Text tone="muted" size="sm">{loadingLabel}</Text>
115
+ </div>
116
+ ) : null}
100
117
  </div>
101
118
  </div>
102
119
  );
@@ -104,6 +121,11 @@ export function PreviewViewport({
104
121
 
105
122
  const hover = "@media (hover: hover)";
106
123
 
124
+ const appear = stylex.keyframes({
125
+ from: { opacity: 0 },
126
+ to: { opacity: 1 },
127
+ });
128
+
107
129
  const styles = stylex.create({
108
130
  group: {
109
131
  display: "inline-flex",
@@ -170,6 +192,29 @@ const styles = stylex.create({
170
192
  minHeight: 0,
171
193
  backgroundColor: colors.bgSurface,
172
194
  },
195
+ loader: {
196
+ position: "absolute",
197
+ inset: 0,
198
+ display: "flex",
199
+ flexDirection: "column",
200
+ alignItems: "center",
201
+ justifyContent: "center",
202
+ gap: space.md,
203
+ backgroundColor: colors.bgSurface,
204
+ // A page that loads at once never shows the loader.
205
+ animationName: appear,
206
+ animationDuration: "150ms",
207
+ animationDelay: "150ms",
208
+ animationTimingFunction: "ease-out",
209
+ animationFillMode: "both",
210
+ "@media (prefers-reduced-motion: reduce)": {
211
+ animationName: "none",
212
+ },
213
+ },
214
+ loaderMark: {
215
+ display: "flex",
216
+ color: colors.textMuted,
217
+ },
173
218
  slotFixed: {
174
219
  flexGrow: 0,
175
220
  flexShrink: 0,