# Toggle

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

Page: https://uicontext.com/docs/components/toggle

Group: Primitives.

## When to use

A button that stays pressed, such as Bold or Pin. Use [Toggle Group](https://uicontext.com/docs/components/toggle-group.md) instead when several pressed buttons share one value.

## Usage

```ts
import { Toggle } from "./ui/uicontext/components/Toggle.ts"

const Message = UiSchema.TaggedEnum({ ClickedPin: {} })

// `pinned` lives in your Model; ClickedPin flips it
export const PinToggle = (h: Html.H<typeof Message.Type>, pinned: boolean) =>
	Toggle(h, { pressed: pinned }).on("click", Message.ClickedPin()).children("Pin")
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### Formatting toolbar

`pressed` comes from your Model and a click asks to flip it.

```ts title="src/demo.ts" source="www/src/examples/toggle/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { Toggle } from "./ui/uicontext/components/Toggle.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"

const styles = stylex.create({
	bar: { display: "flex", gap: 4 },
})

const Mark = Schema.Literals(["bold", "italic", "code"])
type Mark = typeof Mark.Type
const Message = UiSchema.TaggedEnum({ ClickedMark: { mark: Mark } })

export const FormatToggles = Html.component({
	name: "FormatToggles",
	model: Schema.Struct({ marks: Schema.Array(Mark) }),
	message: Message,
}).make({
	init: () => ({ marks: ["bold"] }),
	update: {
		ClickedMark: ($, { mark }) => $.evolve({ marks: (marks) => (marks.includes(mark) ? marks.filter((m) => m !== mark) : [...marks, mark]) }),
	},
	view: (h, { marks }) =>
		h.div.attrs({ role: "toolbar", "aria-label": "Formatting", ...sx(styles.bar) }).children(
			([["bold", "Bold"], ["italic", "Italic"], ["code", "Code"]] as const).map(([mark, label]) =>
				// aria-pressed follows the Model; the toggle never flips itself
				Toggle(h, { pressed: marks.includes(mark), size: "sm" }).key(mark).on("click", Message.ClickedMark({ mark })).children(label)),
		),
})
```

## API reference

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `pressed` (required) | `boolean` | Whether 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

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/toggle
```

With npm: `npx shadcn@latest add checkfu/uicontext/toggle`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/toggle`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/toggle`.

It writes these files:

- `src/ui/uicontext/components/Button.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/Toggle.ts`
- `src/ui/uicontext/components/button.stylex.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/variants.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/Toggle.ts](https://github.com/checkfu/uicontext/blob/main/src/Toggle.ts)

## Used in

- [Workflow builder](https://uicontext.com/blocks/automation-builder.md)
- [Database](https://uicontext.com/blocks/tables.md)

## Related

- [Button](https://uicontext.com/docs/components/button.md) (`button`): A button, and a link that looks like one, in every tone and three sizes.
- [Toggle Group](https://uicontext.com/docs/components/toggle-group.md) (`toggle-group`): Buttons that stay pressed and share one value, such as text styles.
- [Input](https://uicontext.com/docs/components/input.md) (`input`): Text inputs, selects and textareas in a labeled field with a hint and an error, and their styles for controls you lay out yourself.
- [Status](https://uicontext.com/docs/components/status.md) (`status`): Status badges and tags for runs and records.
- [Glyphs](https://uicontext.com/docs/components/glyphs.md) (`glyphs`): Small marks for status, priority, color and people.
- [Action Toolbar](https://uicontext.com/docs/components/action-toolbar.md) (`action-toolbar`): A row of grouped buttons with one Tab stop, such as formatting toggles or bulk actions.
