# Variable Font Hover

> Each letter's font variation axes — weight, slant, width — animate independently on hover with a configurable stagger, giving text a fluid, character-level morphing feel.

Docs: https://ui.cnippet.dev/motion/text-animations/variable-font-hover

```tsx
"use client";
import { VariableFontHover } from "@/registry/default/motion/variable-font-hover";

const navLinks = ["Products", "Solutions", "Pricing", "Company", "Blog"];

export default function VariableFontHoverNav() {
  return (
    <nav className="flex min-h-50 flex-wrap items-center justify-center gap-8 px-6">
      {navLinks.map((link) => (
        <VariableFontHover
          className="cursor-pointer text-base text-muted-foreground transition-colors hover:text-foreground"
          fromFontVariationSettings="'wght' 400"
          key={link}
          label={link}
          staggerDuration={0.03}
          staggerFrom="center"
          toFontVariationSettings="'wght' 700"
        />
      ))}
    </nav>
  );
}
```

## Installation

**CLI**

```bash
npx shadcn@latest add @cnippet/variable-font-hover
```

**Manual**

1. Install the following dependencies:

```bash
npm install motion
```

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

`components/motion/variable-font-hover.tsx`

```tsx
"use client";

import {
  type AnimationOptions,
  motion,
  stagger,
  useAnimate,
  useReducedMotion,
} from "motion/react";
import { useCallback, useState } from "react";
import { cn } from "@/lib/utils";

export type VariableFontHoverProps = {
  label: string;
  fromFontVariationSettings: string;
  toFontVariationSettings: string;
  transition?: AnimationOptions;
  staggerDuration?: number;
  staggerFrom?: "first" | "last" | "center" | number;
  className?: string;
  onClick?: () => void;
};

export function VariableFontHover({
  label,
  fromFontVariationSettings = "'wght' 400, 'slnt' 0",
  toFontVariationSettings = "'wght' 900, 'slnt' -10",
  transition = { duration: 0.7, type: "spring" },
  staggerDuration = 0.03,
  staggerFrom = "first",
  className,
  onClick,
  ...props
}: VariableFontHoverProps) {
  const reduceMotion = useReducedMotion();
  const [scope, animate] = useAnimate();
  const [isHovered, setIsHovered] = useState(false);

  const mergeTransition = useCallback(
    (base: AnimationOptions): AnimationOptions =>
      reduceMotion
        ? { duration: 0 }
        : { ...base, delay: stagger(staggerDuration, { from: staggerFrom }) },
    [staggerDuration, staggerFrom, reduceMotion],
  );

  const hoverStart = useCallback(() => {
    if (isHovered) return;
    setIsHovered(true);
    animate(
      ".letter",
      { fontVariationSettings: toFontVariationSettings },
      mergeTransition(transition),
    );
  }, [
    isHovered,
    animate,
    toFontVariationSettings,
    transition,
    mergeTransition,
  ]);

  const hoverEnd = useCallback(() => {
    setIsHovered(false);
    animate(
      ".letter",
      { fontVariationSettings: fromFontVariationSettings },
      mergeTransition(transition),
    );
  }, [animate, fromFontVariationSettings, transition, mergeTransition]);

  return (
    <motion.span
      aria-label={label}
      className={cn(className)}
      onClick={onClick}
      onHoverEnd={hoverEnd}
      onHoverStart={hoverStart}
      ref={scope}
      {...props}
    >
      <span className="sr-only">{label}</span>
      {label.split("").map((letter, i) => (
        <motion.span
          aria-hidden="true"
          className="letter inline-block whitespace-pre"
          key={i}
        >
          {letter}
        </motion.span>
      ))}
    </motion.span>
  );
}
```

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

## Usage

```tsx
import { VariableFontHover } from "@/components/motion/variable-font-hover";
```

```tsx
<VariableFontHover
  label="Get started"
  fromFontVariationSettings="'wght' 400"
  toFontVariationSettings="'wght' 800"
/>
```

> **Variable font required.** This component animates `font-variation-settings` — your project must load a variable font that supports the axes you specify (e.g. Inter Variable for `wght` and `slnt`).

## Anatomy

```tsx
<VariableFontHover label="Get started" fromFontVariationSettings="'wght' 400" toFontVariationSettings="'wght' 800" />
```

## Good to know

