Getting started

Add Utilities

Install the small helpers that keep component classes predictable and make motion easy to add.

On This Page

Install dependencies

Install clsx for composing conditional class names, tailwind-merge for resolving conflicting Tailwind classes, and motion for animation.

npm install clsx tailwind-merge motion

Compose classes with clsx

clsx combines class names and supports conditional values. It keeps component variants readable when a class should only apply in a certain state.

Example

clsx("button", isActive && "button-active")

Resolve conflicts with tailwind-merge

tailwind-merge removes conflicting Tailwind utilities. When a caller passes a new spacing or color class, the later value takes effect instead of leaving both classes on the element.

Example

twMerge("px-3 py-2", "px-6")  // px-6 py-2

JavaScript: lib/utils.js

Export a cn helper from your shared utilities file:

lib/utils.js

import { clsx } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs) {
  return twMerge(clsx(inputs));
}

TypeScript: lib/utils.ts

If your project uses TypeScript, type the helper inputs with ClassValue:

lib/utils.ts

import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

Use cn in a component

Use cn to combine a component’s default classes with classes passed in by the person using it. Tailwind conflicts resolve in favor of the later class.

components/ui/Panel.jsx

import { cn } from "@/lib/utils";

export function Panel({ className }) {
  return (
    <section className={cn("rounded-xl border p-4", className)}>
      Your content
    </section>
  );
}

Install and use Motion

Motion components use the motion/react entry point. In the App Router, put "use client" at the top of files that use interactive Motion components.

components/AnimatedPanel.jsx

"use client";

import { motion } from "motion/react";

export function AnimatedPanel() {
  return (
    <motion.div
      initial={{ opacity: 0, y: 8 }}
      animate={{ opacity: 1, y: 0 }}
      transition={{ duration: 0.2 }}
    >
      Ready to animate
    </motion.div>
  );
}

See Motion’s React installation guide for more setup details.