AI Face

The agent character: a CSS face with nine moods, five shapes and any colour, as a portrait, a presence pill with a status ring, and a cursor tag for shared canvases.

Installation

Installation

One command adds ai-face and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.

$npx shadcn@latest add https://tweenui.dev/r/ai-face.json
ai-face-demo
components/ui/ai-face-demo.tsx
"use client"

import * as React from "react"

import { AiDemo } from "@/lib/ai-demo"
import {
  AiStyle,
  Face,
  toneColor,
  useAiLoop,
  type AiMood,
  type AiShape,
  type AiStatus,
  type AiTone,
} from "@/lib/ai-kit"
import { AiAvatar, AiCursor, AiPresence } from "@/registry/tween/ai-face"

const CSS = `
.aik-fc-moods{display:grid;grid-template-columns:minmax(0,260px) minmax(0,1fr);gap:28px;align-items:center;width:min(100%,820px)}
.aik-fc-hero{display:grid;justify-items:center;gap:16px;text-align:center}
.aik-fc-cap{display:grid;gap:2px;min-height:42px}
.aik-fc-cap b{font:600 15px/20px var(--aik-sans);letter-spacing:-.01em;text-transform:capitalize}
.aik-fc-cap span{color:var(--aik-text-2);font-size:13px;line-height:18px}
.aik-fc-pick{display:grid;gap:10px;justify-items:center}
.aik-fc-dots{display:flex;gap:8px}
.aik-fc-dots button{inline-size:20px;block-size:20px;border-radius:50%;background:var(--aik-fc);box-shadow:inset 0 0 0 1px rgb(0 0 0 / 12%);transition:transform var(--aik-d-press) var(--aik-ease-out),box-shadow var(--aik-d-state)}
.aik-fc-dots button:active{transform:scale(.9)}
.aik-fc-dots button[aria-pressed="true"]{box-shadow:0 0 0 2px var(--aik-panel),0 0 0 3.5px var(--aik-fc)}
.aik-fc-shapes button{padding:0 7px}
.aik-fc-shapes .aik-face{--aik-fs:16px;--aik-fc:var(--aik-text-3)}
.aik-fc-shapes .aik-face::after{display:none}
.aik-fc-shapes [aria-pressed="true"] .aik-face{--aik-fc:var(--aik-text)}
.aik-fc-grid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:6px}
.aik-fc-grid button{display:grid;justify-items:center;gap:8px;width:100%;padding:14px 6px 10px;border-radius:14px;color:var(--aik-text-2);font-size:12.5px;font-weight:500;text-transform:capitalize;
  transition:background var(--aik-d-state),color var(--aik-d-state),box-shadow var(--aik-d-state),transform var(--aik-d-press) var(--aik-ease-out)}
.aik-fc-grid button:hover{background:var(--aik-hover)}
.aik-fc-grid button:active{transform:scale(.97)}
.aik-fc-grid button[aria-pressed="true"]{background:var(--aik-raised);color:var(--aik-text);box-shadow:inset 0 0 0 1px var(--aik-line-2)}
.aik-fc-grid .aik-face{--aik-fs:44px}
@container (max-width:600px){.aik-fc-moods{grid-template-columns:minmax(0,1fr);gap:20px}.aik-fc-grid .aik-face{--aik-fs:38px}}
.aik-fc-pres{display:grid;gap:26px;justify-items:center;width:100%}
.aik-fc-team{display:flex;gap:22px;flex-wrap:wrap;justify-content:center}
.aik-fc-canvas{position:relative;width:min(100%,400px);height:236px;border-radius:16px;background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line);container-type:inline-size;overflow:hidden}
.aik-fc-blk{position:absolute;display:grid;gap:7px;align-content:start;padding:6px;border-radius:9px;box-shadow:0 0 0 1.5px transparent;transition:box-shadow var(--aik-d-state) var(--aik-ease-out),background var(--aik-d-state)}
.aik-fc-blk i{display:block;height:8px;border-radius:4px;background:var(--aik-line-3)}
.aik-fc-blk[data-t="h"] i{height:13px;border-radius:5px;background:var(--aik-text-3)}
.aik-fc-blk[data-t="img"]{background:var(--aik-line-2);padding:0;overflow:hidden}
.aik-fc-blk[data-t="img"]::after{content:"";position:absolute;inset:0;opacity:0;transition:opacity var(--aik-d-move) var(--aik-ease-out);
  background:radial-gradient(80% 90% at 20% 20%,var(--aik-f-amber),transparent 60%),radial-gradient(90% 90% at 85% 80%,var(--aik-f-rose),transparent 62%),var(--aik-f-violet)}
.aik-fc-blk[data-t="img"][data-made]::after{opacity:1}
.aik-fc-blk[data-t="btn"]{background:var(--aik-text);border-radius:9px;place-items:center;padding:0}
.aik-fc-blk[data-t="btn"] i{inline-size:46%;height:7px;background:var(--aik-panel)}
.aik-fc-blk[data-on]{box-shadow:0 0 0 1.5px var(--on);background:color-mix(in oklab,var(--on) 9%,transparent)}
.aik-fc-blk[data-t="img"][data-on],.aik-fc-blk[data-t="btn"][data-on]{box-shadow:0 0 0 2px var(--aik-panel),0 0 0 3.5px var(--on)}
.aik-fc-blk[data-t="btn"][data-on]{background:var(--aik-text)}
.aik-fc-blk[data-on][data-busy] i{animation:aik-fc-sweep 1s ease-in-out infinite alternate}
.aik-fc-blk[data-on][data-busy] i:nth-child(2){animation-delay:.12s}
.aik-fc-blk[data-on][data-busy] i:nth-child(3){animation-delay:.24s}
@keyframes aik-fc-sweep{to{opacity:.35}}
.aik-fc-canvas{position:relative}
.aik-fc-shapes{display:inline-flex;padding:2px;border-radius:10px;background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line)}
.aik-fc-shapes button{display:grid;place-items:center;height:28px;border-radius:8px;transition:background var(--aik-d-state),transform var(--aik-d-press)}
.aik-fc-shapes button:active{transform:scale(.94)}
.aik-fc-shapes button[aria-pressed="true"]{background:var(--aik-card);box-shadow:var(--aik-sh-card)}
.aik-fc-shapes i{display:block;width:16px;height:16px;border-radius:32%;background:var(--aik-text-3);transition:background var(--aik-d-state)}
.aik-fc-shapes [aria-pressed="true"] i{background:var(--aik-text)}
.aik-fc-shapes i[data-shape="orb"]{border-radius:50%}
.aik-fc-shapes i[data-shape="blob"]{border-radius:58% 42% 46% 54%/52% 60% 40% 48%}
.aik-fc-shapes i[data-shape="arch"]{border-radius:50% 50% 30% 30%/60% 60% 30% 30%}
.aik-fc-shapes i[data-shape="leaf"]{border-radius:62% 38% 62% 38%/58% 42% 58% 42%}
`

