devnonla-ui 0.18.1 → 0.19.1

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.
@@ -1,21 +1,11 @@
1
- import { type ReactNode, useEffect, useMemo, useRef, useState } from "react";
1
+ import { type ReactNode, useEffect, useRef, useState } from "react";
2
2
  import { Icon } from "../icon/Icon";
3
3
  import { cn } from "../lib/cn";
4
4
  import { getColorMode, type NonlaColorMode, subscribeColorMode } from "../theme";
5
5
  import { ReactCodeFrame } from "./ReactCodeFrame";
6
6
  import { type HostMessage, REACT_CODE_SOURCE, readRunnerMessage } from "./reactCodeProtocol";
7
7
 
8
- /** Keep `mode` on the runner URL without dropping a query the caller already set. */
9
- function withColorMode(src: string, mode: NonlaColorMode): string {
10
- const hashAt = src.indexOf("#");
11
- const hash = hashAt >= 0 ? src.slice(hashAt) : "";
12
- const beforeHash = hashAt >= 0 ? src.slice(0, hashAt) : src;
13
- const queryAt = beforeHash.indexOf("?");
14
- const path = queryAt >= 0 ? beforeHash.slice(0, queryAt) : beforeHash;
15
- const params = new URLSearchParams(queryAt >= 0 ? beforeHash.slice(queryAt + 1) : "");
16
- params.set("mode", mode);
17
- return `${path}?${params.toString()}${hash}`;
18
- }
8
+ const RUNNER_URL = new URL("./reactSandboxFrame.ts", import.meta.url).href;
19
9
 
20
10
  /** The runner page must report `ready` within this time. */
21
11
  const READY_TIMEOUT_MS = 20000;
@@ -24,11 +14,81 @@ const RENDER_TIMEOUT_MS = 15000;
24
14
  const MIN_HEIGHT = 32;
25
15
  const MAX_HEIGHT = 1200;
26
16
 
