AI Notch
Ways a run reaches you: a notch that grows into a status card, a toast stack for done, needs-you and failed, and an inline reconnect banner.
"use client"
import * as React from "react"
import { RefreshCw, X } from "lucide-react"
import {
AiKitStyles,
AiStyle,
cx,
Face,
Mark,
Roll,
type AiMood,
type AiShape,
type AiStatus,
type AiTone,
} from "@/lib/ai-kit"
const CSS = `
.aik-nt-notch{--nt-c:var(--aik-f-blue);position:absolute;left:50%;top:4px;translate:-50% 0;inline-size:min(268px,calc(100% - 24px));display:grid;justify-items:center}
.aik-nt-notch[data-s="ask"]{--nt-c:var(--aik-f-amber)}
.aik-nt-notch[data-s="done"]{--nt-c:var(--aik-f-green)}
.aik-nt-notch[data-s="todo"]{--nt-c:rgb(255 255 255 / 45%)}
.aik-nt-nub,.aik-nt-card{grid-area:1/1;background:#000;color:#fff;box-shadow:0 0 0 1px var(--aik-line-2),0 8px 24px -8px rgb(0 0 0 / 45%);transform-origin:50% 0}
.aik-nt-nub{display:flex;align-items:center;gap:7px;height:26px;padding:0 9px 0 5px;border-radius:13px;font-size:12px;font-weight:500;
transition:opacity var(--aik-d-state) var(--aik-ease-out),transform var(--aik-d-panel) var(--aik-ease-soft)}
.aik-nt-nub .aik-face{--aik-fs:16px}
.aik-nt-spin{inline-size:11px;block-size:11px;border-radius:50%;border:1.5px solid rgb(255 255 255 / 22%);border-top-color:var(--nt-c);animation:aik-spin .8s linear infinite}
.aik-nt-notch:not([data-s="run"]) .aik-nt-spin{animation:none;border-color:var(--nt-c);background:var(--nt-c);inline-size:7px;block-size:7px;border-width:0;margin:0 2px}
.aik-nt-card{inline-size:100%;display:grid;gap:11px;padding:11px 14px 13px 11px;border-radius:21px;opacity:0;transform:scale(.42,.3);
transition:opacity var(--aik-d-exit) var(--aik-ease-in),transform var(--aik-d-panel) var(--aik-ease-soft)}
.aik-nt-win[data-open] .aik-nt-card{opacity:1;transform:none;transition:opacity var(--aik-d-state) var(--aik-ease-out),transform var(--aik-d-panel) var(--aik-ease-soft)}
.aik-nt-win[data-open] .aik-nt-nub{opacity:0;transform:scale(1.5,1.3)}
.aik-nt-crow{display:flex;align-items:center;gap:10px;min-width:0}
.aik-nt-crow .aik-face{--aik-fs:30px}
.aik-nt-ctext{display:grid;flex:1;min-width:0}
.aik-nt-ctext b{font-weight:600;font-size:13px;line-height:17px}
.aik-nt-ctext .aik-roll{font-size:12.5px;line-height:17px;color:var(--nt-c);font-weight:500;transition:color var(--aik-d-state)}
.aik-nt-time{font:11.5px var(--aik-mono);color:rgb(255 255 255 / 55%);font-variant-numeric:tabular-nums}
.aik-nt-line{height:2.5px;border-radius:2px;background:rgb(255 255 255 / 16%);overflow:hidden;margin-left:3px}
.aik-nt-line i{display:block;height:100%;border-radius:2px;background:var(--nt-c);transform:scaleX(var(--p,0));transform-origin:0 50%;transition:transform var(--aik-d-move) var(--aik-ease-out),background var(--aik-d-state)}
@media (prefers-reduced-motion: reduce){.aik-nt-spin{animation:none}}
.aik-nt-stack{position:relative;width:min(100%,340px);height:206px}
.aik-nt-toast{position:absolute;left:0;right:0;bottom:4px;height:58px;display:flex;align-items:center;gap:10px;padding:0 10px 0 12px;border-radius:14px;background:var(--aik-card);box-shadow:var(--aik-sh-pop);
transform-origin:50% 100%;transition:transform var(--aik-d-panel) var(--aik-ease-soft),opacity var(--aik-d-state) var(--aik-ease-out);animation:aik-nt-rise var(--aik-d-panel) var(--aik-ease-out) backwards}
.aik-nt-toast[data-pos="1"]{transform:translateY(-66px) scale(.97);opacity:.9}
.aik-nt-toast[data-pos="2"]{transform:translateY(-132px) scale(.94);opacity:.62}
.aik-nt-toast[data-leave]{opacity:0;transform:translateY(calc(var(--at,0)*-66px + 10px)) scale(.94);transition:transform var(--aik-d-exit) var(--aik-ease-in),opacity var(--aik-d-exit) var(--aik-ease-in)}
.aik-nt-tt{display:grid;flex:1;min-width:0}
.aik-nt-tt b{font-weight:600;font-size:13px;line-height:18px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-nt-tt span{font-size:12.5px;line-height:17px;color:var(--aik-text-2);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-nt-stack.aik-nt-rest .aik-nt-toast{animation:none}
@keyframes aik-nt-rise{from{opacity:0;transform:translateY(26px) scale(.98)}}
.aik-nt-ban{display:flex;flex-wrap:wrap;align-items:center;gap:8px 10px;padding:10px 10px 10px 12px;border-radius:13px;
background:color-mix(in oklab,var(--aik-c) 9%,transparent);box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--aik-c) 32%,transparent);
transition:background var(--aik-d-state),box-shadow var(--aik-d-state),opacity var(--aik-d-exit) var(--aik-ease-in),transform var(--aik-d-exit) var(--aik-ease-in)}
.aik-nt-ban[data-gone]{opacity:0;transform:translateY(-4px)}
.aik-nt-btext{display:grid;flex:1 1 170px;min-width:0}
.aik-nt-btext .aik-roll{font-weight:600;font-size:13px;line-height:18px;color:var(--aik-c);transition:color var(--aik-d-state)}
.aik-nt-btext > span:last-child{font-size:12.5px;line-height:17px;color:var(--aik-text-2);font-variant-numeric:tabular-nums}
.aik-nt-btext code{font:12px var(--aik-mono)}
.aik-nt-retry{background:var(--aik-card);box-shadow:var(--aik-sh-card)}
.aik-nt-retry.aik-nt-press{transform:scale(.95)}
.aik-nt-ban[data-s="run"] .aik-nt-retry{color:var(--aik-text-3);pointer-events:none}
.aik-nt-ban[data-s="run"] .aik-nt-retry .ic{animation:aik-spin .9s linear infinite}
.aik-nt-ban[data-s="done"] .aik-nt-retry{display:none}
@media (prefers-reduced-motion: reduce){.aik-nt-ban[data-s="run"] .aik-nt-retry .ic{animation:none}}
.aik-nt-notch{position:relative;left:auto;top:auto;translate:none;inline-size:min(268px,100%)}
.aik-nt-notch[data-s="fail"]{--nt-c:var(--aik-f-rose)}
button.aik-nt-nub{border:0;cursor:pointer;font-family:inherit;z-index:1}
button.aik-nt-nub:active{transform:scale(.96)}
.aik-nt-nub span{white-space:nowrap;overflow:hidden;text-overflow:ellipsis;max-width:150px}
.aik-nt-card{cursor:pointer;text-align:left}
.aik-nt-notch:not([data-open]) .aik-nt-card{pointer-events:none;visibility:hidden;transition:opacity var(--aik-d-exit) var(--aik-ease-in),transform var(--aik-d-panel) var(--aik-ease-soft),visibility 0s var(--aik-d-panel)}
.aik-nt-notch[data-open] .aik-nt-card{opacity:1;transform:none;transition:opacity var(--aik-d-state) var(--aik-ease-out),transform var(--aik-d-panel) var(--aik-ease-soft)}
.aik-nt-notch[data-open] .aik-nt-nub{opacity:0;transform:scale(1.5,1.3);pointer-events:none}
.aik-nt-ctext b{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.aik-nt-ctext .aik-roll{min-width:0;overflow:hidden}
.aik-nt-stack:empty{height:0}
.aik-nt-x{position:absolute;right:-7px;top:-7px;display:grid;place-items:center;inline-size:20px;block-size:20px;border-radius:50%;background:var(--aik-card);box-shadow:var(--aik-sh-card);color:var(--aik-text-2);opacity:0;transition:opacity var(--aik-d-state),background var(--aik-d-state),color var(--aik-d-state)}
.aik-nt-toast:hover .aik-nt-x,.aik-nt-x:focus-visible{opacity:1}
.aik-nt-x:hover{background:var(--aik-raised);color:var(--aik-text)}
@media (hover:none){.aik-nt-toast[data-pos="0"] .aik-nt-x{opacity:1}}
.aik-nt-toast:not([data-pos="0"]) .aik-nt-x,.aik-nt-toast:not([data-pos="0"]) .aik-btn{pointer-events:none}
.aik-nt-ban{width:100%}
.aik-nt-ban[data-gone]{pointer-events:none}
.aik-nt-btext .aik-roll{min-width:0;overflow:hidden}
.aik-nt-btext code{font-variant-numeric:tabular-nums}
.aik-nt-ban[data-s="run"] .aik-nt-retry svg{animation:aik-spin .9s linear infinite}
@media (prefers-reduced-motion: reduce){.aik-nt-ban[data-s="run"] .aik-nt-retry svg{animation:none}.aik-nt-toast{animation:none}}
`
const clock = (seconds: number) =>
`${Math.floor(seconds / 60)}:${String(Math.max(0, Math.floor(seconds % 60))).padStart(2, "0")}`
const STATUS_MOOD: Record<AiStatus, AiMood> = {
todo: "sleep",
run: "work",
ask: "needs",
done: "happy",
fail: "oops",
}
export interface AiNotchProps extends Omit<
React.HTMLAttributes<HTMLDivElement>,
"children"
> {
/** The agent's name, shown on the pill and the card. */
name: string
status: AiStatus
/** What the agent is doing. Rolls over in place when it changes. */
label: string
/** Elapsed time, already formatted. */
time?: string
/** Progress from 0 to 1, drawn as a line under the card. */
progress?: number
/** Whether the card is showing. Leave undefined to let the notch manage it. */
open?: boolean
defaultOpen?: boolean
onOpenChange?: (open: boolean) => void
/** Replaces the name on the closed pill, such as "Done". */
nubLabel?: string
/** Face colour: a named tone or any CSS colour. */
tone?: AiTone | (string & {})
shape?: AiShape
/** Overrides the mood derived from `status`. */
mood?: AiMood
}
/** A small dark pill that grows into a status card and folds back. Opens on click, closes on Escape. */
export function AiNotch({
name,
status,
label,
time,
progress,
open,
defaultOpen = false,
onOpenChange,
nubLabel,
tone = "blue",
shape = "orb",
mood,
className,
onKeyDown,
...rest
}: AiNotchProps) {
const [own, setOwn] = React.useState(defaultOpen)
const shown = open ?? own
const cardId = React.useId()
const set = (next: boolean) => {
if (open === undefined) setOwn(next)
onOpenChange?.(next)
}
const look = mood ?? STATUS_MOOD[status]
return (
<div
{...rest}
className={cx("aik aik-nt-notch", className)}
data-s={status}
data-open={shown ? "" : undefined}
onKeyDown={(event) => {
onKeyDown?.(event)
if (event.key === "Escape" && shown) set(false)
}}
>
<AiKitStyles />
<AiStyle id="ai-notch" css={CSS} />
<button
type="button"
className="aik-nt-nub"
aria-expanded={shown}
aria-controls={cardId}
aria-label={`${name}: ${label}`}
tabIndex={shown ? -1 : 0}
onClick={() => set(true)}
>
<Face size={16} mood={look} shape={shape} tone={tone} />
<span>{nubLabel ?? name}</span>
<i className="aik-nt-spin" />
</button>
<div
id={cardId}
className="aik-nt-card"
role="button"
tabIndex={shown ? 0 : -1}
aria-label={`${name}: ${label}. Press to collapse.`}
onClick={() => set(false)}
onKeyDown={(event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault()
set(false)
}
}}
>
<div className="aik-nt-crow">
<Face size={30} mood={look} shape={shape} tone={tone} />
<div className="aik-nt-ctext" role="status">
<b>{name}</b>
<Roll>{label}</Roll>
</div>
{time ? <span className="aik-nt-time">{time}</span> : null}
</div>
{progress === undefined ? null : (
<div className="aik-nt-line">
<i
style={
{
"--p": Math.min(1, Math.max(0, progress)),
} as React.CSSProperties
}
/>
</div>
)}
</div>
</div>
)
}
export interface AiToast {
id: string
status: AiStatus
title: string
detail?: string
/** Label for an action button, such as "Approve". */
action?: string
}
export interface AiToastsProps extends Omit<
React.HTMLAttributes<HTMLDivElement>,
"children"
> {
/** Oldest first. The last toast sits at the front; up to three show. */
toasts: AiToast[]
onDismiss?: (id: string) => void
onAction?: (id: string) => void
}
const VISIBLE = 3
const LEAVE_MS = 220
/** A stack of run notifications. New ones rise in at the front and push older ones up and back. */
export function AiToasts({
toasts,
onDismiss,
onAction,
className,
...rest
}: AiToastsProps) {
const [leaving, setLeaving] = React.useState<
Array<{ toast: AiToast; pos: number }>
>([])
const previous = React.useRef(toasts)
const timers = React.useRef(new Set<number>())
React.useEffect(() => {
const before = previous.current
previous.current = toasts
const gone = before
.map((toast, index) => ({ toast, pos: before.length - 1 - index }))
.filter(
(entry) =>
entry.pos < VISIBLE &&
!toasts.some((toast) => toast.id === entry.toast.id)
)
if (gone.length === 0) return
setLeaving((list) => [...list, ...gone])
const timer = window.setTimeout(() => {
timers.current.delete(timer)
setLeaving((list) => list.filter((entry) => !gone.includes(entry)))
}, LEAVE_MS)
timers.current.add(timer)
}, [toasts])
React.useEffect(() => {
const pending = timers.current
return () => pending.forEach((timer) => window.clearTimeout(timer))
}, [])
const live = toasts
.map((toast, index) => ({ toast, pos: toasts.length - 1 - index }))
.filter((entry) => entry.pos < VISIBLE)
const gone = leaving.filter(
(entry) => !toasts.some((toast) => toast.id === entry.toast.id)
)
const render = (toast: AiToast, pos: number, leave: boolean) => (
<div
key={toast.id}
className="aik-nt-toast"
data-pos={pos}
data-leave={leave ? "" : undefined}
role={toast.status === "fail" ? "alert" : "status"}
aria-hidden={leave || undefined}
style={leave ? ({ "--at": pos } as React.CSSProperties) : undefined}
>
<Mark status={toast.status} />
<div className="aik-nt-tt">
<b>{toast.title}</b>
{toast.detail ? <span>{toast.detail}</span> : null}
</div>
{toast.action ? (
<button
type="button"
className={cx("aik-btn sm", toast.status === "ask" ? "pri" : "ghost")}
tabIndex={pos === 0 && !leave ? 0 : -1}
onClick={() => onAction?.(toast.id)}
>
{toast.action}
</button>
) : null}
{onDismiss ? (
<button
type="button"
className="aik-nt-x"
aria-label={`Dismiss: ${toast.title}`}
tabIndex={pos === 0 && !leave ? 0 : -1}
onClick={() => onDismiss(toast.id)}
>
<X size={12} />
</button>
) : null}
</div>
)
return (
<div
{...rest}
className={cx("aik aik-nt-stack", className)}
role="region"
aria-label="Notifications"
>
<AiKitStyles />
<AiStyle id="ai-notch" css={CSS} />
{gone.map((entry) => render(entry.toast, entry.pos, true))}
{live.map((entry) => render(entry.toast, entry.pos, false))}
</div>
)
}
export interface AiBannerProps extends Omit<
React.HTMLAttributes<HTMLDivElement>,
"title"
> {
/** `fail` and `ask` wait for the person, `run` is reconnecting, `done` has recovered. */
status: AiStatus
title: string
/** A line under the title. */
detail?: React.ReactNode
/** Seconds until an automatic retry. Shown as a countdown after the detail. */
retryIn?: number
/** The button's label. Hidden once `status` is `done`. */
retryLabel?: string
onRetry?: () => void
/** Fades the banner out without removing it from the layout. */
hidden?: boolean
}
/** Trouble shown where it happened: a dropped stream or a rate limit, with a way back. */
export function AiBanner({
status,
title,
detail,
retryIn,
retryLabel = "Retry",
onRetry,
hidden = false,
className,
...rest
}: AiBannerProps) {
return (
<div
{...rest}
className={cx("aik aik-nt-ban", className)}
data-s={status}
data-gone={hidden ? "" : undefined}
aria-hidden={hidden || undefined}
role={status === "fail" ? "alert" : "status"}
>
<AiKitStyles />
<AiStyle id="ai-notch" css={CSS} />
<Mark status={status} />
<div className="aik-nt-btext">
<Roll>{title}</Roll>
<span>
{detail}
{retryIn === undefined ? null : (
<>
{detail ? " " : null}Retrying on its own in{" "}
<code>{clock(retryIn)}</code>
</>
)}
</span>
</div>
{onRetry ? (
<button
type="button"
className="aik-btn sm aik-nt-retry"
disabled={status === "run" || hidden}
onClick={onRetry}
>
<RefreshCw size={14} />
<span>{status === "run" ? "Retrying" : retryLabel}</span>
</button>
) : null}
</div>
)
}
Installation
One command adds ai-notch 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-notch.json
ai-notch-demo
"use client"
import * as React from "react"
import { AiDemo } from "@/lib/ai-demo"
import {
AiStyle,
paceToken,
useAiLoop,
type AiStatus,
} from "@/lib/ai-kit"
import {
AiBanner,
AiNotch,
AiToasts,
type AiToast,
} from "@/registry/tween/ai-notch"
const CSS = `
.aik-nt-win{position:relative;width:min(100%,380px);height:232px;border-radius:16px;background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line);overflow:hidden}
.aik-nt-chrome{display:flex;gap:6px;align-items:center;height:34px;padding:0 13px;border-bottom:1px solid var(--aik-line)}
.aik-nt-chrome i{inline-size:9px;block-size:9px;border-radius:50%;background:var(--aik-line-3)}
.aik-nt-page{display:grid;gap:10px;padding:26px 22px}
.aik-nt-page i{display:block;height:9px;border-radius:5px;background:var(--aik-line-2)}
.aik-nt-page i:first-child{height:15px;background:var(--aik-line-3);margin-bottom:6px}
.aik-nt-win[data-open] .aik-nt-card{opacity:1;transform:none;transition:opacity var(--aik-d-state) var(--aik-ease-out),transform var(--aik-d-panel) var(--aik-ease-soft)}
.aik-nt-win[data-open] .aik-nt-nub{opacity:0;transform:scale(1.5,1.3)}
.aik-nt-flow{display:grid;gap:14px;width:min(100%,400px)}
.aik-nt-msg{font-size:14.5px;line-height:23px;color:var(--aik-text);min-height:69px}
.aik-nt-msg code{font:12.5px var(--aik-mono);padding:2px 5px;border-radius:5px;background:var(--aik-raised)}
.aik-nt-page{padding-top:58px}
.aik-nt-win .aik-nt-notch{position:absolute;left:50%;top:40px;translate:-50% 0;inline-size:min(268px,calc(100% - 24px))}
.aik-nt-col{display:grid;gap:14px;justify-items:center;width:min(100%,340px)}
`
const clock = (seconds: number) =>
`${Math.floor(seconds / 60)}:${String(Math.floor(seconds % 60)).padStart(2, "0")}`
const NOTCH_STEPS: Array<[AiStatus, string, number, number]> = [
["run", "Building pricing", 0.12, 4],
["run", "Running 24 tests", 0.62, 4],
["ask", "Needs your OK to publish", 0.8, 4],
["run", "Publishing", 0.94, 3],
]
function Notch() {
const root = React.useRef<HTMLDivElement>(null)
const [open, setOpen] = React.useState(true)
const [status, setStatus] = React.useState<AiStatus>("ask")
const [label, setLabel] = React.useState("Needs your OK to publish")
const [progress, setProgress] = React.useState(0.8)
const [seconds, setSeconds] = React.useState(16)
const [hands, setHands] = React.useState(false)
useAiLoop(
root,
async ({ sleep }) => {
let elapsed = 0
setOpen(false)
setStatus("todo")
setLabel("Starting")
setProgress(0)
setSeconds(0)
await sleep(1100)
setStatus("run")
setLabel("Building pricing")
setProgress(0.12)
await sleep(900)
setOpen(true)
for (const [nextStatus, nextLabel, nextProgress, ticks] of NOTCH_STEPS) {
setStatus(nextStatus)
setLabel(nextLabel)
setProgress(nextProgress)
for (let tick = 0; tick < ticks; tick++) {
setSeconds(elapsed)
elapsed += 2
await sleep(430)
}
}
setStatus("done")
setLabel(`Done in ${clock(elapsed)}`)
setProgress(1)
await sleep(1500)
setOpen(false)
await sleep(1500)
},
{ gap: 300, enabled: !hands }
)
return (
<div
ref={root}
className="aik-nt-win"
onPointerDown={() => setHands(true)}
onKeyDown={() => setHands(true)}
>
<div className="aik-nt-chrome">
<i />
<i />
<i />
</div>
<div className="aik-nt-page">
<i style={{ width: "44%" }} />
<i />
<i />
<i style={{ width: "72%" }} />
</div>
<AiNotch
name="Mira"
status={status}
label={label}
progress={progress}
time={status === "done" ? undefined : clock(seconds)}
nubLabel={status === "done" ? "Done" : undefined}
open={open}
onOpenChange={setOpen}
/>
</div>
)
}
const FEED: AiToast[] = [
{
id: "built",
status: "done",
title: "Registry rebuilt",
detail: "102 items, no warnings",
},
{
id: "publish",
status: "ask",
title: "Publish to tweenui.dev?",
detail: "Mira is waiting for you",
action: "Approve",
},
{
id: "tests",
status: "fail",
title: "3 tests failed",
detail: "Odo is fixing them now",
action: "View",
},
{
id: "fixed",
status: "done",
title: "24 tests pass",
detail: "Odo fixed all 3 failures",
},
]
function Toasts() {
const root = React.useRef<HTMLDivElement>(null)
const [list, setList] = React.useState<AiToast[]>(FEED.slice(0, 3))
const [sent, setSent] = React.useState(3)
const [hands, setHands] = React.useState(false)
useAiLoop(
root,
async ({ sleep }) => {
setList([])
await sleep(500)
for (const toast of FEED) {
setList((current) => [...current, toast])
await sleep(1500)
}
for (let left = 3; left > 0; left--) {
setList((current) => current.slice(0, -1))
await sleep(420)
}
setList([])
await sleep(500)
},
{ gap: 300, enabled: !hands, still: () => setList(FEED.slice(0, 3)) }
)
const drop = (id: string) =>
setList((current) => current.filter((toast) => toast.id !== id))
return (
<div ref={root} className="aik-nt-col" onPointerDown={() => setHands(true)}>
<AiToasts toasts={list} onDismiss={drop} onAction={drop} />
<button
type="button"
className="aik-btn sm line"
onClick={() => {
const next = FEED[sent % FEED.length]
if (!next) return
setList((current) => [
...current,
{ ...next, id: `${next.id}-${sent}` },
])
setSent(sent + 1)
}}
>
Send a toast
</button>
</div>
)
}
const TAIL = "animates in 180ms. Next I will rebuild the registry."
interface Ban {
status: AiStatus
title: string
detail?: string
retryIn?: number
retryLabel?: string
}
const DROPPED: Ban = {
status: "fail",
title: "The stream dropped",
detail: "212 tokens arrived. Retry keeps them.",
}
const RECONNECT: Ban = {
status: "run",
title: "Reconnecting",
detail: "Picking up from token 212",
}
const RESUMED: Ban = {
status: "done",
title: "Stream resumed",
detail: "Nothing was lost.",
}
function Banner() {
const root = React.useRef<HTMLDivElement>(null)
const [ban, setBan] = React.useState<Ban>(DROPPED)
const [hidden, setHidden] = React.useState(false)
const [words, setWords] = React.useState<string[]>([])
const [hands, setHands] = React.useState(false)
const round = React.useRef(0)
const timers = React.useRef<number[]>([])
React.useEffect(() => {
const pending = timers.current
return () => pending.forEach((timer) => window.clearTimeout(timer))
}, [])
useAiLoop(
root,
async ({ sleep }) => {
setWords([])
setHidden(false)
if (round.current++ % 2 === 0) {
setBan(DROPPED)
await sleep(2300)
} else {
for (let left = 5; left > 0; left--) {
setBan({
status: "ask",
title: "Rate limit reached",
retryIn: left,
retryLabel: "Retry now",
})
await sleep(800)
}
}
setBan(RECONNECT)
await sleep(1800)
setBan(RESUMED)
for (const word of TAIL.split(" ")) {
setWords((current) => [...current, word])
await sleep(paceToken(word, 60))
}
await sleep(900)
setHidden(true)
await sleep(1100)
},
{
gap: 300,
enabled: !hands,
still: () => {
setBan(DROPPED)
setHidden(false)
},
}
)
const retry = () => {
setBan(RECONNECT)
timers.current.push(
window.setTimeout(() => {
setBan(RESUMED)
setWords(TAIL.split(" "))
}, 1600)
)
}
return (
<div
ref={root}
className="aik-nt-flow"
onPointerDown={() => setHands(true)}
>
<p className="aik-nt-msg">
The pricing section is in <code>pricing-section.tsx</code> and the
toggle{" "}
{words.map((word, at) => (
<span key={at} className="aik-w">
{word}{" "}
</span>
))}
</p>
<AiBanner
status={ban.status}
title={ban.title}
detail={ban.detail}
retryIn={ban.retryIn}
retryLabel={ban.retryLabel}
hidden={hidden}
onRetry={retry}
/>
</div>
)
}
export function AiNotchDemo({ only }: { only?: string }) {
return (
<>
<AiStyle id="ai-notch-demo" css={CSS} />
<AiDemo
only={only}
tabs={[
{ name: "Notch", node: <Notch /> },
{ name: "Toast stack", node: <Toasts /> },
{ name: "Banner", node: <Banner /> },
]}
/>
</>
)
}
Installation
Usage
import { AiBanner, AiNotch, AiToasts } from "@/components/ui/ai-notch"<AiNotch name="Mira" status="run" label="Running 24 tests" time="0:14" progress={0.62} />
<AiToasts
toasts={toasts}
onDismiss={(id) => setToasts((list) => list.filter((toast) => toast.id !== id))}
onAction={approve}
/>
<AiBanner
status="fail"
title="The stream dropped"
detail="212 tokens arrived. Retry keeps them."
onRetry={reconnect}
/>Versions
AiNotchis a small dark pill that grows into a status card from its top edge. It opens on click and closes on Escape or a second click. PassopenandonOpenChangeto control it, for example to open it when the agent needs you.AiToastsstacks up to three notifications. The newest sits at the front; older ones move up and back, and removed ones leave downward. Only the front toast takes clicks.AiBannersits in the thread where the trouble happened.failandaskwait for the person,runshows a reconnecting state and disables the button,doneconfirms recovery.retryInadds a countdown.
Props
AiNotch
| Prop | Type | Default | Description |
|---|---|---|---|
name | string | The agent's name. | |
status | "todo" | "run" | "ask" | "done" | "fail" | Drives the colour and the face's mood. | |
label | string | What the agent is doing. Rolls when it changes. | |
time | string | Elapsed time, already formatted. | |
progress | number | 0 to 1, drawn as a line under the card. | |
open | boolean | Controls the card. Omit to let the notch manage it. | |
defaultOpen | boolean | false | Starting state when uncontrolled. |
onOpenChange | (open: boolean) => void | Called on click and Escape. | |
nubLabel | string | name | Text on the closed pill. |
AiToasts
| Prop | Type | Default | Description |
|---|---|---|---|
toasts | { id, status, title, detail?, action? }[] | Oldest first. The last one is in front. | |
onDismiss | (id: string) => void | Adds a dismiss button to each toast. | |
onAction | (id: string) => void | Called from a toast's action button. |
AiBanner
| Prop | Type | Default | Description |
|---|---|---|---|
status | "todo" | "run" | "ask" | "done" | "fail" | Sets the colour and the mark. | |
title | string | The headline. Rolls when it changes. | |
detail | ReactNode | A line under the title. | |
retryIn | number | Seconds until an automatic retry. | |
retryLabel | string | "Retry" | The button's label. |
onRetry | () => void | Shows the button. | |
hidden | boolean | false | Fades the banner out but keeps its space. |