Gauge
import { useEffect, useRef, useState } from 'react';
const css = `
.card-gauge {
width: 210px;
padding: 14px 14px 12px;
border-radius: 16px;
background: light-dark(#f3f4f7, #1a1c1f);
box-shadow: inset 0 0 0 1px light-dark(#dbdde1, #2e3035);
text-align: center;
cursor: pointer;
}
.card-gauge:focus-visible { outline: 2px solid light-dark(#42454c, #e5e6e9); outline-offset: 3px; }
.card-gauge svg { display: block; width: 182px; height: 104px; overflow: visible; }
.card-gauge__track { fill: none; stroke: light-dark(#ebedf0, #232529); stroke-width: 10; stroke-linecap: round; }
.card-gauge__fill {
fill: none;
stroke: light-dark(#373a40, #f1f2f3);
stroke-width: 10;
stroke-linecap: round;
stroke-dasharray: 1;
transition: stroke-dashoffset 1.1s cubic-bezier(0.34, 1.25, 0.64, 1), opacity 0.2s ease;
}
.card-gauge__tick { stroke: light-dark(#cbced3, #393c41); stroke-width: 1.5; }
.card-gauge__tick--major { stroke: light-dark(#5f626a, #a7abb2); stroke-width: 2; }
.card-gauge__needle {
transform-origin: 91px 92px;
transition: transform 1.1s cubic-bezier(0.34, 1.45, 0.64, 1);
}
.card-gauge__needle path { fill: light-dark(#373a40, #f1f2f3); }
.card-gauge__hub { fill: light-dark(#f3f4f7, #1a1c1f); stroke: light-dark(#373a40, #f1f2f3); stroke-width: 2.5; }
.card-gauge__v { margin-top: -4px; color: light-dark(#373a40, #f1f2f3); font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.card-gauge__v small { color: light-dark(#5f626a, #a7abb2); font-size: 11px; font-weight: 500; margin-left: 2px; }
.card-gauge__k { color: light-dark(#5f626a, #a7abb2); font-size: 11px; }
@media (prefers-reduced-motion: reduce) { .card-gauge__fill, .card-gauge__needle { transition-duration: 0.01s; } }
`;
const CX = 91;
const CY = 92;
const R = 76;
const VALUE = 72; // out of 100
const ARC = `M${CX - R} ${CY}A${R} ${R} 0 0 1 ${CX + R} ${CY}`;
export function GaugeCard() {
const [on, setOn] = useState(false);
const [shown, setShown] = useState(0);
const raf = useRef(0);
// The readout counts toward wherever the needle is heading.
useEffect(() => {
const target = on ? VALUE : 0;
const from = shown;
const start = performance.now();
cancelAnimationFrame(raf.current);
const tick = (now: number) => {
const p = Math.min(1, (now - start) / 1000);
setShown(Math.round(from + (target - from) * (1 - (1 - p) ** 3)));
if (p < 1) raf.current = requestAnimationFrame(tick);
};
raf.current = requestAnimationFrame(tick);
return () => cancelAnimationFrame(raf.current);
}, [on]);
const v = on ? VALUE : 0;
return (
<>
<style>
{css}
</style>
<div
className="card-gauge"
tabIndex={0}
role="meter"
aria-label="CPU load"
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={shown}
onPointerEnter={() => setOn(true)}
onPointerLeave={() => setOn(false)}
onFocus={() => setOn(true)}
onBlur={() => setOn(false)}
>
<svg viewBox="0 0 182 104" aria-hidden="true">
<path className="card-gauge__track" d={ARC} />
<path className="card-gauge__fill" d={ARC} pathLength={1} style={{ strokeDashoffset: 1 - v / 100, opacity: shown > 0 ? 1 : 0 }} />
{Array.from({ length: 21 }, (_, i) => {
const a = Math.PI * (1 - i / 20);
const major = i % 5 === 0;
const r1 = R - 14;
const r2 = R - (major ? 22 : 18);
return (
<line
key={i}
className={`card-gauge__tick${major ? ' card-gauge__tick--major' : ''}`}
// Rounded, so the server and browser print identical attributes.
x1={(CX + Math.cos(a) * r1).toFixed(2)}
y1={(CY - Math.sin(a) * r1).toFixed(2)}
x2={(CX + Math.cos(a) * r2).toFixed(2)}
y2={(CY - Math.sin(a) * r2).toFixed(2)}
/>
);
})}
{/* The needle points left at 0 and turns 180deg across the dial. */}
<g className="card-gauge__needle" style={{ transform: `rotate(${(v / 100) * 180}deg)` }}>
<path d={`M${CX - 58} ${CY}L${CX} ${CY - 3}L${CX} ${CY + 3}Z`} />
</g>
<circle className="card-gauge__hub" cx={CX} cy={CY} r={6} />
</svg>
<div className="card-gauge__v">
{shown}
<small>%</small>
</div>
<div className="card-gauge__k">CPU load</div>
</div>
</>
);
}Markup where the component goes, CSS in your stylesheet, script before </body>. In Vue, Svelte or Angular, call initCardGauge(el) on the mounted element instead.
<div class="card-gauge" tabindex="0" role="meter" aria-label="CPU load" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0">
<svg viewBox="0 0 182 104" aria-hidden="true">
<path class="card-gauge__track" d="M15 92A76 76 0 0 1 167 92"></path>
<path class="card-gauge__fill" d="M15 92A76 76 0 0 1 167 92" pathLength="1" style="stroke-dashoffset:1;opacity:0"></path>
<line class="card-gauge__tick card-gauge__tick--major" x1="29.00" y1="92.00" x2="37.00" y2="92.00"></line>
<line class="card-gauge__tick" x1="29.76" y1="82.30" x2="33.71" y2="82.93"></line>
<line class="card-gauge__tick" x1="32.03" y1="72.84" x2="35.84" y2="74.08"></line>
<line class="card-gauge__tick" x1="35.76" y1="63.85" x2="39.32" y2="65.67"></line>
<line class="card-gauge__tick" x1="40.84" y1="55.56" x2="44.08" y2="57.91"></line>
<line class="card-gauge__tick card-gauge__tick--major" x1="47.16" y1="48.16" x2="52.82" y2="53.82"></line>
<line class="card-gauge__tick" x1="54.56" y1="41.84" x2="56.91" y2="45.08"></line>
<line class="card-gauge__tick" x1="62.85" y1="36.76" x2="64.67" y2="40.32"></line>
<line class="card-gauge__tick" x1="71.84" y1="33.03" x2="73.08" y2="36.84"></line>
<line class="card-gauge__tick" x1="81.30" y1="30.76" x2="81.93" y2="34.71"></line>
<line class="card-gauge__tick card-gauge__tick--major" x1="91.00" y1="30.00" x2="91.00" y2="38.00"></line>
<line class="card-gauge__tick" x1="100.70" y1="30.76" x2="100.07" y2="34.71"></line>
<line class="card-gauge__tick" x1="110.16" y1="33.03" x2="108.92" y2="36.84"></line>
<line class="card-gauge__tick" x1="119.15" y1="36.76" x2="117.33" y2="40.32"></line>
<line class="card-gauge__tick" x1="127.44" y1="41.84" x2="125.09" y2="45.08"></line>
<line class="card-gauge__tick card-gauge__tick--major" x1="134.84" y1="48.16" x2="129.18" y2="53.82"></line>
<line class="card-gauge__tick" x1="141.16" y1="55.56" x2="137.92" y2="57.91"></line>
<line class="card-gauge__tick" x1="146.24" y1="63.85" x2="142.68" y2="65.67"></line>
<line class="card-gauge__tick" x1="149.97" y1="72.84" x2="146.16" y2="74.08"></line>
<line class="card-gauge__tick" x1="152.24" y1="82.30" x2="148.29" y2="82.93"></line>
<line class="card-gauge__tick card-gauge__tick--major" x1="153.00" y1="92.00" x2="145.00" y2="92.00"></line>
<g class="card-gauge__needle" style="transform:rotate(0deg)">
<path d="M33 92L91 89L91 95Z"></path>
</g>
<circle class="card-gauge__hub" cx="91" cy="92" r="6"></circle>
</svg>
<div class="card-gauge__v">0<small>%</small></div>
<div class="card-gauge__k">CPU load</div>
</div>.card-gauge {
width: 210px;
padding: 14px 14px 12px;
border-radius: 16px;
background: light-dark(#f3f4f7, #1a1c1f);
box-shadow: inset 0 0 0 1px light-dark(#dbdde1, #2e3035);
text-align: center;
cursor: pointer;
}
.card-gauge:focus-visible { outline: 2px solid light-dark(#42454c, #e5e6e9); outline-offset: 3px; }
.card-gauge svg { display: block; width: 182px; height: 104px; overflow: visible; }
.card-gauge__track { fill: none; stroke: light-dark(#ebedf0, #232529); stroke-width: 10; stroke-linecap: round; }
.card-gauge__fill {
fill: none;
stroke: light-dark(#373a40, #f1f2f3);
stroke-width: 10;
stroke-linecap: round;
stroke-dasharray: 1;
transition: stroke-dashoffset 1.1s cubic-bezier(0.34, 1.25, 0.64, 1), opacity 0.2s ease;
}
.card-gauge__tick { stroke: light-dark(#cbced3, #393c41); stroke-width: 1.5; }
.card-gauge__tick--major { stroke: light-dark(#5f626a, #a7abb2); stroke-width: 2; }
.card-gauge__needle {
transform-origin: 91px 92px;
transition: transform 1.1s cubic-bezier(0.34, 1.45, 0.64, 1);
}
.card-gauge__needle path { fill: light-dark(#373a40, #f1f2f3); }
.card-gauge__hub { fill: light-dark(#f3f4f7, #1a1c1f); stroke: light-dark(#373a40, #f1f2f3); stroke-width: 2.5; }
.card-gauge__v { margin-top: -4px; color: light-dark(#373a40, #f1f2f3); font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.card-gauge__v small { color: light-dark(#5f626a, #a7abb2); font-size: 11px; font-weight: 500; margin-left: 2px; }
.card-gauge__k { color: light-dark(#5f626a, #a7abb2); font-size: 11px; }
@media (prefers-reduced-motion: reduce) { .card-gauge__fill, .card-gauge__needle { transition-duration: 0.01s; } }function initCardGauge(root) {
var fill = root.querySelector('.card-gauge__fill');
var needle = root.querySelector('.card-gauge__needle');
var value = root.querySelector('.card-gauge__v');
if (!fill || !needle || !value) return;
var VALUE = 72; // out of 100
var shown = 0;
var raf = 0;
function set(on) {
var target = on ? VALUE : 0;
fill.style.strokeDashoffset = String(1 - target / 100);
needle.style.transform = 'rotate(' + (target / 100) * 180 + 'deg)';
// The readout counts toward wherever the needle is heading.
var from = shown;
var start = performance.now();
cancelAnimationFrame(raf);
function tick(now) {
var p = Math.min(1, (now - start) / 1000);
shown = Math.round(from + (target - from) * (1 - Math.pow(1 - p, 3)));
value.firstChild.textContent = String(shown);
fill.style.opacity = shown > 0 ? '1' : '0';
root.setAttribute('aria-valuenow', String(shown));
if (p < 1) raf = requestAnimationFrame(tick);
}
raf = requestAnimationFrame(tick);
}
function on() { set(true); }
function off() { set(false); }
root.addEventListener('pointerenter', on);
root.addEventListener('pointerleave', off);
root.addEventListener('focus', on);
root.addEventListener('blur', off);
return function () {
cancelAnimationFrame(raf);
root.removeEventListener('pointerenter', on);
root.removeEventListener('pointerleave', off);
root.removeEventListener('focus', on);
root.removeEventListener('blur', off);
};
}
document.querySelectorAll('.card-gauge').forEach(initCardGauge);Goal: Create a "Gauge" card.
Visual behavior: A speedometer dial for CPU load: on hover the needle sweeps up from zero with a springy overshoot, the arc fills behind it and the readout counts up to match; leave and it all falls back to zero.
Technique:
- The dial is an SVG semicircle arc used twice: a dim track and a fill with pathLength 1 whose dashoffset is 1 minus the value.
- Twenty-one ticks (every fifth major) are placed by angle; the needle is a thin triangle in a group rotated from 0 to value x 180deg about the hub.
- Fill and needle share a 1.1s overshooting transition, so the needle rocks past the value and settles; the fill hides at zero so its round cap never shows as a dot.
- The number tweens toward the target with an ease-out on requestAnimationFrame and feeds aria-valuenow.
Accessibility: The card is a meter (role="meter", "CPU load") with aria-valuenow; it is focusable and focus plays the sweep like hover. Under prefers-reduced-motion the needle and fill jump.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: neutral only. Black, white and grays, no hues. Use the color values above as written, or my project's closest neutral tokens.
Return a single self-contained component. No dependencies.Goal: Create a "Gauge" card.
Visual behavior: A speedometer dial for CPU load: on hover the needle sweeps up from zero with a springy overshoot, the arc fills behind it and the readout counts up to match; leave and it all falls back to zero.
Technique:
- The dial is an SVG semicircle arc used twice: a dim track and a fill with pathLength 1 whose dashoffset is 1 minus the value.
- Twenty-one ticks (every fifth major) are placed by angle; the needle is a thin triangle in a group rotated from 0 to value x 180deg about the hub.
- Fill and needle share a 1.1s overshooting transition, so the needle rocks past the value and settles; the fill hides at zero so its round cap never shows as a dot.
- The number tweens toward the target with an ease-out on requestAnimationFrame and feeds aria-valuenow.
Accessibility: The card is a meter (role="meter", "CPU load") with aria-valuenow; it is focusable and focus plays the sweep like hover. Under prefers-reduced-motion the needle and fill jump.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: use my project's existing color tokens. The color values above are grayscale placeholders. Map each one by its role (surface, text, border, accent, glow) to my theme, and keep their relative lightness so contrast and the effect still read.
Return a single self-contained component. No dependencies.Paste into Claude, ChatGPT or Cursor after filling in {{USER_STACK}}, {{USER_STYLING}} and {{USER_CONSTRAINTS}}. Neutral keeps the greys you see here; Match my project asks the model to map them onto your own colour tokens.
Free to use in any project, commercial included. MIT-0, no attribution required. Licence
About this component
A speedometer dial for CPU load: on hover the needle sweeps up from zero with a springy overshoot, the arc fills behind it and the readout counts up to match; leave and it all falls back to zero.
- Dependencies
- None
- Difficulty
- Intermediate
- Triggers
- hover, spring, fill, card
- Techniques
- svg, stroke dashoffset, css transition, raf
- Reduced motion
- Respected
- Theme
- Light and dark (follows your page's color-scheme)
The card is a meter (role="meter", "CPU load") with aria-valuenow; it is focusable and focus plays the sweep like hover. Under prefers-reduced-motion the needle and fill jump.