AI Welcome

Empty states for a new chat: a greeting face whose eyes follow you, a bento of animated starting points, and a centred prompt that glides down into a chat on the first send.

Installation

Installation

One command adds ai-welcome 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-welcome.json
ai-welcome-demo
components/ui/ai-welcome-demo.tsx
"use client"

import * as React from "react"
import { Bug, LayoutGrid, Sparkles, WandSparkles } from "lucide-react"

import { AiDemo } from "@/lib/ai-demo"
import {
  AiStyle,
  Face,
  Mark,
  paceToken,
  Roll,
  typeInto,
  useAiLoop,
  useIdleHands,
  type AiMood,
} from "@/lib/ai-kit"
import {
  AiHeroDock,
  AiWelcomeBento,
  AiWelcomeGreeting,
  type AiBentoTile,
  type AiStarter,
} from "@/registry/tween/ai-welcome"

const CSS = `
.aik-wld-scene{display:grid;grid-template-columns:minmax(0,1fr);gap:14px;justify-items:center;width:min(100%,580px)}
.aik-wld-scene > *{min-width:0;max-width:100%}
.aik-wld-strip{display:inline-flex;align-items:center;gap:9px;height:36px;padding:0 14px 0 8px;border-radius:18px;background:var(--aik-card);box-shadow:var(--aik-sh-card);font-size:13.5px;max-width:100%}
.aik-wld-strip .aik-roll{color:var(--aik-text-2);line-height:20px}
.aik-wld-user{align-self:flex-end;max-width:80%;padding:10px 16px;border-radius:18px;background:var(--aik-raised);font-size:15px;line-height:22px;transform-origin:100% 100%;animation:aik-wld-launch 560ms var(--aik-ease-move) both}
.aik-wld-bot{display:grid;grid-template-columns:28px minmax(0,1fr);gap:10px;align-items:start;font-size:15px;line-height:24px;animation:aik-in var(--aik-d-enter) var(--aik-ease-out) both}
.aik-wld-bot p{padding-top:2px}
@keyframes aik-wld-launch{from{opacity:.2;transform:translateY(120px) scale(.9)}}
@media (prefers-reduced-motion: reduce){.aik-wld-user{animation:none}}
`

const wait = (ms: number) =>
  new Promise<void>((resolve) => window.setTimeout(resolve, ms))

const STARTERS: AiStarter[] = [
  {
    id: "build",
    title: "Build a section",
    hint: "Pricing with a monthly toggle",
    icon: <LayoutGrid size={18} />,
  },
  {
    id: "reveal",
    title: "Add a reveal",
    hint: "Bring the hero in word by word",
    icon: <WandSparkles size={18} />,
  },
  {
    id: "fix",
    title: "Fix a component",
    hint: "Why does my slider jump?",
    icon: <Bug size={18} />,
  },
  {
    id: "polish",
    title: "Polish the motion",
    hint: "Make these eases consistent",
    icon: <Sparkles size={18} />,
  },
]
const TOUR: Array<[string, number, number]> = [
  ["build", -0.85, 0.75],
  ["reveal", 0.85, 0.75],
  ["polish", 0.85, 1],
  ["fix", -0.85, 1],
]

function Greeting() {
  const root = React.useRef<HTMLDivElement>(null)
  const [hands, touch] = useIdleHands()
  const [active, setActive] = React.useState<string | null>(null)
  const [look, setLook] = React.useState({ x: 0, y: 0 })
  const [picked, setPicked] = React.useState<AiStarter | null>(null)
  const run = React.useRef(0)

  const choose = React.useCallback(
    async (starter: AiStarter, sleep: (ms: number) => Promise<void>) => {
      const mine = ++run.current
      setPicked(starter)
      await sleep(2600)
      if (run.current === mine) setPicked(null)
    },
    []
  )

  useAiLoop(
    root,
    async ({ sleep }) => {
      run.current++
      setPicked(null)
      setActive(null)
      setLook({ x: 0, y: 0 })
      await sleep(1700)
      for (const [id, x, y] of TOUR) {
        setLook({ x, y })
        setActive(id)
        await sleep(950)
      }
      const starter = STARTERS[2]
      setActive(null)
      setLook({ x: 0, y: 0.2 })
      if (starter) await choose(starter, sleep)
      await sleep(400)
    },
    { gap: 300, enabled: !hands }
  )

  const mood: AiMood | undefined = picked ? "happy" : undefined
  return (
    <div
      ref={root}
      className="aik-wld-scene"
      onPointerDown={touch}
      onKeyDown={touch}
    >
      <AiWelcomeGreeting
        greeting={picked ? "On it." : "What are we building today?"}
        line={
          picked
            ? `Starting with “${picked.hint}”. I will show you a plan first.`
            : "Mira knows this project. Pick a start, or just ask."
        }
        starters={STARTERS}
        active={hands ? null : active}
        look={hands ? undefined : look}
        mood={mood}
        onPick={(starter) => void choose(starter, wait)}
      />
    </div>
  )
}

