Handoff Swimlanes
Who does what in a run: a lane per person, agent and tool, hand-offs as arrows, and approvals as diamonds you can approve in place.
"use client"
import * as React from "react"
import { Check } from "lucide-react"
import {
AgentGlyph,
AiKitStyles,
AiStyle,
BrandIcon,
cx,
Edge,
elbowPath,
Roll,
toneColor,
type AiTone,
} from "@/lib/ai-kit"
export interface SwimLane {
id: string
name: string
role: string
/** Agents and people get a pixel mark in their tone; tools get their brand. */
tone?: AiTone
brand?: string
}
export type SwimStatus = "todo" | "run" | "ask" | "done"
export interface SwimNode {
id: string
lane: string
/** Column on the run's timeline, from 1. */
col: number
kind: "start" | "end" | "task" | "approval"
label?: string
status: SwimStatus
}
export interface SwimLink {
from: string
to: string
}
export interface HandoffSwimlanesProps {
title: string
lanes: readonly SwimLane[]
nodes: readonly SwimNode[]
links: readonly SwimLink[]
/** Called when an approval diamond that is waiting gets approved in place. */
onApprove?: (id: string) => void
className?: string
}
const CSS = `
.aik-sw{--aik-r-shell:26px;--aik-rim:5px;width:min(100%,1080px);container-type:inline-size}
.aik-sw-card{display:grid;grid-template-columns:minmax(0,1fr)}
.aik-sw-head{display:flex;align-items:center;gap:12px;padding:18px 22px;border-bottom:1px solid var(--aik-line)}
.aik-sw-head b{font-size:16px;font-weight:600;letter-spacing:-.01em}
.aik-sw-count{margin-left:auto;font:500 13.5px var(--aik-mono);color:var(--aik-text-3)}
.aik-sw-run{display:inline-flex;align-items:center;gap:7px;height:28px;padding:0 11px;border-radius:9px;font-size:13.5px;font-weight:500;color:var(--aik-blue);background:color-mix(in oklab,var(--aik-blue) 14%,transparent)}
.aik-sw-run i{width:7px;height:7px;border-radius:50%;background:currentColor;animation:aik-glyph 1.2s ease-in-out infinite}
.aik-sw-run[data-s="ask"]{color:var(--aik-amber);background:color-mix(in oklab,var(--aik-amber) 15%,transparent)}
.aik-sw-run[data-s="done"]{color:var(--aik-green);background:color-mix(in oklab,var(--aik-green) 14%,transparent)}
.aik-sw-run[data-s="done"] i{animation:none}
.aik-sw-scroll{overflow-x:auto;scrollbar-width:thin}
.aik-sw-scroll[data-overflow]{-webkit-mask:linear-gradient(90deg,#000 calc(100% - 36px),transparent);mask:linear-gradient(90deg,#000 calc(100% - 36px),transparent)}
.aik-sw-grid{position:relative;display:grid;grid-template-columns:190px repeat(var(--cols),minmax(118px,1fr));grid-auto-rows:minmax(92px,auto);min-width:860px;padding:0 18px 6px 0}
.aik-sw-svg{position:absolute;inset:0;width:100%;height:100%;overflow:visible;pointer-events:none;z-index:0}
.aik-sw-lane{grid-column:1;display:flex;align-items:center;gap:12px;padding:0 18px 0 22px;border-top:1px solid var(--aik-line);min-width:0}
.aik-sw-lane:first-of-type{border-top:0}
.aik-sw-lane > span:first-child{display:grid;place-items:center;width:30px;height:30px;flex:none;border-radius:9px;background:var(--aik-sunk);box-shadow:inset 0 0 0 1px var(--aik-line)}
.aik-sw-lane div{display:grid;line-height:19px;min-width:0}
.aik-sw-lane b{font-size:15px;font-weight:500}
.aik-sw-lane small{font-size:13px;color:var(--aik-text-3);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-sw-rule{grid-column:2 / -1;border-top:1px solid var(--aik-line);pointer-events:none}
.aik-sw-cell{position:relative;z-index:1;display:grid;place-items:center;padding:10px 6px;min-width:0}
.aik-sw-task{display:flex;align-items:center;gap:8px;width:100%;max-width:170px;min-height:50px;padding:8px 12px;border-radius:12px;background:var(--aik-card);box-shadow:var(--aik-sh-card);font-size:14px;line-height:18px;transition:opacity var(--aik-d-panel),box-shadow var(--aik-d-panel),background var(--aik-d-panel);animation:aik-sw-in 460ms var(--aik-ease-soft) both}
.aik-sw-task[data-s="todo"]{opacity:.45;box-shadow:inset 0 0 0 1px var(--aik-line-2);background:transparent}
.aik-sw-task[data-s="run"]{background:color-mix(in oklab,var(--c) 14%,var(--aik-card));box-shadow:0 0 0 1.5px color-mix(in oklab,var(--c) 70%,transparent),0 10px 26px -12px color-mix(in oklab,var(--c) 70%,transparent)}
.aik-sw-task[data-s="done"]{box-shadow:inset 0 0 0 1px color-mix(in oklab,var(--c) 35%,transparent)}
.aik-sw-task svg{flex:none;color:var(--c)}
@keyframes aik-sw-in{from{opacity:0;transform:scale(.95)}}
.aik-sw-dot{width:26px;height:26px;border-radius:50%;box-shadow:inset 0 0 0 2px var(--aik-line-3);background:var(--aik-card);transition:box-shadow var(--aik-d-panel)}
.aik-sw-dot[data-kind="end"]{box-shadow:inset 0 0 0 2px var(--aik-line-3),inset 0 0 0 6px var(--aik-card),inset 0 0 0 13px var(--aik-line-3)}
.aik-sw-dot[data-s="done"]{box-shadow:inset 0 0 0 2px var(--aik-green)}
.aik-sw-dot[data-kind="end"][data-s="done"]{box-shadow:inset 0 0 0 2px var(--aik-green),inset 0 0 0 6px var(--aik-card),inset 0 0 0 13px var(--aik-green)}
.aik-sw-gate{display:grid;justify-items:center;gap:8px;font-size:13px;color:var(--aik-text-3)}
.aik-sw-gate button{position:relative;display:grid;place-items:center;width:34px;height:34px;cursor:default}
.aik-sw-gate button::before{content:"";position:absolute;inset:5px;rotate:45deg;border-radius:5px;background:var(--aik-raised);box-shadow:inset 0 0 0 1.5px var(--aik-line-3);transition:background var(--aik-d-panel),box-shadow var(--aik-d-panel),scale var(--aik-d-move) var(--aik-ease-soft),rotate var(--aik-d-move) var(--aik-ease-move)}
.aik-sw-gate button svg{position:relative;color:var(--aik-on-fill);opacity:0;scale:.5;transition:opacity var(--aik-d-state),scale var(--aik-d-move) var(--aik-ease-soft)}
.aik-sw-gate[data-s="ask"] button{cursor:pointer}
.aik-sw-gate[data-s="ask"] button::before{background:color-mix(in oklab,var(--aik-amber) 22%,var(--aik-card));box-shadow:inset 0 0 0 1.5px var(--aik-amber),0 0 0 6px color-mix(in oklab,var(--aik-amber) 14%,transparent)}
.aik-sw-gate[data-s="ask"] button::after{content:"";position:absolute;inset:5px;rotate:45deg;border-radius:6px;box-shadow:0 0 0 1.5px color-mix(in oklab,var(--aik-amber) 55%,transparent);animation:aik-sw-ask 1.6s var(--aik-ease-out) infinite;pointer-events:none}
.aik-sw-gate[data-s="ask"] button:hover::before{scale:1.12}
.aik-sw-gate[data-s="ask"]{color:var(--aik-amber)}
.aik-sw-gate[data-s="done"] button::before{background:var(--aik-green);box-shadow:none;rotate:135deg;border-radius:50%}
.aik-sw-gate[data-s="done"] button svg{opacity:1;scale:1}
.aik-sw-gate[data-s="done"]{color:var(--aik-green)}
@keyframes aik-sw-ask{from{opacity:.9;scale:1}to{opacity:0;scale:1.7}}
@container (max-width:620px){
.aik-sw-head{flex-wrap:wrap;padding:14px 16px}
.aik-sw-head b{width:100%}
.aik-sw-count{margin-left:0}
.aik-sw-grid{grid-template-columns:54px repeat(var(--cols),minmax(104px,1fr));min-width:780px}
.aik-sw-lane{position:sticky;left:0;z-index:2;padding:0 0 0 12px;background:var(--aik-card)}
.aik-sw-lane div{display:none}
.aik-sw-lane::after{content:"";position:absolute;top:0;bottom:0;right:-18px;width:18px;pointer-events:none;background:linear-gradient(90deg,var(--aik-card),transparent)}
.aik-sw-task{font-size:13px;padding:7px 9px}
}
@media (prefers-reduced-motion: reduce){.aik-sw-task{animation:none}.aik-sw-gate[data-s="ask"] button::after{animation:none;opacity:0}}
`
/**
* Who does what in a run, as swimlanes: a lane per person, agent and tool,
* hand-offs as arrows between lanes, and approvals as diamonds you can
* approve in place.
*/
export function HandoffSwimlanes({
title,
lanes,
nodes,
links,
onApprove,
className,
}: HandoffSwimlanesProps) {
const grid = React.useRef<HTMLDivElement>(null)
const scroller = React.useRef<HTMLDivElement>(null)
const followed = React.useRef<Element | null>(null)
// When the lanes are wider than the card (phones), glide the active step
// into view so the flow never sits offscreen.
React.useEffect(() => {
const box = scroller.current
const g = grid.current
if (!box || !g) return
const overflow = box.scrollWidth > box.clientWidth + 4
box.toggleAttribute("data-overflow", overflow)
if (!overflow) return
const active =
g.querySelector('[data-s="ask"]') ?? g.querySelector('[data-s="run"]')
if (!active || active === followed.current) return
followed.current = active
const a = active.getBoundingClientRect()
const b = box.getBoundingClientRect()
// Centre it in the part of the card not covered by the sticky lane column.
const lane = g.querySelector<HTMLElement>(".aik-sw-lane")?.offsetWidth ?? 0
const view = b.left + lane + (b.width - lane) / 2
const target = box.scrollLeft + (a.left + a.width / 2) - view
box.scrollTo({ left: Math.max(0, target), behavior: "smooth" })
})
const els = React.useRef(new Map<string, HTMLElement>())
const [paths, setPaths] = React.useState<
Array<{
key: string
d: string
end: [number, number]
from: string
to: string
}>
>([])
const laneIndex = new Map(lanes.map((lane, i) => [lane.id, i]))
const laneOf = new Map(lanes.map((lane) => [lane.id, lane]))
const byId = new Map(nodes.map((n) => [n.id, n]))
const cols = Math.max(...nodes.map((n) => n.col))
const box = (node: HTMLElement) => {
let left = 0
let top = 0
let el: HTMLElement | null = node
while (el && el !== grid.current) {
left += el.offsetLeft
top += el.offsetTop
el = el.offsetParent as HTMLElement | null
}
return { left, top, width: node.offsetWidth, height: node.offsetHeight }
}
const measure = React.useCallback(() => {
const next: typeof paths = []
for (const link of links) {
const a = els.current.get(link.from)
const b = els.current.get(link.to)
if (!a || !b) continue
const p = box(a)
const q = box(b)
const x1 = p.left + p.width
const y1 = p.top + p.height / 2
const x2 = q.left - 6
const y2 = q.top + q.height / 2
next.push({
key: `${link.from}-${link.to}`,
d: elbowPath(x1 + 2, y1, x2, y2, "h", 10),
end: [x2, y2],
from: link.from,
to: link.to,
})
}
setPaths(next)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [links, nodes])
React.useLayoutEffect(() => {
measure()
const observer = new ResizeObserver(() => measure())
if (grid.current) observer.observe(grid.current)
return () => observer.disconnect()
}, [measure])
const register = (id: string) => (node: HTMLElement | null) => {
if (node) els.current.set(id, node)
else els.current.delete(id)
}
const step = nodes.filter((n) => n.kind === "task" || n.kind === "approval")
const doneCount = step.filter((n) => n.status === "done").length
const runState = nodes.some((n) => n.status === "ask")
? "ask"
: nodes.find((n) => n.kind === "end")?.status === "done"
? "done"
: "run"
const toneOf = (node: SwimNode) => {
const lane = laneOf.get(node.lane)
return lane?.tone ? toneColor(lane.tone) : "var(--aik-text-2)"
}
return (
<div className={cx("aik aik-sw aik-shell", className)}>
<AiKitStyles />
<AiStyle id="handoff-swimlanes" css={CSS} />
<div className="aik-surface aik-sw-card">
<div className="aik-sw-head">
<b>{title}</b>
<span className="aik-sw-count">
<Roll>{`${doneCount} of ${step.length}`}</Roll>
</span>
<span className="aik-sw-run" data-s={runState}>
<i />
<Roll>
{runState === "ask"
? "Waiting on you"
: runState === "done"
? "Done"
: "Running"}
</Roll>
</span>
</div>
<div className="aik-sw-scroll" ref={scroller}>
<div
ref={grid}
className="aik-sw-grid"
style={{ "--cols": cols } as React.CSSProperties}
>
<svg className="aik-sw-svg" aria-hidden="true">
{paths.map((path) => {
const from = byId.get(path.from)
const to = byId.get(path.to)
const tone = to ? (laneOf.get(to.lane)?.tone ?? "ink") : "ink"
const state =
from?.status === "done" &&
(to?.status === "run" || to?.status === "ask")
? "live"
: from?.status === "done" && to?.status === "done"
? "done"
: "idle"
const [x, y] = path.end
const color =
state === "idle" ? "var(--aik-line-3)" : toneColor(tone)
return (
<g key={path.key}>
<Edge
d={path.d}
tone={tone}
state={state}
dashed={state === "idle"}
duration={1.3}
/>
<path
d={`M${x - 6} ${y - 4.5}L${x + 1} ${y}L${x - 6} ${y + 4.5}Z`}
fill={color}
style={{ transition: "fill var(--aik-d-panel)" }}
/>
</g>
)
})}
</svg>
{lanes.map((lane, i) => (
<React.Fragment key={lane.id}>
<div className="aik-sw-lane" style={{ gridRow: i + 1 }}>
<span>
{lane.brand ? (
<BrandIcon name={lane.brand} tint="brand" size={16} />
) : (
<AgentGlyph
seed={lane.id}
tone={lane.tone ?? "ink"}
size={16}
/>
)}
</span>
<div>
<b>{lane.name}</b>
<small>{lane.role}</small>
</div>
</div>
{i > 0 ? (
<div className="aik-sw-rule" style={{ gridRow: i + 1 }} />
) : null}
</React.Fragment>
))}
{nodes.map((node) => {
const row = (laneIndex.get(node.lane) ?? 0) + 1
const place = { gridRow: row, gridColumn: node.col + 1 }
if (node.kind === "start" || node.kind === "end") {
return (
<div key={node.id} className="aik-sw-cell" style={place}>
<span
ref={register(node.id)}
className="aik-sw-dot"
data-kind={node.kind}
data-s={node.status}
/>
</div>
)
}
if (node.kind === "approval") {
return (
<div key={node.id} className="aik-sw-cell" style={place}>
<div className="aik-sw-gate" data-s={node.status}>
<button
ref={register(node.id) as React.Ref<HTMLButtonElement>}
type="button"
disabled={node.status !== "ask"}
aria-label={
node.status === "ask"
? `Approve: ${node.label}`
: node.label
}
onClick={() => onApprove?.(node.id)}
>
<Check size={15} strokeWidth={3} />
</button>
<Roll>
{node.status === "ask"
? "Click to approve"
: (node.label ?? "Approval")}
</Roll>
</div>
</div>
)
}
return (
<div key={node.id} className="aik-sw-cell" style={place}>
<div
ref={register(node.id)}
className="aik-sw-task"
data-s={node.status}
style={{ "--c": toneOf(node) } as React.CSSProperties}
>
{node.status === "done" ? (
<Check size={15} strokeWidth={2.5} />
) : (
<AgentGlyph
seed={node.lane}
tone={laneOf.get(node.lane)?.tone ?? "ink"}
size={13}
live={node.status === "run"}
/>
)}
<span>{node.label}</span>
</div>
</div>
)
})}
</div>
</div>
</div>
</div>
)
}
Installation
One command adds handoff-swimlanes 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/handoff-swimlanes.json