Rafi Wirana

Design Engineer

Preview
0%
Back to philosophy
Copy link

Fluid Switch

A switch that flows between states with a fluid motion, inspired by natural movements in nature.

Installation

✨ No external dependencies required

Component location

components/craft/FluidSwitch/index.tsx

Usage

Basic example

import { FluidSwitch } from '@/components/craft/FluidSwitch';
import { useState } from 'react';

export function Example() {
  const [isEnabled, setIsEnabled] = useState(false);
  
  return (
    <FluidSwitch 
      checked={isEnabled}
      onChange={setIsEnabled}
      aria-label="Toggle feature"
    />
  );
}

More Examples

Controlled Switch

Controlled component with external state management

const [isOn, setIsOn] = useState(true);

<FluidSwitch 
  checked={isOn}
  onChange={setIsOn}
  aria-label="Power toggle"
/>

Uncontrolled Switch

Uncontrolled switch with default value

<FluidSwitch 
  defaultChecked={false}
  onChange={(checked) => console.log('Changed to:', checked)}
  aria-label="Setting toggle"
/>

Disabled State

Switch in disabled state

<FluidSwitch 
  checked={true}
  disabled
  aria-label="Disabled toggle"
/>

Custom Styling

Customize track and thumb appearance

<FluidSwitch 
  checked={isOn}
  onChange={setIsOn}
  trackClassName="bg-green-500 dark:bg-green-600"
  thumbClassName="bg-white shadow-xl"
  aria-label="Custom styled toggle"
/>

With Form

Use within forms with name attribute

<form onSubmit={handleSubmit}>
  <FluidSwitch 
    name="notifications"
    checked={settings.notifications}
    onChange={(checked) => setSettings({ ...settings, notifications: checked })}
    aria-label="Enable notifications"
  />
</form>

API Reference

PropTypeDefaultDescription
checked
booleanControlled checked state of the switch
defaultChecked
booleanfalseDefault checked state for uncontrolled switch
onChange
(checked: boolean) => voidCallback fired when the switch state changes
onCheckedChange
(checked: boolean) => voidAlternative callback for state changes (alias for onChange)
disabled
booleanfalseDisable the switch interaction
name
stringName attribute for form submission
thumbClassName
stringAdditional CSS classes for the thumb (sliding circle)
trackClassName
stringAdditional CSS classes for the track (background container)
className
stringAdditional CSS classes for the root element
aria-label
stringAccessible label for screen readers
aria-labelledby
stringID of element that labels the switch