devnonla-ui 0.18.1 → 0.19.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.
@@ -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
- }