Components
Progressbar
A hand-drawn progress bar with animated pencil-stroke shading that sketches itself in.
Pencil shading
68%
Dots
45%
Zigzag
82%
Change the value
35%
Progressbar draws its track, hachure shading, and outline as separate RoughJS paths. On mount (and whenever the value changes) each path is revealed with a staggered stroke-dashoffset animation, so the bar looks like it is being shaded in by a pencil. A clipped fill group sweeps the shading across to the current value.
Code
tsx
346 lines
"use client" import { useEffect, useId, useRef, useState } from "react" import rough from "roughjs" import { cn } from "@/lib/utils" type ProgressbarVariant = | "yellow" | "purple" | "green" | "pink" | "blue" | "orange" | "gray" | "red" | "sky" | "paper" type ProgressbarFillStyle = | "solid" | "hachure" | "zigzag" | "cross-hatch" | "dots" | "dashed" | "zigzag-line" type ProgressbarProps = { value?: number max?: number width?: number | string height?: number variant?: ProgressbarVariant fillColor?: string trackColor?: string borderColor?: string textColor?: string fillStyle?: ProgressbarFillStyle hachureGap?: number hachureAngle?: number strokeWidth?: number roughness?: number bowing?: number seed?: number duration?: number delay?: number stagger?: number showValue?: boolean showReplay?: boolean label?: string animateOnChange?: boolean className?: string valueClassName?: string replayClassName?: string } const colors: Record<ProgressbarVariant, string> = { yellow: "#fde047", purple: "#d8c7ff", green: "#bbf7d0", pink: "#fbcfe8", blue: "#bfdbfe", orange: "#fed7aa", gray: "#e5e7eb", red: "#fecaca", sky: "#bae6fd", paper: "#fff7df", } function roundedRectPath( x: number, y: number, width: number, height: number, radius: number ) { const r = Math.min(radius, width / 2, height / 2) return ` M ${x + r} ${y} L ${x + width - r} ${y} Q ${x + width} ${y} ${x + width} ${y + r} L ${x + width} ${y + height - r} Q ${x + width} ${y + height} ${x + width - r} ${y + height} L ${x + r} ${y + height} Q ${x} ${y + height} ${x} ${y + height - r} L ${x} ${y + r} Q ${x} ${y} ${x + r} ${y} Z ` } export function Progressbar({ value = 0, max = 100, width = "100%", height = 28, variant = "green", fillColor, trackColor = "#fffbf2", borderColor = "#111", textColor = "#111", fillStyle = "hachure", hachureGap = 5, hachureAngle = -10, strokeWidth = 1.8, roughness = 1.3, bowing = 0.8, seed, duration = 900, delay = 100, stagger = 45, showValue = false, showReplay = false, label = "Progress", animateOnChange = true, className, valueClassName, replayClassName, }: ProgressbarProps) { const wrapperRef = useRef<HTMLDivElement | null>(null) const svgRef = useRef<SVGSVGElement | null>(null) const firstDrawRef = useRef(true) const [animationKey, setAnimationKey] = useState(0) const rawId = useId() const clipId = `sketch-progress-${rawId.replace(/:/g, "")}` const clampedValue = Math.max(0, Math.min(max, value)) const progress = max > 0 ? clampedValue / max : 0 const percent = Math.round(progress * 100) const finalFill = fillColor ?? colors[variant] const finalSeed = seed ?? 460 useEffect(() => { const wrapper = wrapperRef.current const svg = svgRef.current if (!wrapper || !svg) return let timer: number | undefined const draw = () => { const rect = wrapper.getBoundingClientRect() const drawWidth = rect.width const drawHeight = rect.height if (drawWidth <= 0 || drawHeight <= 0) return if (timer) { window.clearTimeout(timer) timer = undefined } svg.replaceChildren() svg.setAttribute("viewBox", `0 0 ${drawWidth} ${drawHeight}`) const ns = "http://www.w3.org/2000/svg" const radius = Math.max(4, drawHeight / 2 - 4) const shape = roundedRectPath( 4, 4, drawWidth - 8, drawHeight - 8, radius ) const defs = document.createElementNS(ns, "defs") const clip = document.createElementNS(ns, "clipPath") clip.setAttribute("id", clipId) clip.setAttribute("clipPathUnits", "userSpaceOnUse") const clipRect = document.createElementNS(ns, "rect") clipRect.setAttribute("x", "0") clipRect.setAttribute("y", "0") clipRect.setAttribute("width", String(drawWidth)) clipRect.setAttribute("height", String(drawHeight)) clipRect.style.transformOrigin = "left center" clipRect.style.transform = "scaleX(0)" clip.appendChild(clipRect) defs.appendChild(clip) svg.appendChild(defs) const rc = rough.svg(svg) const track = rc.path(shape, { seed: finalSeed + 4, stroke: "transparent", strokeWidth: 0, fill: trackColor, fillStyle: "solid", roughness: roughness * 0.5, bowing, }) svg.appendChild(track) const fillGroup = document.createElementNS(ns, "g") fillGroup.setAttribute("clip-path", `url(#${clipId})`) const fill = rc.path(shape, { seed: finalSeed, stroke: finalFill, strokeWidth, fill: finalFill, fillStyle, hachureGap, hachureAngle, roughness, bowing, }) fillGroup.appendChild(fill) svg.appendChild(fillGroup) const outline = rc.path(shape, { seed: finalSeed + 8, stroke: borderColor, strokeWidth, roughness, bowing, }) svg.appendChild(outline) const paths = Array.from(svg.querySelectorAll("path")) const reducedMotion = window.matchMedia( "(prefers-reduced-motion: reduce)" ).matches const shouldAnimate = firstDrawRef.current || animateOnChange || animationKey > 0 firstDrawRef.current = false if (reducedMotion || !shouldAnimate) { paths.forEach((path) => { path.style.strokeDashoffset = "0" }) clipRect.style.transition = `transform ${duration}ms ease-out` clipRect.style.transform = `scaleX(${progress})` return } paths.forEach((path) => { const length = path.getTotalLength() path.style.strokeDasharray = `${length}` path.style.strokeDashoffset = `${length}` path.getBoundingClientRect() }) clipRect.style.transition = `transform ${duration}ms ease-out` timer = window.setTimeout(() => { paths.forEach((path, index) => { path.style.transition = `stroke-dashoffset ${duration}ms ease-out ${index * stagger}ms` path.style.strokeDashoffset = "0" }) clipRect.style.transform = `scaleX(${progress})` }, delay) } draw() const observer = new ResizeObserver(draw) observer.observe(wrapper) return () => { if (timer) { window.clearTimeout(timer) } observer.disconnect() } }, [ animationKey, progress, animateOnChange, clipId, finalFill, finalSeed, trackColor, borderColor, fillStyle, hachureGap, hachureAngle, strokeWidth, roughness, bowing, duration, delay, stagger, value, ]) return ( <div className={cn("inline-flex flex-col gap-2", className)} style={{ width }} > <div ref={wrapperRef} role="progressbar" aria-label={label} aria-valuenow={clampedValue} aria-valuemin={0} aria-valuemax={max} className="relative" style={{ width: "100%", height }} > <svg ref={svgRef} className="pointer-events-none absolute inset-0 h-full w-full overflow-visible" aria-hidden="true" /> {showValue && ( <span className={cn( "absolute inset-y-0 right-3 z-10 flex items-center", "font-family-hand text-sm font-black", valueClassName )} style={{ color: textColor }} > {percent}% </span> )} </div> {showReplay && ( <button type="button" onClick={() => setAnimationKey((prev) => prev + 1)} className={cn( "self-start rounded-lg border-2 border-black bg-[#fffbf2] px-3 py-1", "font-family-hand text-xs font-bold text-black", "outline-none transition-transform hover:-rotate-1 active:scale-95", "focus-visible:ring-2 focus-visible:ring-black focus-visible:ring-offset-2", replayClassName )} > Redraw strokes </button> )} </div> ) }
Usage
tsx
"use client" import { useState } from "react" import { Progressbar } from "@/components/ui/Progressbar" export function Example() { const [value, setValue] = useState(35) return ( <> <Progressbar value={value} variant="yellow" fillStyle="hachure" showValue showReplay width={320} /> <button type="button" onClick={() => setValue(80)}> Set to 80% </button> </> ) }
Pencil-stroke animation
- On each draw the track, the hachure shading, and the outline are converted to dashed paths with
strokeDashoffsetequal to their length, then transitioned to0with a per-pathstaggerdelay. This is what makes the pencil strokes appear one by one. - At the same time an SVG
clipPathscales fromscaleX(0)to the value fraction (duration), revealing the shading from left to right. durationcontrols both animations,delaywaits before the animation begins, andstaggerspaces the strokes.animateOnChangedefaults totrue, so changingvaluereplays the sketch. Set it tofalsefor a simple width transition on updates.- Reduced-motion preferences are respected: the strokes appear instantly and the fill jumps to its value.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | 0 | Current progress value. |
max | number | 100 | Maximum value. |
width | number | string | "100%" | Width of the bar. |
height | number | 28 | Height of the bar. |
variant | ProgressbarVariant | "green" | Fill color theme. |
fillColor | string | - | Overrides the variant fill color. |
trackColor | string | "#fffbf2" | Background color of the empty bar. |
borderColor | string | "#111" | Outline stroke color. |
textColor | string | "#111" | Color of the value label. |
fillStyle | ProgressbarFillStyle | "hachure" | RoughJS fill pattern for the shading. |
hachureGap | number | 5 | Gap between pencil strokes. |
hachureAngle | number | -10 | Angle of the pencil strokes. |
strokeWidth | number | 1.8 | Stroke thickness. |
roughness | number | 1.3 | Hand-drawn roughness. |
bowing | number | 0.8 | Line curvature. |
seed | number | 460 | RoughJS seed; set it for a stable sketch. |
duration | number | 900 | Animation length in milliseconds. |
delay | number | 100 | Delay before the animation starts. |
stagger | number | 45 | Delay between individual stroke animations. |
showValue | boolean | false | Shows the percentage inside the bar. |
showReplay | boolean | false | Shows a "Redraw strokes" button. |
label | string | "Progress" | Accessible label for the progressbar role. |
animateOnChange | boolean | true | Replays the stroke animation when value changes. |
className | string | - | Classes for the outer wrapper. |
valueClassName | string | - | Classes for the percentage label. |
replayClassName | string | - | Classes for the replay button. |
Fill Styles
- solid
- hachure
- zigzag
- cross-hatch
- dots
- dashed
- zigzag-line
Variants
- yellow
- purple
- green
- pink
- blue
- orange
- gray
- red
- sky
- paper
Accessibility
The bar uses role="progressbar" with aria-valuenow, aria-valuemin, aria-valuemax, and an aria-label. The decorative SVG is hidden from assistive technology, and the replay button has a visible focus ring.