- The text goes in the `label` prop, not in `children`.
- It needs a variable font that supports the axes you animate. With a normal font nothing changes.
- The effect plays on hover, so there's nothing to see on touch screens.
- Screen readers get the text through `aria-label`; the animated letters are hidden from them.
- When the user has turned on reduced motion, hovering switches the font settings instantly, with no stagger.

## Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `label` | `string` | — | The text to display and animate |
| `fromFontVariationSettings` | `string` | — | Font variation settings at rest (e.g. `"'wght' 400"`) |
| `toFontVariationSettings` | `string` | — | Font variation settings on hover (e.g. `"'wght' 800"`) |
| `staggerDuration` | `number` | `0.03` | Seconds between each letter's transition |
| `staggerFrom` | `"first" \| "last" \| "center" \| number` | `"first"` | Where the stagger wave starts |
| `transition` | `AnimationOptions` | `{ type: "spring", duration: 0.7 }` | Motion transition applied to each letter |
| `className` | `string` | — | Class on the outer `<motion.span>` |
| `onClick` | `() => void` | — | Click handler |

## Examples

### Navigation Links

Nav links bold from the center outward on hover. `staggerFrom="center"` creates a symmetric wave that feels more natural for short words.

```tsx
"use client";
import { VariableFontHover } from "@/registry/default/motion/variable-font-hover";

const navLinks = ["Products", "Solutions", "Pricing", "Company", "Blog"];

export default function VariableFontHoverNav() {
  return (
    <nav className="flex min-h-50 flex-wrap items-center justify-center gap-8 px-6">
      {navLinks.map((link) => (
        <VariableFontHover
          className="cursor-pointer text-base text-muted-foreground transition-colors hover:text-foreground"
          fromFontVariationSettings="'wght' 400"
          key={link}
          label={link}
          staggerDuration={0.03}
          staggerFrom="center"
          toFontVariationSettings="'wght' 700"
        />
      ))}
    </nav>
  );
}
```

### Hero Headline

Two headline lines with inverted settings — the first goes thin-to-bold while the second goes bold-to-thin, creating contrast as you hover between them.

```tsx
"use client";
import { VariableFontHover } from "@/registry/default/motion/variable-font-hover";

export default function VariableFontHoverHero() {
  return (
    <div className="flex min-h-50 flex-col items-center justify-center gap-2 px-6 text-center">
      <p className="text-muted-foreground text-sm">Hover the headline</p>
      <VariableFontHover
        className="cursor-default text-5xl tracking-tight"
        fromFontVariationSettings="'wght' 100, 'slnt' 0"
        label="Build faster."
        staggerDuration={0.02}
        staggerFrom="first"
        toFontVariationSettings="'wght' 900, 'slnt' -10"
        transition={{ duration: 0.5, type: "spring" }}
      />
      <VariableFontHover
        className="cursor-default text-5xl text-muted-foreground tracking-tight"
        fromFontVariationSettings="'wght' 900, 'slnt' -10"
        label="Ship with confidence."
        staggerDuration={0.02}
        staggerFrom="last"
        toFontVariationSettings="'wght' 100, 'slnt' 0"
        transition={{ duration: 0.5, type: "spring" }}
      />
    </div>
  );
}
```

### CTA Links

Three call-to-action links each use a different `staggerFrom` origin — `first`, `center`, and `last` — so each line feels distinct despite sharing the same component.

```tsx
"use client";
import { VariableFontHover } from "@/registry/default/motion/variable-font-hover";

const items = [
  {
    from: "'wght' 400",
    from2: "first" as const,
    label: "Get started free",
    to: "'wght' 800",
  },
  {
    from: "'wght' 300",
    from2: "center" as const,
    label: "View documentation",
    to: "'wght' 700",
  },
  {
    from: "'wght' 400",
    from2: "last" as const,
    label: "See examples",
    to: "'wght' 900",
  },
];

export default function VariableFontHoverCTA() {
  return (
    <div className="flex min-h-50 flex-col items-center justify-center gap-5 px-6">
      {items.map((item) => (
        <VariableFontHover
          className="cursor-pointer text-2xl text-foreground"
          fromFontVariationSettings={item.from}
          key={item.label}
          label={item.label}
          staggerDuration={0.02}
          staggerFrom={item.from2}
          toFontVariationSettings={item.to}
        />
      ))}
    </div>
  );
}
```
