# Lens 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 Lens's. Measured from the running Lens app (developer.lens.xyz). 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 Copy Address, Change and Disable verification on an app page, and Create App on the new-app form, at rest and on hover. Black, red and ghost colour variants come from the button CSS; the small and large sizes are derived. The dark theme is derived.

## Variants

- `variant`: `green`, `black`, `white`, `ghost`, `black-ghost`, `light`, `red`, `red-outline`, `red-ghost`, `green-ghost`

## Tokens

| Token | Light | Dark |
| --- | --- | --- |
| `--lens-blackberry-black` | `#2c2d30` | `#ececed` |
| `--lens-blackberry-black-a03` | `#2c2d3008` | `#ececed0d` |
| `--lens-blackberry-black-a3` | `#2c2d304d` | `#ececed54` |
| `--lens-white` | `#fff` | `#1c1d1f` |
| `--lens-red` | `#f00020` | `#ff3b4f` |
| `--lens-red-a5` | `#f0002080` | `#ff3b4f80` |
| `--lens-green` | `#00caa0` | `#00caa0` |
| `--lens-text-primary` | `#2c2d30` | `#ececed` |
| `--lens-text-secondary` | `#2c2d3099` | `#ececed9e` |
| `--lens-text-menu-destructive` | `#f00020` | `#ff3b4f` |
| `--lens-black-hover` | `color(display-p3 0.2658 0.2719 0.29)` | `#d4d4d6` |
| `--lens-black-disabled` | `color(display-p3 0.1283 0.1312 0.14)` | `#ececed` |
| `--lens-white-hover` | `color(display-p3 0.98 0.98 0.98)` | `#242527` |
| `--lens-green-ghost` | `color(display-p3 0.1734 0.7612 0.338)` | `color(display-p3 0.1734 0.7612 0.338)` |
| `--lens-green-ghost-bg` | `color(display-p3 0.1725 0.7608 0.3373 / 0.1)` | `color(display-p3 0.1725 0.7608 0.3373 / 0.1)` |
| `--lens-red-ghost` | `color(display-p3 1 0.2314 0.1882)` | `color(display-p3 1 0.2314 0.1882)` |
| `--lens-red-ghost-bg` | `color(display-p3 1 0.2314 0.1882 / 0.1)` | `color(display-p3 1 0.2314 0.1882 / 0.1)` |
| `--lens-ring-control` | `0 1px 2px #00000005, 0 0 #2c2d3005, 0 0 0 1px var(--lens-hairline)` | `0 1px 2px #00000026, 0 0 #0000, 0 0 0 1px var(--lens-hairline)` |
| `--lens-ring-control-focus` | `0 0 0 4px #2c2d301a, 0 0 0 1px #2c2d3033, 0 0 0 1px var(--lens-hairline)` | `0 0 0 4px #ececed1f, 0 0 0 1px #ececed38, 0 0 0 1px var(--lens-hairline)` |
| `--lens-tracking-12` | `-0.09px` | `-0.09px` |
| `--lens-tracking-13` | `-0.00315856em` | `-0.00315856em` |
| `--duration-short` | `0.2s` | `0.2s` |
| `--duration-spinner` | `1s` | `1s` |
| `--ease-spring-content` | `linear(0, 0.0275, 0.0934, 0.179, 0.272, 0.3647, 0.4527, 0.5332, 0.6053, 0.6685, 0.7233, 0.7702, 0.8101, 0.8436, 0.8717, 0.895, 0.9144, 0.9303, 0.9435, 0.9542, 0.963, 0.9701, 0.9759, 0.9806, 0.9844, 0.9875, 0.99, 0.992, 0.9936, 0.9949, 0.9959, 0.9967, 0.9974, 0.9979, 0.9984, 0.9987, 0.999, 0.9992, 0.9993, 0.9995, 1)` | `linear(0, 0.0275, 0.0934, 0.179, 0.272, 0.3647, 0.4527, 0.5332, 0.6053, 0.6685, 0.7233, 0.7702, 0.8101, 0.8436, 0.8717, 0.895, 0.9144, 0.9303, 0.9435, 0.9542, 0.963, 0.9701, 0.9759, 0.9806, 0.9844, 0.9875, 0.99, 0.992, 0.9936, 0.9949, 0.9959, 0.9967, 0.9974, 0.9979, 0.9984, 0.9987, 0.999, 0.9992, 0.9993, 0.9995, 1)` |
| `--duration-spring-content` | `390ms` | `390ms` |

## CSS

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

