Skip to content
All articles Tutorials

Build a Liquid Glass Toggle in React

Wire a spring-animated switch to React state and give it a stable accessible label.

Use LiquidToggle for a binary setting that applies immediately. The parent owns the value, and the component renders a native button with role="switch" and aria-checked.

Connect the switch to state

import { useState } from 'react';
import { LiquidToggle } from 'quick-liquid/react';

export function PreviewSetting() {
  const [enabled, setEnabled] = useState(false);

  return (
    <section>
      <span>Show live preview</span>
      <LiquidToggle
        checked={enabled}
        onChange={setEnabled}
        label="Show live preview"
        spring="snappy"
      />
      {enabled && <p>Preview is on.</p>}
    </section>
  );
}

Keep the label stable across both states. The accessible name is “Show live preview” and the checked state supplies the on or off information. The click and keyboard paths go through the same onChange handler, so they update the setting the same way.

Decide how much stretch you want

maxStretch controls how far the thumb can elongate on press. Its default is 0.28. Start there and dial it down in a compact settings panel where multiple switches sit close together. Keep the focus indicator easy to see on every background and use the disabled prop when a setting truly cannot be changed.

For persisted settings, initialize the parent state from storage or server data and save from the same application-level handler. Check the restored value after a reload, then use Tab and Space to verify operation without a pointer. Try the switch on the playground animation bench.