17
+ function escapeAttr(value: string): string {
18
+ return value.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
19
+ }
20
+
21
+ type ViteEnv = { DEV?: boolean; BASE_URL?: string };
22
+
23
+ function viteEnv(): ViteEnv {
24
+ return (import.meta as ImportMeta & { env?: ViteEnv }).env ?? {};
25
+ }
26
+
27
+ /** Local Vite needs a websocket. A deployed host does not get network from the frame. */
28
+ function connectSrc(origin: string): string {
29
+ const host = new URL(origin).hostname;
30
+ if (host !== "localhost" && host !== "127.0.0.1") return "'none'";
31
+ return `${origin} ${origin.replace(/^http/, "ws")}`;
32
+ }
33
+
34
+ /** Vite's React refresh check fails unless this runs before the runner module. Dev only. */
35
+ function reactRefreshPreamble(): string {
36
+ const env = viteEnv();
37
+ if (env.DEV !== true) return "";
38
+ const base = env.BASE_URL ?? "/";
39
+ return `<script type="module">
40
+ import { injectIntoGlobalHook } from ${JSON.stringify(`${base}@react-refresh`)};
41
+ injectIntoGlobalHook(window);
42
+ window.$RefreshReg$ = function () {};
43
+ window.$RefreshSig$ = function () { return function (type) { return type; }; };
44
+ </script>`;
45
+ }
46
+
47
+ /** Document for the sandboxed frame. The runner and its styles load from this package. */
48
+ function sandboxDocument(mode: NonlaColorMode): string {
49
+ const origin = new URL(RUNNER_URL).origin;
50
+ const csp = [
51
+ "default-src 'none'",
52
+ `script-src 'unsafe-inline' 'unsafe-eval' ${origin}`,
53
+ `style-src 'unsafe-inline' ${origin}`,
54
+ `img-src data: blob: ${origin}`,
55
+ `font-src data: ${origin}`,
56
+ `connect-src ${connectSrc(origin)}`,
57
+ "form-action 'none'",
58
+ "base-uri 'none'",
59
+ "frame-src 'none'",
60
+ ].join("; ");
61
+ return `<!doctype html>
62
+ <html lang="en">
63
+ <head>
64
+ <meta charset="UTF-8" />
65
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
+ <meta http-equiv="Content-Security-Policy" content="${escapeAttr(csp)}" />
67
+ <script>
68
+ document.documentElement.dataset.pendingMode=${JSON.stringify(mode)};
69
+ window.addEventListener("error", function (event) {
70
+ var message = event.message || "The sandbox did not start.";
71
+ parent.postMessage({ source: ${JSON.stringify(REACT_CODE_SOURCE)}, type: "error", message: String(message).slice(0, 300) }, "*");
72
+ });
73
+ </script>
74
+ <style>
75
+ html { color-scheme: dark; background: #141414; }
76
+ html:has(.nonla-ui.light) { color-scheme: light; background: #ffffff; }
77
+ .nonla-ui { min-height: 100%; }
78
+ </style>
79
+ </head>
80
+ <body>
81
+ <div id="root"></div>
82
+ ${reactRefreshPreamble()}
83
+ <script type="module" src="${escapeAttr(RUNNER_URL)}"></script>
84
+ </body>
85
+ </html>`;
86
+ }
87
+
27
88
  export type ReactCodeSandboxProps = {
89
+ /** Component source. Must `export default` a React component. */
28
90
  code: string;
29
- /** URL of a page that calls `mountReactCodeRunner`. Serve it from its own origin. Loaded with `?mode=light|dark` so the runner can paint before its script. */
30
- src: string;
31
- /** Header above the result. Default `true`. */
91
+ /** Header above the result. Default `false`. */
32
92
  showHeader?: boolean;
33
93
  /** Preview / Code switch in the header. Needs `showHeader`. Default `true`. */
34
94
  showCode?: boolean;
@@ -43,29 +103,37 @@ export type ReactCodeSandboxProps = {
43
103
  /**
44
104
  * Runs untrusted `live-react` source in a sandboxed iframe.
45
105
  *
106
+ * Pass `code`. The frame is built here and loads this package's runner.
107
+ * `react`, `devnonla-ui`, `devnonla-ui/chat`, `devnonla-ui/markdown`, and
108
+ * `react-hook-form` can be imported. Nothing else can.
109
+ *
46
110
  * `sandbox="allow-scripts"` without `allow-same-origin` gives the frame an opaque origin,
47
111
  * so it cannot read this page's cookies, storage, or DOM.
48
112
  */
49
- export function ReactCodeSandbox({ code, src, showHeader = true, showCode = true, title = "Sandbox", icon, className }: ReactCodeSandboxProps) {
113
+ export function ReactCodeSandbox({ code, showHeader = false, showCode = true, title = "Sandbox", icon, className }: ReactCodeSandboxProps) {
50
114
  const frameRef = useRef<HTMLIFrameElement>(null);
51
115
  const [height, setHeight] = useState(MIN_HEIGHT);
52
116
  const [error, setError] = useState<string | null>(null);
53
117
  const [mode, setMode] = useState(getColorMode);
54
118
  const [painted, setPainted] = useState(false);
55
- // Mode stays out of the URL after first load. A later theme change is a message, so the frame does not reload.
119
+ const [srcDoc, setSrcDoc] = useState<string | null>(null);
120
+ // Mode is baked into the document once. A later theme change is a message, so the frame does not reload.
56
121
  const modeOnLoad = useRef(getColorMode());
57
- const frameSrc = useMemo(() => withColorMode(src, modeOnLoad.current), [src]);
58
122
 
59
123
  useEffect(() => subscribeColorMode(() => setMode(getColorMode())), []);
60
124
 
125
+ useEffect(() => {
126
+ setSrcDoc(sandboxDocument(modeOnLoad.current));
127
+ }, []);
128
+
61
129
  useEffect(() => {
62
130
  setPainted(false);
63
131
  setError(null);
64
- }, [frameSrc, code]);
132
+ }, [srcDoc, code]);
65
133
 
66
134
  useEffect(() => {
67
135
  const frame = frameRef.current;
68
- if (!frame) return;
136
+ if (!frame || !srcDoc) return;
69
137
  let alive = true;
70
138
  // Once the runner has painted, a late timer must not replace the preview with an error.
71
139
  let didRender = false;
@@ -131,7 +199,7 @@ export function ReactCodeSandbox({ code, src, showHeader = true, showCode = true
131
199
  clearTimeout(readyTimer);
132
200
  clearTimeout(renderTimer);
133
201
  };
134
- }, [code, src]);
202
+ }, [code, srcDoc]);
135
203
 
136
204
  return (
137
205
  <ReactCodeFrame
@@ -142,15 +210,17 @@ export function ReactCodeSandbox({ code, src, showHeader = true, showCode = true
142
210
  className={className}
143
211
  >
144
212
  {error ? <p className="m-0 text-sm text-destructive">{error}</p> : null}
145
- <iframe
146
- ref={frameRef}
147
- src={frameSrc}
148
- title="React example"
149
- sandbox="allow-scripts"
150
- referrerPolicy="no-referrer"
151
- className={cn("block w-full border-0 bg-background", (!painted || error) && "opacity-0")}
152
- style={{ height, colorScheme: mode }}
153
- />
213
+ {srcDoc ? (
214
+ <iframe
215
+ ref={frameRef}
216
+ srcDoc={srcDoc}
217
+ title="React example"
218
+ sandbox="allow-scripts"
219
+ referrerPolicy="no-referrer"
220
+ className={cn("block w-full border-0 bg-background", (!painted || error) && "opacity-0")}
221
+ style={{ height, colorScheme: mode }}
222
+ />
223
+ ) : null}
154
224
  </ReactCodeFrame>
155
225
  );
156
226
  }
@@ -0,0 +1,14 @@
1
+ @import "tailwindcss";
2
+ @import "../styles.css";
3
+
4
+ /* The frame reports its content height, so the root must not fill the frame. */
5
+ html,
6
+ body,
7
+ #root {
8
+ height: auto;
9
+ background: var(--background);
10
+ }
11
+
12
+ body {
13
+ margin: 0;
14
+ }
@@ -0,0 +1,26 @@
1
+ import "./reactSandboxFrame.css";
2
+ import * as rhf from "react-hook-form";
3
+ import * as chat from "../chat/index";
4
+ import * as ui from "../index";
5
+ import * as markdown from "./index";
6
+ import { mountReactCodeRunner } from "./ReactCodeRunner";
7
+
8
+ // Fixed allowlist. The host page only sends source text, so it cannot add modules.
9
+ const modules = {
10
+ "devnonla-ui": ui,
11
+ "devnonla-ui/markdown": markdown,
12
+ "devnonla-ui/chat": chat,
13
+ "@nonla-agents/ui": ui,
14
+ "@nonla-agents/ui/markdown": markdown,
15
+ "@nonla-agents/ui/chat": chat,
16
+ "react-hook-form": rhf,
17
+ };
18
+
19
+ const root = document.getElementById("root");
20
+ if (!root) throw new Error("Sandbox root element not found");
21
+
22
+ const stop = mountReactCodeRunner(root, modules);
23
+
24
+ type Hot = { dispose: (callback: () => void) => void };
25
+ const hot = (import.meta as ImportMeta & { hot?: Hot }).hot;
26
+ hot?.dispose(stop);
package/src/styles.css CHANGED
@@ -3,7 +3,6 @@
3
3
  @import "@fontsource-variable/inter/opsz-italic.css";
4
4
  @import "@fontsource-variable/geist-mono/wght.css";
5
5
  @import "@fontsource-variable/geist-mono/wght-italic.css";
6
- @import "./avatar/avatar.css";
7
6
 
8
7
  /**
9
8
  * NonlaUI theme.
@@ -1,332 +0,0 @@
1
- import { createContext, type ReactNode, useContext, useEffect, useId, useState } from "react";
2
- import { cn } from "../lib/cn";
3
-
4
- /** `still` holds the pose. `idle` bobs the body, blinks, and glances. Defaults to `still`. */
5
- export type AgentAvatarMotion = "still" | "idle";
6
-
7
- /** Fixed facing. The shape stays centered and the eyes move that way. No straight-up glance. */
8
- export type AgentAvatarLook = "forward" | "left" | "right" | "down" | "up-left" | "up-right" | "down-left" | "down-right";
9
-
10
- /** Pupil color. Omitted picks black, or white when the face is dark. */
11
- export type AgentAvatarEyeColor = "black" | "white";
12
-
13
- export const AGENT_AVATAR_PARTS = {
14
- shape: ["circle", "square", "triangle", "diamond", "hex", "gem", "pill", "arch", "cloud", "blob", "drop", "shield", "nonla"],
15
- color: ["#6E56F0", "#1C1C1C", "#F0B429", "#3DCFC0", "#F47820", "#3B82F6", "#F2549B", "#A56B3C"],
16
- } as const;
17
-
18
- type Part<K extends keyof typeof AGENT_AVATAR_PARTS> = (typeof AGENT_AVATAR_PARTS)[K][number];
19
-
20
- /** A character is one shape, a color, and two eyes. */
21
- export type AgentAvatarConfig = {
22
- shape: Part<"shape">;
23
- color: string;
24
- eyeColor?: AgentAvatarEyeColor;
25
- };
26
-
27
- export type AgentAvatarProps = {
28
- /** Shape and color. Omitted parts use circle and `#6E56F0`. */
29
- config?: Partial<AgentAvatarConfig>;
30
- /** Square size in px. Defaults to 64. */
31
- size?: number;
32
- motion?: AgentAvatarMotion;
33
- /** Which way the face is turned. Defaults to straight ahead. */
34
- look?: AgentAvatarLook;
35
- className?: string;
36
- /** Accessible name. Defaults to "Agent". */
37
- label?: string;
38
- };
39
-
40
- function hash(seed: string) {
41
- let h = 2166136261;
42
- for (let i = 0; i < seed.length; i++) {
43
- h ^= seed.charCodeAt(i);
44
- h = Math.imul(h, 16777619);
45
- }
46
- return h >>> 0;
47
- }
48
-
49
- const DEFAULT_CONFIG: AgentAvatarConfig = {
50
- shape: AGENT_AVATAR_PARTS.shape[0],
51
- color: AGENT_AVATAR_PARTS.color[0],
52
- };
53
-
54
- function resolveConfig(config?: Partial<AgentAvatarConfig>): AgentAvatarConfig {
55
- return {
56
- shape: config?.shape ?? DEFAULT_CONFIG.shape,
57
- color: config?.color ?? DEFAULT_CONFIG.color,
58
- eyeColor: config?.eyeColor,
59
- };
60
- }
61
-
62
- function stagger(seed: string) {
63
- return (hash(seed) % 8) * 0.2;
64
- }
65
-
66
- const EYE = "#111";
67
- const EYE_ON_DARK = "#fff";
68
-
69
- function eyeInk(color: string) {
70
- const hex = color.replace("#", "");
71
- if (hex.length < 6) return EYE;
72
- const r = Number.parseInt(hex.slice(0, 2), 16);
73
- const g = Number.parseInt(hex.slice(2, 4), 16);
74
- const b = Number.parseInt(hex.slice(4, 6), 16);
75
- const luma = (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255;
76
- return luma < 0.28 ? EYE_ON_DARK : EYE;
77
- }
78
-
79
- function eyeFill(color: string, eyeColor?: AgentAvatarEyeColor) {
80
- if (eyeColor === "white") return EYE_ON_DARK;
81
- if (eyeColor === "black") return EYE;
82
- return eyeInk(color);
83
- }
84
-
85
- function randomLook(current: AgentAvatarLook) {
86
- const pool = LOOKS.filter((item) => item !== current);
87
- return pool[Math.floor(Math.random() * pool.length)];
88
- }
89
-
90
- /** Idle holds one facing, then picks a different one. */
91
- function useIdleLook(active: boolean, home: AgentAvatarLook, delay: number) {
92
- const [glance, setGlance] = useState(home);
93
-
94
- useEffect(() => {
95
- if (!active) return;
96
- const media = window.matchMedia("(prefers-reduced-motion: reduce)");
97
- if (media.matches) {
98
- setGlance(home);
99
- return;
100
- }
101
-
102
- let timer = 0;
103
- const arm = (wait: number) => {
104
- timer = window.setTimeout(() => {
105
- setGlance((current) => randomLook(current));
106
- arm(1100 + Math.random() * 1700);
107
- }, wait);
108
- };
109
-
110
- setGlance(home);
111
- arm(700 + delay * 1000 + Math.random() * 900);
112
-
113
- const onChange = () => {
114
- if (!media.matches) return;
115
- window.clearTimeout(timer);
116
- setGlance(home);
117
- };
118
- media.addEventListener("change", onChange);
119
- return () => {
120
- window.clearTimeout(timer);
121
- media.removeEventListener("change", onChange);
122
- };
123
- }, [active, home, delay]);
124
-
125
- return active ? glance : home;
126
- }
127
-
128
- /** Eye travel in viewBox units. The shape stays on the center of the box. */
129
- const LOOK: Record<AgentAvatarLook, [number, number]> = {
130
- forward: [0, 0],
131
- left: [-6.4, 0],
132
- right: [6.4, 0],
133
- down: [0, 4.4],
134
- "up-left": [-4.5, -3.1],
135
- "up-right": [4.5, -3.1],
136
- "down-left": [-4.5, 3.1],
137
- "down-right": [4.5, 3.1],
138
- };
139
-
140
- const LOOKS = Object.keys(LOOK) as AgentAvatarLook[];
141
-
142
- function mixHex(color: string, toward: "#fff" | "#000", amount: number) {
143
- const hex = color.replace("#", "");
144
- const channel = (start: number) => Number.parseInt(hex.slice(start, start + 2), 16);
145
- const target = toward === "#fff" ? 255 : 0;
146
- const mix = (value: number) => Math.round(value + (target - value) * amount)
147
- .toString(16)
148
- .padStart(2, "0");
149
- return `#${mix(channel(0))}${mix(channel(2))}${mix(channel(4))}`;
150
- }
151
-
152
- const PaintContext = createContext("#111");
153
- const ShadowContext = createContext<string | undefined>(undefined);
154
- const InkContext = createContext(EYE);
155
-
156
- /** Logo mark, fitted to the avatar box. The mark stays centered. Eyes and the smile travel. */
157
- const NONLA_HAT =
158
- "M32.05 11.6C31.32 11.6 26.28 15.07 17.23 22.05C12.68 25.52 8.71 28.77 6.19 31.06C5.2 31.92 4.21 32.86 3.49 33.77C2.95 34.49 2.36 35.16 2.09 35.57C1.91 36.15 1.95 36.83 2 37.37A30 4.86 0 0 1 62 37.37C62.05 36.83 62.09 36.15 61.91 35.57C61.64 35.16 61.05 34.49 60.51 33.77C59.79 32.86 58.8 31.92 57.81 31.06C55.29 28.77 51.32 25.52 46.77 22.05C37.72 15.07 32.68 11.6 32.05 11.6Z";
159
-
160
- function nonlaTones(color: string) {
161
- return {
162
- brim: mixHex(color, "#000", 0.3),
163
- face: mixHex(color, "#fff", 0.25),
164
- hat: color,
165
- };
166
- }
167
-
168
- function NonlaHat({ tone }: { tone: ReturnType<typeof nonlaTones> }) {
169
- return (
170
- <>
171
- <path fill={tone.hat} d={NONLA_HAT} />
172
- <path fill="none" stroke={tone.hat} strokeWidth="0.4" d="M2.5 37.37A29.5 4.86 0 0 1 61.5 37.37" />
173
- <path fill="none" stroke={tone.brim} strokeWidth="1" d="M2.5 37.37A29.5 4.77 0 0 1 61.5 37.37" />
174
- <g fill="none" stroke={tone.brim} strokeWidth="0.5" strokeLinecap="round" opacity="0.55">
175
- <path d="M31.23 12.86C25.6 20.25 17.95 27.5 10.15 33.9" />
176
- <path d="M31.59 12.86C28.98 19.4 26.46 25.52 23.94 31.38" />
177
- <path d="M32.5 12.86C35.11 19.4 37.63 25.52 40.15 31.38" />
178
- <path d="M32.86 12.86C38.49 20.25 46.14 27.5 53.94 33.9" />
179
- </g>
180
- </>
181
- );
182
- }
183
-
184
- function NonlaMark({ color }: { color: string }) {
185
- const tone = nonlaTones(color);
186
- const ink = useContext(InkContext);
187
- const shadow = useContext(ShadowContext);
188
- const catchlight = ink === EYE_ON_DARK ? EYE : "#fff";
189
- const clip = `nonla-face-${useId().replace(/:/g, "")}`;
190
- return (
191
- <>
192
- <g filter={shadow}>
193
- <ellipse cx="32" cy="37.37" rx="30" ry="8.56" fill={tone.brim} />
194
- <circle cx="32.05" cy="35.66" r="16.74" fill={tone.face} />
195
- <NonlaHat tone={tone} />
196
- </g>
197
- <clipPath id={clip}>
198
- <circle cx="32.05" cy="35.66" r="16.74" />
199
- </clipPath>
200
- <g clipPath={`url(#${clip})`}>
201
- <g className="nonla-avatar-gaze">
202
- <g className="nonla-avatar-eyes">
203
- <ellipse cx="24.82" cy="40.3" rx="2" ry="3.06" fill={ink} />
204
- <ellipse cx="38.96" cy="40.3" rx="2" ry="3.06" fill={ink} />
205
- <ellipse cx="25.29" cy="39.08" rx="0.59" ry="0.86" fill={catchlight} />
206
- <ellipse cx="39.43" cy="39.08" rx="0.59" ry="0.86" fill={catchlight} />
207
- </g>
208
- <path fill="none" stroke={EYE} strokeWidth="1.08" strokeLinecap="round" d="M28.8 44.67C30.47 46.65 33.53 46.65 35.15 44.67" />
209
- </g>
210
- </g>
211
- {/* Covers eyes that travel up into the cone. The smile stays below this edge. */}
212
- <NonlaHat tone={tone} />
213
- </>
214
- );
215
- }
216
-
217
- function Eyes({ cx, cy, w = 5.2, h = 12 }: { cx: number; cy: number; w?: number; h?: number }) {
218
- const gap = w * 1.5;
219
- const top = cy - h / 2;
220
- const fill = useContext(InkContext);
221
- return (
222
- <g className="nonla-avatar-gaze">
223
- <g className="nonla-avatar-eyes">
224
- <rect x={cx - gap / 2 - w} y={top} width={w} height={h} rx={w / 2} fill={fill} />
225
- <rect x={cx + gap / 2} y={top} width={w} height={h} rx={w / 2} fill={fill} />
226
- </g>
227
- </g>
228
- );
229
- }
230
-
231
- function Shape({ shape, color, eyeColor }: AgentAvatarConfig) {
232
- const paint = useContext(PaintContext);
233
- const shadow = useContext(ShadowContext);
234
- const soft = { fill: paint, stroke: paint, strokeLinejoin: "round" as const };
235
- const face = (mark: ReactNode, pupils: ReactNode, shiftY = 0) => (
236
- <g transform={shiftY ? `translate(0 ${shiftY})` : undefined}>
237
- <g filter={shadow}>{mark}</g>
238
- {pupils}
239
- </g>
240
- );
241
- const body: Record<AgentAvatarConfig["shape"], ReactNode> = {
242
- circle: face(<circle cx="32" cy="32" r="22" fill={paint} />, <Eyes cx={32} cy={30} />),
243
- square: face(<rect x="9" y="9" width="46" height="46" rx="16" fill={paint} />, <Eyes cx={32} cy={30} />),
244
- triangle: face(<path d="M32 16 L50 50 H14 Z" strokeWidth="14" {...soft} />, <Eyes cx={32} cy={38} />, -1),
245
- diamond: face(<path d="M32 14 L50 32 L32 50 L14 32 Z" strokeWidth="8" {...soft} />, <Eyes cx={32} cy={30} />),
246
- hex: face(<path d="M32 12 L49 22 L49 42 L32 52 L15 42 L15 22 Z" strokeWidth="8" {...soft} />, <Eyes cx={32} cy={30} />),
247
- gem: face(<path d="M22 12 H42 L52 22 V42 L42 52 H22 L12 42 V22 Z" strokeWidth="8" {...soft} />, <Eyes cx={32} cy={30} />),
248
- pill: face(<rect x="6" y="18" width="52" height="28" rx="14" fill={paint} />, <Eyes cx={32} cy={30} />),
249
- arch: face(<path d="M14 50V32C14 18 50 18 50 32V50C50 56 14 56 14 50Z" fill={paint} />, <Eyes cx={32} cy={36} />, -6),
250
- cloud: face(
251
- <g fill={paint}>
252
- <circle cx="20" cy="38" r="12" />
253
- <circle cx="44" cy="38" r="12" />
254
- <circle cx="32" cy="28" r="14" />
255
- <rect x="16" y="34" width="32" height="14" />
256
- </g>,
257
- <Eyes cx={32} cy={36} />,
258
- ),
259
- blob: face(<ellipse cx="32" cy="34" rx="24" ry="18" fill={paint} />, <Eyes cx={32} cy={32} />, -2),
260
- drop: face(
261
- <path d="M27 23C19 30 15 37 15 46C15 55 22.5 60 32 60C41.5 60 49 55 49 46C49 37 45 30 37 23Q32 16 27 23Z" strokeWidth="5" {...soft} />,
262
- <Eyes cx={32} cy={42} />,
263
- -8,
264
- ),
265
- shield: face(
266
- <path d="M18 18H46C52 18 54 24 54 32V36C54 48 46 56 32 58C18 56 10 48 10 36V32C10 24 12 18 18 18Z" fill={paint} />,
267
- <Eyes cx={32} cy={34} />,
268
- -6,
269
- ),
270
- nonla: <NonlaMark color={color} />,
271
- };
272
- return <InkContext.Provider value={eyeFill(color, eyeColor)}>{body[shape]}</InkContext.Provider>;
273
- }
274
-
275
- export {
276
- eyeFill,
277
- InkContext,
278
- LOOK,
279
- mixHex,
280
- PaintContext,
281
- resolveConfig as resolveAvatarConfig,
282
- ShadowContext,
283
- Shape as AvatarShape,
284
- stagger as avatarStagger,
285
- useIdleLook,
286
- };
287
-
288
- export function AgentAvatar({ config, size = 64, motion = "still", look = "forward", className, label = "Agent" }: AgentAvatarProps) {
289
- const resolved = resolveConfig(config);
290
- const delay = stagger(`${resolved.shape}${resolved.color}`);
291
- const shown = useIdleLook(motion === "idle", look, delay);
292
- const [ex, ey] = LOOK[shown];
293
- const uid = useId().replace(/:/g, "");
294
- const light = `nonla-av-${uid}`;
295
- const shadow = `nonla-av-s-${uid}`;
296
-
297
- return (
298
- <span
299
- role="img"
300
- aria-label={label}
301
- data-motion={motion}
302
- data-look={shown}
303
- className={cn("nonla-avatar", className)}
304
- style={{
305
- width: size,
306
- height: size,
307
- ["--nonla-avatar-delay" as string]: `${delay}s`,
308
- ["--nonla-avatar-ex" as string]: `${(ex / 64) * 100}%`,
309
- ["--nonla-avatar-ey" as string]: `${(ey / 64) * 100}%`,
310
- }}
311
- >
312
- <span className="nonla-avatar-bob" aria-hidden>
313
- <svg viewBox="0 0 64 64" width="100%" height="100%">
314
- <defs>
315
- <linearGradient id={light} gradientUnits="userSpaceOnUse" x1="32" y1="4" x2="32" y2="60">
316
- <stop offset="0" stopColor={mixHex(resolved.color, "#fff", 0.16)} />
317
- <stop offset="1" stopColor={resolved.color} />
318
- </linearGradient>
319
- <filter id={shadow} x="-20%" y="-20%" width="140%" height="150%">
320
- <feDropShadow dx="0" dy="0.8" stdDeviation="0.6" floodColor="#000" floodOpacity="0.18" />
321
- </filter>
322
- </defs>
323
- <PaintContext.Provider value={`url(#${light})`}>
324
- <ShadowContext.Provider value={`url(#${shadow})`}>
325
- <Shape {...resolved} />
326
- </ShadowContext.Provider>
327
- </PaintContext.Provider>
328
- </svg>
329
- </span>
330
- </span>
331
- );
332
- }