Toggle
harshkanjiya/toggleFreeComponent
A beautiful toggle switch component.
Live preview
live · rendered by the registry
Rendered by harshkanjiya on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://harshkanjiya.com/r/toggle.jsonSource
1'use client'23import React, { useState } from 'react'4import styles from './toggle.module.css'5import { useSound } from '@/hooks/use-sound'67export function Toggle() {8 const [isOn, setIsOn] = useState(false)910 const toggle = () => setIsOn(p => !p)1112 const handleKeyDown = (e: React.KeyboardEvent) => {13 if (e.key === ' ' || e.key === 'Enter') {14 e.preventDefault()15 toggle()16 }17 }1819 const playClick = useSound("/audio/ui-sounds/toggle.mp3");2021 return (22 <button23 role="switch"24 aria-checked={isOn}25 onClick={() => {26 toggle();27 playClick();28 }}29 onKeyDown={handleKeyDown}30 className={`${styles.track} relative flex items-center w-[88px] h-[44px] rounded-full cursor-pointer outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-neutral-400 select-none bg-[#e8e8e8] dark:bg-[#151518]`}31 >32 {/* Left dot — subtle, visible when ON */}33 <span34 className={`pointer-events-none absolute left-[11px] top-[19px] w-[6px] h-[6px] rounded-full bg-[#a8b8c4] dark:bg-[#607a8c] transition-opacity duration-300 ${isOn ? 'opacity-70' : 'opacity-0'}`}35 />3637 {/* Right dot — orange, visible when OFF */}38 <span39 className={`pointer-events-none absolute right-[11px] top-[19px] w-[6px] h-[6px] rounded-full bg-[#f05a00] dark:bg-[#ff6820] transition-opacity duration-300 ${isOn ? 'opacity-0' : 'opacity-100'}`}40 />4142 {/* Thumb — raised pintch button */}43 <span44 className={`${styles.thumb} pointer-events-none absolute top-[4px] left-[4px] w-[36px] h-[36px] rounded-full bg-[#f8f8f8] dark:bg-[#2a2a32] transition-transform duration-300 ease-in-out ${isOn ? 'translate-x-[44px]' : 'translate-x-0'}`}45 />46 </button>47 )48}4950export default Toggle
Files it writes
More from harshkanjiya
All 6 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Hello Worldhello-world | harshkanjiya | Components | Free | no deps · 2 files | |
| Pintch Switchpintch-switch | harshkanjiya | Components | Free | no deps · 4 files | |
| Realistic Buttonrealistic-button | harshkanjiya | Components | Free | no deps · 4 files | |
| Shutter CTAshutter-cta | harshkanjiya | Components | Free | no deps · 2 files |
