Lynx/Modules/Zero/Toggle
@sigx/lynx-zero · Beta · Component library

Toggle#

A button with one bit of state, such as bold on or off, or starred or not. A tap flips the root between on and off. The model concept is pressed. It is a mode you flip, not a value you submit: for a setting, use Switch.

The anatomy is zero's toggle scope, shared with the web. See Toggle in @sigx/zero for the contract. This page covers what is different on Lynx.

Import#

TSX
import { Toggle } from '@sigx/lynx-zero';

Toggle is a compound whose only part is Toggle.Root. <Toggle> and <Toggle.Root> are the same component.

Usage#

Bind the pressed state with model, or leave the model off and seed it with defaultPressed. pressedChange fires either way:

TSX
import { component } from '@sigx/lynx';
import { Row, Toggle } from '@sigx/lynx-zero';

export const Formatting = component(({ signal }) => {
    const state = signal({ bold: false });
    return () => (
        <Row gap={8}>
            <Toggle.Root label="Bold" model={() => state.bold}>
                <text>B</text>
            </Toggle.Root>
            <Toggle.Root label="Star" defaultPressed color="warning" onPressedChange={(on) => star(on)}>
                <text>★</text>
            </Toggle.Root>
        </Row>
    );
});

What the reader hears#

Lynx has no aria-pressed. The root is announced with the button trait, and the on state is announced as selected, which is how native screen readers speak a pressed toggle button. Pass label for an icon-only toggle.

Pressed flag and on state#

The pressed flag (zx-f-pressed) means a finger is down. The on state (zx-s-on) means the mode is set. They are independent, so a held on-toggle carries both. A touch also scales the root on the main thread. pressFeel={false} turns off the scale and keeps the flag. See Press feedback.

disabled, or an enclosing Field's, blocks both the tap and the press feedback. Inside a Field.Root, the toggle also adopts the field's size when it sets none.

Props#

PropTypeDefaultDescription
modelboolean—Two-way binding of the pressed state.
defaultPressedbooleanfalseThe initial state when there is no model.
disabledbooleanfalseBlocks the tap and the press. ORed with an enclosing Field's.
pressFeelbooleantruefalse turns off the main-thread press scale. The pressed flag stays.
color / sizestringskin defaultThe design system's variant axes. size falls back to the Field's.
labelstring—Accessible name. Required for icon-only toggles.
classstring—Extra classes.

Events#

EventPayloadDescription
pressedChange (onPressedChange)booleanThe pressed state changed.

Slots#

SlotDescription
defaultThe toggle's content. Put text and glyphs in <text>.

Anatomy on Lynx#

PartElementStatesFlags
rootview (button trait, selected while on)on, offdisabled, pressed

press-animating is the web runtime's ripple bookkeeping and has no Lynx counterpart. focus-visible can only be forced for display, through ForceStates from @sigx/lynx-zero/testing.

See also#