# Toggle

A button that stays pressed, such as Bold in a text toolbar.

A button that stays pressed: bold in a text toolbar, pin, follow, the open view of an activity bar. It is the Button (same sizes, shapes, square and muted, ghost and outline looks) whose pressed state keeps the darker fill, the way an open ghost button does. Icon-only toggles are named by a Tooltip. Controlled or uncontrolled.

Kind: component · Category: Actions · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `toggle` machine, through Ark UI

## Add

```bash
npx fndn add toggle
```

It writes the folder `toggle/` into the project's ui folder (`index.ts`, `toggle.tsx`, `Toggle.vue`, `Toggle.svelte`, `toggle.recipe.ts`), and what it is built from: [Button](/components/button.md), [Tooltip](/components/tooltip.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { Toggle } from "@/components/ui/toggle";
```

```vue
import { Toggle } from "@/components/ui/toggle";
```

```svelte
import { Toggle } from "$lib/components/ui/toggle";
```

## Parts

In the DOM the root carries `data-slot="toggle"` and each part `data-slot="toggle-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `button` | `toggle` |  | Ark UI's toggle root: a <button> with aria-pressed (true / false) and data-state (on / off). Its classes are the button recipe's, extended with the pressed fill. |
| `icon` | `span` | `toggle-icon` | optional | Wraps and centres the icon; aria-hidden. Sized as the Button's icon part (1em of the label, a size larger when square). |
| `label` | `span` | `toggle-label` | optional | Text of the toggle; cut at the end with a title when the toggle has less room than its label, as the Button's label. |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `pressed` | `boolean` | `false` | Bindable (React: `defaultPressed` and `onPressedChange`; Vue: `v-model:pressed`; Svelte: `bind:pressed`). Controlled: pressed with onPressedChange. Uncontrolled: defaultPressed. |
| `variant` | `"outline" \| "ghost"` | `"outline"` | The Button's looks. outline: surface with shadow.btn; pressed fills hover-2, hovered or not. ghost: transparent; hover fills hover, pressed fills hover-2, hovered or not (toolbars and app chrome: an activity bar, a panel switch, a bookmark star). |
| `size` | `"xs" \| "sm" \| "md" \| "lg"` | `"md"` | The Button's sizes. The one control height scale: xs 24px, sm 28px, md 32px, lg 36px (an sm Toggle lines up with an sm Button or Select). Dimensions only, as the Button. |
| `shape` | `"pill" \| "rounded"` |  | Radius only, as the Button. Left out, the app's shape: a pill, or rounded in a rounded app ("shape": "rounded" in the project's fndn.json). Named: pill, or rounded (radius.chip at xs and sm, radius.control at md and lg). |
| `square` | `boolean` | `false` | Icon-only: as wide as it is tall. Needs an accessible name: tooltip (which also names it) or aria-label. |
| `muted` | `boolean` | `false` | Quiet at rest, ink when hovered, held down or pressed (with variant ghost). An icon-only toggle (square) rests in ink-3: toolbars of small icon toggles. A toggle with a label rests in ink-2. |
| `tooltip` | `string` |  | Wraps the toggle in a Tooltip with this text. A square (or label-less) toggle without an aria-label is named by it. |
| `disabled` | `boolean` | `false` | Inert: no hover, no press, not clickable (the native disabled attribute, which Ark sets). |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `pressedChange` | (pressed: boolean) after a click, Space or Enter |

## Slots

| Slot | Content |
| --- | --- |
| `default` | Label |
| `icon` | Icon (wrapped in the icon part). React: the icon prop (a node). |

## States

`pressed`, `hover`, `press`, `focus-visible`, `disabled`

## Keyboard and accessibility

Role: button.

Keyboard:

- Space / Enter toggle

Notes:

- State is aria-pressed (true / false), announced as "toggle button, pressed"; the label must not change with the state (that is a two-states Button, not a toggle).
- An icon-only toggle needs an accessible name: the tooltip prop names it (aria-label from the tooltip text) unless an aria-label is given.
- Disabled toggles are inert: no hover, no press, not clickable.
- To set it from outside, control `pressed`: React pressed / onPressedChange.

## Motion

