Liquid Gauge
An interactive liquid-filled gauge with realistic physics simulation, including wave dynamics, bubbles, and smooth animations.
The Liquid Gauge is a visually stunning and interactive component that simulates realistic liquid physics. It features wave dynamics, rising bubbles, spring-based animations, and interactive gestures for an engaging user experience. Perfect for visualizing storage, progress, or capacity metrics.
Installation
npx @ayushmxxn/shadcn add liquid-gaugeCopy and paste the component source into your project.
File Structure
The component follows a simple structure with the main component file:
Usage
Import the component and use it to display a liquid-filled gauge:
import LiquidGauge from "@/components/craft-blocks/cards/liquid-gauge/liquid-gauge";
export default function StorageDemo() {
return <LiquidGauge />;
}Controlled Component
Control the fill level externally with the value prop:
import { useState } from "react";
import LiquidGauge from "@/components/craft-blocks/cards/liquid-gauge/liquid-gauge";
export default function ControlledGauge() {
const [storage, setStorage] = useState(72);
return (
<div>
<LiquidGauge
value={storage}
onValueChange={setStorage}
label="Storage"
total={128}
unit="GB"
/>
<button onClick={() => setStorage(storage + 10)}>
Add 10%
</button>
</div>
);
}Read-Only Mode
Display the gauge as a non-interactive visualization:
<LiquidGauge
value={85}
readOnly
label="CPU Usage"
total={100}
unit="%"
warnAt={80}
/>Custom Warning Threshold
Set a custom threshold where the liquid changes to a warning color:
<LiquidGauge
defaultValue={95}
label="Memory"
total={16}
unit="GB"
warnAt={90}
/>Theming
The component uses Tailwind CSS classes and supports automatic dark mode:
- Container: Rounded card with border (
rounded-3xl,border-zinc-200) - Liquid Colors: Changes based on warning threshold
- Normal:
bg-zinc-900(dark mode:bg-zinc-100) - Warning:
bg-red-500
- Normal:
- Meniscus: Lighter band above liquid surface (
bg-zinc-300/bg-red-300) - Typography: Clean hierarchy with percentage display and capacity readout
Customize the appearance:
<LiquidGauge
className="w-80 h-96 bg-gradient-to-b from-slate-50 to-slate-100"
// Custom warning colors can be modified in the component
/>Animations
The component features sophisticated physics-based animations:
Wave Dynamics
- Surface Springs: 28 columns connected by springs create realistic wave motion
- Propagation: Disturbances travel across the surface naturally
- Damping: Waves gradually settle to a flat surface at rest
- Clamp: Waves limited to realistic height (
MAX_WAVE = 26px)
Level Changes
- Spring Physics: Fill level follows an underdamped spring simulation
- Overshoot: Large changes overshoot slightly before settling (like pouring liquid)
- Smooth Transitions: Stiffness and damping values create natural motion
Bubbles
- Rising Bubbles: Generated while level is climbing
- Physics: Each bubble has unique size, speed, and trajectory
- Horizontal Sway: Bubbles drift side-to-side as they rise
- Pop Effect: Bubbles pop at the surface, creating small ripples
Interactive Gestures
- Drag to Fill: Vertical drag adjusts the fill level
- Rock Motion: Horizontal drag creates side-to-side wave motion
- Drop Effect: Pointer down creates impact ripples
- Hover Stir: Moving through liquid creates trailing disturbances
Performance
- Fixed Timestep: Simulation runs at 120Hz for stability
- RAF Loop: RequestAnimationFrame for smooth 60fps rendering
- Auto Sleep: Animation pauses when settled and no user interaction
- Visibility Aware: Pauses when offscreen or in background tab
How It Works
The liquid gauge implements a sophisticated physics simulation:
1. Surface Simulation
The surface is modeled as 28 columns connected by springs. Each column:
- Pulls toward its neighbors (spread force)
- Returns to the fill line (restore force)
- Experiences damping to settle over time
const accel =
(left + right - 2 * heights[i]) * SPREAD -
heights[i] * RESTORE -
speeds[i] * SURFACE_DAMPING;2. Fill Level Spring
The fill line follows the target value on an underdamped spring:
const force =
(target - level) * LEVEL_STIFFNESS -
levelSpeed * LEVEL_DAMPING;3. Fixed Timestep Integration
Physics updates use a fixed 1/120s timestep for stability:
- Accumulates delta time
- Steps simulation in fixed increments
- Draws at display refresh rate
4. Clip Path Rendering
The liquid shape is rendered using CSS clip-path:
- Polygon connects all column heights
- Updated every frame for smooth animation
- Separate layer for meniscus band
5. Gesture System
- Pointer Events: Tracks pointer position and type
- Vertical Drag: Maps to fill percentage
- Horizontal Drag: Creates rocking motion across columns
- Keyboard: Arrow keys adjust level with optional Shift multiplier
Props
Prop
Type
Accessibility
The component implements comprehensive accessibility features:
- Slider Role: Uses
role="slider"for proper semantic meaning - ARIA Labels: Provides descriptive labels and value text
- Keyboard Control: Full keyboard navigation support
- Arrow keys: Adjust by 1% (Shift: 10%)
- Page Up/Down: Adjust by 10%
- Home/End: Jump to 0% or 100%
- Screen Readers: Announces current value and context
- Focus Indicators: Clear focus ring for keyboard navigation
- Motion Preferences: Respects
prefers-reduced-motionsetting - Haptic Feedback: Vibration on mobile devices (when supported)
ARIA attributes:
<div
role="slider"
aria-label="Storage"
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={72}
aria-valuetext="72%, 92.2 of 128 GB used"
aria-readonly={readOnly || undefined}
aria-orientation="vertical"
/>Registry Bundle
This component is available as a registry bundle and can be installed with a single command. The bundle includes all necessary dependencies and configurations.
Contact
If you have any questions or need assistance with this component, please don't hesitate to reach out via email.
Last updated on