# Linear Toggle

A two-state button that can be on or off.

Build a Toggle in this project that looks and behaves like Linear's. Measured from the running Linear app (linear.app). Keep every value exact. Map the tokens onto the project's own theme variables instead of hard-coding them. Use the project's own component library for behaviour, or [Base UI](https://base-ui.com/react/components/toggle) if it has none.

Fidelity: Derived from Linear's button sizes and chip tokens.

## Variants

- `size`: `small`, `medium`, `normal`, `large`

## Tokens

| Token | Light | Dark |
| --- | --- | --- |
| `--text-primary` | `lch(9.794% 0 282)` | `lch(100% 0 272)` |
| `--text-tertiary` | `lch(39.176% 1.25 282)` | `lch(61.803% 1.2 272)` |
| `--chip-bg` | `lch(100% 0 282)` | `lch(17.574% 1.589 272)` |
| `--chip-ring` | `lch(0% 0 0 / 0.088)` | `lch(100% 0 0 / 0.149)` |
| `--ghost-text-hover` | `lch(9.444% 0 282)` | `lch(100% 0 272)` |
| `--ghost-bg-hover` | `lch(0% 0 0 / 0.06)` | `lch(10.149% 0.689 272)` |
| `--shadow-low` | `lch(0% 0 0 / 0.02) 0 3px 6px -2px, lch(0% 0 0 / 0.04) 0 1px 1px 0` | `lch(0% 0 0 / 0.3) 0 0.5px 1px 1px` |
| `--focus-ring` | `#5e69d1` | `#5e69d1` |
| `--font-size-mini` | `0.75rem` | `0.75rem` |
| `--font-size-small` | `0.8125rem` | `0.8125rem` |
| `--font-weight-medium` | `500` | `500` |
| `--radius-rounded` | `9999px` | `9999px` |
| `--speed-highlight-fade-out` | `0.15s` | `0.15s` |

## CSS

Scoped under `[data-style="linear"]`; dark rules sit under `[data-theme="dark"]`. The `ui-*` classes and data attributes match the structure below.

```css
[data-style="linear"] {
  .ui-toggle {
    box-sizing: border-box;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    margin: 0;
    border: none;
    border-radius: var(--radius-rounded);
    background-color: transparent;
    color: var(--text-tertiary);
    font-family: inherit;
    font-weight: var(--font-weight-medium);
    line-height: 1;
    white-space: nowrap;
    user-select: none;
    cursor: default;
    transition:
      background-color var(--speed-highlight-fade-out),
      color var(--speed-highlight-fade-out),
      box-shadow var(--speed-highlight-fade-out),
      opacity var(--speed-highlight-fade-out);

    &:focus-visible {
      outline: 1px solid var(--focus-ring);
    }

    @media (hover: hover) {
      &:hover:not([data-disabled], [data-pressed]) {
        background-color: var(--ghost-bg-hover);
        color: var(--ghost-text-hover);
      }
    }

    &[data-pressed] {
      background-color: var(--chip-bg);
      color: var(--text-primary);
      box-shadow:
        0 0 0 0.5px var(--chip-ring),
        var(--shadow-low);
    }

    &[data-disabled] {
      opacity: 0.6;
    }

    & svg {
      width: 14px;
      height: 14px;
      flex-shrink: 0;
    }
  }

  .ui-toggle[data-size="small"] {
    height: 24px;
    min-width: 24px;
    padding: 0 8px;
    font-size: var(--font-size-mini);
  }

  .ui-toggle[data-size="medium"] {
    height: 28px;
    min-width: 28px;
    padding: 0 10px;
    font-size: var(--font-size-mini);
  }

  .ui-toggle[data-size="normal"] {
    height: 32px;
    min-width: 32px;
    padding: 0 12px;
    font-size: var(--font-size-small);

    & svg {
      width: 16px;
      height: 16px;
    }
  }

  .ui-toggle[data-size="large"] {
    height: 44px;
    min-width: 44px;
    padding: 0 18px;
    font-size: var(--font-size-small);

    & svg {
      width: 16px;
      height: 16px;
    }
  }

  .ui-toggle[data-icon-only] {
    aspect-ratio: 1;
    padding: 0;
  }
}
```

## Structure

How Replicas assembles it on Base UI.

```tsx
import { Toggle as BaseToggle } from "@base-ui/react/toggle";
import { join } from "./utils";

export type ToggleProps = Omit<BaseToggle.Props, "className"> & {
  className?: string;
  variant?: string;
  size?: string;
  iconOnly?: boolean;
};

export function Toggle({ className, variant, size = "medium", iconOnly = false, ...props }: ToggleProps) {
  return (
    <BaseToggle
      className={join("ui-toggle", className)}
      data-variant={variant}
      data-size={size}
      data-icon-only={iconOnly || undefined}
      {...props}
    />
  );
}
```

## Example

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

export default function ToggleDemo() {
  return (
    <>
      <Toggle defaultPressed>Show hidden files</Toggle>
      <Toggle>Show file extensions</Toggle>
      <Toggle aria-label="Favorite" iconOnly>
        <svg viewBox="0 0 16 16" fill="currentColor" aria-hidden>
          <path d="M8 1.5l1.9 3.9 4.3.6-3.1 3 .7 4.3L8 11.3l-3.8 2 .7-4.3-3.1-3 4.3-.6L8 1.5z" />
        </svg>
      </Toggle>
      <Toggle disabled>Archived</Toggle>
    </>
  );
}
```

## Foundations

The rest of the Linear style, as a skill: https://replicas.raphaelsalaja.com/skills/linear/SKILL.md