# Toggle

A button that stays pressed until it is pressed again, such as Bold in a text editor. It renders a native button with aria-pressed. Keep its label the same in both states: a Mute toggle stays Mute while pressed, and screen readers announce it as pressed.

- Entry point: `menhir/toggle`
- Size: 1.0 kB, minified and gzipped, Solid left out

> To add Toggle to a project, install the packages below with the project's package manager, then copy the example that matches how the project is styled, Tailwind CSS or plain CSS, and adapt its markup and class names to the project.

## Install

```sh
pnpm add menhir solid-js @solidjs/web
```

## Anatomy

```tsx
<Toggle.Root />
```

## Example

### Tailwind CSS

`ToggleExample.tsx`:

```tsx
import { Toggle } from "menhir/toggle";

export default function ToggleExample() {
  return (
    <Toggle.Root
      aria-label="Bold"
      class="size-9 cursor-pointer rounded-md border border-stone-300 font-bold focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 data-pressed:border-transparent data-pressed:bg-stone-800 data-pressed:text-white dark:border-stone-700 dark:data-pressed:bg-stone-300 dark:data-pressed:text-stone-900"
    >
      B
    </Toggle.Root>
  );
}
```

### CSS

`ToggleExample.tsx`:

```tsx
import { Toggle } from "menhir/toggle";

import "./toggle.css";

export default function ToggleExample() {
  return (
    <Toggle.Root aria-label="Bold" class="toggle">
      B
    </Toggle.Root>
  );
}
```

`toggle.css`:

```css
/* The least CSS that makes the Toggle visible: a square button, filled
   while pressed, selected by data-pressed. Everything else is the
   browser's. Colours are Tailwind's stone palette; light-dark() picks the
   second one in dark mode (color-scheme: dark). */
.toggle {
  box-sizing: border-box;
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  border: 1px solid
    light-dark(
      oklch(86.9% 0.005 56.366) /* stone-300 */,
      oklch(37.4% 0.01 67.558) /* stone-700 */
    );
  border-radius: 0.375rem;
  background: transparent;
  color: inherit;
  font:
    bold 1rem system-ui,
    sans-serif;
  cursor: pointer;
}
.toggle[data-pressed] {
  border-color: transparent;
  background: light-dark(
    oklch(26.8% 0.007 34.298) /* stone-800 */,
    oklch(86.9% 0.005 56.366) /* stone-300 */
  );
  color: light-dark(#fff, oklch(21.6% 0.006 56.043) /* stone-900 */);
}
```

## API reference

### Root

<button type="button" aria-pressed>. Name it with its content, or with aria-label when it shows only an icon. Its render function must render a <button>.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `pressed` | `boolean` | — | Whether the toggle is pressed, when controlled. Pair it with onPressedChange. |
| `defaultPressed` | `boolean` | `false` | Whether the toggle starts pressed, when uncontrolled. |
| `onPressedChange` | `(pressed: boolean) => void` | — | Called with the new value when the user presses the toggle. A controlled toggle changes only if pressed does. |
| `disabled` | `boolean` | `false` | Whether the toggle ignores the user. It cannot be focused either. |
| `render` | `(props) => JSX.Element` | — | Renders the toggle with your own button component. Read once. |

## Keyboard

| Keys | Action |
| --- | --- |
| `Space` | Presses or releases the toggle. |
| `Enter` | Presses or releases the toggle. |
| `Tab` | Moves focus to or away from the toggle. |
