Switch
solid-ui/switchFreeComponent
A switch component
Live preview
live · rendered by the registry
Rendered by solid-ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://www.solid-ui.com/r/switch.jsonSource
1import type { JSX, ValidComponent } from "solid-js"2import { splitProps } from "solid-js"34import type { PolymorphicProps } from "@kobalte/core"5import * as SwitchPrimitive from "@kobalte/core/switch"67import { cn } from "~/lib/utils"89const Switch = SwitchPrimitive.Root10const SwitchDescription = SwitchPrimitive.Description11const SwitchErrorMessage = SwitchPrimitive.ErrorMessage1213type SwitchControlProps = SwitchPrimitive.SwitchControlProps & {14 class?: string | undefined15 children?: JSX.Element16}1718const SwitchControl = <T extends ValidComponent = "input">(19 props: PolymorphicProps<T, SwitchControlProps>20) => {21 const [local, others] = splitProps(props as SwitchControlProps, ["class", "children"])22 return (23 <>24 <SwitchPrimitive.Input25 class={cn(26 "[&:focus-visible+div]:outline-none [&:focus-visible+div]:ring-2 [&:focus-visible+div]:ring-ring [&:focus-visible+div]:ring-offset-2 [&:focus-visible+div]:ring-offset-background",27 local.class28 )}29 />30 <SwitchPrimitive.Control31 class={cn(32 "inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent bg-input transition-[color,background-color,box-shadow] data-[disabled]:cursor-not-allowed data-[checked]:bg-primary data-[disabled]:opacity-50",33 local.class34 )}35 {...others}36 >37 {local.children}38 </SwitchPrimitive.Control>39 </>40 )41}4243type SwitchThumbProps = SwitchPrimitive.SwitchThumbProps & { class?: string | undefined }4445const SwitchThumb = <T extends ValidComponent = "div">(46 props: PolymorphicProps<T, SwitchThumbProps>47) => {48 const [local, others] = splitProps(props as SwitchThumbProps, ["class"])49 return (50 <SwitchPrimitive.Thumb51 class={cn(52 "pointer-events-none block size-5 translate-x-0 rounded-full bg-background shadow-lg ring-0 transition-transform data-[checked]:translate-x-5",53 local.class54 )}55 {...others}56 />57 )58}5960type SwitchLabelProps = SwitchPrimitive.SwitchLabelProps & { class?: string | undefined }6162const SwitchLabel = <T extends ValidComponent = "label">(63 props: PolymorphicProps<T, SwitchLabelProps>64) => {65 const [local, others] = splitProps(props as SwitchLabelProps, ["class"])66 return (67 <SwitchPrimitive.Label68 class={cn(69 "text-sm font-medium leading-none data-[disabled]:cursor-not-allowed data-[disabled]:opacity-70",70 local.class71 )}72 {...others}73 />74 )75}7677// … truncated
What it pulls in
npm packages
Files it writes
More from solid-ui
All 55 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | solid-ui | Components | Free | 2 deps | |
| Alertalert | solid-ui | Components | Free | 3 deps | |
| Alert Dialogalert-dialog | solid-ui | Components | Free | 2 deps | |
| Aspect Ratioaspect-ratio | solid-ui | Components | Free | 1 dep | |
| Avataravatar | solid-ui | Components | Free | 2 deps | |
| Badgebadge | solid-ui | Components | Free | 2 deps | |
| Badge Deltabadge-delta | solid-ui | Components | Free | 2 deps | |
| Bar Listbar-list | solid-ui | Components | Free | 1 dep |