```css
@keyframes lens-button-spinner-enter {
  from {
    opacity: 0;
    scale: 0.75;
  }
}

[data-style="lens"] {
  .ui-button {
    position: relative;
    z-index: 1;
    box-sizing: content-box;
    display: inline-flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: 32px;
    margin: 0;
    padding: 0 12px;
    border: 0;
    border-radius: 8px;
    outline: 3px solid transparent;
    outline-offset: 0;
    background: var(--lens-white);
    box-shadow: var(--lens-ring-control);
    color: var(--lens-text-primary);
    font-family: inherit;
    font-size: 13px;
    font-weight: 450;
    line-height: 20px;
    letter-spacing: var(--lens-tracking-13);
    white-space: nowrap;
    user-select: none;
    cursor: pointer;
    transition: all var(--duration-short);

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

    &:disabled,
    &[data-disabled] {
      cursor: not-allowed;
    }
  }

  .ui-button:is([data-variant="white"], [data-variant="secondary"]) {
    @media (hover: hover) {
      &:hover:not(:disabled, [data-disabled]) {
        background: var(--lens-white-hover);
      }
    }

    &:focus-visible {
      background: var(--lens-white);
      box-shadow: var(--lens-ring-control-focus);
    }

    &:is(:disabled, [data-disabled]) {
      opacity: 0.5;
    }
  }

  .ui-button:is([data-variant="green"], [data-variant="primary"]) {
    background: var(--lens-green);
    box-shadow: none;
    color: var(--lens-white);

    & svg {
      fill: currentColor;
    }

    @media (hover: hover) {
      &:hover:not(:disabled, [data-disabled]) {
        filter: saturate(0.5);
      }
    }

    &:focus-visible {
      outline-color: var(--lens-blackberry-black-a3);
      outline-offset: 2px;
    }

    &:is(:disabled, [data-disabled]) {
      opacity: 0.5;
    }
  }

  .ui-button[data-variant="black"] {
    background: var(--lens-blackberry-black);
    box-shadow: none;
    color: var(--lens-white);

    & svg {
      fill: currentColor;
    }

    @media (hover: hover) {
      &:hover:not(:disabled, [data-disabled]) {
        background: var(--lens-black-hover);
      }
    }

    &:focus-visible {
      outline: 3px solid var(--lens-blackberry-black-a3);
      outline-offset: 2px;
    }

    &:is(:disabled, [data-disabled]) {
      opacity: 0.3;
      background: var(--lens-black-disabled);
    }
  }

  .ui-button[data-variant="ghost"] {
    padding: 0;
    background: none;
    box-shadow: none;
    color: var(--lens-blackberry-black-a3);
    font-weight: 400;

    @media (hover: hover) {
      &:hover:not(:disabled, [data-disabled]) {
        color: var(--lens-blackberry-black);
      }
    }

    &:focus-visible {
      color: var(--lens-blackberry-black);
    }

    &:is(:disabled, [data-disabled]) {
      opacity: 0.5;
    }
  }

  .ui-button[data-variant="black-ghost"] {
    border-radius: 6px;
    background: var(--lens-blackberry-black-a03);
    box-shadow: none;
    color: var(--lens-text-secondary);

    @media (hover: hover) {
      &:hover:not(:disabled, [data-disabled]) {
        background: var(--lens-white-hover);
      }
    }

    &:focus-visible {
      outline-color: var(--lens-blackberry-black-a3);
      outline-offset: 2px;
    }
  }

  .ui-button[data-variant="light"] {
    height: auto;
    padding: 4px 8px;
    border-radius: 6px;
    background: var(--lens-blackberry-black-a03);
    box-shadow: none;
    color: var(--lens-text-secondary);
    font-size: 12px;
    font-weight: 450;
    line-height: 18px;
    letter-spacing: var(--lens-tracking-12);

    @media (hover: hover) {
      &:hover:not(:disabled, [data-disabled]) {
        background: var(--lens-white-hover);
      }
    }

    &:focus-visible {
      outline-color: var(--lens-blackberry-black-a3);
      outline-offset: 2px;
    }
  }

  .ui-button:is([data-variant="red"], [data-variant="danger"]) {
    background: var(--lens-red);
    box-shadow: none;
    color: var(--lens-white);

    & svg {
      fill: currentColor;
    }

    @media (hover: hover) {
      &:hover:not(:disabled, [data-disabled]) {
        filter: saturate(0.8);
      }
    }

    &:focus-visible {
      outline-color: var(--lens-red-a5);
      outline-offset: 2px;
    }

    &:is(:disabled, [data-disabled]) {
      opacity: 0.5;
    }
  }

  .ui-button[data-variant="red-outline"] {
    color: var(--lens-text-menu-destructive);

    @media (hover: hover) {
      &:hover:not(:disabled, [data-disabled]) {
        background: var(--lens-white-hover);
      }
    }

    &:focus-visible {
      background: var(--lens-white);
      box-shadow: var(--lens-ring-control-focus);
    }

    &:is(:disabled, [data-disabled]) {
      opacity: 0.5;
    }
  }

  .ui-button:is([data-variant="red-ghost"], [data-variant="green-ghost"]) {
    box-shadow: none;

    @media (hover: hover) {
      &:hover:not(:disabled, [data-disabled]) {
        opacity: 0.5;
      }
    }

    &:is(:disabled, [data-disabled]) {
      opacity: 0.5;
    }
  }

  .ui-button[data-variant="red-ghost"] {
    background: var(--lens-red-ghost-bg);
    color: var(--lens-red-ghost);
  }

  .ui-button[data-variant="green-ghost"] {
    background: var(--lens-green-ghost-bg);
    color: var(--lens-green-ghost);
  }

  .ui-button[data-size="small"] {
    gap: 6px;
    height: 24px;
    padding: 0 8px;
    border-radius: 6px;
    font-size: 12px;
    line-height: 18px;
    letter-spacing: var(--lens-tracking-12);

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

  .ui-button[data-size="large"] {
    height: 36px;
    padding: 0 14px;
  }

  .ui-button[data-variant="ghost"]:is([data-size="small"], [data-size="large"]) {
    padding: 0;
  }

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

  .ui-button[data-loading] {
    opacity: 1;
    cursor: wait;
  }

  .ui-button-spinner {
    box-sizing: border-box;
    width: 16px;
    height: 16px;
    flex-shrink: 0;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    opacity: 0.6;
    animation:
      lens-button-spinner-enter var(--duration-spring-content) var(--ease-spring-content),
      lens-spinner-spin var(--duration-spinner) linear infinite;
  }

  @media (prefers-reduced-motion: reduce) {
    .ui-button-spinner {
      animation: lens-spinner-spin 2s 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 Lens style, as a skill: https://replicas.raphaelsalaja.com/skills/lens/SKILL.md