Toggle
A two-state button that can be either on or off.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { Toggle } from "~/components/ui/toggle"
export default function ToggleDemo() {
return (
<Toggle aria-label="Toggle bookmark" size="sm" variant="outline">
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="group-data-pressed/toggle:fill-foreground"
>
<path d="M17 3a2 2 0 0 1 2 2v15a1 1 0 0 1-1.496.868l-4.512-2.578a2 2 0 0 0-1.984 0l-4.512 2.578A1 1 0 0 1 5 20V5a2 2 0 0 1 2-2z" />
</svg>
Bookmark
</Toggle>
)
}Installation
Install the following dependencies:
Copy and paste the following code into your project.
import { type ComponentProps } from "@solidjs/web"
import { Toggle as SdkToggle } from "@ui-sdk/solid/toggle"
import { cva, type VariantProps } from "class-variance-authority"
import { omit } from "solid-js"
import { cn } from "~/lib/utils"
const toggleVariants = cva(
"group/toggle gap-1 rounded-lg text-sm font-medium transition-all hover:text-foreground focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 aria-pressed:bg-muted data-[state=on]:bg-muted dark:aria-invalid:ring-destructive/40 [&_svg:not([class*='size-'])]:size-4 inline-flex items-center justify-center whitespace-nowrap outline-none hover:bg-muted focus-visible:ring-[3px] disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0",
{
variants: {
variant: {
default: "bg-transparent",
outline: "border border-input bg-transparent hover:bg-muted"
},
size: {
default: "h-8 min-w-8 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",
sm: "h-7 min-w-7 rounded-[min(var(--radius-md),12px)] px-2.5 text-[0.8rem] has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&_svg:not([class*='size-'])]:size-3.5",
lg: "h-9 min-w-9 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2"
}
},
defaultVariants: {
variant: "default",
size: "default"
}
}
)
type ToggleProps = (Omit<ComponentProps<typeof SdkToggle>, "class"> & {
class?: string
}) &
VariantProps<typeof toggleVariants>
const Toggle = (props: ToggleProps) => {
const others = omit(props, "variant", "size", "class")
return (
<SdkToggle
class={cn(
toggleVariants({ variant: props.variant, size: props.size }),
props.class
)}
{...others}
/>
)
}
export { Toggle, toggleVariants, type ToggleProps }Update the import paths to match your project setup.
Usage
import { Toggle } from "~/components/ui/toggle"<Toggle>Toggle</Toggle>Outline
Use variant="outline" for an outline style.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { Toggle } from "~/components/ui/toggle"
export default function ToggleOutline() {
return (
<div class="flex flex-wrap items-center gap-2">
<Toggle variant="outline" aria-label="Toggle italic">
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<line x1="19" x2="10" y1="4" y2="4" />
<line x1="14" x2="5" y1="20" y2="20" />
<line x1="15" x2="9" y1="4" y2="20" />
</svg>
Italic
</Toggle>
<Toggle variant="outline" aria-label="Toggle bold">
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8" />
</svg>
Bold
</Toggle>
</div>
)
}With Text
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { Toggle } from "~/components/ui/toggle"
export default function ToggleText() {
return (
<Toggle aria-label="Toggle italic">
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<line x1="19" x2="10" y1="4" y2="4" />
<line x1="14" x2="5" y1="20" y2="20" />
<line x1="15" x2="9" y1="4" y2="20" />
</svg>
Italic
</Toggle>
)
}Size
Use the size prop to change the size of the toggle.
import { Toggle } from "~/components/ui/toggle"
export default function ToggleSizes() {
return (
<div class="flex flex-wrap items-center gap-2">
<Toggle aria-label="Toggle small" size="sm" variant="outline">
Small
</Toggle>
<Toggle aria-label="Toggle default" size="default" variant="outline">
Default
</Toggle>
<Toggle aria-label="Toggle large" size="lg" variant="outline">
Large
</Toggle>
</div>
)
}Disabled
import { Toggle } from "~/components/ui/toggle"
export default function ToggleDisabled() {
return (
<div class="flex flex-wrap items-center gap-2">
<Toggle aria-label="Toggle disabled" disabled>
Disabled
</Toggle>
<Toggle aria-label="Toggle disabled outline" disabled variant="outline">
Disabled
</Toggle>
</div>
)
}API Reference
See the UI SDK documentation for more information.