Pull cord animation in React: a roller blind announcement

Build a pull cord that drops a roller blind announcement in React with Motion drag: an elastic bead, a stretching cord, a threshold and a spring-loaded blind.

5 min read

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

Most announcement bars take the top of every page whether you want them or not. A roller blind waits: pull the cord and it drops.

The bead

The bead is a motion button with drag on the y axis, constrained to 70 pixels and elastic. dragSnapToOrigin sends it back up on release, like a real cord on a spring.

The cord stretches with a transform

As you pull, the drag offset goes into a motion value, and the cord's scaleY follows it with its origin at the top. Scaling instead of changing height means no layout work on each frame.

src/registry/announcement-bars/pull-cord/announcement-pull-cord.tsx

"use client";

import { useRef, useState } from "react";
import Link from "next/link";
import { AnimatePresence, motion, useMotionValue, useTransform } from "motion/react";
import { usePrefersReducedMotion } from "@/hooks/use-prefers-reduced-motion";
import { cn } from "@/lib/utils";

// How far the bead must be pulled, in px, before letting go works the blind.
const PULL_AT = 36;
const MAX_PULL = 70;
const CORD = 28;

export interface AnnouncementPullCordProps {
  message: string;
  action: { label: string; href: string };
  // The bead's name for screen readers, closed and open.
  pullLabel: string;
  closeLabel: string;
  className?: string;
}

// An announcement on a roller blind. A cord with a bead hangs from the top of the page; pull the bead down and let go,
// and the blind drops with the message on it while the cord springs back. Pull again to roll it up. The bead is a
// button too, so a click or a key works it, and with reduced motion the blind just appears.
export function AnnouncementPullCord({ message, action, pullLabel, closeLabel, className }: AnnouncementPullCordProps) {
  const [isDown, setIsDown] = useState(false);
  const pull = useMotionValue(0);
  // The cord stretches with a transform as the bead is pulled, rather than by changing its height.
  const cordStretch = useTransform(pull, (value) => (CORD + value) / CORD);
  const isReduced = usePrefersReducedMotion();
  const wasDragged = useRef(false);

  return (
    <div className={cn("relative", className)}>
      <AnimatePresence initial={false}>
        {isDown ? (
          <motion.div
            key="blind"
            initial={isReduced ? { opacity: 0 } : { scaleY: 0 }}
            animate={{ scaleY: 1, opacity: 1 }}
            exit={isReduced ? { opacity: 0 } : { scaleY: 0 }}
            transition={{ type: "spring", stiffness: 260, damping: 22 }}
            style={{ originY: 0 }}
            className="overflow-hidden bg-amber-200 text-amber-950 shadow-md"
          >
            <div className="mx-auto flex min-h-12 max-w-6xl items-center justify-center gap-3 px-16 py-2 text-sm">
              <p className="font-medium">{message}</p>
              <Link
                href={action.href}
                className="shrink-0 rounded-md bg-amber-950 px-2.5 py-1 text-xs font-semibold text-amber-50 outline-none hover:bg-amber-800 focus-visible:ring-2 focus-visible:ring-amber-950 focus-visible:ring-offset-2 focus-visible:ring-offset-amber-200"
              >
                {action.label}
              </Link>
            </div>
            <span aria-hidden className="block h-1.5 bg-amber-300 shadow-inner" />
          </motion.div>
        ) : null}
      </AnimatePresence>
      <div className={cn("absolute right-6 flex flex-col items-center", isDown ? "top-full" : "top-0")}>
        <motion.span aria-hidden style={{ scaleY: cordStretch, originY: 0 }} className="h-7 w-px bg-neutral-400" />
        <motion.button
          type="button"
          aria-label={isDown ? closeLabel : pullLabel}
          aria-expanded={isDown}
          drag="y"
          dragConstraints={{ top: 0, bottom: MAX_PULL }}
          dragElastic={0.1}
          dragSnapToOrigin
          onDrag={(_, info) => pull.set(Math.max(info.offset.y, 0))}
          onDragStart={() => (wasDragged.current = true)}
          onDragEnd={(_, info) => {
            pull.set(0);
            if (info.offset.y > PULL_AT) setIsDown((value) => !value);
          }}
          onClick={() => {
            if (wasDragged.current) {
              wasDragged.current = false;
              return;
            }
            setIsDown((value) => !value);
          }}
          whileHover={{ scale: 1.1 }}
          className="size-4 cursor-grab touch-none rounded-full bg-linear-to-b from-amber-300 to-amber-600 shadow-md ring-1 ring-amber-700/40 outline-none focus-visible:ring-2 focus-visible:ring-neutral-950 focus-visible:ring-offset-2 active:cursor-grabbing"
        />
      </div>
    </div>
  );
}
Bead, cord and blind in one component

A threshold makes it feel mechanical

Only a pull past 36 pixels works the blind; a small tug does nothing. The blind enters with scaleY from 0 on a spring, origin at the top, so it unrolls downward.

Click works too

The bead is a button with aria-expanded and two labels, one for pulling down and one for rolling up. A ref swallows the click that follows a drag, so dragging doesn't toggle twice.

More articles