const TILES: AiBentoTile[] = [
  {
    id: "chart",
    title: "Chart my data",
    hint: "Paste a table and get a chart you can restyle.",
    art: "chart",
    big: true,
  },
  {
    id: "cards",
    title: "Build a section",
    hint: "Describe it and Mira lays it out from your registry.",
    art: "cards",
    big: true,
  },
  {
    id: "type",
    title: "Animate a headline",
    hint: "Word by word, on load.",
    art: "type",
  },
  {
    id: "toggle",
    title: "Add a toggle",
    hint: "Monthly or yearly.",
    art: "toggle",
  },
  {
    id: "voice",
    title: "Talk it through",
    hint: "Say it out loud.",
    art: "voice",
  },
]

function Bento() {
  const root = React.useRef<HTMLDivElement>(null)
  const [hands, touch] = useIdleHands()
  const [active, setActive] = React.useState<string | null>(null)
  const [picked, setPicked] = React.useState<string | null>(null)
  const run = React.useRef(0)

  useAiLoop(
    root,
    async ({ sleep }) => {
      run.current++
      setPicked(null)
      setActive(null)
      await sleep(1200)
      for (const tile of TILES) {
        setActive(tile.id)
        await sleep(1500)
      }
      setActive("cards")
      await sleep(900)
      setPicked("Build a section")
      await sleep(2200)
    },
    { gap: 300, enabled: !hands, still: () => setActive("chart") }
  )

  return (
    <div
      ref={root}
      className="aik-wld-scene"
      onPointerDown={touch}
      onKeyDown={touch}
    >
      <div className="aik-wld-strip">
        {picked ? (
          <Mark status="run" />
        ) : (
          <Face size={22} shape="orb" mood="idle" />
        )}
        <Roll>
          {picked ? `Mira is starting: ${picked}` : "What should we try first?"}
        </Roll>
      </div>
      <AiWelcomeBento
        tiles={TILES}
        active={hands ? null : active}
        onPick={(tile) => {
          const mine = ++run.current
          setPicked(tile.title)
          void wait(2600).then(() => {
            if (run.current === mine) setPicked(null)
          })
        }}
      />
    </div>
  )
}

const PROMPT = "Add a pricing section with a toggle"
const REPLY =
  "On it. I will reuse the cards from your registry and add a monthly and yearly toggle. Here is the plan."

function Hero() {
  const root = React.useRef<HTMLDivElement>(null)
  const [hands, touch] = useIdleHands()
  const [value, setValue] = React.useState("")
  const [docked, setDocked] = React.useState(false)
  const [working, setWorking] = React.useState(false)
  const [asked, setAsked] = React.useState("")
  const [reply, setReply] = React.useState("")
  const run = React.useRef(0)

  const send = React.useCallback(
    async (text: string, sleep: (ms: number) => Promise<void>) => {
      const mine = ++run.current
      const alive = () => run.current === mine
      setAsked(text)
      setReply("")
      setValue("")
      setDocked(true)
      setWorking(true)
      await sleep(1300)
      let out = ""
      for (const word of REPLY.split(" ")) {
        if (!alive()) return
        out += (out ? " " : "") + word
        setReply(out)
        await sleep(paceToken(word, 40))
      }
      if (!alive()) return
      setWorking(false)
      await sleep(2600)
      if (!alive()) return
      setDocked(false)
      await sleep(500)
      if (!alive()) return
      setAsked("")
      setReply("")
    },
    []
  )

  useAiLoop(
    root,
    async ({ sleep }) => {
      run.current++
      setDocked(false)
      setWorking(false)
      setValue("")
      setAsked("")
      setReply("")
      await sleep(3000)
      await typeInto(setValue, PROMPT, sleep, 34)
      await sleep(600)
      await send(PROMPT, sleep)
      await sleep(400)
    },
    { gap: 300, enabled: !hands }
  )

  return (
    <div
      ref={root}
      className="aik-wld-scene"
      onPointerDown={touch}
      onKeyDown={touch}
    >
      <AiHeroDock
        greeting="What should we make?"
        placeholders={[
          "Add a pricing section with a toggle",
          "Reveal the hero headline word by word",
          "Find out why my slider jumps",
          "Write the docs page for this component",
        ]}
        value={value}
        onValueChange={setValue}
        docked={docked}
        working={working}
        height={430}
        onSubmit={(text) => void send(text, wait)}
      >
        {asked ? (
          <div key={asked} className="aik-wld-user">
            {asked}
          </div>
        ) : null}
        {reply ? (
          <div className="aik-wld-bot">
            <Face size={28} shape="orb" mood={working ? "talk" : "happy"} />
            <p>
              {reply}
              {working ? <i className="aik-caret" /> : null}
            </p>
          </div>
        ) : null}
      </AiHeroDock>
    </div>
  )
}