const MOODS: Array<[AiMood, string]> = [
  ["idle", "Glances around, blinks now and then"],
  ["think", "Looks up and away while it reasons"],
  ["work", "Squints and scans from side to side"],
  ["needs", "Eyes wide, looking straight at you"],
  ["listen", "Tall, attentive eyes while you speak"],
  ["talk", "Eyes bounce in time with the reply"],
  ["happy", "Smiling eyes when a run lands"],
  ["oops", "A flat wince and a small shake"],
  ["sleep", "Closed and breathing when paused"],
]
const TONES: AiTone[] = [
  "blue",
  "violet",
  "green",
  "amber",
  "rose",
  "teal",
  "ink",
]
const SHAPES: AiShape[] = ["blob", "squircle", "orb", "arch", "leaf"]

function MoodSet() {
  const root = React.useRef<HTMLDivElement>(null)
  const [index, setIndex] = React.useState(0)
  const [picked, setPicked] = React.useState(false)
  const [tone, setTone] = React.useState<AiTone>("blue")
  const [shape, setShape] = React.useState<AiShape>("blob")
  const [mood, line] = MOODS[index] ?? MOODS[0]!

  useAiLoop(
    root,
    async ({ sleep }) => {
      await sleep(700)
      setIndex((current) => (current + 1) % MOODS.length)
    },
    { gap: 1500, enabled: !picked, still: () => setIndex(2) }
  )

  return (
    <div className="aik-fc-moods" ref={root}>
      <div className="aik-fc-hero">
        <Face mood={mood} shape={shape} tone={tone} size={112} />
        <div className="aik-fc-cap">
          <b>{mood}</b>
          <span>{line}</span>
        </div>
        <div className="aik-fc-pick">
          <div className="aik-fc-dots" role="group" aria-label="Colour">
            {TONES.map((item) => (
              <button
                key={item}
                type="button"
                aria-label={item}
                aria-pressed={item === tone}
                style={{ "--aik-fc": toneColor(item) } as React.CSSProperties}
                onClick={() => setTone(item)}
              />
            ))}
          </div>
          <div className="aik-fc-shapes" role="group" aria-label="Shape">
            {SHAPES.map((item) => (
              <button
                key={item}
                type="button"
                aria-label={item}
                aria-pressed={item === shape}
                onClick={() => setShape(item)}
              >
                <i data-shape={item} />
              </button>
            ))}
          </div>
        </div>
      </div>
      <div className="aik-fc-grid" role="group" aria-label="Mood">
        {MOODS.map(([item], at) => (
          <button
            key={item}
            type="button"
            aria-pressed={at === index}
            onClick={() => {
              setPicked(true)
              setIndex(at)
            }}
          >
            <Face mood={item} shape={shape} tone={tone} size={44} />
            <span>{item}</span>
          </button>
        ))}
      </div>
    </div>
  )
}

