# Clerk Button

A button component that can be rendered as another tag or focusable when disabled.

Build a Button in this project that looks and behaves like Clerk's. Measured from the running Clerk app (clerk.com). 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/button) if it has none.

Fidelity: Measured from the Continue and GitHub buttons on Clerk's live sign-in preview.

## Variants

- `variant`: `solid`, `outline`, `bordered`, `ghost`, `ghost-neutral`, `link`, `danger`, `ghost-danger`
- `size`: `xs`, `sm`, `md`

## Tokens

| Token | Light | Dark |
| --- | --- | --- |
| `--clerk-background` | `#ffffff` | `#212126` |
| `--clerk-foreground` | `#212126` | `#ffffff` |
| `--clerk-primary` | `#2f3037` | `#ffffff` |
| `--clerk-primary-hover` | `color-mix(in srgb, #2f3037, white 20%)` | `color-mix(in srgb, #ffffff, white 20%)` |
| `--clerk-primary-foreground` | `#ffffff` | `#000000` |
| `--clerk-neutral-50` | `rgb(0 0 0 / 0.03)` | `rgb(255 255 255 / 0.03)` |
| `--clerk-neutral-600` | `rgb(0 0 0 / 0.62)` | `rgb(255 255 255 / 0.62)` |
| `--clerk-neutral-700` | `rgb(0 0 0 / 0.73)` | `rgb(255 255 255 / 0.73)` |
| `--clerk-danger` | `#ef4444` | `#ef4444` |
| `--clerk-danger-hover` | `hsl(from #ef4444 h s calc(l - ((l - 12) / 7)))` | `hsl(from #ef4444 h s calc(l - ((l - 12) / 7)))` |
| `--clerk-danger-50` | `rgb(239 68 68 / 0.03)` | `rgb(239 68 68 / 0.03)` |
| `--clerk-danger-200` | `rgb(239 68 68 / 0.15)` | `rgb(239 68 68 / 0.15)` |
| `--clerk-shadow-button` | `0 1px 1px 0 rgb(255 255 255 / 0.07) inset, 0 2px 3px 0 rgb(34 42 53 / 0.2), 0 1px 1px 0 rgb(0 0 0 / 0.24)` | `0 1px 1px 0 rgb(255 255 255 / 0.07) inset, 0 2px 3px 0 rgb(34 42 53 / 0.2), 0 1px 1px 0 rgb(0 0 0 / 0.24)` |
| `--clerk-ring` | `0 0 0 4px var(--clerk-neutral-200)` | `0 0 0 4px var(--clerk-neutral-200)` |
| `--clerk-shadow-outline-ring` | `0 0 0 1px var(--clerk-neutral-100), var(--clerk-shadow-outline)` | `0 0 0 1px var(--clerk-neutral-100), var(--clerk-shadow-outline)` |
| `--clerk-radius-md` | `6px` | `6px` |
| `--duration-controls` | `100ms` | `100ms` |
| `--duration-focus-ring` | `200ms` | `200ms` |
| `--duration-spinner` | `400ms` | `400ms` |
| `--font-size-mini` | `0.75rem` | `0.75rem` |
| `--font-size-small` | `0.8125rem` | `0.8125rem` |
| `--line-height-mini` | `calc(16 / 12)` | `calc(16 / 12)` |
| `--line-height-small` | `calc(18 / 13)` | `calc(18 / 13)` |
| `--font-weight-medium` | `500` | `500` |

## CSS

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

