GitHub

Reference headless

toggle

interface 3 type 4 function 1
interface Interfaces 3

# CreateToggleOptions

Members

disabled?
optional disabled?: boolean

Construct the machine in the disabled state. Defaults to false.

###### Inherited from

CreateToggleInput.disabled

id?
optional id?: string

Stable id override. If not provided, an id is generated via @kumiki/primitives/id (crypto.randomUUID or a counter fallback).

initial?
optional initial?: boolean

Initial pressed value. Defaults to false.

###### Inherited from

CreateToggleInput.initial

onPressedChange?
optional onPressedChange?: (pressed) => void

Called whenever the pressed value changes via user interaction (TOGGLE). Not called for controlled set() updates that already came from the parent.

###### Parameters

###### pressed

boolean

###### Returns

void

interface

# ToggleContext

Reactive state surfaced to consumers.

Members

pressed
pressed: boolean

Whether the toggle is currently pressed. Mirrored as aria-pressed.

toggles
toggles: number

Number of user-initiated toggles since construction. Useful for analytics and for detecting infinite-loop bugs in test fixtures.

# ToggleController

Members

context
readonly context: Readonly<ToggleContext>
disabled
readonly disabled: boolean
id
readonly id: string

A stable unique id for this controller.

machine
readonly machine: ToggleMachine

Underlying machine — exposed for advanced use, debugging, and tests.

pressed
readonly pressed: boolean
root
readonly root: Attachment

The Svelte 5 attachment to spread on the toggle's button element.

state
readonly state: ToggleState
set()
set(pressed): void

###### Parameters

###### pressed

boolean

###### Returns

void

setDisabled()
setDisabled(disabled): void

###### Parameters

###### disabled

boolean

###### Returns

void

subscribe()
subscribe(listener): () => void

Subscribe to state changes. Returns an unsubscribe.

###### Parameters

###### listener

(snapshot) => void

###### Returns

() => void

toggle()
toggle(): void

###### Returns

void

type Type Aliases 4

# Attachment

Attachment = (node) => void | (() => void)

Svelte 5 attachment function. Receives the DOM element and returns an optional teardown.

Parameters

node
HTMLElement

Returns

void | (() => void)
type

# ToggleEvent

ToggleEvent = { type: "TOGGLE"; } | { pressed: boolean; type: "SET"; } | { type: "DISABLE"; } | { type: "ENABLE"; }

All events the Toggle machine can receive.

type

# ToggleState

ToggleState = "unpressed" | "pressed" | "disabled"

State names the machine can be in.

function Functions 1

# createToggle ( )

createToggle(options?): ToggleController

Create a Toggle controller plus its {@attach}-compatible root attachment.

Parameters

options?
CreateToggleOptions = {}

Returns

ToggleController