- the Button's: press scales to motion.press-scale over duration.press with easing.out-strong; hover fill and colour over duration.fast (pointer only)
- pressed fill: over duration.fast from a pointer; instant from the keyboard (kbd-instant)
- no entrance and no other motion

## Tokens

`color.surface`, `color.ink`, `color.ink-3`, `color.hover`, `color.hover-2`, `shadow.btn`, `radius.pill`, `radius.shape`, `radius.shape-sm`, `radius.control`, `radius.chip`, `text.xs`, `text.sm`, `text.md`, `duration.fast`, `duration.base`, `duration.press`, `easing.out-strong`, `motion.press-scale`

## Dependencies

- [Button](/components/button.md)
- [Tooltip](/components/tooltip.md)

## Examples

### Watch a pull request

React · `watch-pull-request.tsx`

```tsx
import { useState } from "react";
import { Bell } from "@phosphor-icons/react";
import { Toggle } from "@/components/ui/toggle";

/* a toggle keeps its label; the pressed fill and the line under it say the state */
export default function WatchPullRequest() {
  const [watching, setWatching] = useState(true);
  return (
    <div className="flex max-w-80 flex-col items-start gap-2">
      <p className="text-sm font-medium text-ink">#482 Add retry to webhook delivery</p>
      <div className="flex items-center gap-3">
        <Toggle size="sm" pressed={watching} onPressedChange={setWatching} icon={<Bell data-slot="icon" weight="bold" />}>
          Watch
        </Toggle>
        <span className="text-sm text-ink-2">{watching ? "Every review and comment" : "Only when you are mentioned"}</span>
      </div>
    </div>
  );
}
```

Vue · `watch-pull-request.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { PhBell } from "@phosphor-icons/vue";
import { Toggle } from "@/components/ui/toggle";

/* a toggle keeps its label; the pressed fill and the line under it say the state */
const watching = ref(true);
</script>

<template>
  <div class="flex max-w-80 flex-col items-start gap-2">
    <p class="text-sm font-medium text-ink">#482 Add retry to webhook delivery</p>
    <div class="flex items-center gap-3">
      <Toggle v-model:pressed="watching" size="sm">
        <template #icon><PhBell data-slot="icon" weight="bold" /></template>
        Watch
      </Toggle>
      <span class="text-sm text-ink-2">{{ watching ? "Every review and comment" : "Only when you are mentioned" }}</span>
    </div>
  </div>
</template>
```

Svelte · `watch-pull-request.svelte`

```svelte
<script lang="ts">
  import Bell from "phosphor-svelte/lib/BellIcon";
  import { Toggle } from "$lib/components/ui/toggle";

  /* a toggle keeps its label; the pressed fill and the line under it say the state */
  let watching = $state(true);
</script>

<div class="flex max-w-80 flex-col items-start gap-2">
  <p class="text-sm font-medium text-ink">#482 Add retry to webhook delivery</p>
  <div class="flex items-center gap-3">
    <Toggle bind:pressed={watching} size="sm">
      {#snippet icon()}<Bell data-slot="icon" weight="bold" />{/snippet}
      Watch
    </Toggle>
    <span class="text-sm text-ink-2">{watching ? "Every review and comment" : "Only when you are mentioned"}</span>
  </div>
</div>
```

### Log options, icon-only

React · `log-options.tsx`

```tsx
import { ArrowDown, Clock, TextAlignLeft } from "@phosphor-icons/react";
import { Toggle } from "@/components/ui/toggle";

/* icon-only toggles: ghost, muted and square; the tooltip names each one */
export default function LogOptions() {
  return (
    <div className="flex items-center gap-3">
      <span className="text-sm text-ink-2">Build log · dpl_7f3a9c</span>
      <div role="toolbar" aria-label="Log options" className="flex items-center gap-0.5">
        <Toggle variant="ghost" size="sm" square muted tooltip="Show timestamps" defaultPressed icon={<Clock data-slot="icon" weight="bold" />} />
        <Toggle variant="ghost" size="sm" square muted tooltip="Wrap long lines" icon={<TextAlignLeft data-slot="icon" weight="bold" />} />
        <Toggle variant="ghost" size="sm" square muted tooltip="Follow new output" defaultPressed icon={<ArrowDown data-slot="icon" weight="bold" />} />
      </div>
    </div>
  );
}
```

