Files
SnowLuma/packages/ui/src/components/switch.tsx
T
DrSmoothl 8a94188dc6 fix(ui): dark-mode Switch + Checkbox visibility
Both controls used `bg-muted` / `bg-background` for unchecked / thumb
which collapse to the same hue as the surrounding card in dark mode,
making them effectively invisible.

- Switch: unchecked track now bg-input (light) / bg-muted-foreground/25
  (dark) with a shadow-inner; thumb uses bg-card (light) /
  bg-foreground/95 (dark) so it pops in both states.
- Checkbox: explicit bg-card + hover:border-border/80; slightly larger
  radius (5px) for visual rhythm with the rest of the surface.
2026-05-17 14:11:29 +08:00

36 lines
1.4 KiB
TypeScript

import * as React from 'react';
import * as SwitchPrimitive from '@radix-ui/react-switch';
import { cn } from '../lib/cn';
const Switch = React.forwardRef<
React.ElementRef<typeof SwitchPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof SwitchPrimitive.Root>
>(({ className, ...props }, ref) => (
<SwitchPrimitive.Root
ref={ref}
className={cn(
'peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full border border-border shadow-inner transition-colors',
'focus-visible:outline-none focus-visible:ring-[3px] focus-visible:ring-ring/40',
'disabled:cursor-not-allowed disabled:opacity-50',
// Checked: filled with primary
'data-[state=checked]:bg-primary data-[state=checked]:border-primary',
// Unchecked: visible track that contrasts with both surface modes
'data-[state=unchecked]:bg-input dark:data-[state=unchecked]:bg-muted-foreground/25',
className,
)}
{...props}
>
<SwitchPrimitive.Thumb
className={cn(
'pointer-events-none block size-4 rounded-full shadow-md ring-0 transition-transform',
// Thumb always uses card surface so it pops against the track in both states.
'bg-card dark:bg-foreground/95',
'data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0',
)}
/>
</SwitchPrimitive.Root>
));
Switch.displayName = 'Switch';
export { Switch };