Switch
Um alternador para ligar/desligar uma opção
Código:
'use client';
import { Switch as SwitchPrimitive } from 'radix-ui';
import * as React from 'react';
import { cn } from '@/lib/utils';
export type SwitchProps = React.ComponentProps<typeof SwitchPrimitive.Root>;
export function Switch({ className, ...props }: SwitchProps) {
return (
<SwitchPrimitive.Root
data-slot='switch'
className={cn(
'data-[state=checked]:bg-primary data-[state=unchecked]:bg-input focus-visible:border-ring focus-visible:ring-ring/50 dark:data-[state=unchecked]:bg-input/80 shadow-xs peer inline-flex w-14 shrink-0 items-center rounded-xl border border-transparent py-0.5 outline-none transition-all focus-visible:ring-[3px] disabled:cursor-not-allowed disabled:opacity-50',
className
)}
{...props}
>
<SwitchPrimitive.Thumb
data-slot='switch-thumb'
className={cn(
'bg-background dark:data-[state=unchecked]:bg-foreground dark:data-[state=checked]:bg-primary-foreground pointer-events-none block size-6 rounded-full ring-0 transition-transform data-[state=checked]:translate-x-[calc(100%+4px)] data-[state=unchecked]:translate-x-[calc(0%+2px)]'
)}
/>
</SwitchPrimitive.Root>
);
}
yarn add radix-ui
Prévia:
Ativado
Ativado
Desativado
Desabilitado
Checked
Disabled
Props:
| Property | Type | Default | Description |
|---|---|---|---|
checked | boolean | - | Controla o estado ligado/desligado de forma controlada. |
defaultChecked | boolean | false | Estado inicial quando não controlado. |
onCheckedChange | (checked: boolean) => void | - | Chamado quando o estado é alterado. |
disabled | boolean | false | Desabilita a interação com o switch. |
