# Rocker Switch

> A rocker switch, as on a mixer or a synth, the side pressed down sunk into the panel.

```tsx
import { useState } from "react";
import { RockerSwitch } from "@/components/audio-ui/rocker-switch";
import { Led } from "@/components/audio-ui/led";

export function RockerSwitchDemo() {
  const [power, setPower] = useState(true);
  return (
    <div className="flex items-center gap-8">
      <div className="flex flex-col items-center gap-2">
        <Led label="Power" on={power} />
        <RockerSwitch
          aria-label="Power"
          checked={power}
          onCheckedChange={setPower}
        />
        <span className="text-muted-foreground text-xs uppercase tracking-wide">
          Power
        </span>
      </div>
      <RockerSwitch
        aria-label="Phase invert"
        defaultChecked={false}
        offLabel="Ø"
        onLabel="Ø"
        orientation="horizontal"
        size="sm"
      />
      <RockerSwitch aria-label="Pad, disabled" disabled size="sm" />
    </div>
  );
}
```

A rocker, as on a mixer's or a synth's panel: two sides, the one pressed down sunk into the panel. Underneath it is a switch, with its role, Space to flip it, and its form value (`name`). Its shape follows the style, from a pill to a square.

## Installation

**CLI**

```bash
npx shadcn@latest add @audio-ui/rocker-switch
```

**Manual**

**1. Install the following dependencies:**

```bash
npm install @ui-sdk/core@0.1.0-beta.14 @ui-sdk/react@0.1.0-beta.14 class-variance-authority
```

**2. Copy and paste the following code into your project.**

```tsx title="components/audio-ui/rocker-switch.tsx"
import { Switch } from "@ui-sdk/react/switch";
import { cva, type VariantProps } from "class-variance-authority";
import type { ComponentProps, ReactNode } from "react";
import { cn } from "@/lib/utils";

const rockerSwitchVariants = cva(
  "rounded-lg bg-muted p-[3px] shadow-inner outline-none ring-1 ring-border transition-shadow focus-visible:ring-2 focus-visible:ring-ring data-disabled:cursor-not-allowed data-disabled:opacity-50 group/rocker inline-flex shrink-0",
  {
    compoundVariants: [
      {
        className: "h-14 w-8 text-[0.6875rem]",
        orientation: "vertical",
        size: "default",
      },
      {
        className: "h-10 w-6 text-[0.5625rem]",
        orientation: "vertical",
        size: "sm",
      },
      {
        className: "h-8 w-14 text-[0.6875rem]",
        orientation: "horizontal",
        size: "default",
      },
      {
        className: "h-6 w-10 text-[0.5625rem]",
        orientation: "horizontal",
        size: "sm",
      },
    ],
    defaultVariants: { orientation: "vertical", size: "default" },
    variants: {
      /** Stood up, on at the top, or laid down, on at the right. */
      orientation: {
        horizontal: "",
        vertical: "",
      },
      size: {
        default: "",
        sm: "",
      },
    },
  }
);

export interface RockerSwitchProps
  extends Omit<ComponentProps<typeof Switch>, "children" | "className">,
    VariantProps<typeof rockerSwitchVariants> {
  className?: string;
  /** What the off side reads. @default "O" */
  offLabel?: ReactNode;
  /** What the on side reads. @default "I" */
  onLabel?: ReactNode;
}

/**
 * A rocker, as on a mixer's or a synth's panel: one cap in a bezel that tips
 * on its middle, the side pressed down sloping into the panel and the other
 * standing out, the on side lit while it is on. A switch underneath, with its
 * role, its keyboard (Space) and its form value; name it with `aria-label`.
 */
export function RockerSwitch({
  className,
  offLabel = "O",
  onLabel = "I",
  orientation,
  size,
  ...props
}: RockerSwitchProps) {
  return (
    <Switch
      className={cn(rockerSwitchVariants({ orientation, size }), className)}
      data-orientation={orientation ?? "vertical"}
      data-size={size ?? "default"}
      data-slot="rocker-switch"
      {...props}
    >
      <span
        aria-hidden="true"
        className="rounded-md overflow-hidden bg-border shadow-sm flex flex-1 gap-px group-data-[orientation=horizontal]/rocker:flex-row-reverse group-data-[orientation=vertical]/rocker:flex-col"
        data-slot="rocker-switch-cap"
      >
        {/* Each side slopes toward its own edge: `--rocker-edge` is where that edge is. */}
        <span
          className="font-mono font-semibold text-muted-foreground transition-[color,box-shadow] duration-100 bg-[linear-gradient(var(--rocker-edge),color-mix(in_oklab,white_6%,var(--muted)),color-mix(in_oklab,white_16%,var(--muted)))] group-data-checked/rocker:bg-[linear-gradient(var(--rocker-edge),color-mix(in_oklab,black_4%,var(--muted)),color-mix(in_oklab,black_14%,var(--muted)))] group-data-checked/rocker:text-primary group-data-checked/rocker:shadow-[inset_0_0_3px_rgb(0_0_0/0.18)] flex flex-1 items-center justify-center group-data-[orientation=horizontal]/rocker:[--rocker-edge:to_right] group-data-[orientation=vertical]/rocker:[--rocker-edge:to_top]"
          data-slot="rocker-switch-on"
        >
          {onLabel}
        </span>
        <span
          className="font-mono font-semibold text-muted-foreground transition-[color,box-shadow] duration-100 bg-[linear-gradient(var(--rocker-edge),color-mix(in_oklab,black_4%,var(--muted)),color-mix(in_oklab,black_14%,var(--muted)))] shadow-[inset_0_0_3px_rgb(0_0_0/0.18)] group-data-checked/rocker:bg-[linear-gradient(var(--rocker-edge),color-mix(in_oklab,white_6%,var(--muted)),color-mix(in_oklab,white_16%,var(--muted)))] group-data-checked/rocker:shadow-none flex flex-1 items-center justify-center group-data-[orientation=horizontal]/rocker:[--rocker-edge:to_left] group-data-[orientation=vertical]/rocker:[--rocker-edge:to_bottom]"
          data-slot="rocker-switch-off"
        >
          {offLabel}
        </span>
      </span>
    </Switch>
  );
}
```

**3. Update the import paths to match your project setup.**

**Agent**

Paste this prompt to your agent. It does the rest: it installs the skill, adds the component and wires it to your audio.

```text
Install the audio/ui skill with `npx skills add ouestlabs/audio-ui`, then follow it to add the rocker switch from audio/ui to my project and wire it to my audio code. Read the docs at https://www.audio-ui.dev/llms.txt first.
```

To let the agent read the docs live, connect the [MCP server](/docs/mcp). More in [Agent Skill](/docs/agents).

## Usage

```tsx
import { RockerSwitch } from "@/components/audio-ui/rocker-switch";

<RockerSwitch aria-label="Power" checked={power} onCheckedChange={setPower} />
<RockerSwitch aria-label="Phase invert" offLabel="Ø" onLabel="Ø" orientation="horizontal" size="sm" />
```

## API reference

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `checked, onCheckedChange` | `boolean, (checked) => void` |  | Whether it is on, when it is held outside. |
| `defaultChecked` | `boolean` | `false` | Whether it starts on, when it keeps its own state. |
| `onLabel, offLabel` | `ReactNode` | `"I", "O"` | What the on and the off side read. |
| `orientation` | `"vertical" \| "horizontal"` | `"vertical"` | Stood up, on at the top, or laid down, on at the right. |
| `size` | `"default" \| "sm"` | `"default"` | Its size. |
| `disabled` | `boolean` | `false` | Takes no input. |
