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—
- Name
- Description
A unique string that identifies the toggle when used inside a toggle group.
- Type
string | undefined
defaultPressedbooleanfalse
- Name
- Description
Whether the toggle button is currently pressed. This is the uncontrolled counterpart of
pressed.- Type
boolean | undefined- Default
false
pressedboolean—
- Name
- Description
Whether the toggle button is currently pressed. This is the controlled counterpart of
defaultPressed.- Type
boolean | undefined
onPressedChangefunction—
- Name
- Description
Callback fired when the pressed state is changed.
- Type
| (( pressed: boolean, eventDetails: Toggle.ChangeEventDetails, ) => void) | undefined
nativeButtonbooleantrue
- 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
- Name
- Description
Whether the component should ignore user interaction.
- Type
boolean | undefined- Default
false
classNamestring | 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—
- Name
- Type
| React.CSSProperties | (( state: Toggle.State, ) => React.CSSProperties | undefined) | undefined
renderReactElement | 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
Present when the toggle button is pressed.
| Attribute | Description | |
|---|---|---|
data-pressed | Present when the toggle button is pressed. | |
Toggle.StateHide
type ToggleState = {
/** Whether the toggle is currently pressed. */
pressed: boolean;
/** Whether the toggle should ignore user interaction. */
disabled: boolean;
}Toggle.ChangeEventReasonHide
type ToggleChangeEventReason = 'none'Toggle.ChangeEventDetailsHide
type ToggleChangeEventDetails = {
/** The reason for the event. */
reason: 'none';
/** The native event associated with the custom event. */
event: Event;
/** Cancels Base UI from handling the event. */
cancel: () => void;
/** Allows the event to propagate in cases where Base UI will stop the propagation. */
allowPropagation: () => void;
/** Indicates whether the event has been canceled. */
isCanceled: boolean;
/** Indicates whether the event is allowed to propagate. */
isPropagationAllowed: boolean;
/** The element that triggered the event, if applicable. */
trigger: Element | undefined;
}