How to build a zipper reveal animation in React

Build a draggable zipper that unzips to reveal content in React with Motion: one motion value, two clip-paths, a spring that decides open or shut, and a keyboard path.

6 min read

The real component. Click the picture to open it live and try it before you read how it was built.

A zipper is a good reveal because everyone already knows how it works. You grab the pull, drag it along, and what is underneath appears behind it. Here is how our zipper announcement bar does it with very little code.

One number drives everything

The pull's horizontal position is a single motion value, x. Every other moving part reads from it: the closed zipper is clipped to everything right of the pull, the open message to everything left of it, and the hint fades as the pull leaves the start. Nothing re-renders while you drag, because motion values update styles directly.

src/registry/announcement-bars/zipper/announcement-zipper.tsx

"use client";

import { useRef, useState } from "react";
import Link from "next/link";
import { animate, motion, useMotionValue, useMotionTemplate, useTransform } from "motion/react";
import { useElementSize } from "@/hooks/use-element-size";
import { usePrefersReducedMotion } from "@/hooks/use-prefers-reduced-motion";
import { cn } from "@/lib/utils";
import { ZipperTeeth } from "./zipper-teeth";

// How far along the bar the pull must travel, as a share, to stay open when let go.
const OPEN_AT = 0.6;
const PULL_WIDTH = 44;
const SPRING = { type: "spring", stiffness: 300, damping: 30 } as const;

export interface AnnouncementZipperProps {
  // Written on the closed zipper's pull side: "Unzip for the winter sale".
  hint: string;
  // What the open bar says, and its link.
  message: string;
  action: { label: string; href: string };
  className?: string;
}

// An announcement you unzip. The bar is a closed zipper; drag its pull along and the teeth part behind it, opening
// onto the message underneath. Let go past the middle and it runs open to the end; before that it springs shut. The
// pull is also a button, so a keyboard opens it in one press, and with reduced motion it simply opens.
export function AnnouncementZipper({ hint, message, action, className }: AnnouncementZipperProps) {
  const trackRef = useRef<HTMLDivElement>(null);
  const { width } = useElementSize(trackRef);
  const travel = Math.max(width - PULL_WIDTH, 1);
  const x = useMotionValue(0);
  const [isOpen, setIsOpen] = useState(false);
  // A drag ends with a click on release; this keeps that click from toggling the zipper a second time.
  const wasDragged = useRef(false);
  const isReduced = usePrefersReducedMotion();
  // The closed part of the zipper is everything to the right of the pull.
  const opened = useTransform(x, (value) => value + PULL_WIDTH / 2);
  const closedClip = useMotionTemplate`inset(0 0 0 ${opened}px)`;
  const openClip = useMotionTemplate`inset(0 calc(100% - ${opened}px) 0 0)`;
  const hintOpacity = useTransform(x, [0, travel * 0.3], [1, 0]);

  function settle(open: boolean) {
    setIsOpen(open);
    if (isReduced) x.set(open ? travel : 0);
    else animate(x, open ? travel : 0, SPRING);
  }

  return (
    // Tall enough that the open message and its button clear the rows of teeth above and below them.
    <div className={cn("relative h-14 overflow-hidden bg-neutral-950 text-white", className)}>
      <motion.div
        style={{ clipPath: openClip }}
        className="absolute inset-0 flex items-center justify-center gap-3 px-14 text-sm"
      >
        <p className="truncate">{message}</p>
        <Link
          href={action.href}
          tabIndex={isOpen ? 0 : -1}
          className="shrink-0 rounded-md bg-white px-2.5 py-1 text-xs font-semibold text-neutral-950 outline-none hover:bg-amber-200 focus-visible:ring-2 focus-visible:ring-white"
        >
          {action.label}
        </Link>
      </motion.div>
      <motion.div aria-hidden style={{ clipPath: openClip }} className="pointer-events-none absolute inset-x-0 top-0">
        <ZipperTeeth side="top" />
      </motion.div>
      <motion.div
        aria-hidden
        style={{ clipPath: openClip }}
        className="pointer-events-none absolute inset-x-0 bottom-0"
      >
        <ZipperTeeth side="bottom" />
      </motion.div>
      <motion.div
        aria-hidden
        style={{ clipPath: closedClip }}
        className="absolute inset-0 flex flex-col justify-center bg-neutral-800"
      >
        <ZipperTeeth side="top" />
        <ZipperTeeth side="bottom" />
        <motion.p
          style={{ opacity: hintOpacity }}
          className="absolute inset-0 flex items-center justify-center text-xs font-medium tracking-wide text-neutral-200"
        >
          {/* A label sewn over the closed teeth: on the bare teeth the words would be lost in their pattern. */}
          <span className="rounded-sm bg-neutral-900 px-2.5 py-1 shadow-sm ring-1 ring-white/10">{hint}</span>
        </motion.p>
      </motion.div>
      <div ref={trackRef} className="absolute inset-y-0 right-2 left-2">
        <motion.button
          type="button"
          aria-label={isOpen ? "Close the announcement" : hint}
          aria-expanded={isOpen}
          drag="x"
          dragConstraints={{ left: 0, right: travel }}
          dragElastic={0.04}
          dragMomentum={false}
          onDragStart={() => (wasDragged.current = true)}
          onDragEnd={() => settle(x.get() > travel * OPEN_AT)}
          onClick={() => {
            if (wasDragged.current) {
              wasDragged.current = false;
              return;
            }
            settle(!isOpen);
          }}
          style={{ x, width: PULL_WIDTH }}
          whileTap={{ scale: 1.06 }}
          className="absolute top-1/2 -mt-4 flex h-8 cursor-grab touch-none items-center justify-center rounded-md bg-linear-to-b from-neutral-200 to-neutral-400 shadow-md ring-1 shadow-black/40 ring-white/40 outline-none focus-visible:ring-2 focus-visible:ring-amber-300 active:cursor-grabbing"
        >
          <span aria-hidden className="h-3.5 w-5 rounded-full border-2 border-neutral-600" />
        </motion.button>
      </div>
    </div>
  );
}
The bar: x drives two clip-path templates and the hint's opacity

