Toggle

View as Markdown

A button that stays pressed, such as Bold or Pin.

When to use

A button that stays pressed, such as Bold or Pin. Use Toggle Group instead when several pressed buttons share one value.

Usage

import { Toggle } from "./ui/uicontext/components/Toggle.ts"
const Message = UiSchema.TaggedEnum({ ClickedPin: {} })// `pinned` lives in your Model; ClickedPin flips itexport const PinToggle = (h: Html.H<typeof Message.Type>, pinned: boolean) =>	Toggle(h, { pressed: pinned }).on("click", Message.ClickedPin()).children("Pin")

API reference

Options

Toggle options
NameTypeDescription
pressedrequiredbooleanWhether it's on. The caller keeps it and flips it on click.

Keyboard and accessibility

  • Renders a native <button> with aria-pressed, so Enter and Space press it and screen readers say whether it's on.
  • An icon-only toggle needs an aria-label.

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/toggle

Source