Theme

Customize appearance

Components

Toggle

Represent a button that can remain pressed.

View component source ↗

Preview

A minimal example to get started.

Installation

Install this component and its dependencies in a project initialized with shadcn.

pnpm dlx shadcn@latest add https://ui.hyunjoon.net/r/toggle.json

Usage

This is the basic example. Use the Code tab above for the selected example. Adjust import aliases to match your project.

import { Toggle } from "@/components/ui/toggle";

export default function Example() {
  return (
    <Toggle aria-label="Toggle bold" className="font-bold">
      B
    </Toggle>
  );
}

API reference

Derived from the installed component types. Native DOM props are also supported where applicable. A dash means no explicit default is documented. Some props depend on the selected mode.

Toggle
Toggle properties
PropTypeDefaultDescription
pressed
boolean | undefined
boolean | undefined
—Whether the toggle button is currently pressed. This is the controlled counterpart of `defaultPressed`.
defaultPressed
boolean | undefined
boolean | undefined
falseWhether the toggle button is currently pressed. This is the uncontrolled counterpart of `pressed`.
disabled
boolean | undefined
boolean | undefined
falseWhether the component should ignore user interaction.
onPressedChange
((pressed: boolean, eventDetails: TogglePrimitive.ChangeEventDetails) => void) | undefined
((pressed: boolean, eventDetails: TogglePrimitive.ChangeEventDetails) => void) | undefined
—Callback fired when the pressed state is changed.
value
string | undefined
string | undefined
—A unique string that identifies the toggle when used inside a toggle group.
nativeButton
boolean | undefined
boolean | undefined
trueWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`).
ref
React.Ref<HTMLButtonElement> | undefined
React.Ref<HTMLButtonElement> | undefined
—Allows getting a ref to the component instance. Once the component unmounts, React will set `ref.current` to `null` (or call the ref with `null` if you passed a callback ref).
id
string | undefined
string | undefined
—Unique identifier used to associate labels and descriptions.
children
React.ReactNode
React.ReactNode
—Content rendered inside this component.
onFocus
((event: BaseUIEvent<React.FocusEvent<HTMLButtonElement, Element>>) => void) | undefined
((event: BaseUIEvent<React.FocusEvent<HTMLButtonElement, Element>>) => void) | undefined
—Called when the control receives focus.
onBlur
((event: BaseUIEvent<React.FocusEvent<HTMLButtonElement, Element>>) => void) | undefined
((event: BaseUIEvent<React.FocusEvent<HTMLButtonElement, Element>>) => void) | undefined
—Called when the control loses focus.
onChange
((event: BaseUIEvent<React.ChangeEvent<HTMLButtonElement, Element>>) => void) | undefined
((event: BaseUIEvent<React.ChangeEvent<HTMLButtonElement, Element>>) => void) | undefined
—Called when the underlying form control changes; inspect the event for its value.
onKeyDown
((event: BaseUIEvent<React.KeyboardEvent<HTMLButtonElement>>) => void) | undefined
((event: BaseUIEvent<React.KeyboardEvent<HTMLButtonElement>>) => void) | undefined
—Called when a key is pressed while this element is focused.
onClick
((event: BaseUIEvent<React.MouseEvent<HTMLButtonElement, MouseEvent>>) => void) | undefine…
((event: BaseUIEvent<React.MouseEvent<HTMLButtonElement, MouseEvent>>) => void) | undefined
—Called when the element is activated.
name
string | undefined
string | undefined
—Field name used in form data.
type
"button" | "submit" | "reset" | undefined
"button" | "submit" | "reset" | undefined
—The native input or button behavior.
className
string | ((state: ToggleState) => string | undefined) | undefined
string | ((state: ToggleState) => string | undefined) | undefined
—CSS class applied to the element, or a function that returns a class based on the component's state.
render
React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn…
React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, ToggleState> | undefined
—Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render.
style
React.CSSProperties | ((state: ToggleState) => React.CSSProperties | undefined) | undefine…
React.CSSProperties | ((state: ToggleState) => React.CSSProperties | undefined) | undefined
—Style applied to the element, or a function that returns a style object based on the component's state.
variant
"default" | "outline" | null | undefined
"default" | "outline" | null | undefined
"default"Visual treatment of the component.
size
"default" | "sm" | "lg" | null | undefined
"default" | "sm" | "lg" | null | undefined
"default"Visual size of the component.

Usage notes

Give the toggle an accessible name describing the option. Use pressed and onPressedChange when controlling its state.