Clip-path, not width

Animating width or left makes the browser lay the page out again on every frame, and text reflows. An inset clip-path only changes what is painted, so it stays smooth on phones. useMotionTemplate turns the number into a CSS string without React in the loop.

Let go, and decide

On release the bar asks one question: is the pull past 60 percent? If yes it springs to the end and stays open; if not it springs back shut. A spring with stiffness 300 and damping 30 feels like metal running on teeth without bouncing.

The teeth

The teeth are plain spans with a small gradient, two rows offset by half a tooth so they interlock. They are chunky on purpose: fine repeating lines shimmer on screens.

src/registry/announcement-bars/zipper/zipper-teeth.tsx

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

const TEETH = 64;

// One row of zipper teeth: chunky metal blocks with a light top edge, drawn as plain spans so they stay crisp.
// `side` decides whether the blocks hang from the top or stand on the bottom, so two rows interlock.
export function ZipperTeeth({ side }: { side: "top" | "bottom" }) {
  return (
    <div aria-hidden className={cn("flex h-2.5 w-full justify-between", side === "top" ? "items-start" : "items-end")}>
      {Array.from({ length: TEETH }, (_, index) => (
        <span
          key={index}
          className={cn(
            "h-2.5 w-1.5 rounded-xs bg-linear-to-b from-neutral-300 to-neutral-500",
            side === "bottom" && "translate-x-1",
          )}
        />
      ))}
    </div>
  );
}
Two interlocking rows of teeth

Keyboard and reduced motion

  • The pull is a real button: Enter or Space opens and closes it, and aria-expanded tells screen readers which.
  • A drag ends with a click event; a ref remembers the drag so that click does not toggle a second time.
  • With reduced motion the zipper jumps open instead of springing.

More articles