Skip to the content

Helpers

cn

Joins class names and resolves Tailwind conflicts, so a class passed in wins over a component's own.

Motion and transitions

When to use it

Every component uses it. Use it wherever classes are combined conditionally.

See it working

The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Motion and transitions page.

Reveal, stagger, text reveal

Words arrive one after another when they come into view.

1
2
3
4

Content already on screen when the page loads stays put; only what scrolls in is animated.

Number ticker and presence

128,450

Mounted and unmounted with a transition

Tilt, spotlight, magnetic

Tilt (move the pointer)
Spotlight

Shared element and view transition

1234

Scroll scene

A section that holds still

while the story moves with the scroll.

Then the next beat

Under reduced motion it is a plain stack.

Add it

npx shadcn@latest add @rivier-ui/cn

The registry is named once in components.json: see Installation.

Example

Import it from @/lib/cn.

cn("px-4 py-2", isActive && "bg-primary", className)

What it needs

Plain markup: it can stay a server component. Packages: clsx, tailwind-merge.

It is used by

Source

The whole file. Once copied it is yours: the registry item holds the same text.

src/lib/cn.ts
/**
 * @name cn
 * @category lib
 * @description Joins class names and resolves Tailwind conflicts, so a class passed in wins over a component's own.
 * @when Every component uses it. Use it wherever classes are combined conditionally.
 * @example
 * cn("px-4 py-2", isActive && "bg-primary", className)
 */
import { clsx, type ClassValue } from "clsx";
import { extendTailwindMerge } from "tailwind-merge";

// The theme adds utilities Tailwind does not have by default. Naming them here lets a class passed in
// ("h-12", "rounded-none", "duration-500") replace a component's own ("h-control", "rounded-control").
const merge = extendTailwindMerge({
  extend: {
    classGroups: {
      h: [{ h: ["control", "control-sm", "control-lg"] }],
      rounded: [{ rounded: ["control", "surface"] }],
      duration: [{ duration: ["instant", "base", "slow"] }],
    },
  },
});

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