interface Member {
  status: AiStatus
  label: string
  mood?: AiMood
}
const TEAM = [
  { name: "Odo", tone: "violet", shape: "blob" },
  { name: "Juno", tone: "amber", shape: "arch" },
  { name: "Pim", tone: "green", shape: "leaf" },
] as const

const PRESENCE_STEPS: Array<{
  pill: [AiStatus, string, AiMood?]
  team: [Member, Member, Member]
}> = [
  {
    pill: ["run", "Thinking", "think"],
    team: [
      { status: "run", label: "Writing tests" },
      { status: "todo", label: "Queued" },
      { status: "run", label: "Planning", mood: "think" },
    ],
  },
  {
    pill: ["run", "Reading 4 files"],
    team: [
      { status: "run", label: "Writing tests" },
      { status: "todo", label: "Queued" },
      { status: "run", label: "Editing" },
    ],
  },
  {
    pill: ["ask", "Needs your OK"],
    team: [
      { status: "run", label: "Writing tests" },
      { status: "run", label: "Reviewing" },
      { status: "run", label: "Editing" },
    ],
  },
  {
    pill: ["run", "Shipping the fix"],
    team: [
      { status: "done", label: "Done" },
      { status: "ask", label: "Needs you" },
      { status: "run", label: "Editing" },
    ],
  },
  {
    pill: ["done", "Done in 0:16"],
    team: [
      { status: "done", label: "Done" },
      { status: "ask", label: "Needs you" },
      { status: "done", label: "Done" },
    ],
  },
]

function Presence() {
  const root = React.useRef<HTMLDivElement>(null)
  const [step, setStep] = React.useState(0)
  const [seconds, setSeconds] = React.useState(0)

  useAiLoop(
    root,
    async ({ sleep }) => {
      for (let at = 0; at < PRESENCE_STEPS.length; at++) {
        setStep(at)
        for (let tick = 0; tick < 4; tick++) {
          setSeconds(at * 4 + tick)
          await sleep(520)
        }
      }
    },
    { gap: 900, still: () => setStep(2) }
  )

  const current = PRESENCE_STEPS[step] ?? PRESENCE_STEPS[0]!
  const [status, label, mood] = current.pill
  return (
    <div className="aik-fc-pres" ref={root}>
      <AiPresence
        name="Mira"
        status={status}
        label={label}
        mood={mood}
        time={
          status === "done"
            ? undefined
            : `0:${String(seconds).padStart(2, "0")}`
        }
      />
      <div className="aik-fc-team">
        {TEAM.map((agent, at) => {
          const member = current.team[at]!
          return (
            <AiAvatar
              key={agent.name}
              name={agent.name}
              tone={agent.tone}
              shape={agent.shape}
              status={member.status}
              label={member.label}
              mood={member.mood}
            />
          )
        })}
      </div>
    </div>
  )
}

type Block = "h" | "p" | "img" | "btn"
interface Scene {
  mira: { x: string; y: number; task: string; mood?: AiMood; ask?: boolean }
  odo: { x: string; y: number; task: string; mood?: AiMood }
  on: Partial<Record<Block, string>>
  busy?: Block
  made?: boolean
}
const SCENES: Array<[number, Scene]> = [
  [
    1500,
    {
      mira: { x: "30cqw", y: 26, task: "Reading the hero", mood: "think" },
      odo: { x: "26cqw", y: 196, task: "Making the image" },
      on: { h: "blue", img: "violet" },
    },
  ],
  [
    1700,
    {
      mira: { x: "30cqw", y: 26, task: "Rewriting the headline" },
      odo: { x: "26cqw", y: 196, task: "Image ready", mood: "happy" },
      on: { h: "blue" },
      busy: "h",
      made: true,
    },
  ],
  [
    2100,
    {
      mira: { x: "40cqw", y: 92, task: "Tightening the copy" },
      odo: { x: "12cqw", y: 150, task: "Checking contrast", mood: "think" },
      on: { p: "blue" },
      busy: "p",
      made: true,
    },
  ],
  [
    2400,
    {
      mira: {
        x: "60cqw",
        y: 172,
        task: "Publish this?",
        mood: "needs",
        ask: true,
      },
      odo: { x: "12cqw", y: 150, task: "Waiting for Mira", mood: "idle" },
      on: { btn: "amber" },
      made: true,
    },
  ],
]

