devnonla-ui 0.10.0 → 0.12.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.
package/src/logo/Logo.tsx CHANGED
@@ -1,18 +1,12 @@
1
- import "@fontsource-variable/plus-jakarta-sans/wght.css";
2
1
  import type { CSSProperties } from "react";
3
2
  import { cn } from "../lib/cn";
4
3
 
5
4
  /** `icon` is the mark. `text` is the wordmark. `full` is both. */
6
5
  export type LogoVariant = "icon" | "text" | "full";
7
6
 
8
- /** `pixel` is the grid. `jakarta` is Plus Jakarta Sans. */
9
- export type LogoFace = "pixel" | "jakarta";
10
-
11
7
  export type LogoProps = {
12
8
  variant?: LogoVariant;
13
- /** Wordmark face. Ignored when `variant` is `icon`. Defaults to `pixel`. */
14
- face?: LogoFace;
15
- /** Icon height in px. Width follows each asset's aspect ratio. Defaults to 32. */
9
+ /** Icon height in px. Width follows the mark's aspect ratio. Defaults to 32. */
16
10
  size?: number;
17
11
  /** Icon and wordmark fill. Eyes and the smile stay. Omit to keep the mascot palette and the current text color. */
18
12
  color?: string;
@@ -26,161 +20,6 @@ const WORDMARK = "Nonla Agents";
26
20
  const MARK_W = 1336;
27
21
  const MARK_H = 960;
28
22
 
29
- /** Pixel grid wordmark. 11 rows; cap letters use 7, so the box is taller than the caps. */
30
- const WORD_W = 1340;
31
- const WORD_H = 220;
32
- const WORD_CELLS: ReadonlyArray<readonly [number, number]> = [
33
- [2.4, 2.4],
34
- [82.4, 2.4],
35
- [2.4, 22.4],
36
- [22.4, 22.4],
37
- [82.4, 22.4],
38
- [2.4, 42.4],
39
- [22.4, 42.4],
40
- [82.4, 42.4],
41
- [2.4, 62.4],
42
- [42.4, 62.4],
43
- [82.4, 62.4],
44
- [2.4, 82.4],
45
- [62.4, 82.4],
46
- [82.4, 82.4],
47
- [2.4, 102.4],
48
- [62.4, 102.4],
49
- [82.4, 102.4],
50
- [2.4, 122.4],
51
- [82.4, 122.4],
52
- [142.4, 42.4],
53
- [162.4, 42.4],
54
- [182.4, 42.4],
55
- [122.4, 62.4],
56
- [202.4, 62.4],
57
- [122.4, 82.4],
58
- [202.4, 82.4],
59
- [122.4, 102.4],
60
- [202.4, 102.4],
61
- [142.4, 122.4],
62
- [162.4, 122.4],
63
- [182.4, 122.4],
64
- [242.4, 42.4],
65
- [262.4, 42.4],
66
- [282.4, 42.4],
67
- [302.4, 42.4],
68
- [242.4, 62.4],
69
- [322.4, 62.4],
70
- [242.4, 82.4],
71
- [322.4, 82.4],
72
- [242.4, 102.4],
73
- [322.4, 102.4],
74
- [242.4, 122.4],
75
- [322.4, 122.4],
76
- [362.4, 2.4],
77
- [382.4, 2.4],
78
- [382.4, 22.4],
79
- [382.4, 42.4],
80
- [382.4, 62.4],
81
- [382.4, 82.4],
82
- [382.4, 102.4],
83
- [382.4, 122.4],
84
- [402.4, 122.4],
85
- [462.4, 42.4],
86
- [482.4, 42.4],
87
- [502.4, 42.4],
88
- [522.4, 62.4],
89
- [462.4, 82.4],
90
- [482.4, 82.4],
91
- [502.4, 82.4],
92
- [522.4, 82.4],
93
- [442.4, 102.4],
94
- [522.4, 102.4],
95
- [462.4, 122.4],
96
- [482.4, 122.4],
97
- [502.4, 122.4],
98
- [522.4, 122.4],
99
- [662.4, 2.4],
100
- [642.4, 22.4],
101
- [682.4, 22.4],
102
- [622.4, 42.4],
103
- [702.4, 42.4],
104
- [622.4, 62.4],
105
- [702.4, 62.4],
106
- [622.4, 82.4],
107
- [642.4, 82.4],
108
- [662.4, 82.4],
109
- [682.4, 82.4],
110
- [702.4, 82.4],
111
- [622.4, 102.4],
112
- [702.4, 102.4],
113
- [622.4, 122.4],
114
- [702.4, 122.4],
115
- [762.4, 42.4],
116
- [782.4, 42.4],
117
- [802.4, 42.4],
118
- [822.4, 42.4],
119
- [742.4, 62.4],
120
- [822.4, 62.4],
121
- [742.4, 82.4],
122
- [822.4, 82.4],
123
- [742.4, 102.4],
124
- [822.4, 102.4],
125
- [762.4, 122.4],
126
- [782.4, 122.4],
127
- [802.4, 122.4],
128
- [822.4, 122.4],
129
- [822.4, 142.4],
130
- [762.4, 162.4],
131
- [782.4, 162.4],
132
- [802.4, 162.4],
133
- [882.4, 42.4],
134
- [902.4, 42.4],
135
- [922.4, 42.4],
136
- [862.4, 62.4],
137
- [942.4, 62.4],
138
- [862.4, 82.4],
139
- [882.4, 82.4],
140
- [902.4, 82.4],
141
- [922.4, 82.4],
142
- [942.4, 82.4],
143
- [862.4, 102.4],
144
- [882.4, 122.4],
145
- [902.4, 122.4],
146
- [922.4, 122.4],
147
- [942.4, 122.4],
148
- [982.4, 42.4],
149
- [1002.4, 42.4],
150
- [1022.4, 42.4],
151
- [1042.4, 42.4],
152
- [982.4, 62.4],
153
- [1062.4, 62.4],
154
- [982.4, 82.4],
155
- [1062.4, 82.4],
156
- [982.4, 102.4],
157
- [1062.4, 102.4],
158
- [982.4, 122.4],
159
- [1062.4, 122.4],
160
- [1122.4, 22.4],
161
- [1102.4, 42.4],
162
- [1122.4, 42.4],
163
- [1142.4, 42.4],
164
- [1122.4, 62.4],
165
- [1122.4, 82.4],
166
- [1122.4, 102.4],
167
- [1142.4, 122.4],
168
- [1162.4, 122.4],
169
- [1222.4, 42.4],
170
- [1242.4, 42.4],
171
- [1262.4, 42.4],
172
- [1282.4, 42.4],
173
- [1202.4, 62.4],
174
- [1222.4, 82.4],
175
- [1242.4, 82.4],
176
- [1262.4, 82.4],
177
- [1282.4, 102.4],
178
- [1202.4, 122.4],
179
- [1222.4, 122.4],
180
- [1242.4, 122.4],
181
- [1262.4, 122.4],
182
- ];
183
-
184
23
  /** One paint color, split so the brim stays darker and the face stays lighter than the hat. */
185
24
  function markTones(color?: string) {
186
25
  if (!color) return { brim: "#dd7627", face: "#ffb45c", hat: "#ffa333" };
@@ -219,54 +58,35 @@ function LogoMark({ height, color }: { height: number; color?: string }) {
219
58
  );
220
59
  }
221
60
 
61
+ /** K2D regular outlines. Cap height is 709 units; the box includes the g. */
62
+ const WORD_CAP = 709;
63
+ const WORD_X = 70;
64
+ const WORD_Y = -709;
65
+ const WORD_W = 5908;
66
+ const WORD_H = 918;
67
+ const WORD_PATH = "M70 9L70-628Q70-666 93.50-687.50Q117-709 159-709L547-119L554-125L525-173L525-709Q571-709 594.50-689Q618-669 618-631L618-65Q618-33 595.50-12Q573 9 538 9L141-594L134-588L163-540L163-69Q163-31 139.50-11Q116 9 70 9M923 9Q862 9 812.50-14Q763-37 735.50-76.50Q708-116 708-165L708-329Q708-379 735.50-420Q763-461 812-485Q861-509 923-509Q985-509 1034.50-485Q1084-461 1111.50-420Q1139-379 1139-329L1139-165Q1139-116 1111-76.50Q1083-37 1034-14Q985 9 923 9M923-66Q983-66 1018.50-94.50Q1054-123 1054-169L1054-326Q1054-374 1018.50-404Q983-434 923-434Q864-434 828-404Q792-374 792-326L792-169Q792-123 827.50-94.50Q863-66 923-66M1254 9L1254-435Q1254-509 1338-509L1338-445Q1329-436 1317-418L1321-414Q1351-456 1399-482.50Q1447-509 1503-509Q1584-509 1631-461Q1678-413 1678-337L1678-55Q1678-22 1656.50-6.50Q1635 9 1591 9L1591-328Q1591-376 1564-404Q1537-432 1482-432Q1445-432 1412.50-416.50Q1380-401 1360.50-374.50Q1341-348 1341-317L1341-55Q1341-22 1319.50-6.50Q1298 9 1254 9M1953 9Q1880 9 1844-31.50Q1808-72 1808-138L1808-645Q1808-678 1829.50-693.50Q1851-709 1895-709L1895-141Q1895-102 1911-85Q1927-68 1964-68Q2001-68 2032-86Q2045-71 2045-51Q2045-27 2020-9Q1995 9 1953 9M2291 9Q2205 9 2152.50-37Q2100-83 2100-158Q2100-234 2155-280.50Q2210-327 2300-327Q2396-327 2458-259L2462-263Q2451-275 2437-288L2437-330Q2437-381 2405-409Q2373-437 2314-437Q2209-437 2164-354Q2148-362 2137.50-375.50Q2127-389 2127-403Q2127-430 2154-454.50Q2181-479 2225.50-494Q2270-509 2319-509Q2414-509 2468.50-462Q2523-415 2523-332L2523-58Q2523-23 2503-7Q2483 9 2440 9L2440-38Q2461-60 2463-64L2459-68Q2394 9 2291 9M2306-60Q2368-60 2403.50-86Q2439-112 2439-158Q2439-204 2404-231Q2369-258 2308-258Q2251-258 2218.50-231.50Q2186-205 2186-158Q2186-111 2217.50-85.50Q2249-60 2306-60M2955 9Q2925 3 2908-16.50Q2891-36 2891-64Q2891-83 2900-104L3139-679Q3151-709 3185-709Q3217-709 3230-679L3469-104Q3478-83 3478-64Q3478-37 3461-17Q3444 3 3414 9L3328-200L3041-200L2955 9M3070-281L3299-281L3170-608L3162-605L3180-559L3070-281M3757 209Q3705 209 3660 192Q3615 175 3589 146.50Q3563 118 3563 85Q3563 59 3580.50 47Q3598 35 3628 35Q3630 82 3664.50 109.50Q3699 137 3757 137Q3885 137 3885 13L3885-45Q3899-57 3906-67L3903-72Q3874-41 3830.50-21Q3787-1 3743-1Q3657-1 3602.50-48.50Q3548-96 3548-174L3548-335Q3548-386 3573.50-425.50Q3599-465 3643.50-487Q3688-509 3742-509Q3840-509 3906-423L3910-427L3890-452Q3890-480 3909.50-494.50Q3929-509 3973-509L3973 4Q3973 64 3945.50 110.50Q3918 157 3869 183Q3820 209 3757 209M3759-77Q3812-77 3849-105.50Q3886-134 3886-173L3886-336Q3886-377 3849.50-405Q3813-433 3761-433Q3701-433 3668-406.50Q3635-380 3635-331L3635-178Q3635-131 3667.50-104Q3700-77 3759-77M4314 9Q4253 9 4205-14.50Q4157-38 4130-79.50Q4103-121 4103-173L4103-321Q4103-374 4131.50-417Q4160-460 4208.50-484.50Q4257-509 4314-509Q4371-509 4418.50-485Q4466-461 4493.50-419Q4521-377 4521-324L4521-260Q4521-244 4509-233Q4497-222 4480-222L4188-222L4188-177Q4188-126 4220.50-95Q4253-64 4314-64Q4375-64 4407-91Q4439-118 4439-164Q4469-166 4489.50-152.50Q4510-139 4510-113Q4510-81 4482.50-53Q4455-25 4410-8Q4365 9 4314 9M4188-290L4440-290L4440-324Q4440-374 4405.50-405.50Q4371-437 4314-437Q4257-437 4222.50-405.50Q4188-374 4188-324L4188-290M4636 9L4636-435Q4636-509 4720-509L4720-445Q4711-436 4699-418L4703-414Q4733-456 4781-482.50Q4829-509 4885-509Q4966-509 5013-461Q5060-413 5060-337L5060-55Q5060-22 5038.50-6.50Q5017 9 4973 9L4973-328Q4973-376 4946-404Q4919-432 4864-432Q4827-432 4794.50-416.50Q4762-401 4742.50-374.50Q4723-348 4723-317L4723-55Q4723-22 4701.50-6.50Q4680 9 4636 9M5389 9Q5318 9 5283-31.50Q5248-72 5248-138L5248-430L5155-430Q5155-464 5170-482Q5185-500 5211-500L5249-500L5249-595Q5249-628 5270.50-643.50Q5292-659 5335-659L5335-500L5468-500Q5468-430 5405-430L5336-430L5336-141Q5336-101 5350.50-84.50Q5365-68 5403-68Q5438-68 5469-86Q5482-73 5482-51Q5482-27 5457-9Q5432 9 5389 9M5768 9Q5717 9 5672-6.50Q5627-22 5599.50-51Q5572-80 5572-119Q5572-167 5630-171Q5640-115 5677.50-88.50Q5715-62 5773-62Q5831-62 5863-79Q5895-96 5895-131Q5895-156 5882-171.50Q5869-187 5840.50-198.50Q5812-210 5759-221Q5693-236 5654-253Q5615-270 5594.50-298Q5574-326 5574-372Q5574-411 5598-442.50Q5622-474 5664.50-491.50Q5707-509 5761-509Q5811-509 5855.50-495Q5900-481 5927.50-455Q5955-429 5955-395Q5955-372 5937-358.50Q5919-345 5887-343Q5884-387 5851.50-412.50Q5819-438 5761-438Q5713-438 5685-420Q5657-402 5657-372Q5657-340 5683.50-324.50Q5710-309 5774-295Q5879-274 5928.50-237.50Q5978-201 5978-134Q5978-66 5923-28.50Q5868 9 5768 9";
222
68
 
223
- /** Cap height is half the icon. Caps are 7 of the 11 viewBox rows. */
224
- function wordmarkHeight(iconH: number) {
225
- return Math.round(iconH * (1 / 2) * (11 / 7));
226
- }
227
-
228
- /** Plus Jakarta Sans caps are 0.7em, so this em size makes them half the icon. */
229
- function jakartaSize(iconH: number) {
230
- return (iconH / 2) / 0.7;
231
- }
232
-
233
- function LogoJakarta({ iconH, color }: { iconH: number; color?: string }) {
234
- return (
235
- <span
236
- aria-hidden
237
- className="shrink-0 font-bold leading-none tracking-[-0.02em]"
238
- style={{ fontFamily: '"Plus Jakarta Sans Variable", sans-serif', fontSize: jakartaSize(iconH), color }}
239
- >
240
- {WORDMARK}
241
- </span>
242
- );
243
- }
244
-
245
- function LogoWordmark({ height, color }: { height: number; color?: string }) {
69
+ function LogoWordmark({ iconH, color }: { iconH: number; color?: string }) {
70
+ const scale = (iconH * 4) / 10 / WORD_CAP;
246
71
  return (
247
72
  <svg
248
73
  xmlns="http://www.w3.org/2000/svg"
249
- viewBox={`0 0 ${WORD_W} ${WORD_H}`}
250
- width={Math.round(height * (WORD_W / WORD_H))}
251
- height={height}
74
+ viewBox={`${WORD_X} ${WORD_Y} ${WORD_W} ${WORD_H}`}
75
+ width={WORD_W * scale}
76
+ height={WORD_H * scale}
252
77
  aria-hidden
253
78
  className="shrink-0"
254
- shapeRendering="crispEdges"
255
79
  >
256
- <g transform="translate(20 20)" fill={color ?? "currentColor"}>
257
- {WORD_CELLS.map(([x, y]) => (
258
- <rect key={`${x}-${y}`} x={x} y={y} width={15.2} height={15.2} />
259
- ))}
260
- </g>
80
+ <path fill={color ?? "currentColor"} d={WORD_PATH} />
261
81
  </svg>
262
82
  );
263
83
  }
264
84
 
265
- export function Logo({ variant = "full", size = 32, face = "pixel", color, className, style }: LogoProps) {
85
+ export function Logo({ variant = "full", size = 32, color, className, style }: LogoProps) {
266
86
  const iconH = size;
267
87
  const mark = <LogoMark height={iconH} color={color} />;
268
- const word = face === "jakarta" ? <LogoJakarta iconH={iconH} color={color} /> : <LogoWordmark height={wordmarkHeight(iconH)} color={color} />;
269
- const root = cn("inline-flex items-center text-foreground", variant === "full" && "gap-2", className);
88
+ const word = <LogoWordmark iconH={iconH} color={color} />;
89
+ const root = cn("inline-flex items-center text-foreground", variant === "full" && "gap-3", className);
270
90
 
271
91
  return (
272
92
  <span role="img" aria-label={WORDMARK} className={root} style={style}>
@@ -57,7 +57,7 @@ function BlockEditor({ value, mono, className, caret = "end", onChange, onBlur,
57
57
  });
58
58
  }}
59
59
  className={cn(
60
- "block w-full resize-none overflow-hidden border-0 bg-transparent p-0 text-[15px] leading-7 text-foreground shadow-none outline-none ring-0 placeholder:text-placeholder focus:outline-none focus:ring-0",
60
+ "block w-full resize-none overflow-hidden border-0 bg-transparent p-0 text-[14px] leading-[22px] text-foreground shadow-none outline-none ring-0 placeholder:text-placeholder focus:outline-none focus:ring-0",
61
61
  mono && "font-mono text-[13px] leading-5",
62
62
  className,
63
63
  )}
@@ -35,7 +35,7 @@ export function MarkdownTable({ children }: { children: ReactNode }) {
35
35
  <div className="overflow-x-auto">
36
36
  <table
37
37
  ref={tableRef}
38
- className="w-max min-w-full wrap-normal border-separate border-spacing-0 text-[14px] [&_th]:bg-(--nonla-table-head) [&_th]:text-foreground [&_th]:font-semibold [&_th]:px-2.5 [&_th]:py-1.5 [&_th]:text-left [&_th]:align-top [&_th]:border-r [&_th]:border-b [&_th]:border-border [&_th:last-child]:border-r-0 [&_td]:px-2.5 [&_td]:py-1.5 [&_td]:align-top [&_td]:text-foreground [&_td]:bg-card [&_td]:border-r [&_td]:border-b [&_td]:border-border [&_td:last-child]:border-r-0 [&_tbody_tr:last-child_td]:border-b-0"
38
+ className="w-max min-w-full border-separate border-spacing-0 text-[14px] [&_th]:max-w-[360px] [&_td]:max-w-[360px] [&_th]:whitespace-normal [&_td]:whitespace-normal [&_th]:wrap-normal [&_td]:wrap-normal [&_th]:bg-(--nonla-table-head) [&_th]:text-foreground [&_th]:font-semibold [&_th]:px-2.5 [&_th]:py-1.5 [&_th]:text-left [&_th]:align-top [&_th]:border-r [&_th]:border-b [&_th]:border-border [&_th:last-child]:border-r-0 [&_td]:px-2.5 [&_td]:py-1.5 [&_td]:align-top [&_td]:text-foreground [&_td]:bg-card [&_td]:border-r [&_td]:border-b [&_td]:border-border [&_td:last-child]:border-r-0 [&_tbody_tr:last-child_td]:border-b-0"
39
39
  >
40
40
  {children}
41
41
  </table>
@@ -7,15 +7,17 @@ import { cn } from "../lib/cn";
7
7
  import { type InlineEdit, type MarkdownBlock, parseMarkdownDoc, readFence, readInlineEdit } from "./blocks";
8
8
  import { MarkdownTable } from "./MarkdownTable";
9
9
  import { MermaidBlock } from "./MermaidBlock";
10
+ import { ReactCode } from "./ReactCode";
11
+ import { ReactCodeSandbox } from "./ReactCodeSandbox";
10
12
 
11
13
  const documentClass = cn(
12
- "min-w-0 wrap-anywhere text-[15px] leading-7 text-foreground",
14
+ "min-w-0 wrap-anywhere text-[14px] leading-[22px] text-foreground",
13
15
  "[&_h1]:my-0 [&_h1]:text-[32px] [&_h1]:leading-tight [&_h1]:font-bold [&_h1]:tracking-tight",
14
16
  "[&_h2]:my-0 [&_h2]:text-[24px] [&_h2]:leading-tight [&_h2]:font-bold [&_h2]:tracking-tight",
15
17
  "[&_h3]:my-0 [&_h3]:text-[20px] [&_h3]:leading-snug [&_h3]:font-semibold",
16
18
  "[&_h4]:my-0 [&_h4]:text-[18px] [&_h4]:leading-snug [&_h4]:font-semibold",
17
19
  "[&_h5]:my-0 [&_h5]:text-[16px] [&_h5]:leading-snug [&_h5]:font-semibold",
18
- "[&_h6]:my-0 [&_h6]:text-[15px] [&_h6]:leading-snug [&_h6]:font-semibold",
20
+ "[&_h6]:my-0 [&_h6]:text-[14px] [&_h6]:leading-snug [&_h6]:font-semibold",
19
21
  "[&_p]:my-0",
20
22
  "[&_ul]:my-1 [&_ul]:list-disc [&_ul]:pl-6",
21
23
  "[&_ol]:my-1 [&_ol]:list-decimal [&_ol]:pl-6",
@@ -37,7 +39,7 @@ const previewComponents: Components = {
37
39
  const codeText = String(children).replace(/\n$/, "");
38
40
  const isBlock = codeText.includes("\n") || !!match;
39
41
  if (!isBlock) {
40
- return <code className="rounded-sm bg-secondary px-1 py-0.5 font-mono text-[0.9em]">{children}</code>;
42
+ return <code className="rounded-sm bg-secondary px-1 py-0.5 font-mono text-[0.9em] shadow-button-outline">{children}</code>;
41
43
  }
42
44
  if (lang.toLowerCase() === "mermaid") return <MermaidView code={codeText} />;
43
45
  return <CodeBlock code={codeText} language={lang || undefined} className="my-2" />;
@@ -55,7 +57,7 @@ const previewComponents: Components = {
55
57
  input({ type, checked }) {
56
58
  if (type !== "checkbox") return <input type={type} checked={checked} readOnly />;
57
59
  return (
58
- <span className="nonla-md-task-check pointer-events-none inline-flex h-7 items-center">
60
+ <span className="nonla-md-task-check pointer-events-none inline-flex h-[22px] items-center">
59
61
  <Checkbox checked={Boolean(checked)} tabIndex={-1} />
60
62
  </span>
61
63
  );
@@ -75,7 +77,7 @@ export function headingClass(level: number): string {
75
77
  if (level === 3) return "text-[20px] font-semibold leading-snug";
76
78
  if (level === 4) return "text-[18px] font-semibold leading-snug";
77
79
  if (level === 5) return "text-[16px] font-semibold leading-snug";
78
- return "text-[15px] font-semibold leading-snug";
80
+ return "text-[14px] font-semibold leading-snug";
79
81
  }
80
82
 
81
83
  type MdNode = {
@@ -137,13 +139,13 @@ export function ListItem({ view, children }: { view: Extract<InlineEdit, { kind:
137
139
  return (
138
140
  <div className="flex items-start gap-1.5" style={level > 0 ? { marginLeft: `${level * 1.25}rem` } : undefined}>
139
141
  {view.task ? (
140
- <span className="pointer-events-none inline-flex h-7 w-5 shrink-0 items-center justify-center">
142
+ <span className="pointer-events-none inline-flex h-[22px] w-5 shrink-0 items-center justify-center">
141
143
  <Checkbox checked={view.checked} tabIndex={-1} />
142
144
  </span>
143
145
  ) : view.ordered ? (
144
- <span className="w-6 shrink-0 text-right text-[15px] leading-7 tabular-nums">{ordinal}.</span>
146
+ <span className="w-6 shrink-0 text-right text-[14px] leading-[22px] tabular-nums">{ordinal}.</span>
145
147
  ) : (
146
- <span className="flex h-7 w-5 shrink-0 items-center justify-center" aria-hidden>
148
+ <span className="flex h-[22px] w-5 shrink-0 items-center justify-center" aria-hidden>
147
149
  <span className="size-1.5 rounded-full bg-current" />
148
150
  </span>
149
151
  )}
@@ -181,7 +183,14 @@ function fenceClosed(body: string): boolean {
181
183
  return /^(?:`{3,}|~{3,})$/.test(last);
182
184
  }
183
185
 
184
- export function BlockBody({ block, streaming }: { block: MarkdownBlock; streaming?: boolean }): ReactNode {
186
+ type LiveReactOptions = {
187
+ trustedModules?: Record<string, unknown>;
188
+ sandboxSrc?: string;
189
+ showHeader?: boolean;
190
+ showCode?: boolean;
191
+ };
192
+
193
+ export function BlockBody({ block, streaming, trustedModules, sandboxSrc, showHeader, showCode }: { block: MarkdownBlock; streaming?: boolean } & LiveReactOptions): ReactNode {
185
194
  if (block.kind === "mermaid") {
186
195
  const fence = readFence(block.body);
187
196
  const code = fence?.code ?? block.body;
@@ -191,6 +200,11 @@ export function BlockBody({ block, streaming }: { block: MarkdownBlock; streamin
191
200
  if (block.kind === "code") {
192
201
  const fence = readFence(block.body);
193
202
  if (!fence) return <CodeBlock code={block.body} className="my-0" />;
203
+ // An unclosed fence is still being written, so it never runs.
204
+ if (fence.lang === "live-react" && fenceClosed(block.body)) {
205
+ if (sandboxSrc) return <ReactCodeSandbox key={fence.code} code={fence.code} src={sandboxSrc} showHeader={showHeader} showCode={showCode} />;
206
+ if (trustedModules) return <ReactCode code={fence.code} trustedModules={trustedModules} showHeader={showHeader} showCode={showCode} />;
207
+ }
194
208
  return <CodeBlock code={fence.code} language={fence.lang || undefined} className="my-0" />;
195
209
  }
196
210
  const view = readInlineEdit(block.body);
@@ -210,10 +224,25 @@ export type MarkdownViewerProps = {
210
224
  streaming?: boolean;
211
225
  placeholder?: string;
212
226
  className?: string;
227
+ /**
228
+ * Runs ```live-react fences in a sandboxed iframe. Use this for markdown written by
229
+ * users or agents. Points at a page that calls `mountReactCodeRunner`, on its own origin.
230
+ * Wins over `trustedModules` when both are set.
231
+ */
232
+ sandboxSrc?: string;
233
+ /**
234
+ * Runs ```live-react fences in this page, with its cookies, storage, and network.
235
+ * Only for markdown you wrote. `react` is provided. Other imports must be listed here.
236
+ */
237
+ trustedModules?: Record<string, unknown>;
238
+ /** Header on `live-react` results (`Sandbox` or `Live`). `false` shows the result inline. Default `true`. */
239
+ showHeader?: boolean;
240
+ /** Code tab in the header of `live-react` results. Needs `showHeader`. Default `true`. */
241
+ showCode?: boolean;
213
242
  };
214
243
 
215
244
  /** Read-only markdown. Same blocks as the editor preview, without editing. */
216
- export function MarkdownViewer({ value = "", streaming = false, placeholder, className }: MarkdownViewerProps) {
245
+ export function MarkdownViewer({ value = "", streaming = false, sandboxSrc, trustedModules, showHeader = true, showCode = true, placeholder, className }: MarkdownViewerProps) {
217
246
  const doc = useMemo(() => parseMarkdownDoc(value), [value]);
218
247
  if (doc.blocks.length === 0) {
219
248
  return placeholder ? <p className={cn("m-0 text-sm text-placeholder", className)}>{placeholder}</p> : null;
@@ -227,7 +256,7 @@ export function MarkdownViewer({ value = "", streaming = false, placeholder, cla
227
256
  data-md-block={block.kind}
228
257
  className={cn("outline-none", spaceBefore(block, doc.blocks[index - 1]))}
229
258
  >
230
- <BlockBody block={block} streaming={streaming} />
259
+ <BlockBody block={block} streaming={streaming} trustedModules={trustedModules} sandboxSrc={sandboxSrc} showHeader={showHeader} showCode={showCode} />
231
260
  </div>
232
261
  ))}
233
262
  </div>
@@ -0,0 +1,75 @@
1
+ import { Component, type ReactNode, useMemo } from "react";
2
+ import { SolarIcon } from "../icon/SolarIcon";
3
+ import { ReactCodeFrame } from "./ReactCodeFrame";
4
+ import { compileReactCode, type LiveComponent } from "./reactCompile";
5
+
6
+ export type ReactCodeProps = {
7
+ /** Component source. Must `export default` a React component. */
8
+ code: string;
9
+ /** Modules `import` can resolve. `react` is always available. */
10
+ trustedModules: Record<string, unknown>;
11
+ /** Header above the result. Default `true`. */
12
+ showHeader?: boolean;
13
+ /** Code tab in the header. Needs `showHeader`. Default `true`. */
14
+ showCode?: boolean;
15
+ /** Header text. Default `Live`. */
16
+ title?: ReactNode;
17
+ /** Header icon. `null` hides it. */
18
+ icon?: ReactNode;
19
+ /** Classes for the outer box. */
20
+ className?: string;
21
+ };
22
+
23
+ type Compiled = { Demo: LiveComponent } | { error: string };
24
+
25
+ function Live({ code, trustedModules }: Pick<ReactCodeProps, "code" | "trustedModules">) {
26
+ const compiled = useMemo<Compiled>(() => {
27
+ try {
28
+ return { Demo: compileReactCode(code, trustedModules) };
29
+ } catch (error) {
30
+ return { error: error instanceof Error ? error.message : "Could not run this React block." };
31
+ }
32
+ }, [code, trustedModules]);
33
+
34
+ if ("error" in compiled) return <p className="m-0 text-sm text-destructive">{compiled.error}</p>;
35
+ const { Demo } = compiled;
36
+ return (
37
+ <div className="flex w-full flex-wrap items-center justify-center gap-6">
38
+ <Demo />
39
+ </div>
40
+ );
41
+ }
42
+
43
+ class RenderBoundary extends Component<{ children: ReactNode }, { error: string | null }> {
44
+ state = { error: null as string | null };
45
+
46
+ static getDerivedStateFromError(error: Error) {
47
+ return { error: error.message };
48
+ }
49
+
50
+ render() {
51
+ if (this.state.error) return <p className="m-0 text-sm text-destructive">{this.state.error}</p>;
52
+ return this.props.children;
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Runs a `live-react` fence in this page, with this page's cookies, storage, and network.
58
+ *
59
+ * Only for source you wrote. Markdown from users or agents must use `ReactCodeSandbox`.
60
+ */
61
+ export function ReactCode({ code, trustedModules, showHeader = true, showCode = true, title = "Live", icon, className }: ReactCodeProps) {
62
+ return (
63
+ <ReactCodeFrame
64
+ title={title}
65
+ icon={icon === undefined ? <SolarIcon name="play-circle-linear" size={16} className="text-warn" /> : icon}
66
+ showHeader={showHeader}
67
+ code={showCode ? code : undefined}
68
+ className={className}
69
+ >
70
+ <RenderBoundary key={code}>
71
+ <Live code={code} trustedModules={trustedModules} />
72
+ </RenderBoundary>
73
+ </ReactCodeFrame>
74
+ );
75
+ }
@@ -0,0 +1,46 @@
1
+ import { type ReactNode, useState } from "react";
2
+ import { CodeBlock } from "../codeblock/CodeBlock";
3
+ import { cn } from "../lib/cn";
4
+ import { Segmented } from "../segmented/Segmented";
5
+
6
+ type Tab = "preview" | "code";
7
+
8
+ const TABS = [
9
+ { label: "Preview", value: "preview" },
10
+ { label: "Code", value: "code" },
11
+ ] satisfies { label: string; value: Tab }[];
12
+
13
+ export type ReactCodeFrameProps = {
14
+ /** Header text, such as `Sandbox`. */
15
+ title: ReactNode;
16
+ /** Icon before the title. `null` hides it. */
17
+ icon?: ReactNode;
18
+ /** `false` drops the header and the border, so the result sits inline with the text around it. */
19
+ showHeader?: boolean;
20
+ /** Source for the Code tab. Omit to show the result only. The tab lives in the header, so no header means no tab. */
21
+ code?: string;
22
+ className?: string;
23
+ /** The rendered result. */
24
+ children: ReactNode;
25
+ };
26
+
27
+ /** Header, result, and an optional Code tab around a rendered `live-react` block. */
28
+ export function ReactCodeFrame({ title, icon, showHeader = true, code, className, children }: ReactCodeFrameProps) {
29
+ const [tab, setTab] = useState<Tab>("preview");
30
+ if (!showHeader) return <div className={cn("mb-2 w-full min-w-0", className)}>{children}</div>;
31
+ const hasCode = code !== undefined;
32
+ return (
33
+ <div className={cn("mb-2 flex w-full min-w-0 flex-col overflow-clip rounded-xl border border-border text-foreground", className)}>
34
+ <div className="flex h-10 items-center justify-between gap-2 border-b border-border pr-2 pl-3 text-xs font-medium">
35
+ <div className="flex min-w-0 items-center gap-2">
36
+ {icon}
37
+ <span className="truncate">{title}</span>
38
+ </div>
39
+ {hasCode ? <Segmented<Tab> size="small" options={TABS} value={tab} onChange={setTab} /> : null}
40
+ </div>
41
+ {/* Stays mounted on the Code tab, so an iframe keeps its state. */}
42
+ <div className={cn("min-w-0 px-3 py-3", hasCode && tab === "code" && "hidden")}>{children}</div>
43
+ {hasCode && tab === "code" ? <CodeBlock code={code} language="tsx" className="my-0 rounded-none border-0" /> : null}
44
+ </div>
45
+ );
46
+ }
@@ -0,0 +1,82 @@
1
+ import { Component, type ReactNode } from "react";
2
+ import { flushSync } from "react-dom";
3
+ import { createRoot } from "react-dom/client";
4
+ import { applyColorMode } from "../theme";
5
+ import { REACT_CODE_SOURCE, type RunnerPayload, readHostMessage } from "./reactCodeProtocol";
6
+ import { compileReactCode } from "./reactCompile";
7
+
8
+ type Report = (message: RunnerPayload) => void;
9
+
10
+ class Boundary extends Component<{ report: Report; children: ReactNode }, { error: string | null }> {
11
+ state = { error: null as string | null };
12
+
13
+ static getDerivedStateFromError(error: Error) {
14
+ return { error: error.message };
15
+ }
16
+
17
+ componentDidCatch(error: Error) {
18
+ this.props.report({ type: "error", message: error.message });
19
+ }
20
+
21
+ render() {
22
+ if (this.state.error) return <p style={{ margin: 0, color: "crimson", fontSize: 14 }}>{this.state.error}</p>;
23
+ return this.props.children;
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Runner side of the sandbox. Call this once from the page `ReactCodeSandbox` loads.
29
+ *
30
+ * `modules` is the only thing the markdown can import, and the host cannot change it.
31
+ * Serve the page with `Content-Security-Policy: connect-src 'none'`, and with
32
+ * `Access-Control-Allow-Origin: *` on its assets, since a sandboxed frame loads them cross-origin.
33
+ */
34
+ export function mountReactCodeRunner(root: HTMLElement, modules: Record<string, unknown>): () => void {
35
+ const reactRoot = createRoot(root);
36
+ const report: Report = (message) => window.parent.postMessage({ source: REACT_CODE_SOURCE, ...message }, "*");
37
+
38
+ let waitingForRun = true;
39
+ const onMessage = (event: MessageEvent) => {
40
+ if (event.source !== window.parent) return;
41
+ const msg = readHostMessage(event.data);
42
+ if (!msg) return;
43
+ applyColorMode(msg.mode);
44
+ if (msg.type === "mode") return;
45
+ waitingForRun = false;
46
+ try {
47
+ const Demo = compileReactCode(msg.code, modules);
48
+ flushSync(() =>
49
+ reactRoot.render(
50
+ <Boundary report={report}>
51
+ <div style={{ display: "flex", flexWrap: "wrap", alignItems: "center", justifyContent: "center", gap: 24 }}>
52
+ <Demo />
53
+ </div>
54
+ </Boundary>,
55
+ ),
56
+ );
57
+ report({ type: "rendered" });
58
+ } catch (error) {
59
+ report({ type: "error", message: error instanceof Error ? error.message : "Could not run this React block." });
60
+ }
61
+ };
62
+
63
+ const sendHeight = () => report({ type: "resize", height: root.getBoundingClientRect().height });
64
+ const observer = new ResizeObserver(sendHeight);
65
+ observer.observe(root);
66
+
67
+ window.addEventListener("message", onMessage);
68
+ report({ type: "ready" });
69
+ // Host effect may miss the first ping (React Strict Mode remounts the listener).
70
+ const readyBeat = window.setInterval(() => {
71
+ if (waitingForRun) report({ type: "ready" });
72
+ else window.clearInterval(readyBeat);
73
+ }, 250);
74
+
75
+ return () => {
76
+ waitingForRun = false;
77
+ window.clearInterval(readyBeat);
78
+ window.removeEventListener("message", onMessage);
79
+ observer.disconnect();
80
+ reactRoot.unmount();
81
+ };
82
+ }