Vue · `log-options.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { PhArrowDown, PhClock, PhTextAlignLeft } from "@phosphor-icons/vue";
import { Toggle } from "@/components/ui/toggle";

const timestamps = ref(true);
const wrap = ref(false);
const follow = ref(true);
</script>

<!-- icon-only toggles: ghost, muted and square; the tooltip names each one -->
<template>
  <div class="flex items-center gap-3">
    <span class="text-sm text-ink-2">Build log · dpl_7f3a9c</span>
    <div role="toolbar" aria-label="Log options" class="flex items-center gap-0.5">
      <Toggle variant="ghost" size="sm" square muted v-model:pressed="timestamps" tooltip="Show timestamps">
        <template #icon><PhClock data-slot="icon" weight="bold" /></template>
      </Toggle>
      <Toggle variant="ghost" size="sm" square muted v-model:pressed="wrap" tooltip="Wrap long lines">
        <template #icon><PhTextAlignLeft data-slot="icon" weight="bold" /></template>
      </Toggle>
      <Toggle variant="ghost" size="sm" square muted v-model:pressed="follow" tooltip="Follow new output">
        <template #icon><PhArrowDown data-slot="icon" weight="bold" /></template>
      </Toggle>
    </div>
  </div>
</template>
```

Svelte · `log-options.svelte`

```svelte
<script lang="ts">
  import ArrowDown from "phosphor-svelte/lib/ArrowDownIcon";
  import Clock from "phosphor-svelte/lib/ClockIcon";
  import TextAlignLeft from "phosphor-svelte/lib/TextAlignLeftIcon";
  import { Toggle } from "$lib/components/ui/toggle";
</script>

<!-- icon-only toggles: ghost, muted and square; the tooltip names each one -->
<div class="flex items-center gap-3">
  <span class="text-sm text-ink-2">Build log · dpl_7f3a9c</span>
  <div role="toolbar" aria-label="Log options" class="flex items-center gap-0.5">
    <Toggle variant="ghost" size="sm" square muted tooltip="Show timestamps" pressed>
      {#snippet icon()}<Clock data-slot="icon" weight="bold" />{/snippet}
    </Toggle>
    <Toggle variant="ghost" size="sm" square muted tooltip="Wrap long lines">
      {#snippet icon()}<TextAlignLeft data-slot="icon" weight="bold" />{/snippet}
    </Toggle>
    <Toggle variant="ghost" size="sm" square muted tooltip="Follow new output" pressed>
      {#snippet icon()}<ArrowDown data-slot="icon" weight="bold" />{/snippet}
    </Toggle>
  </div>
</div>
```

### An editor's activity bar

React · `activity-bar.tsx`

```tsx
import { useState } from "react";
import { Folder, GitBranch, MagnifyingGlass, Terminal } from "@phosphor-icons/react";
import { Toggle } from "@/components/ui/toggle";

const views = [
  { value: "explorer", label: "Explorer", Icon: Folder },
  { value: "search", label: "Search", Icon: MagnifyingGlass },
  { value: "source-control", label: "Source control", Icon: GitBranch },
  { value: "terminal", label: "Terminal", Icon: Terminal },
];

/* an editor's activity bar: muted ghost toggles. The open view keeps the pressed fill and its ink;
 * pressing it again closes the side bar */
export default function ActivityBar() {
  const [view, setView] = useState<string | null>("explorer");
  return (
    <div role="toolbar" aria-orientation="vertical" aria-label="Views" className="flex w-11 flex-col items-center gap-1 rounded-card bg-canvas py-1.5">
      {views.map(({ value, label, Icon }) => (
        <Toggle
          key={value}
          variant="ghost"
          size="md"
          shape="rounded"
          square
          muted
          tooltip={label}
          pressed={view === value}
          onPressedChange={(pressed) => setView(pressed ? value : null)}
          icon={<Icon data-slot="icon" weight="bold" />}
        />
      ))}
    </div>
  );
}
```

