"use client";
// src/components/HeartbeatLoader/HeartbeatLoader.tsx
import { motion, useReducedMotion } from "motion/react";
import { useId } from "react";
type HeartbeatLoaderProps = {
className?: string;
/** Width of the heart in px - the heart is always square */
size?: number;
/** Fill color - use your clinic or brand color */
color?: string;
/** Seconds for one fill-and-empty cycle */
speed?: number;
/**
* Optional 0-100. When set, the heart fills to that level and stays there
* (a real progress bar) instead of looping.
*/
progress?: number;
/** Beats per minute - controls how fast the heart pulses and the ECG line runs */
bpm?: number;
/** Make the heart pulse like a real heartbeat */
beat?: boolean;
/** Show the ECG (heart monitor) line under the heart */
ecg?: boolean;
/** Text under the loader. Pass an empty string to hide it */
label?: string;
};
// viewBox is 120 x 120
const HEART =
"M 60 104 C 60 104 14 77 14 44 C 14 28 26 16 41 16 C 50 16 57 21 60 29 C 63 21 70 16 79 16 C 94 16 106 28 106 44 C 106 77 60 104 60 104 Z";
// Liquid level positions (y of the surface)
const LEVEL_EMPTY = 108;
const LEVEL_FULL = 20;
const WAVE_AMP = 3;
const FRONT_WAVELENGTH = 40;
const BACK_WAVELENGTH = 52;
// ECG line, viewBox 120 x 30: flat, small P bump, sharp QRS spike, T bump, flat
const ECG =
"M 0 15 H 26 Q 30 10 34 15 H 40 L 44 19 L 49 2 L 54 28 L 58 15 H 66 Q 72 8 78 15 H 120";
/**
* A repeating wave with its resting surface at y = 0, filled down to y = 200.
* It's wider than the heart so it can slide sideways by one wavelength
* and loop without any jump.
*/
function wavePath(wavelength: number) {
const start = -wavelength * 2;
const end = 120 + wavelength * 2;
let d = `M ${start} 0`;
for (let x = start; x < end; x += wavelength) {
d +=
` Q ${x + wavelength / 4} ${-WAVE_AMP} ${x + wavelength / 2} 0` +
` Q ${x + (wavelength * 3) / 4} ${WAVE_AMP} ${x + wavelength} 0`;
}
return `${d} L ${end} 200 L ${start} 200 Z`;
}
const FRONT_WAVE = wavePath(FRONT_WAVELENGTH);
const BACK_WAVE = wavePath(BACK_WAVELENGTH);
export function HeartbeatLoader({
className = "",
size = 120,
color = "#E11D48",
speed = 4,
progress,
bpm = 72,
beat = true,
ecg = true,
label = "Loading",
}: HeartbeatLoaderProps) {
const uid = useId().replace(/:/g, "");
const reduce = useReducedMotion();
const isProgress = typeof progress === "number";
const clamped = isProgress ? Math.min(100, Math.max(0, progress)) : 65;
const targetLevel =
LEVEL_EMPTY - (clamped / 100) * (LEVEL_EMPTY - LEVEL_FULL);
// One heartbeat in seconds (72 bpm = 0.83s)
const beatDuration = 60 / Math.min(200, Math.max(30, bpm));
// How the liquid surface moves:
// progress mode -> glide to the target level
// loop mode -> fill up, hold, then drain
// reduced motion -> sit still
const fill =
reduce || isProgress
? {
animate: { y: targetLevel },
transition: reduce
? { duration: 0 }
: { type: "spring" as const, stiffness: 60, damping: 16 },
}
: {
animate: { y: [LEVEL_EMPTY, LEVEL_FULL, LEVEL_FULL, LEVEL_EMPTY] },
transition: {
duration: speed,
times: [0, 0.55, 0.75, 1],
ease: "easeInOut" as const,
repeat: Infinity,
},
};
const startLevel = isProgress || reduce ? targetLevel : LEVEL_EMPTY;
// "lub-dub": a big pulse, a small pulse, then rest
const pulse =
beat && !reduce
? {
animate: { scale: [1, 1.08, 1, 1.04, 1] },
transition: {
duration: beatDuration,
times: [0, 0.14, 0.28, 0.4, 1],
ease: "easeOut" as const,
repeat: Infinity,
},
}
: { animate: { scale: 1 } };
const a11y = isProgress
? {
role: "progressbar",
"aria-valuemin": 0,
"aria-valuemax": 100,
"aria-valuenow": Math.round(clamped),
"aria-label": label || "Loading",
}
: { role: "status", "aria-label": label || "Loading" };
return (
<div className={`flex flex-col items-center gap-3 ${className}`} {...a11y}>
<motion.div
className="relative aspect-square"
style={{ width: size, maxWidth: "100%" }}
{...pulse}
>
<svg
viewBox="0 0 120 120"
className="h-full w-full overflow-visible"
fill="none"
aria-hidden="true"
>
<defs>
<clipPath id={`heart-${uid}`}>
<path d={HEART} />
</clipPath>
{/* Slightly darker toward the bottom for depth */}
<linearGradient id={`depth-${uid}`} x1="0" y1="0" x2="0" y2="1">
<stop offset="0%" stopColor="#000" stopOpacity={0} />
<stop offset="100%" stopColor="#000" stopOpacity={0.25} />
</linearGradient>
</defs>
{/* Soft tint inside the empty heart */}
<path d={HEART} fill={color} fillOpacity={0.08} />
<g clipPath={`url(#heart-${uid})`}>
{/* Liquid */}
<motion.g initial={{ y: startLevel }} {...fill}>
{/* Back wave - lighter, slower, moving the other way */}
<motion.path
d={BACK_WAVE}
fill={color}
fillOpacity={0.45}
initial={{ x: -BACK_WAVELENGTH }}
animate={
reduce
? { x: -BACK_WAVELENGTH }
: { x: [-BACK_WAVELENGTH, 0] }
}
transition={
reduce
? { duration: 0 }
: { duration: 2.6, ease: "linear", repeat: Infinity }
}
style={{ translateY: -2 }}
/>
{/* Front wave, with a bright surface line */}
<motion.path
d={FRONT_WAVE}
fill={color}
stroke="#fff"
strokeOpacity={0.4}
strokeWidth={1.2}
initial={{ x: 0 }}
animate={reduce ? { x: 0 } : { x: [0, -FRONT_WAVELENGTH] }}
transition={
reduce
? { duration: 0 }
: { duration: 1.8, ease: "linear", repeat: Infinity }
}
/>
</motion.g>
{/* Depth shading over the liquid */}
<rect
x={0}
y={10}
width={120}
height={100}
fill={`url(#depth-${uid})`}
/>
</g>
{/* Heart outline and shine */}
<path
d={HEART}
stroke={color}
strokeWidth={3}
strokeLinejoin="round"
/>
<path
d="M 25 44 Q 25 31 37 27"
stroke="#fff"
strokeOpacity={0.55}
strokeWidth={3.5}
strokeLinecap="round"
/>
</svg>
</motion.div>
{/* ECG heart monitor line */}
{ecg && (
<svg
viewBox="0 0 120 30"
className="h-auto overflow-visible"
style={{ width: size * 1.1, maxWidth: "100%" }}
fill="none"
aria-hidden="true"
>
{/* Faint track so the shape is always readable */}
<path
d={ECG}
stroke="currentColor"
strokeOpacity={0.12}
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
/>
<motion.path
d={ECG}
stroke={color}
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
initial={{ pathLength: reduce ? 1 : 0 }}
animate={
reduce
? { pathLength: 1 }
: { pathLength: [0, 1, 1], opacity: [1, 1, 0] }
}
transition={
reduce
? { duration: 0 }
: {
duration: beatDuration * 2,
times: [0, 0.75, 1],
ease: "linear",
repeat: Infinity,
}
}
/>
</svg>
)}
{label && (
<motion.span
aria-hidden="true"
className="text-sm font-medium text-neutral-500 dark:text-neutral-400"
animate={reduce || isProgress ? {} : { opacity: [0.45, 1, 0.45] }}
transition={{ duration: 1.6, repeat: Infinity, ease: "easeInOut" }}
>
{isProgress ? `${label} ${Math.round(clamped)}%` : label}
</motion.span>
)}
</div>
);
}
export default HeartbeatLoader;
// Component Source: https://codekk.devComponent details
Overview, install command, dependencies, and project links.
A heart-shaped liquid fill loader for React built with TypeScript, Motion, SVG, and Tailwind CSS v4, designed for doctors, clinics, hospitals, diagnostic labs, pharmacies, and health apps. The heart fills with liquid, holds for a moment, then drains and refills in a smooth loop. The surface is made of two wave layers that slide in opposite directions at different speeds, so the liquid looks like it is really moving. Each wave is wider than the heart and shifts by exactly one wavelength per loop, so the motion never jumps. While it fills, the heart pulses with a real "lub-dub" rhythm: one big beat, one small beat, then a rest. The bpm prop controls the speed, so 60 feels calm and 100 feels active. Under the heart, an ECG monitor line draws itself from left to right and fades out, in sync with the heart rate, over a faint track so the shape is always readable. The color prop sets the heart, waves, and ECG line to any brand color, such as red for cardiology, teal for clinics, or blue for hospitals. The beat and ecg props turn the pulse and the monitor line on or off. The optional progress prop turns the loader into a real progress indicator: the heart fills smoothly to the given level with spring motion and stays there, the label shows the percentage, and the wrapper switches to role="progressbar" with proper ARIA values. This is useful for report uploads, appointment booking, and payment steps. Clip and gradient IDs come from React's useId, so multiple loaders can share a page without conflicts. When prefers-reduced-motion is enabled, the heart stops beating, the ECG line is shown fully drawn, and the heart sits still at 65% full, or at the progress level if one is set. "use client" is included, so it works in Vite, Create React App, and Next.js.
Install
npm create vite@latest my-app -- --template react-ts cd my-app npm install motion npm run devDependencies
Discussion
Feedback, implementation tips, and usage notes.
Comments
Sign in to join the conversation
Similar Components
More from the same category