function Cursors() {
  const root = React.useRef<HTMLDivElement>(null)
  const [at, setAt] = React.useState(0)

  useAiLoop(
    root,
    async ({ sleep }) => {
      for (let index = 0; index < SCENES.length; index++) {
        setAt(index)
        await sleep(SCENES[index]![0])
      }
    },
    { gap: 700, still: () => setAt(2) }
  )

  const scene = (SCENES[at] ?? SCENES[0]!)[1]
  const block = (kind: Block) => {
    const color = scene.on[kind]
    return {
      "data-t": kind,
      "data-on": color ? "" : undefined,
      "data-busy": scene.busy === kind ? "" : undefined,
      "data-made": kind === "img" && scene.made ? "" : undefined,
    } as const
  }
  const tint = (kind: Block): React.CSSProperties | undefined => {
    const color = scene.on[kind]
    if (!color) return undefined
    return {
      "--on": color === "amber" ? "var(--aik-amber)" : toneColor(color),
    } as React.CSSProperties
  }

  return (
    <div className="aik-fc-canvas" ref={root}>
      <div
        className="aik-fc-blk"
        {...block("h")}
        style={{ left: "6%", top: 16, width: "58%", ...tint("h") }}
      >
        <i />
      </div>
      <div
        className="aik-fc-blk"
        {...block("p")}
        style={{ left: "6%", top: 50, width: "84%", ...tint("p") }}
      >
        <i />
        <i />
        <i style={{ width: "64%" }} />
      </div>
      <div
        className="aik-fc-blk"
        {...block("img")}
        style={{
          left: "6%",
          top: 118,
          width: "42%",
          height: 96,
          ...tint("img"),
        }}
      />
      <div
        className="aik-fc-blk"
        {...block("btn")}
        style={{
          left: "56%",
          top: 150,
          width: "32%",
          height: 34,
          ...tint("btn"),
        }}
      >
        <i />
      </div>
      <AiCursor
        name="Mira"
        task={scene.mira.task}
        x={scene.mira.x}
        y={scene.mira.y}
        mood={scene.mira.mood ?? "work"}
        status={scene.mira.ask ? "ask" : "run"}
      />
      <AiCursor
        name="Odo"
        tone="violet"
        shape="blob"
        task={scene.odo.task}
        x={scene.odo.x}
        y={scene.odo.y}
        mood={scene.odo.mood ?? "work"}
      />
    </div>
  )
}

export function AiFaceDemo({ only }: { only?: string }) {
  return (
    <>
      <AiStyle id="ai-face-demo" css={CSS} />
      <AiDemo
        only={only}
        tabs={[
          { name: "Mood set", node: <MoodSet /> },
          { name: "Presence", node: <Presence /> },
          { name: "Cursor tag", node: <Cursors /> },
        ]}
      />
    </>
  )
}

Installation

pnpm dlx shadcn@latest add https://tweenui.dev/r/ai-face.json

Usage

import { AiAvatar, AiCursor, AiFace, AiPresence } from "@/components/ui/ai-face"
<AiFace mood="think" shape="blob" tone="violet" size={64} />
 
<AiPresence name="Mira" status="run" label="Reading 4 files" time="0:04" />
 
<AiAvatar name="Odo" tone="violet" status="ask" label="Needs you" />
 
<div className="relative h-60">
  <AiCursor name="Mira" task="Rewriting the headline" x={120} y={26} />
</div>

Versions

  • AiFace is the character on its own. The eyes are one masked element, so it stays crisp from 16px to a full portrait.
  • AiPresence is a status pill. The ring spins while working, turns dashed and amber when the agent needs you, and the label rolls over in place.
  • AiAvatar adds a status mark on the corner, a name and a short label.
  • AiCursor is an agent as a cursor with a name tag. It glides to each new x and y.

When you pass only a status, the face picks a matching mood: working squints and scans, needs-you looks straight at you, done smiles, failed winces, queued sleeps.

Props

AiFace

PropTypeDefaultDescription
mood"idle" | "think" | "work" | "needs" | "listen" | "talk" | "happy" | "oops" | "sleep""idle"What the eyes act out.
shape"blob" | "squircle" | "orb" | "arch" | "leaf""blob"Body shape.
tone"blue" | "violet" | "green" | "amber" | "rose" | "teal" | "ink" | string"blue"A named tone or any CSS colour.
sizenumber40Edge length in pixels.

AiPresence

PropTypeDefaultDescription
namestringThe agent's name.
status"todo" | "run" | "ask" | "done" | "fail"Drives the ring, the colour and the default mood.
labelstringWhat the agent is doing. Rolls when it changes.
timestringElapsed time, already formatted.
moodmoodOverrides the mood derived from status.

AiAvatar takes name, status, an optional label, and size. AiCursor takes name, task, x, y (numbers are pixels) and an optional status. All four accept tone and shape.