# Liquid Toggle

> A switch whose knob stretches like liquid on its way across, then settles.

Category: Inputs · Free · Updated 2026-10-20 · Live demo: https://www.motionkitui.com/components/liquid-toggle

## When to use it

Settings screens, dark-mode switches and pricing toggles, where a switch should feel tactile.

## How it works

It's a real switch (role="switch" with aria-checked) wrapped in a label. When toggled, the knob springs across the track while its scaleX briefly stretches to 1.45 and settles, anchored on the side it is leaving. That stretch is the liquid part.

## Installation

```bash
npx shadcn@latest add https://www.motionkitui.com/r/liquid-toggle.json
```

Or copy `components/motionkit/liquid-toggle.tsx` below. Requires react, motion and Tailwind CSS v4. Free for personal projects; commercial use needs MotionKit Plus (https://www.motionkitui.com/pricing).

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` |  | Visible label, also the accessible name. |
| `checked / onChange` | `boolean / (checked) => void` |  | Use both to control it yourself. |
| `defaultChecked` | `boolean` | `false` | Starting state when uncontrolled. |
| `size` | `number` | `28` | Track height in pixels; the width follows. |
| `color` | `string` | `"#0E7C86"` | Track colour when on. |

## Accessibility

- **Keyboard: Pass.** A native <button> with role="switch": Tab to it, Space or Enter flips it. Clicking the label works too.
- **Screen readers: Pass.** Announced as a switch, on or off, named by its visible label.
- **Reduced motion: Pass.** With reduced motion on, the knob moves without stretching or bouncing.
- **Colour contrast: Pass.** The knob, which shows the state, measures 13.6:1 off and 4.4:1 on. The off track itself is subtle (1.25:1 on a dark page), so the knob carries the meaning, as WCAG 1.4.11 allows.

## Source

```tsx
"use client";

import { useId, useState } from "react";
import { motion, useReducedMotion } from "motion/react";

type LiquidToggleProps = {
  /** Visible label, also used as the accessible name. */
  label: string;
  /** Controlled state. Leave unset to let the toggle manage itself. */
  checked?: boolean;
  defaultChecked?: boolean;
  onChange?: (checked: boolean) => void;
  /** Track height in pixels; the width follows. */
  size?: number;
  /** Track colour when on. */
  color?: string;
  className?: string;
};

export function LiquidToggle({
  label,
  checked,
  defaultChecked = false,
  onChange,
  size = 28,
  color = "#0E7C86",
  className = "",
}: LiquidToggleProps) {
  const [internal, setInternal] = useState(defaultChecked);
  const on = checked ?? internal;
  const reduceMotion = useReducedMotion();
  const id = useId();
  const pad = 3;
  const knob = size - pad * 2;
  const travel = size * 0.85;

  function toggle() {
    setInternal(!on);
    onChange?.(!on);
  }

  return (
    <label htmlFor={id} className={`inline-flex cursor-pointer select-none items-center gap-3 text-sm text-[#DCE3E2] ${className}`}>
      <button
        id={id}
        type="button"
        role="switch"
        aria-checked={on}
        onClick={toggle}
        className="relative shrink-0 rounded-full border border-white/10 transition-colors duration-300 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[#5FAEB4]"
        style={{ width: size + travel, height: size, backgroundColor: on ? color : "#1D2727" }}
      >
        {/* the knob stretches toward where it is going, then settles: the "liquid" part */}
        <motion.span
          aria-hidden
          className="absolute rounded-full bg-[#F6F1E8] shadow-md shadow-black/30"
          style={{ top: pad - 1, left: pad - 1, height: knob, width: knob, originX: on ? 1 : 0 }}
          initial={false}
          animate={{ x: on ? travel : 0, scaleX: reduceMotion ? 1 : [1, 1.45, 1] }}
          transition={
            reduceMotion
              ? { duration: 0 }
              : { x: { type: "spring", bounce: 0.3, duration: 0.45 }, scaleX: { duration: 0.4, times: [0, 0.4, 1] } }
          }
        />
      </button>
      {label}
    </label>
  );
}
```
