Funnel chart
Inspect sequential conversion counts and retention rates, switching audiences with a motion-aware trapezoid morph.
pnpm dlx shadcn@latest add @haze/funnel-chart---
import { ChevronDown } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
interface Step{label:string;value:number}
interface Segment{label:string;steps:Step[]}
interface Props{label?:string;segments?:Segment[];class?:string}
const{label="Signup conversion",segments=[{label:"All visitors",steps:[{label:"Visited",value:8400},{label:"Started",value:3900},{label:"Verified",value:2600},{label:"Activated",value:1840}]},{label:"Returning visitors",steps:[{label:"Visited",value:2600},{label:"Started",value:1820},{label:"Verified",value:1540},{label:"Activated",value:1310}]}],class:className}=Astro.props;
const groups=segments.map(segment=>({...segment,steps:segment.steps.map(step=>({...step,value:Number.isFinite(step.value)&&step.value>=0?step.value:0}))}));
const geometry=(steps:Step[])=>{const max=Math.max(0,...steps.map(step=>step.value));return steps.map((step,index)=>{const next=steps[index+1]??step,top=max?step.value/max*160:0,bottom=max?next.value/max*160:0,y=index*200/steps.length,height=200/steps.length,gap=Math.min(3,height*.1);return [150-top/2,y,150+top/2,y,150+bottom/2,y+height-gap,150-bottom/2,y+height-gap];});};
const group=groups[0],shapes=geometry(group?.steps??[]),colors=["fill-brand","fill-success","fill-warning","fill-muted-foreground","fill-destructive"];
const rate=(value:number,base:number)=>base>0?(value/base*100).toLocaleString("en",{maximumFractionDigits:1})+"%":"Not defined";
---
<figure data-haze-funnel-chart data-groups={JSON.stringify(groups)} class={cn("grid min-w-0 gap-3 rounded-xl border border-border bg-card p-4.5 text-card-foreground",className)}>
<figcaption class="text-[0.9375rem]/5 font-medium tracking-[-0.01em] wrap-anywhere">{label}</figcaption>
{groups.length>1&&<label data-funnel-control hidden class="grid gap-1 text-xs/5 text-muted-foreground">Audience<select data-funnel-segment class="h-10 min-w-0 rounded-lg border border-frame bg-field px-3 text-[0.8125rem] text-foreground hover:border-frame-strong hover:bg-accent focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/24 focus-visible:outline-hidden">{groups.map((segment,index)=><option value={index}>{segment.label}</option>)}</select></label>}
{group?.steps.length?<div class="relative h-60 w-full"><svg data-funnel-plot aria-hidden="true" viewBox="0 0 300 200" preserveAspectRatio="none" class="absolute inset-0 size-full">{shapes.map((points,index)=><polygon data-funnel-band={index} points={Array.from({length:4},(_,i)=>points[i*2]+","+points[i*2+1]).join(" ")} class={cn("transition-opacity duration-200 ease-snappy data-[dimmed]:opacity-30",colors[index%colors.length])}/>)}</svg><div data-funnel-labels aria-hidden="true" class="pointer-events-none absolute inset-0">{group.steps.map((step,index)=><div data-funnel-label style={`top:${(index+0.5)/group.steps.length*100}%`} class="group absolute inset-x-0 flex -translate-y-1/2 items-baseline justify-between gap-1 text-xs/4"><span data-funnel-name class="min-w-0 font-medium text-foreground group-data-[dimmed]:font-normal group-data-[dimmed]:text-muted-foreground">{step.label}</span><span data-funnel-value class="shrink-0 tabular-nums text-muted-foreground">{step.value.toLocaleString("en")}</span></div>)}</div><template data-funnel-label-tpl><div data-funnel-label style="" class="group absolute inset-x-0 flex -translate-y-1/2 items-baseline justify-between gap-1 text-xs/4"><span data-funnel-name class="min-w-0 font-medium text-foreground group-data-[dimmed]:font-normal group-data-[dimmed]:text-muted-foreground"></span><span data-funnel-value class="shrink-0 tabular-nums text-muted-foreground"></span></div></template></div>:<p class="text-[0.8125rem]/5 text-muted-foreground">No conversion steps supplied.</p>}
{group&&group.steps.length>0&&<label data-funnel-explore hidden class="grid gap-1 text-xs/5 text-muted-foreground">Inspect a step<input data-funnel-cursor aria-label="Conversion step" type="range" min="0" max={group.steps.length-1} value="0" step="1" class="h-10 w-full min-w-0 cursor-pointer appearance-none rounded-md bg-transparent focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring disabled:cursor-not-allowed [&::-webkit-slider-runnable-track]:h-1.5 [&::-webkit-slider-runnable-track]:rounded-full [&::-webkit-slider-runnable-track]:bg-input [&::-webkit-slider-thumb]:-mt-2 [&::-webkit-slider-thumb]:size-5.5 [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:border-0 [&::-webkit-slider-thumb]:bg-foreground [&::-webkit-slider-thumb]:shadow-key [&::-webkit-slider-thumb]:transition-[scale] [&::-webkit-slider-thumb]:duration-150 [&::-webkit-slider-thumb]:ease-snappy enabled:active:[&::-webkit-slider-thumb]:scale-95 [&::-moz-range-track]:h-1.5 [&::-moz-range-track]:rounded-full [&::-moz-range-track]:bg-input [&::-moz-range-thumb]:size-5.5 [&::-moz-range-thumb]:rounded-full [&::-moz-range-thumb]:border-0 [&::-moz-range-thumb]:bg-foreground [&::-moz-range-thumb]:shadow-key [&::-moz-range-thumb]:transition-[scale] [&::-moz-range-thumb]:duration-150 [&::-moz-range-thumb]:ease-snappy enabled:active:[&::-moz-range-thumb]:scale-95 forced-colors:appearance-auto"/></label>}
<p data-funnel-readout role="status" aria-live="polite" class="min-h-10 text-xs/5 text-muted-foreground">Step widths use a common scale; an increase remains an increase.</p>
<details class="group min-w-0 border-t border-border pt-3"><summary class="flex min-h-7 cursor-pointer list-none items-center justify-between gap-2 rounded-sm text-[0.8125rem]/5 font-medium text-muted-foreground transition-colors duration-150 ease-snappy hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring [&::-webkit-details-marker]:hidden">Conversion and drop-off data<ChevronDown aria-hidden="true" class="size-3.5 shrink-0 transition-[rotate] duration-200 ease-snappy group-open:rotate-180" /></summary><table class="mt-3 w-full table-fixed text-left text-xs/5"><caption class="sr-only">{label}</caption><thead><tr><th scope="col" class="p-1">Audience / step</th><th scope="col" class="p-1">Count / retained / of first</th></tr></thead><tbody>{groups.flatMap(segment=>segment.steps.map((step,index)=><tr class="border-t border-border"><th scope="row" class="p-1 font-normal wrap-anywhere">{segment.label} / {step.label}</th><td class="p-1 tabular-nums wrap-anywhere">{step.value} / {index?rate(step.value,segment.steps[index-1].value):"Start"} / {rate(step.value,segment.steps[0].value)}</td></tr>))}</tbody></table></details>
</figure>
<script>
function initFunnel(){document.querySelectorAll<HTMLElement>("[data-haze-funnel-chart]:not([data-enhanced])").forEach(root=>{
root.dataset.enhanced="";const groups:{label:string;steps:{label:string;value:number}[]}[]=JSON.parse(root.dataset.groups||"[]"),select=root.querySelector<HTMLSelectElement>("[data-funnel-segment]"),cursor=root.querySelector<HTMLInputElement>("[data-funnel-cursor]"),plot=root.querySelector<SVGSVGElement>("[data-funnel-plot]");let raf=0;
const polygons=()=>Array.from(root.querySelectorAll<SVGPolygonElement>("[data-funnel-band]"));let previous=polygons().map(p=>(p.getAttribute("points")||"").split(/[ ,]+/).map(Number));
const read=()=>{const segment=groups[Number(select?.value||0)],index=Number(cursor?.value||0),step=segment?.steps[index];if(!step)return;const previousStep=segment.steps[index-1],first=segment.steps[0],percent=(base:number)=>base>0?(step.value/base*100).toFixed(1)+"%":"not defined";const text=segment.label+" · "+step.label+": "+step.value+"; "+(previousStep?percent(previousStep.value)+" retained; drop-off "+(previousStep.value-step.value).toLocaleString("en"):"start")+"; "+percent(first.value)+" of first.";root.querySelector("[data-funnel-readout]")!.textContent=text;cursor?.setAttribute("aria-valuetext",text);polygons().forEach((p,i)=>p.toggleAttribute("data-dimmed",i!==index));root.querySelectorAll("[data-funnel-label]").forEach((l,i)=>l.toggleAttribute("data-dimmed",i!==index));};
const change=()=>{const segment=groups[Number(select?.value||0)];if(!segment||!plot)return;const max=Math.max(0,...segment.steps.map(step=>step.value)),n=segment.steps.length,target=segment.steps.map((step,i)=>{const top=max?step.value/max*160:0,bottom=max?(segment.steps[i+1]?.value??step.value)/max*160:0,y=i*200/n,h=200/n,gap=Math.min(3,h*.1);return[150-top/2,y,150+top/2,y,150+bottom/2,y+h-gap,150-bottom/2,y+h-gap];});
while(polygons().length<n){const p=document.createElementNS("http://www.w3.org/2000/svg","polygon");p.setAttribute("data-funnel-band",String(polygons().length));p.setAttribute("class",["fill-brand","fill-success","fill-warning","fill-muted-foreground","fill-destructive"][polygons().length%5]);plot.append(p);}while(polygons().length>n)polygons().at(-1)?.remove();
const labels=root.querySelector("[data-funnel-labels]"),tpl=root.querySelector<HTMLTemplateElement>("[data-funnel-label-tpl]");if(labels&&tpl){labels.replaceChildren(...segment.steps.map((step,i)=>{const l=tpl.content.firstElementChild!.cloneNode(true) as HTMLElement;l.style.top=((i+0.5)/n*100)+"%";l.querySelector("[data-funnel-name]")!.textContent=step.label;l.querySelector("[data-funnel-value]")!.textContent=step.value.toLocaleString("en");return l;}));}
if(cursor){cursor.max=String(Math.max(0,n-1));cursor.value=String(Math.min(Number(cursor.value),n-1));}cancelAnimationFrame(raf);const from=target.map((p,i)=>previous[i]??p),began=performance.now();const frame=(now:number)=>{const t=matchMedia("(prefers-reduced-motion: reduce)").matches?1:Math.min(1,(now-began)/250);previous=target.map((p,i)=>p.map((v,j)=>from[i][j]+(v-from[i][j])*t));polygons().forEach((p,i)=>p.setAttribute("points",previous[i].join(" ")));if(t<1)raf=requestAnimationFrame(frame);};raf=requestAnimationFrame(frame);read();};
root.querySelector("[data-funnel-control]")?.removeAttribute("hidden");root.querySelector("[data-funnel-explore]")?.removeAttribute("hidden");select?.addEventListener("change",change);cursor?.addEventListener("input",read);read();
});}
initFunnel();if(!document.documentElement.hasAttribute("data-haze-funnel-chart-listener")){document.documentElement.setAttribute("data-haze-funnel-chart-listener","");document.addEventListener("astro:page-load",initFunnel);}
</script>When to use
- A fixed sequence needs counts and drop-off at each transition.
- Two audiences follow the same conversion journey and need comparison.
When not to use
- Flow can split and rejoin across stages. Use sankey-flow.
Installation
Add the Haze registry to components.json once, then install Funnel chart with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/funnel-chartnpx shadcn@latest add @haze/funnel-chartyarn dlx shadcn@latest add @haze/funnel-chartbunx --bun shadcn@latest add @haze/funnel-chartFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| label | string | Signup conversion | Measurement caption. |
| segments | { label: string; steps: { label: string; value: number }[] }[] | – | Audience-specific ordered steps. Invalid or negative counts become zero; the common scale preserves increases. |
| class | string | – | Additional classes on the chart figure. |
Keyboard interactions
- Arrow keysHomeEnd
- Inspect a funnel step with the native range.
- Arrow keys
- Change the audience in the native select.
Accessibility
- A native step range announces counts, previous-step retention, drop-off and first-step share without relying on the band colors.
- Zero denominators are reported as undefined; increasing steps retain their true geometry and can show negative drop-off.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Funnel chart" with `npx shadcn@latest add @haze/funnel-chart`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import FunnelChart from "@/components/haze/ui/funnel-chart.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/funnel-chart.md
Customize with real content
Adapt the Haze funnel-chart ui for our product. Replace the demo values of these props with our real content: label, segments, class. Keep the layout, the heading level rules and these accessibility details: A native step range announces counts, previous-step retention, drop-off and first-step share without relying on the band colors. Zero denominators are reported as undefined; increasing steps retain their true geometry and can show negative drop-off. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using funnel-chart, check it fits: use it when a fixed sequence needs counts and drop-off at each transition.; two audiences follow the same conversion journey and need comparison.. Avoid it when flow can split and rejoin across stages. use sankey-flow.. Alternatives: @haze/sankey-flow, @haze/bar-chart, @haze/waffle-chart.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.