Vue · `activity-bar.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { PhFolder, PhGitBranch, PhMagnifyingGlass, PhTerminal } from "@phosphor-icons/vue";
import { Toggle } from "@/components/ui/toggle";

const views = [
  { value: "explorer", label: "Explorer", icon: PhFolder },
  { value: "search", label: "Search", icon: PhMagnifyingGlass },
  { value: "source-control", label: "Source control", icon: PhGitBranch },
  { value: "terminal", label: "Terminal", icon: PhTerminal },
];

const view = ref<string | null>("explorer");
</script>

<!-- an editor's activity bar: muted ghost toggles. The open view keeps the pressed fill and its ink;
      pressing it again closes the side bar -->
<template>
  <div role="toolbar" aria-orientation="vertical" aria-label="Views" class="flex w-11 flex-col items-center gap-1 rounded-card bg-canvas py-1.5">
    <Toggle
      v-for="v in views"
      :key="v.value"
      variant="ghost"
      size="md"
      shape="rounded"
      square
      muted
      :tooltip="v.label"
      :pressed="view === v.value"
      @update:pressed="(pressed: boolean) => (view = pressed ? v.value : null)"
    >
      <template #icon><component :is="v.icon" data-slot="icon" weight="bold" /></template>
    </Toggle>
  </div>
</template>
```

Svelte · `activity-bar.svelte`

```svelte
<script lang="ts">
  import Folder from "phosphor-svelte/lib/FolderIcon";
  import GitBranch from "phosphor-svelte/lib/GitBranchIcon";
  import MagnifyingGlass from "phosphor-svelte/lib/MagnifyingGlassIcon";
  import Terminal from "phosphor-svelte/lib/TerminalIcon";
  import { Toggle } from "$lib/components/ui/toggle";

  const views = [
    { value: "explorer", label: "Explorer", Icon: Folder },
    { value: "search", label: "Search", Icon: MagnifyingGlass },
    { value: "source-control", label: "Source control", Icon: GitBranch },
    { value: "terminal", label: "Terminal", Icon: Terminal },
  ];

  let view = $state<string | null>("explorer");
</script>

<!-- an editor's activity bar: muted ghost toggles. The open view keeps the pressed fill and its ink;
      pressing it again closes the side bar -->
<div role="toolbar" aria-orientation="vertical" aria-label="Views" class="flex w-11 flex-col items-center gap-1 rounded-card bg-canvas py-1.5">
  {#each views as { value, label, Icon } (value)}
    <Toggle
      variant="ghost"
      size="md"
      shape="rounded"
      square
      muted
      tooltip={label}
      pressed={view === value}
      onpressedchange={(pressed) => (view = pressed ? value : null)}
    >
      {#snippet icon()}<Icon data-slot="icon" weight="bold" />{/snippet}
    </Toggle>
  {/each}
</div>
```

### A filter in a list header (ghost)

React · `failed-only.tsx`

```tsx
import { useState } from "react";
import { WarningCircle } from "@phosphor-icons/react";
import { Toggle } from "@/components/ui/toggle";

/* ghost: no fill at rest, for a filter that sits in a list header */
export default function FailedOnly() {
  const [failedOnly, setFailedOnly] = useState(false);
  return (
    <div className="flex w-full max-w-80 items-center justify-between gap-3">
      <p className="text-sm font-medium text-ink">{failedOnly ? "3 failed deploys" : "48 deploys this week"}</p>
      <Toggle variant="ghost" size="sm" pressed={failedOnly} onPressedChange={setFailedOnly} icon={<WarningCircle data-slot="icon" weight="bold" />}>
        Failed only
      </Toggle>
    </div>
  );
}
```

Vue · `failed-only.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { PhWarningCircle } from "@phosphor-icons/vue";
import { Toggle } from "@/components/ui/toggle";

/* ghost: no fill at rest, for a filter that sits in a list header */
const failedOnly = ref(false);
</script>

<template>
  <div class="flex w-full max-w-80 items-center justify-between gap-3">
    <p class="text-sm font-medium text-ink">{{ failedOnly ? "3 failed deploys" : "48 deploys this week" }}</p>
    <Toggle v-model:pressed="failedOnly" variant="ghost" size="sm">
      <template #icon><PhWarningCircle data-slot="icon" weight="bold" /></template>
      Failed only
    </Toggle>
  </div>
</template>
```

Svelte · `failed-only.svelte`