```css
[data-style="clerk"] {
  .ui-button {
    --button-shadow: 0 0 #0000;
    --button-ring: var(--clerk-ring);
    --button-fg: var(--clerk-neutral-600);

    box-sizing: border-box;
    position: relative;
    isolation: isolate;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin: 0;
    border: 0;
    border-radius: var(--clerk-radius-md);
    background-color: transparent;
    box-shadow: var(--button-shadow);
    color: var(--button-fg);
    font-family: inherit;
    font-weight: var(--font-weight-medium);
    letter-spacing: normal;
    white-space: nowrap;
    user-select: none;
    cursor: pointer;
    outline: 0;
    transition-property: background-color, background, border-color, color, fill, stroke, opacity, box-shadow, transform;
    transition-duration: var(--duration-controls);

    &:focus-visible {
      box-shadow: var(--button-shadow), var(--button-ring);
      transition-duration: var(--duration-focus-ring);
    }

    &[data-disabled] {
      opacity: 0.5;
      cursor: not-allowed;
      pointer-events: none;
    }

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

  .ui-button:is([data-size="small"], [data-size="xs"]) {
    height: 24px;
    min-width: 24px;
    padding: 0 12px;
    gap: 6px;
    font-size: var(--font-size-mini);
    line-height: var(--line-height-mini);

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

  .ui-button:is([data-size="medium"], [data-size="sm"]) {
    height: 32px;
    min-width: 32px;
    padding: 7px 12px;
    font-size: var(--font-size-small);
    line-height: var(--line-height-small);
  }

  .ui-button:is([data-size="large"], [data-size="md"]) {
    height: 38px;
    min-width: 38px;
    padding: 0 20px;
    font-size: var(--font-size-small);
    line-height: var(--line-height-small);
  }

  .ui-button[data-icon-only] {
    padding: 0;
  }

  .ui-button:is([data-variant="primary"], [data-variant="solid"], [data-variant="danger"]) {
    --solid: var(--clerk-primary);
    --solid-hover: var(--clerk-primary-hover);
    --button-fg: var(--clerk-primary-foreground);
    --button-shadow: 0 0 0 1px var(--solid), var(--clerk-shadow-button);

    background-color: var(--solid);

    &::after {
      content: "";
      position: absolute;
      inset: 0;
      z-index: -1;
      border-radius: inherit;
      background: linear-gradient(180deg, rgb(255 255 255 / 0.11) 0%, transparent 100%);
      opacity: 1;
      transition-property: background-color, background, border-color, color, fill, stroke, opacity, box-shadow, transform;
      transition-duration: var(--duration-controls);
    }

    @media (hover: hover) {
      &:hover {
        background-color: var(--solid-hover);

        &::after {
          opacity: 0;
        }
      }
    }

    &:active::after {
      opacity: 1;
    }
  }

  .ui-button[data-variant="danger"] {
    --solid: var(--clerk-danger);
    --solid-hover: var(--clerk-danger-hover);
    --button-fg: white;
    --button-ring: 0 0 0 4px var(--clerk-danger-200);
  }

  .ui-button:is([data-variant="secondary"], [data-variant="outline"]) {
    --button-shadow: var(--clerk-shadow-outline-ring);

    @media (hover: hover) {
      &:hover {
        background-color: var(--clerk-neutral-50);
      }
    }
  }

  .ui-button[data-variant="bordered"] {
    --button-shadow: var(--clerk-shadow-outline-ring);
    --button-fg: var(--clerk-foreground);

    background-color: var(--clerk-background);

    @media (hover: hover) {
      &:hover {
        background-color: color-mix(in srgb, var(--clerk-background), var(--clerk-neutral-50));
      }
    }
  }

  .ui-button:is([data-variant="ghost"], [data-variant="ghost-neutral"], [data-variant="ghost-danger"]) {
    --button-fg: var(--clerk-primary);
    --button-fg-hover: var(--clerk-primary-hover);
    --button-bg-hover: var(--clerk-neutral-50);

    @media (hover: hover) {
      &:hover {
        background-color: var(--button-bg-hover);
        color: var(--button-fg-hover);
      }
    }
  }

  .ui-button[data-variant="ghost-neutral"] {
    --button-fg: var(--clerk-neutral-600);
    --button-fg-hover: var(--clerk-neutral-700);
  }

  .ui-button[data-variant="ghost-danger"] {
    --button-fg: var(--clerk-danger);
    --button-fg-hover: var(--clerk-danger-hover);
    --button-bg-hover: var(--clerk-danger-50);
    --button-ring: 0 0 0 4px var(--clerk-danger-200);
  }

  .ui-button[data-variant="link"] {
    --button-fg: var(--clerk-primary);

    height: fit-content;
    min-width: 0;
    padding: 0;

    @media (hover: hover) {
      &:hover {
        text-decoration: underline;
      }
    }
  }

  .ui-button[data-loading] {
    color: transparent;
  }

  .ui-button-spinner {
    position: absolute;
    inset: 0;
    width: 16px;
    height: 16px;
    margin: auto;
    border: 2px solid var(--button-fg);
    border-bottom-color: transparent;
    border-left-color: transparent;
    border-radius: 99999px;
    animation: clerk-spinner-spin var(--duration-spinner) linear infinite;
  }
}
```

## Structure

How Replicas assembles it on Base UI.

```tsx
import { Button as BaseButton } from "@base-ui/react/button";
import { join } from "./utils";

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

export function Button({
  className,
  variant = "secondary",
  size = "medium",
  iconOnly = false,
  loading = false,
  disabled,
  children,
  ...props
}: ButtonProps) {
  return (
    <BaseButton
      className={join("ui-button", className)}
      data-variant={variant}
      data-size={size}
      data-icon-only={iconOnly || undefined}
      data-loading={loading || undefined}
      aria-busy={loading || undefined}
      disabled={disabled || loading}
      focusableWhenDisabled={loading}
      {...props}
    >
      {loading ? <span className="ui-button-spinner" aria-hidden /> : null}
      {children}
    </BaseButton>
  );
}
```

## Example

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

export default function ButtonDemo() {
  return (
    <>
      <Button variant="primary">Save changes</Button>
      <Button>Share</Button>
      <Button variant="ghost">Cancel</Button>
      <Button size="small">Edit</Button>
      <Button disabled>Disabled</Button>
    </>
  );
}
```

## Foundations

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