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.
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
Choose something else when
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
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.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | — | Optional class hook applied to the public root element owned by the component. |
as | string | Component<Record<string, unknown>> | — | Changes the rendered root element or component while preserving Tavo.js UI styling and public props. |
pressed | boolean | — | Configures pressed for this component. |
size | Size | — | Configures size for this component. |
variant | "default" | "outline" | "ghost" | — | Configures variant for this component. |
href | string | — | Configures href for this component. |
Complete TypeScript API
Import from @tavojs/ui/toggle. Published exports: Toggle, ToggleProps, ToggleVariant.
ToggleProps
| Property | Type | Required | Description |
|---|---|---|---|
children | Child | No | Inherited children attribute accepted by Toggle. |
className | string | No | Optional class hook applied to the public root element owned by the component. |
sx | Sx | No | Inherited sx attribute accepted by Toggle. |
id | string | No | Inherited id attribute accepted by Toggle. |
role | string | No | Inherited role attribute accepted by Toggle. |
style | Record<string, unknown> | No | Inherited style attribute accepted by Toggle. |
title | string | No | Inherited title attribute accepted by Toggle. |
tabIndex | number | No | Inherited tabIndex attribute accepted by Toggle. |
hidden | boolean | No | Inherited hidden attribute accepted by Toggle. |
disabled | boolean | No | Inherited disabled attribute accepted by Toggle. |
onClick | TavoEventHandler<MouseEvent> | No | Inherited onClick attribute accepted by Toggle. |
onChange | TavoEventHandler<Event> | No | Inherited onChange attribute accepted by Toggle. |
onInput | TavoEventHandler<Event> | No | Inherited onInput attribute accepted by Toggle. |
onKeyDown | TavoEventHandler<KeyboardEvent> | No | Inherited onKeyDown attribute accepted by Toggle. |
onFocus | TavoEventHandler<FocusEvent> | No | Inherited onFocus attribute accepted by Toggle. |
onBlur | TavoEventHandler<FocusEvent> | No | Inherited onBlur attribute accepted by Toggle. |
as | PolymorphicAs | No | Changes the rendered root element or component while preserving Tavo.js UI styling and public props. |
pressed | boolean | No | Configures pressed for this component. |
size | Size | No | Configures size for this component. |
variant | ToggleVariant | No | Configures variant for this component. |
href | string | No | Configures href for this component. |
State ownership and DOM target
| Concern | Contract |
|---|---|
| State | Use pressed to supply rendered state. Update it through the documented callback or native event handler. |
| DOM target | The 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
| Contract | Current value |
|---|---|
| Maturity | stable |
| Category | forms |
| Component import | @tavojs/ui/toggle |
| Explicit CSS import | @tavojs/ui/css/toggle |
| Preview | Interactive preview available on this page |