Components

Toggle

A two-state button that can be on or off.

Anatomy#

Import the component and use it as a single part:

Anatomytsx
import { Toggle } from '@egoist/solid-base/toggle';

<Toggle />;

Examples#

Changing the content when pressed#

The render function receives the component state as its second argument. Like every render function in this library, it is called once with live props and state objects: read state.pressed inside JSX (or <Show>) rather than branching on it at the top of the function.

Swapping the icontsx
<Toggle
  aria-label="Favorite"
  render={(props, state) => (
    <button type="button" {...props}>
      <Show when={state.pressed} fallback={<HeartOutlineIcon />}>
        <HeartFilledIcon />
      </Show>
    </button>
  )}
/>

API reference#

<Toggle>

A two-state button that can be on or off. Renders a <button> element.

PropTypeDefault
pressedboolean—

Whether the toggle button is currently pressed. This is the controlled counterpart of defaultPressed.

defaultPressedbooleanfalse

Whether the toggle button is currently pressed. This is the uncontrolled counterpart of pressed.

disabledbooleanfalse

Whether the component should ignore user interaction.

onPressedChange| ((pressed: boolean, eventDetails: Toggle.ChangeEventDetails) => void)—

Callback fired when the pressed state is changed.

valueValue—

A unique string that identifies the toggle when used inside a toggle group.

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>).

classstring | ClassValue | ((state: State) => ClassValue)—

CSS class applied to the element, or a function that returns a class based on the component's state.

styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—

Style applied to the element, or a function that returns a style based on the component's state.

renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—

Allows you to replace the component's HTML element with a different tag, or compose it with another component.

Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.

Data attributeDescription
data-pressed

Present when the toggle button is pressed.

data-disabled

Present when the toggle button is disabled.