Toggle
A two-state button that can be on or off.
import type { JSX } from '@solidjs/web';
import { Toggle } from 'base-ui-solid/toggle';
import styles from './index.module.css';
export default function ExampleToggle() {
return (
<div class={styles.Panel}>
<Toggle
aria-label="Favorite"
class={styles.Button}
render={(props, state) => (
<button type="button" {...props}>
{state.pressed ? <HeartFilledIcon /> : <HeartOutlineIcon />}
</button>
)}
/>
</div>
);
}
function HeartFilledIcon(
props: Omit<JSX.IntrinsicElements['svg'], 'style'> & { style?: JSX.CSSProperties },
) {
return (
<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="currentColor"
{...props}
style={{ display: 'block', ...props.style }}
>
<path d="M7.99961 13.8667C7.88761 13.8667 7.77561 13.8315 7.68121 13.7611C7.43321 13.5766 1.59961 9.1963 1.59961 5.8667C1.59961 3.80856 3.27481 2.13336 5.33294 2.13336C6.59054 2.13336 7.49934 2.81176 7.99961 3.3131C8.49988 2.81176 9.40868 2.13336 10.6663 2.13336C12.7244 2.13336 14.3996 3.80803 14.3996 5.8667C14.3996 9.1963 8.56601 13.5766 8.31801 13.7616C8.22361 13.8315 8.11161 13.8667 7.99961 13.8667Z" />
</svg>
);
}
function HeartOutlineIcon(
props: Omit<JSX.IntrinsicElements['svg'], 'style'> & { style?: JSX.CSSProperties },
) {
return (
<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="currentColor"
{...props}
style={{ display: 'block', ...props.style }}
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
d="m7.99961 4.8232-.75505-.75666c-.40333-.40419-1.0559-.86651-1.91162-.86651-1.46903 0-2.66666 1.19764-2.66666 2.66667 0 .5412.24648 1.2356.75339 2.04713.49581.79376 1.17682 1.59861 1.89311 2.33647 1.06989 1.1022 2.1604 1.9962 2.68705 2.4102.52751-.4149 1.61735-1.3085 2.68657-2.4101.7163-.73792 1.3973-1.54278 1.8932-2.33656.5069-.81154.7533-1.50594.7533-2.04714 0-1.46947-1.1975-2.66667-2.6666-2.66667-.85574 0-1.50831.46232-1.91164.86651zm-.01387-1.52394c-.5031-.49988-1.40673-1.1659-2.6528-1.1659-2.05813 0-3.73333 1.6752-3.73333 3.73334 0 3.3296 5.8336 7.7099 6.0816 7.8944a.532.532 0 0 0 .3184.1056c.112 0 .224-.0352.3184-.1051.248-.185 6.08159-4.5653 6.08159-7.8949 0-2.05867-1.6752-3.73334-3.7333-3.73334-1.24617 0-2.14985.66611-2.65293 1.166q-.0069.00686-.0137.01367c.00002-.00003-.00002.00002 0 0-.00459-.0046-.00927-.00914-.01393-.01377"
/>
</svg>
);
}
Anatomy
Import the component and use it as a single part:
Anatomy
import { Toggle } from 'base-ui-solid/toggle';
<Toggle />;
API reference
Prop
Type
Default
valuestring—
A unique string that identifies the toggle when used
inside a toggle group.
stringdefaultPressedbooleanfalse
Whether the toggle button is currently pressed.
This is the uncontrolled counterpart of
pressed.booleanpressedboolean—
Whether the toggle button is currently pressed.
This is the controlled counterpart of
defaultPressed.booleanonPressedChangefunction—
Callback fired when the pressed state is changed.
((pressed: boolean, eventDetails: Toggle.ChangeEventDetails) => void)nativeButtonbooleantrue
Whether 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>).booleandisabledbooleanfalse
Whether the component should ignore user interaction.
booleanclassfunction—
CSS class applied to the element, or a function that
returns a class based on the component’s state.
JSX.ClassValue | ((state) => JSX.ClassValue)stylefunction—
Style applied to the element, or a function that
returns a style object based on the component’s state.
JSX.CSSProperties | string | ((state) => JSX.CSSProperties | string | undefined)renderfunction—
Replace the default element with a tag name, component, or render function.
keyof JSX.IntrinsicElements | Component | ((props, state) => JSX.Element)Data attributes
Name
Type
Default
data-pressed-—
Present when the toggle button is pressed.
-data-disabled-—
Present when the toggle button is disabled.
-Attribute | Description | |
|---|---|---|
data-pressed | Present when the toggle button is pressed. | |
data-disabled | Present when the toggle button is disabled. | |
Toggle.State
type ToggleState = {
/** Whether the toggle is currently pressed. */
pressed: boolean;
/** Whether the toggle should ignore user interaction. */
disabled: boolean;
};Toggle.ChangeEventReason
type ToggleChangeEventReason = 'none';Toggle.ChangeEventDetails
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;
};