Skip to main content
On this pageOverview

Switch

Overview

An on/off toggle. Semantically different from Checkbox — Switch represents an immediate action (like a light switch), while Checkbox represents a form value that gets submitted. Switch uses the Submodel pattern with the same wiring as Checkbox.

See it in an app

Check out how Switch is wired up in a real Foldkit app.

Examples

The switch renders as a <button> with role="switch". The typical visual is a track with a sliding knob, styled with the data-checked attribute for the on state.

Get notified when something important happens.

// Pseudocode walkthrough of the Foldkit integration points. Each labeled
// block below is an excerpt — fit them into your own Model, init, Message,
// update, and view definitions.
import { Effect } from 'effect'
import { Command, Ui } from 'foldkit'
import { m } from 'foldkit/message'
import { evo } from 'foldkit/struct'

import { Class, button, div, label, p } from './html'

// Add a field to your Model for the Switch Submodel:
const Model = S.Struct({
  switchDemo: Ui.Switch.Model,
  // ...your other fields
})

// In your init function, initialize the Switch Submodel with a unique id:
const init = () => [
  {
    switchDemo: Ui.Switch.init({ id: 'notifications' }),
    // ...your other fields
  },
  [],
]

// Embed the Switch Message in your parent Message:
const GotSwitchMessage = m('GotSwitchMessage', {
  message: Ui.Switch.Message,
})

// Inside your update function's M.tagsExhaustive({...}), delegate to Switch.update:
GotSwitchMessage: ({ message }) => {
  const [nextSwitch, commands] = Ui.Switch.update(model.switchDemo, message)

  return [
    // Merge the next state into your Model:
    evo(model, { switchDemo: () => nextSwitch }),
    // Forward the Submodel's Commands through your parent Message:
    commands.map(
      Command.mapEffect(Effect.map(message => GotSwitchMessage({ message }))),
    ),
  ]
}

// Inside your view function, render the switch:
Ui.Switch.view({
  model: model.switchDemo,
  toParentMessage: message => GotSwitchMessage({ message }),
  toView: attributes =>
    div(
      [Class('flex items-center gap-3')],
      [
        button(
          [
            ...attributes.button,
            Class(
              'relative h-6 w-11 rounded-full transition-colors data-[checked]:bg-blue-600 bg-gray-200',
            ),
          ],
          [
            div(
              [
                Class(
                  'absolute top-0.5 left-0.5 h-5 w-5 rounded-full bg-white transition-transform',
                ),
              ],
              [],
            ),
          ],
        ),
        div(
          [],
          [
            label(
              [...attributes.label, Class('text-sm font-medium')],
              ['Enable notifications'],
            ),
            p(
              [...attributes.description, Class('text-sm text-gray-500')],
              ['Get notified when something important happens.'],
            ),
          ],
        ),
      ],
    ),
})

Styling

Switch is headless — your toView callback controls all markup and styling. Use data-[checked] to change the track color and translate the knob.

AttributeCondition
data-checkedPresent when the switch is on.
data-disabledPresent when isDisabled is true.

Keyboard Interaction

KeyDescription
SpaceToggles the switch.

Accessibility

The switch button receives role="switch" and aria-checked. The label is linked via aria-labelledby and the description via aria-describedby. Clicking the label toggles the switch.

API Reference

InitConfig

Configuration object passed to Switch.init().

NameTypeDefaultDescription
idstring-Unique ID for the switch instance.
isCheckedbooleanfalseInitial on/off state.

ViewConfig

Configuration object passed to Switch.view().

NameTypeDefaultDescription
modelSwitch.Model-The switch state from your parent Model.
toParentMessage(childMessage: Switch.Message) => ParentMessage-Wraps Switch Messages in your parent Message type for Submodel delegation.
toView(attributes: SwitchAttributes) => Html-Callback that receives attribute groups for the button, label, description, and hidden input elements.
isDisabledbooleanfalseWhether the switch is disabled.
namestring-Form field name. When provided, a hidden input is included for native form submission.
valuestring'on'Value sent in the form when checked.

SwitchAttributes

Attribute groups provided to the toView callback.

NameTypeDefaultDescription
buttonReadonlyArray<Attribute<Message>>-Spread onto the switch button element. Includes role, aria-checked, tabindex, and click/keyboard handlers.
labelReadonlyArray<Attribute<Message>>-Spread onto the label element. Includes an id for aria-labelledby and a click handler that toggles the switch.
descriptionReadonlyArray<Attribute<Message>>-Spread onto a description element. Includes an id referenced by aria-describedby on the switch.
hiddenInputReadonlyArray<Attribute<Message>>-Spread onto a hidden <input> for form submission. Only needed when the name prop is set.

Stay in the update loop.

New releases, patterns, and the occasional deep dive.


Built with Foldkit.

© 2026 Devin Jameson