Recorded demo
Source
/* Error budget — free block published by UI Stacks (public page source).
Kept verbatim: the author publishes this block free, in full, at https://uistacks.dev/blocks/devops-error-budget
See replica/README.md for provenance. */
"use client";
import { useEffect, useId, useRef, useState, type ReactNode } from "react";
import { MotionConfig, motion } from "motion/react";
import { CircleAlert, CircleCheck, CircleX, Gauge, Info, RotateCw, TrendingDown } from "lucide-react";
import { Badge } from "../components/badge";
import { Button } from "../components/button";
import { Skeleton, SkeletonGroup } from "../components/skeleton";
/* ── Sample data: replace with your own ─────────────────────── */
export type BudgetPart = "left" | "before" | "today";
export type ErrorBudget = {
status: "within" | "exhausted";
/** "27 min 12 s" and "left", or "4 min 18 s" and "over budget". */
value: string;
unit: string;
/** Meter segments in minutes, in order. Their sum is the meter's full width. */
parts: { kind: BudgetPart; minutes: number; label: string; display: string }[];
/** Where the budget ends on the meter when it has run over (minutes from the start). */
budgetEnd?: { minutes: number; display: string };
rows: { label: string; value: string; tone?: "danger" | "warning"; icon?: ReactNode }[];
policy: { tone: "info" | "danger"; text: string };
};
export type ErrorBudgetState = "within" | "exhausted" | "loading" | "error";
/**
* SLO 99.9% over 30 days = 43 min 12 s of allowed downtime (43,200 min × 0.1%).
* Within: 27:12 left + 6:00 before today + 10:00 today (INC-2291) = 43:12; 16 min down → 99.963%.
* Exhausted: 26:30 + 21:00 = 47:30 = 43:12 + 4:18 over; 47.5 min down → 99.890%.
*/
const SAMPLE: Record<"within" | "exhausted", ErrorBudget> = {
within: {
status: "within",
value: "27 min 12 s",
unit: "left",
parts: [
{ kind: "left", minutes: 27.2, label: "Left", display: "27 min 12 s" },
{ kind: "before", minutes: 6, label: "Used before today", display: "6 min" },
{ kind: "today", minutes: 10, label: "Used today", display: "10 min · INC-2291" },
],
rows: [
{ label: "Availability", value: "99.963%" },
{ label: "vs September", value: "0.04 pp worse", tone: "danger", icon: <TrendingDown className="size-4!" strokeWidth={1.5} /> },
{ label: "Burn rate, 1 h", value: "1.4× · lasts 13 days" },
{ label: "Burn rate, 6 h", value: "27.8× · paged 09:52", tone: "warning" },
],
policy: { tone: "info", text: "Ship low-risk deploys only" },
},
exhausted: {
status: "exhausted",
value: "4 min 18 s",
unit: "over budget",
parts: [
{ kind: "before", minutes: 26.5, label: "Used before today", display: "26 min 30 s" },
{ kind: "today", minutes: 21, label: "Used today", display: "21 min · INC-2291" },
],
budgetEnd: { minutes: 43.2, display: "43 min 12 s" },
rows: [
{ label: "Availability", value: "99.890%" },
{ label: "Burn rate, 1 h", value: "3.2×" },
{ label: "Burn rate, 6 h", value: "58× · paged 09:52", tone: "danger" },
{ label: "Back in budget", value: "Mon 2 Nov" },
],
policy: { tone: "danger", text: "Deploy freeze: rollbacks and fixes only" },
},
};
/* ── Block ──────────────────────────────────────────────────── */
export interface ErrorBudgetProps {
className?: string;
state?: ErrorBudgetState;
/** Defaults to the sample for the state. */
budget?: ErrorBudget;
subtitle?: string;
/** Called by Retry in the error state. Without it, Retry reloads the sample locally. */
onRetry?: () => void;
}
const TONE_ICON = "text-[#007ba7] dark:text-[#5db7e1]";
const PART: Record<BudgetPart, string> = {
left: "bg-success",
before: "bg-[#b4858f] dark:bg-[#946a6c]",
today: "bg-[#b91c1c] dark:bg-[#f06464]",
};
const REFERENCE = "bg-[#8b8b8b] dark:bg-[#929292]";
// Secondary buttons get a raised fill in Dark (white 15% over the card, #474747 on #292929), as in the Figma.
const RAISED = "dark:bg-[color-mix(in_srgb,var(--fg)_15%,transparent)]";
export default function DevopsErrorBudgetBlock({ className = "", state = "within", budget, subtitle = "api-gateway · SLO 99.9% · 30 days", onRetry }: ErrorBudgetProps) {
const id = useId();
const [retry, setRetry] = useState<{ from: ErrorBudgetState; phase: "loading" | "done" } | null>(null);
const timer = useRef<ReturnType<typeof setTimeout>>(undefined);
useEffect(() => () => clearTimeout(timer.current), []);
const local = retry?.from === state ? retry : null;
const shown: ErrorBudgetState = local ? (local.phase === "loading" ? "loading" : "within") : state;
const [announce, setAnnounce] = useState("");
const retryNow = () => {
if (onRetry) return onRetry();
setRetry({ from: state, phase: "loading" });
setAnnounce("Loading the error budget");
clearTimeout(timer.current);
timer.current = setTimeout(() => {
setRetry({ from: state, phase: "done" });
setAnnounce("Error budget loaded");
}, 1200);
};
const b = shown === "within" || shown === "exhausted" ? (budget ?? SAMPLE[shown]) : null;
const over = b?.status === "exhausted";
return (
<MotionConfig reducedMotion="user">
<section
aria-labelledby={id}
aria-busy={shown === "loading" || undefined}
className={"@container flex w-full min-w-0 flex-col gap-4 rounded-[24px] bg-surface p-5 text-fg shadow-[var(--shadow-card)] " + className}
>
<header className="flex flex-col gap-1">
<div className="flex min-h-5 items-center gap-2">
<Gauge aria-hidden className={`size-5 shrink-0 ${TONE_ICON}`} strokeWidth={1.5} />
<h2 id={id} className="min-w-0 flex-1 truncate text-[13px] font-medium leading-5 tracking-[-0.003em]">
Error budget
</h2>
{b &&
(over ? (
<Badge tone="danger" icon={<CircleX className="size-4!" strokeWidth={1.5} />}>
Exhausted
</Badge>
) : (
<Badge tone="success" icon={<CircleCheck className="size-4!" strokeWidth={1.5} />}>
Within budget
</Badge>
))}
</div>
<p className="text-[12px] leading-4 text-fg-muted">{subtitle}</p>
</header>
{shown === "loading" ? (
<LoadingBody />
) : shown === "error" || !b ? (
// Error sits over an invisible copy of the skeleton, which mirrors the Default layout, so the card keeps its height.
<div className="grid flex-1">
<div aria-hidden className="invisible [grid-area:1/1]">
<LoadingBody />
</div>
<div className="flex flex-col items-center justify-center gap-2 text-center [grid-area:1/1]">
<CircleAlert aria-hidden className="size-5 text-danger" strokeWidth={1.5} />
<div role="status" className="flex flex-col items-center gap-2">
<p className="text-[13px] font-medium leading-5">Couldn't load the error budget</p>
<p className="max-w-[320px] text-[12px] leading-4 text-fg-muted">Timed out after 30 s. Try again.</p>
</div>
<div className="pt-2">
<Button variant="secondary" size="md" className={RAISED} leadingIcon={<RotateCw strokeWidth={1.5} />} onClick={retryNow}>
Retry
</Button>
</div>
</div>
</div>
) : (
// From 600 px of room the meter and the numbers sit side by side.
<div className="flex flex-col gap-4 @min-[600px]:grid @min-[600px]:grid-cols-2 @min-[600px]:gap-x-10">
<div className="flex flex-col gap-4">
<p className="flex flex-wrap items-baseline gap-x-2">
<span className={`text-[32px] font-medium leading-10 tracking-[-0.035em] tabular-nums ${over ? "text-danger-text" : ""}`}>{b.value}</span>
<span className="text-[12px] leading-4 text-fg-muted">{b.unit}</span>
</p>
<Meter budget={b} />
</div>
<div className="flex flex-col gap-4">
<dl className="flex flex-col">
{b.rows.map((r) => (
<div key={r.label} className="flex min-h-7 items-center gap-3 text-[13px] leading-5">
<dt className="min-w-0 flex-1 text-fg-muted">{r.label}</dt>
<dd className="shrink-0 text-right font-medium tabular-nums">
{r.tone ? (
<Badge tone={r.tone} icon={r.icon}>
{r.value}
</Badge>
) : (
r.value
)}
</dd>
</div>
))}
</dl>
<p
className={`flex items-start gap-2 rounded-[12px] px-3 py-2 text-[12px] leading-5 text-fg ${b.policy.tone === "danger" ? "bg-danger-soft" : "bg-select-soft"}`}
>
{b.policy.tone === "danger" ? (
<CircleAlert aria-hidden className="size-5 shrink-0 text-danger" strokeWidth={1.5} />
) : (
<Info aria-hidden className="size-5 shrink-0 text-select-text" strokeWidth={1.5} />
)}
<span className="pt-px">
<span className="sr-only">Deploy policy: </span>
{b.policy.text}
</span>
</p>
</div>
</div>
)}
<span className="sr-only" role="status" aria-live="polite">
{announce}
</span>
</section>
</MotionConfig>
);
}
function Meter({ budget: b }: { budget: ErrorBudget }) {
const total = b.parts.reduce((s, p) => s + p.minutes, 0);
const end = b.budgetEnd ? (b.budgetEnd.minutes / total) * 100 : null;
const label =
b.parts.map((p) => `${p.label} ${p.display}`).join(", ") + (b.budgetEnd ? `, budget ${b.budgetEnd.display}, ${b.value} ${b.unit}` : "");
return (
<div className="flex flex-col gap-2">
<div role="img" aria-label={`Error budget: ${label}`} className="relative">
<div className="flex h-2 gap-px overflow-hidden rounded-[4px]">
{b.parts.map((p, i) => (
<motion.span
key={p.kind}
className={`h-full origin-left ${PART[p.kind]}`}
style={{ flex: `${p.minutes} 1 0%` }}
initial={{ scaleX: 0 }}
animate={{ scaleX: 1 }}
transition={{ duration: 0.5, delay: i * 0.12, ease: [0.22, 1, 0.36, 1] }}
/>
))}
</div>
{end !== null && (
// The budget's end: a 1 px tick through the bar with stubs above and below.
<span aria-hidden className={`absolute -inset-y-[3px] w-px ${REFERENCE}`} style={{ left: `${end}%` }} />
)}
</div>
<ul className="flex flex-col gap-1">
{b.parts.map((p) => (
<li key={p.kind} className="flex items-center gap-2 text-[12px] leading-4">
{/* The swatch sits at the start of a 16 px slot, so labels line up with the Budget mark below. */}
<span aria-hidden className="flex w-4 shrink-0">
<span className={`size-2 rounded-[2px] ${PART[p.kind]}`} />
</span>
<span className="min-w-0 flex-1 truncate text-fg-muted">{p.label}</span>
<span className="shrink-0 text-right tabular-nums">{p.display}</span>
</li>
))}
{b.budgetEnd && (
<li className="flex items-center gap-2 text-[12px] leading-4">
{/* A small meter: the track with the budget's end tick, as drawn on the bar above. */}
<span aria-hidden className="relative h-2 w-4 shrink-0 rounded-full bg-track">
<span className={`absolute -inset-y-[3px] right-[3px] w-px ${REFERENCE}`} />
</span>
<span className="min-w-0 flex-1 truncate text-fg-muted">Budget</span>
<span className="shrink-0 text-right tabular-nums">{b.budgetEnd.display}</span>
</li>
)}
</ul>
</div>
);
}
// Slots match the Default rows (the sample's three legend lines, a 37 px policy note), and from 600 px the same two
// columns, so loading and error keep the card's height at every width.
function LoadingBody() {
return (
<SkeletonGroup label="Loading the error budget" className="flex flex-col gap-4 @min-[600px]:grid @min-[600px]:grid-cols-2 @min-[600px]:gap-x-10">
<div className="flex flex-col gap-4">
<div className="flex h-10 items-center gap-2">
<Skeleton shape="block" width={140} height={28} radius={8} />
<Skeleton width={24} height={8} radius={6} className="mt-3" />
</div>
<div className="flex flex-col gap-2">
<Skeleton width="100%" height={8} radius={6} />
<div className="flex flex-col gap-1">
{[0, 1, 2].map((i) => (
<div key={i} className="flex h-4 items-center justify-between">
<Skeleton width={[64, 108, 84][i]} height={8} radius={6} />
<Skeleton width={[64, 40, 96][i]} height={8} radius={6} />
</div>
))}
</div>
</div>
</div>
<div className="flex flex-col gap-4">
<div className="flex flex-col">
{[0, 1, 2, 3].map((i) => (
<div key={i} className="flex h-7 items-center justify-between">
<Skeleton width={[84, 88, 92, 96][i]} height={8} radius={6} />
<Skeleton width={[64, 96, 108, 112][i]} height={8} radius={6} />
</div>
))}
</div>
<Skeleton shape="block" width="100%" height={37} radius={12} />
</div>
</SkeletonGroup>
);
}
Usage
Add the TSX to your React project and install the required dependencies. Styles use Tailwind CSS and theme tokens。
Package dependencies · lucide-react、motion、react
Shared source · badge.tsx、button.tsx、skeleton.tsx