# Morph

> Tactile corner-radius morphing, spring curves, and shared sizing tokens for interactive controls.

Canonical URL: https://taut-ui.com/docs/lib/morph

A CVA utility module that powers shape morphing, press-state corner contraction, and consistent sizing tokens across interactive controls like `Button`.

## Installation [#installation]

**CLI**

```bash
npx shadcn@latest add https://taut-ui.com/r/morph.json
```

**Manual**

- Install the following dependencies:

```bash
npm install class-variance-authority
```

- Copy and paste the following code into your project.

```tsx title="lib/morph.tsx"
import { cva } from "class-variance-authority";

export type MorphShape = "round" | "square";

export const morphClasses = cva(
  [
    "[font-variation-settings:'GRAD'_0]",
    "[--taut-spring:linear(0,0.08_6%,0.25_14%,0.55_26%,0.82_40%,0.98_55%,1.015_68%,1.008_80%,1)]",
    "[transition:border-radius_250ms_var(--taut-spring),box-shadow_150ms_cubic-bezier(0.2,0,0,1)]",
    "motion-reduce:transition-none",
  ],
  {
    defaultVariants: { press: true, radius: "full" },
    variants: {
      press: {
        false: "",
        true: "active:rounded-(--r-pressed) active:[font-variation-settings:'GRAD'_-50] active:[transition-duration:120ms,100ms]",
      },
      radius: {
        full: "rounded-(--r-full)",
        square: "rounded-(--r-square)",
      },
    },
  },
);

export const morphRadius = (
  shape: MorphShape,
  selected?: boolean,
  enabled = true,
): "full" | "square" =>
  (shape === "round") !== (enabled && selected === true) ? "full" : "square";

export const tautSize = cva("", {
  defaultVariants: { size: "default" },
  variants: {
    size: {
      xs: "h-8 gap-1.5 px-3 text-xs [--icon:1rem] [--r-full:1rem] [--r-pressed:0.375rem] [--r-square:0.5rem]",
      sm: "h-9 gap-2 px-3.5 text-sm [--icon:1rem] [--r-full:1.125rem] [--r-pressed:0.375rem] [--r-square:0.625rem]",
      default:
        "h-10 gap-2 px-4 text-sm [--icon:1.125rem] [--r-full:1.25rem] [--r-pressed:0.5rem] [--r-square:0.75rem]",
      md: "h-10 gap-2 px-4 text-sm [--icon:1.125rem] [--r-full:1.25rem] [--r-pressed:0.5rem] [--r-square:0.75rem]",
      lg: "h-12 gap-2.5 px-5 text-base [--icon:1.25rem] [--r-full:1.5rem] [--r-pressed:0.625rem] [--r-square:0.875rem]",
      xl: "h-14 gap-3 px-6 text-base [--icon:1.5rem] [--r-full:1.75rem] [--r-pressed:0.75rem] [--r-square:1rem]",
      "icon-xs":
        "h-8 w-(--iw) px-0 [--icon:1rem] [--iw-narrow:1.75rem] [--iw-wide:2.5rem] [--iw:2rem] [--r-full:1rem] [--r-pressed:0.375rem] [--r-square:0.5rem]",
      "icon-sm":
        "h-9 w-(--iw) px-0 [--icon:1rem] [--iw-narrow:2rem] [--iw-wide:2.75rem] [--iw:2.25rem] [--r-full:1.125rem] [--r-pressed:0.375rem] [--r-square:0.625rem]",
      icon: "h-10 w-(--iw) px-0 [--icon:1.125rem] [--iw-narrow:2rem] [--iw-wide:3.25rem] [--iw:2.5rem] [--r-full:1.25rem] [--r-pressed:0.5rem] [--r-square:0.75rem]",
      "icon-md":
        "h-10 w-(--iw) px-0 [--icon:1.125rem] [--iw-narrow:2rem] [--iw-wide:3.25rem] [--iw:2.5rem] [--r-full:1.25rem] [--r-pressed:0.5rem] [--r-square:0.75rem]",
      "icon-lg":
        "h-12 w-(--iw) px-0 [--icon:1.25rem] [--iw-narrow:2.5rem] [--iw-wide:4rem] [--iw:3rem] [--r-full:1.5rem] [--r-pressed:0.625rem] [--r-square:0.875rem]",
      "icon-xl":
        "h-14 w-(--iw) px-0 [--icon:1.5rem] [--iw-narrow:3rem] [--iw-wide:5rem] [--iw:3.5rem] [--r-full:1.75rem] [--r-pressed:0.75rem] [--r-square:1rem]",
    },
    width: {
      narrow: "w-(--iw-narrow)",
      wide: "w-(--iw-wide)",
    },
  },
});
```