export function AiWelcomeDemo({ only }: { only?: string }) {
  return (
    <>
      <AiStyle id="ai-welcome-demo" css={CSS} />
      <AiDemo
        only={only}
        tabs={[
          { name: "Greeting", node: <Greeting /> },
          { name: "Bento", node: <Bento /> },
          { name: "Hero dock", node: <Hero /> },
        ]}
      />
    </>
  )
}

Installation

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

Usage

import {
  AiHeroDock,
  AiWelcomeBento,
  AiWelcomeGreeting,
} from "@/components/ui/ai-welcome"
<AiWelcomeGreeting
  greeting="What are we building today?"
  line="Mira knows this project. Pick a start, or just ask."
  starters={[
    {
      id: "build",
      title: "Build a section",
      hint: "Pricing with a monthly toggle",
      icon: <LayoutGrid size={18} />,
    },
    // three more
  ]}
  onPick={(starter) => send(starter.hint)}
/>
const [docked, setDocked] = useState(false)
 
<AiHeroDock
  greeting="What should we make?"
  placeholders={["Add a pricing section", "Reveal the hero word by word"]}
  value={value}
  onValueChange={setValue}
  docked={docked}
  working={status === "streaming"}
  onSubmit={(text) => {
    setDocked(true)
    send(text)
  }}
>
  {messages}
</AiHeroDock>

Versions

  • AiWelcomeGreeting is an empty state with a face that watches. Its eyes follow the pointer and turn to whichever starter is hovered or focused. Pass look and active to drive it yourself.
  • AiWelcomeBento is a bento of things to try. Each tile is a button with a small illustration that plays under the pointer or on focus: a chart draws, cards fan, a line types, a switch flips, a voice meter moves.
  • AiHeroDock is the empty state that becomes the chat. One centred prompt rolls through examples. Set docked on the first send and it glides to the bottom while the thread you pass as children opens above.

Props

AiWelcomeGreeting

PropTypeDefaultDescription
greetingstringThe headline.
linestringA sentence under it.
startersAiStarter[]id, title, hint, icon. Four fill the grid.
onPick(starter: AiStarter) => voidCalled on click or Enter.
activestring | nullnullHighlights a starter, as hover does.
look{ x: number; y: number }Where the face looks, -1 to 1. Omit to follow the pointer.
moodmoodOverrides the face's own mood.
agent{ tone?, shape? }The face.

AiWelcomeBento

PropTypeDefaultDescription
tilesAiBentoTile[]id, title, hint, art, big. Two big and three small.
onPick(tile: AiBentoTile) => voidCalled on click or Enter.
activestring | nullnullPlays a tile's illustration, as hover does.

art is "chart", "cards", "type", "toggle", "voice", or your own node.

AiHeroDock

PropTypeDefaultDescription
greetingstringThe headline over the centred prompt.
placeholdersstring[]Examples the empty field rolls through.
valuestringThe field's text.
onValueChange(value: string) => voidCalled on every edit.
onSubmit(value: string) => voidCalled on Enter or send.
dockedbooleanTrue glides the prompt to the bottom and shows children.
workingbooleanfalseRuns the edge light round the dock.
heightnumber420The frame's height in pixels.