Get Started
Input
Feedback
Display
Installation
i18n
import { Label } from "@/components/ui/label"
import { Switch } from "@/components/ui/switch"
Installation#
pnpm dlx shadcn@latest add https://ui.tyap.me/r/styles/base/switch.jsonnpx shadcn@latest add https://ui.tyap.me/r/styles/base/switch.jsonyarn dlx shadcn@latest add https://ui.tyap.me/r/styles/base/switch.jsonbunx --bun shadcn@latest add https://ui.tyap.me/r/styles/base/switch.json
Install the following dependencies:
pnpm add @base-ui/reactnpm install @base-ui/reactyarn add @base-ui/reactbun add @base-ui/react
Copy and paste the following code into your project.
"use client"
import * as React from "react"
import { Switch as SwitchPrimitive } from "@base-ui/react/switch"
import { useShakeOnInvalid } from "@/hooks/use-shake-on-invalid"
import { cn } from "@/lib/utils"
const SWITCH_MOTION_CSS = `
.cn-switch {
transition:
background-color 200ms cubic-bezier(0.22, 1, 0.36, 1),
border-color 200ms cubic-bezier(0.22, 1, 0.36, 1),
box-shadow 220ms cubic-bezier(0.22, 1, 0.36, 1);
}
.cn-switch .cn-switch-thumb {
transition:
transform 650ms cubic-bezier(0.34, 1.45, 0.64, 1),
scale 180ms ease-out;
will-change: transform;
}
.cn-switch:not([data-disabled]):active .cn-switch-thumb {
scale: 1.25 0.875;
transition-property: transform, scale;
transition-duration: 650ms, 80ms;
transition-timing-function: cubic-bezier(0.34, 1.45, 0.64, 1), ease-out;
}
@media (prefers-reduced-motion: reduce) {
.cn-switch { transition: none !important; }
.cn-switch .cn-switch-thumb { transition: none !important; }
}
`
function Switch({
className,
size = "default",
...props
}: SwitchPrimitive.Root.Props & {
size?: "sm" | "default"
}) {
const ref = React.useRef<HTMLButtonElement>(null)
useShakeOnInvalid(ref)
return (
<>
<style precedence="component" href="cn-switch-motion">
{SWITCH_MOTION_CSS}
</style>
<SwitchPrimitive.Root
ref={ref}
data-slot="switch"
data-size={size}
className={cn(
"t-input peer group/switch relative inline-flex items-center outline-none after:absolute after:-inset-x-3 after:-inset-y-2 data-disabled:cursor-not-allowed data-disabled:opacity-50",
className
)}
{...props}
>
<SwitchPrimitive.Thumb
data-slot="switch-thumb"
className="pointer-events-none block ring-0"
/>
</SwitchPrimitive.Root>
</>
)
}
export { Switch }
Update the import paths to match your project setup.
Usage#
import { Switch } from "@/components/ui/switch"<Switch />Examples#
Description#
Focus is shared across devices, and turns off when you leave the app.
import {
Field,
FieldContent,Choice Card#
Card-style selection where FieldLabel wraps the entire Field for a clickable card pattern.
import {
Field,
FieldContent,Disabled#
Add the disabled prop to the Switch component to disable the switch. Add the data-disabled prop to the Field component for styling.
import { Field, FieldLabel } from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
Invalid#
Add the aria-invalid prop to the Switch component to indicate an invalid state. Add the data-invalid prop to the Field component for styling.
You must accept the terms and conditions to continue.
import {
Field,
FieldContent,Size#
Use the size prop to change the size of the switch.
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
RTL#
To enable RTL support in shadcn/ui, see the RTL configuration guide.
يتم مشاركة التركيز عبر الأجهزة، ويتم إيقاف تشغيله عند مغادرة التطبيق.
"use client"
import * as React from "react"API Reference#
See the Base UI Switch documentation.