- Update the import paths to match your project setup.

## How It Works [#how-it-works]

`morph.tsx` pairs three building blocks that work together via CSS custom properties (`--r-full`, `--r-square`, `--r-pressed`, `--icon`, `--iw`):

1. **`tautSize`** sets the height, padding, icon size, and all three radius tokens (`--r-full`, `--r-square`, `--r-pressed`) for each size preset (`xs` through `xl`, plus `icon-*` presets).
2. **`morphClasses`** binds an inline `linear()` spring curve (`--taut-spring`) to `border-radius` alongside a subtle `box-shadow` transition—intentionally omitting color/background transitions so theme switches stay instant. When `press` is enabled, holding the control morphs its corners to `--r-pressed` and subtly adjusts variable font grade (`GRAD -50`).
3. **`morphRadius(shape, selected, enabled)`** resolves whether a control should display `"full"` or `"square"` corners, inverting the resting `shape` when `selected` and `enabled` are both `true`.

All transitions collapse to `0ms` automatically under `prefers-reduced-motion: reduce`.

## Usage [#usage]

### Combining `morphClasses`, `tautSize`, and `morphRadius` [#combining-morphclasses-tautsize-and-morphradius]

```tsx
import {
  type MorphShape,
  morphClasses,
  morphRadius,
  tautSize,
} from "@/lib/morph";
import { cn } from "@/lib/utils";

interface PillActionProps extends React.ComponentProps<"button"> {
  shape?: MorphShape;
  morph?: boolean;
  selected?: boolean;
}

export function PillAction({
  className,
  shape = "round",
  morph = true,
  selected = false,
  ...props
}: PillActionProps) {
  return (
    <button
      className={cn(
        tautSize({ size: "sm" }),
        morphClasses({
          press: morph,
          radius: morphRadius(shape, selected, morph),
        }),
        className,
      )}
      {...props}
    />
  );
}
```

### Icon Width Presets [#icon-width-presets]

Icon sizes (`icon-xs`, `icon-sm`, `icon`, `icon-md`, `icon-lg`, `icon-xl`) support optional `width` variants (`narrow` and `wide`) that override `--iw`:

```tsx
<button
  className={cn(
    tautSize({ size: "icon-md", width: "wide" }),
    morphClasses({ radius: "full" }),
  )}
>
  <PlusIcon />
</button>
```

## API [#api]

### `morphClasses` [#morphclasses]

| Variant | Values | Default | Description |
|---------|--------|---------|-------------|
| `radius` | `"full" \| "square"` | `"full"` | Applies `--r-full` or `--r-square` border radius. |
| `press` | `boolean` | `true` | Morphs corners to `--r-pressed` and shifts `GRAD` to `-50` while active. |

### `morphRadius` [#morphradius]

```ts
function morphRadius(
  shape: MorphShape,
  selected?: boolean,
  enabled?: boolean,
): "full" | "square"
```

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `shape` | `"round" \| "square"` | — | Resting shape of the control. |
| `selected` | `boolean` | `undefined` | When `true` and `enabled` is `true`, inverts the resolved radius. |
| `enabled` | `boolean` | `true` | Enables selection-based shape inversion. |

### `tautSize` [#tautsize]

| Variant | Values | Default | Description |
|---------|--------|---------|-------------|
| `size` | `"xs" \| "sm" \| "default" \| "md" \| "lg" \| "xl" \| "icon-xs" \| "icon-sm" \| "icon" \| "icon-md" \| "icon-lg" \| "icon-xl"` | `"default"` | Sets control height, gap, padding, `--icon`, and radius variables. |
| `width` | `"narrow" \| "wide"` | — | Adjusts `--iw` width on `icon-*` size variants. |