```svelte
<script lang="ts">
  import WarningCircle from "phosphor-svelte/lib/WarningCircleIcon";
  import { Toggle } from "$lib/components/ui/toggle";

  /* ghost: no fill at rest, for a filter that sits in a list header */
  let failedOnly = $state(false);
</script>

<div class="flex w-full max-w-80 items-center justify-between gap-3">
  <p class="text-sm font-medium text-ink">{failedOnly ? "3 failed deploys" : "48 deploys this week"}</p>
  <Toggle bind:pressed={failedOnly} variant="ghost" size="sm">
    {#snippet icon()}<WarningCircle data-slot="icon" weight="bold" />{/snippet}
    Failed only
  </Toggle>
</div>
```

### Read-only for viewers

React · `viewers-read-only.tsx`

```tsx
import { GitBranch } from "@phosphor-icons/react";
import { Toggle } from "@/components/ui/toggle";

/* disabled keeps its pressed state visible and says who can change it */
export default function ViewersReadOnly() {
  return (
    <div className="flex max-w-80 flex-col items-start gap-2">
      <Toggle size="sm" disabled defaultPressed aria-describedby="auto-merge-why" icon={<GitBranch data-slot="icon" weight="bold" />}>
        Auto-merge
      </Toggle>
      <p id="auto-merge-why" className="text-sm text-ink-2">You have viewer access to web-app. Ask a maintainer to change this.</p>
    </div>
  );
}
```

Vue · `viewers-read-only.vue`

```vue
<script setup lang="ts">
import { PhGitBranch } from "@phosphor-icons/vue";
import { Toggle } from "@/components/ui/toggle";
</script>

<!-- disabled keeps its pressed state visible and says who can change it -->
<template>
  <div class="flex max-w-80 flex-col items-start gap-2">
    <Toggle size="sm" disabled pressed aria-describedby="auto-merge-why">
      <template #icon><PhGitBranch data-slot="icon" weight="bold" /></template>
      Auto-merge
    </Toggle>
    <p id="auto-merge-why" class="text-sm text-ink-2">You have viewer access to web-app. Ask a maintainer to change this.</p>
  </div>
</template>
```

Svelte · `viewers-read-only.svelte`

```svelte
<script lang="ts">
  import GitBranch from "phosphor-svelte/lib/GitBranchIcon";
  import { Toggle } from "$lib/components/ui/toggle";
</script>

<!-- disabled keeps its pressed state visible and says who can change it -->
<div class="flex max-w-80 flex-col items-start gap-2">
  <Toggle size="sm" disabled pressed aria-describedby="auto-merge-why">
    {#snippet icon()}<GitBranch data-slot="icon" weight="bold" />{/snippet}
    Auto-merge
  </Toggle>
  <p id="auto-merge-why" class="text-sm text-ink-2">You have viewer access to web-app. Ask a maintainer to change this.</p>
</div>
```

## Recipe

`toggle.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";
import { button } from "../button/button.recipe";

/* toggle — a button that stays pressed (Ark UI toggle: aria-pressed, data-state on|off). It is the
 * Button: the button recipe extended, so sizes, shapes, square, muted, the press scale and every
 * hover and motion rule come from button.recipe.ts and are never copied here. What it adds:
 *
 *  - pressed keeps the darker fill, hovered or not, the way an open ghost button keeps hover-2
 *    with aria-expanded: hover-2 for both looks (on white, the outline's open fill `hover` is
 *    too close to the surface to read as on). muted turns ink.
 *  - a change from the keyboard (Space / Enter) is instant: kbd-instant zeroes the fill's fade
 *    while the last input was the keyboard (trackModality sets it).
 *
 * Only the ghost and outline looks are a toggle's (the adapter's types say so); the others are the
 * Button's and have no pressed fill. Styled from aria-pressed, which Ark writes; data-state is not
 * used because a Tooltip around the toggle writes its own data-state on the same element. */
export const toggle = tv({
  extend: button,
  slots: {
    base: "kbd-instant",
  },
  variants: {
    variant: {
      outline: { base: "aria-pressed:bg-hover-2 aria-pressed:hover:bg-hover-2" },
      ghost: { base: "aria-pressed:bg-hover-2 aria-pressed:hover:bg-hover-2" },
    },
    muted: {
      true: { base: "aria-pressed:text-ink" },
    },
  },
  defaultVariants: { variant: "outline", size: "md" },
});

export type ToggleRecipeProps = VariantProps<typeof toggle>;
```
