Navigated to /docs/ui/components/toggle

Toggle

Button with an application-controlled pressed/unpressed state.

Preview

A live, theme-aware example rendered with the published component.

Loading preview…

Import

Import from the focused entry point below, or use the same named export from @tavojs/ui. Both are public APIs; measure the built application when comparing their bundle cost.

TSX
tsximport { Toggle } from "@tavojs/ui/toggle";

The component import loads compiled component CSS. Enable tavoUi() for your project theme variables, or import @tavojs/ui/theme.css once for the package default theme. The equivalent CSS-safe component alias is @tavojs/ui/css/toggle.

When to use

Use it when

Use Toggle for an action that remains on or off, such as bold formatting, pinning, muting, or showing a layer.

Choose something else when

Use Checkbox or Switch for form settings, Button for one-time actions, and Tabs for switching content panels.

Examples

These examples include imports and local state where interaction requires it. Render the exported component in a page after completing UI installation. Demo state stays in the mounted component; connect file handling, persistence, and data loading to your application as needed.

Toggle example

TSXCreate: src/components/examples/ToggleExample1.tsx
tsximport { createTavo } from "@tavojs/core";
import { Toggle } from "@tavojs/ui/toggle";
import { Text } from "@tavojs/ui";

type State = { pressed: boolean };

export const ToggleExample1 = createTavo<Record<string, never>, State>({
  model: () => ({ pressed: false }),
  view: ({ state, model }) => (
    <>
      <Toggle pressed={state.pressed} onClick={() => model.set("pressed", !state.pressed)}>Preview</Toggle>
      <Text>{state.pressed ? "Preview is enabled" : "Preview is disabled"}</Text>
    </>
  )
});

Common props and defaults

A curated starting point. The complete TypeScript API below includes additional props and compound member contracts.

PropTypeDefaultDescription
classNamestring

Optional class hook applied to the public root element owned by the component.
asstring | Component<Record<string, unknown>>

Changes the rendered root element or component while preserving Tavo.js UI styling and public props.
pressedboolean

Configures pressed for this component.
sizeSize

Configures size for this component.
variant"default" | "outline" | "ghost"

Configures variant for this component.
hrefstring

Configures href for this component.

Complete TypeScript API

Import from @tavojs/ui/toggle. Published exports: Toggle, ToggleProps, ToggleVariant.

ToggleProps

PropertyTypeRequiredDescription
childrenChild

No

Inherited children attribute accepted by Toggle.

classNamestring

No

Optional class hook applied to the public root element owned by the component.

sxSx

No

Inherited sx attribute accepted by Toggle.

idstring

No

Inherited id attribute accepted by Toggle.

rolestring

No

Inherited role attribute accepted by Toggle.

styleRecord<string, unknown>

No

Inherited style attribute accepted by Toggle.

titlestring

No

Inherited title attribute accepted by Toggle.

tabIndexnumber

No

Inherited tabIndex attribute accepted by Toggle.

hiddenboolean

No

Inherited hidden attribute accepted by Toggle.

disabledboolean

No

Inherited disabled attribute accepted by Toggle.

onClickTavoEventHandler<MouseEvent>

No

Inherited onClick attribute accepted by Toggle.

onChangeTavoEventHandler<Event>

No

Inherited onChange attribute accepted by Toggle.

onInputTavoEventHandler<Event>

No

Inherited onInput attribute accepted by Toggle.

onKeyDownTavoEventHandler<KeyboardEvent>

No

Inherited onKeyDown attribute accepted by Toggle.

onFocusTavoEventHandler<FocusEvent>

No

Inherited onFocus attribute accepted by Toggle.

onBlurTavoEventHandler<FocusEvent>

No

Inherited onBlur attribute accepted by Toggle.

asPolymorphicAs

No

Changes the rendered root element or component while preserving Tavo.js UI styling and public props.

pressedboolean

No

Configures pressed for this component.

sizeSize

No

Configures size for this component.

variantToggleVariant

No

Configures variant for this component.

hrefstring

No

Configures href for this component.

State ownership and DOM target

ConcernContract
StateUse pressed to supply rendered state. Update it through the documented callback or native event handler.
DOM targetThe public root follows the as/component prop. Root attributes, className, style, and supported events are forwarded there.

Anatomy and related components

This component exposes one public component root rather than a compound member API.

Limitations

Use Checkbox or Switch for form settings, Button for one-time actions, and Tabs for switching content panels.

Use only the documented props, entry points, and compound members. Internal DOM nesting and CSS class names can change without becoming part of the public component contract.

Accessibility

Emits `aria-pressed`.

  • Emits `aria-pressed`.

Component status

ContractCurrent value
Maturitystable
Categoryforms
Component import@tavojs/ui/toggle
Explicit CSS import@tavojs/ui/css/toggle
PreviewInteractive preview available on this page