Toggle
A two-state button that can be on or off.
View as MarkdownAnatomy
Import the component and use it as a single part:
import { Toggle } from '@base-ui/react/toggle';
<Toggle />API reference
valuestring—
string- Name
- Description
A unique string that identifies the toggle when used inside a toggle group.
- Type
string | undefined
defaultPressedbooleanfalse
boolean- Name
- Description
Whether the toggle button is currently pressed. This is the uncontrolled counterpart of
pressed.- Type
boolean | undefined- Default
false
pressedboolean—
boolean- Name
- Description
Whether the toggle button is currently pressed. This is the controlled counterpart of
defaultPressed.- Type
boolean | undefined
onPressedChangefunction—
function- Name
- Description
Callback fired when the pressed state is changed.
- Type
| (( pressed: boolean, eventDetails: Toggle.ChangeEventDetails, ) => void) | undefined
nativeButtonbooleantrue
boolean- Name
- Description
Whether the component renders a native
<button>element when replacing it via therenderprop. Set tofalseif the rendered element is not a button (for example,<div>).- Type
boolean | undefined- Default
true
disabledbooleanfalse
boolean- Name
- Description
Whether the component should ignore user interaction.
- Type
boolean | undefined- Default
false
classNamestring | function—
string | function- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Toggle.State) => string | undefined) | undefined
styleReact.CSSProperties | function—
React.CSSProperties | function- Name
- Type
| React.CSSProperties | (( state: Toggle.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | function—
ReactElement | function- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Toggle.State, ) => ReactElement) | undefined
data-pressed
Additional Types
Toggle.Props
Re-Export of Toggle props as ToggleProps
Toggle.State
type ToggleState = { pressed: boolean; disabled: boolean };Toggle.ChangeEventReason
type ToggleChangeEventReason = 'none';Toggle.ChangeEventDetails
type ToggleChangeEventDetails = {
reason: 'none';
event: Event;
cancel: () => void;
allowPropagation: () => void;
isCanceled: boolean;
isPropagationAllowed: boolean;
trigger: Element | undefined;
};