# Text Gradient

> An animated gradient flows continuously through text using background-clip. Fully customisable colour stops, angle, and speed.

Docs: https://ui.cnippet.dev/motion/text-animations/text-gradient

```tsx
"use client";

import { TextGradient } from "@/registry/default/motion/text-gradient";

export default function TextGradientHero() {
  return (
    <div className="flex min-h-50 flex-col items-center justify-center gap-3 px-6">
      <TextGradient
        as="h1"
        className="font-bold text-4xl tracking-tight sm:text-5xl"
        colors={["#a855f7", "#6366f1", "#3b82f6", "#06b6d4", "#a855f7"]}
        duration={5}
      >
        Build with motion
      </TextGradient>
      <p className="text-muted-foreground text-sm">
        Looping violet → indigo → blue gradient
      </p>
    </div>
  );
}
```

## Installation

**CLI**

```bash
npx shadcn@latest add @cnippet/text-gradient
```

**Manual**

1. Install the following dependencies:

```bash
npm install motion
```

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

`components/motion/text-gradient.tsx`

```tsx
"use client";

import { motion, type Transition, useReducedMotion } from "motion/react";
import type { JSX } from "react";
import { cn } from "@/lib/utils";

export type TextGradientProps = {
  children: string;
  as?: React.ElementType;
  className?: string;
  colors?: string[];
  duration?: number;
  angle?: number;
  transition?: Transition;
};

export function TextGradient({
  children,
  as: Component = "p",
  className,
  colors = ["#ff6b6b", "#ffd93d", "#6bcb77", "#4d96ff", "#c77dff"],
  duration = 4,
  angle = 135,
  transition,
}: TextGradientProps) {
  const MotionComponent = motion.create(
    Component as keyof JSX.IntrinsicElements,
  );
  const reduceMotion = useReducedMotion();
  const gradientSize = colors.length * 100;

  return (
    <MotionComponent
      animate={{ backgroundPosition: ["0% 50%", "100% 50%", "0% 50%"] }}
      className={cn("inline-block bg-clip-text text-transparent", className)}
      style={{
        backgroundImage: `linear-gradient(${angle}deg, ${[...colors, ...colors].join(", ")})`,
        backgroundSize: `${gradientSize}% 100%`,
      }}
      transition={
        reduceMotion
          ? { duration: 0 }
          : {
              duration,
              ease: "linear",
              repeat: Number.POSITIVE_INFINITY,
              ...transition,
            }
      }
    >
      {children}
    </MotionComponent>
  );
}
```

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

## Usage

```tsx
import { TextGradient } from "@/components/motion/text-gradient";
```

```tsx
<TextGradient colors={["#a855f7", "#3b82f6", "#06b6d4", "#a855f7"]}>
  Build with motion
</TextGradient>
```

## Anatomy

```tsx
<TextGradient colors={["#a855f7", "#3b82f6", "#a855f7"]}>Build with motion</TextGradient> {/* <p> with a moving gradient fill */}
```

## Good to know

- `children` must be a plain string.
- Repeat the first color at the end of `colors` for a seamless loop.
- `angle` sets the gradient direction in degrees. `duration` is the seconds per loop.
- Check contrast: some parts of the gradient may be hard to read on your background.
- When the user has turned on reduced motion, the gradient stays still.

## Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `children` | `string` | — | Text to apply the gradient to |
| `as` | `React.ElementType` | `"p"` | HTML element rendered as the container |
| `colors` | `string[]` | `["#ff6b6b", "#ffd93d", "#6bcb77", "#4d96ff", "#c77dff"]` | Array of colour stops for the gradient |
| `duration` | `number` | `4` | Seconds for one full gradient cycle |
| `angle` | `number` | `135` | Gradient angle in degrees |
| `transition` | `Transition` | — | Override the Motion transition |
| `className` | `string` | — | Additional classes for font size, weight, etc. |

## Examples

### Hero Heading

A violet-to-cyan gradient sweeps across a large heading. Supply colours that repeat at start and end to ensure a seamless loop.

```tsx
"use client";

import { TextGradient } from "@/registry/default/motion/text-gradient";

export default function TextGradientHero() {
  return (
    <div className="flex min-h-50 flex-col items-center justify-center gap-3 px-6">
      <TextGradient
        as="h1"
        className="font-bold text-4xl tracking-tight sm:text-5xl"
        colors={["#a855f7", "#6366f1", "#3b82f6", "#06b6d4", "#a855f7"]}
        duration={5}
      >
        Build with motion
      </TextGradient>
      <p className="text-muted-foreground text-sm">
        Looping violet → indigo → blue gradient
      </p>
    </div>
  );
}
```

### Inline Word Accent

A single word inside a static heading uses `TextGradient` as a `<span>`, warming the key term with a fast orange-to-green sweep.

```tsx
"use client";

import { TextGradient } from "@/registry/default/motion/text-gradient";

export default function TextGradientInline() {
  return (
    <div className="flex min-h-50 items-center justify-center px-6">
      <h2 className="max-w-xl text-center font-bold text-3xl text-foreground tracking-tight sm:text-4xl">
        The fastest way to ship{" "}
        <TextGradient
          as="span"
          colors={["#f97316", "#eab308", "#22c55e", "#f97316"]}
          duration={3}
        >
          animated
        </TextGradient>{" "}
        interfaces
      </h2>
    </div>
  );
}
```

### Pricing Cards

Each pricing tier gets its own gradient palette at a slightly different speed, making the prices feel distinct without extra markup.

```tsx
"use client";

import { TextGradient } from "@/registry/default/motion/text-gradient";

const tiers = [
  {
    colors: ["#a855f7", "#ec4899", "#a855f7"],
    duration: 4,
    label: "Pro",
    price: "$12",
  },
  {
    colors: ["#3b82f6", "#06b6d4", "#3b82f6"],
    duration: 5,
    label: "Team",
    price: "$49",
  },
  {
    colors: ["#f97316", "#eab308", "#f97316"],
    duration: 6,
    label: "Enterprise",
    price: "$199",
  },
];

export default function TextGradientPricing() {
  return (
    <div className="flex min-h-50 items-center justify-center px-6">
      <div className="flex gap-6">
        {tiers.map((tier) => (
          <div
            className="flex flex-col items-center gap-1 rounded-xl border border-border bg-card p-5 text-center"
            key={tier.label}
          >
            <TextGradient
              as="p"
              className="font-bold text-3xl"
              colors={tier.colors}
              duration={tier.duration}
            >
              {tier.price}
            </TextGradient>
            <p className="text-muted-foreground text-xs">{tier.label}</p>
          </div>
        ))}
      </div>
    </div>
  );
}
```
