Try 'magnetic button'
View code

Specification

Catalogue
No. 104 of 110
Interaction
Type, Feedback
Element
Form, Input, Card
Preview
Live component
Imports
react 19.3.1, @number-flow/react 0.6.2, framer-motion 12.40.0
Added

Notes

An email field framed by a small stack of past issues. Subscribing turns the button into its own spinner and then a tick, the next issue, addressed to the visitor, rises onto the front of the stack, and the reader count goes up by one with "including you".

The confirmation shows what the visitor gets instead of saying thanks. It suits studios, coaches and agencies that send a letter, and the success state doubles as proof that the letter exists.

Each issue behind the front one sits 12px higher and 4.5% smaller per step. The new issue comes in from 56px below; the oldest steps back and fades. Position rides a 0.3s spring with no bounce and opacity resolves in 100ms, so two issues never show through each other. Button labels swap with a 6px lift and a 2px blur. An empty or malformed address shakes the field (0, -6, 5, -3, 1, 0px over 360ms) and the error takes the place of the note; every message has its space reserved, so nothing below moves. The address is checked when the visitor leaves the field or presses Subscribe, never while they type. The count rolls with NumberFlow.

The newsletter, issues and reader count are sample content, and sending is simulated; a live build shows the next issue only once the server accepts the address.

Code

React and plain CSS
"use client";

import { useEffect, useId, useRef, useState, type FormEvent, type ReactNode } from "react";
import { AnimatePresence, motion, useAnimate, type Variants } from "framer-motion";
import type { LivePreviewProps } from "@/components/live/types";
import { EASE_ENTER, physicalSpring } from "@/components/pieces/kit/physical-spring";
import { useSafeReducedMotion } from "@/components/pieces/kit/use-safe-reduced-motion";
import { Num } from "@/components/pieces/shared";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./newsletter-issue.css";

type Issue = { number: number; date: string; subject: string; stories: { title: string; img: string; min: number }[] };
type Phase = "idle" | "sending" | "done";

const S = "/samples";
const READERS = 1284;
const STEP = 12;
const SETTLE = physicalSpring(0.3);
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
const MSG = {
  note: "One letter a month. Unsubscribe from any issue.",
  empty: "Enter your email address",
  format: "Enter an email like name@studio.com",
  done: "Check your inbox to confirm.",
};

const RECENT: Issue[] = [
  { number: 41, date: "November", subject: "Glazes that survive the dishwasher", stories: [{ title: "Testing six celadons", img: "studio-vases.svg", min: 4 }, { title: "A shelf for drying", img: "studio-shelf.svg", min: 3 }] },
  { number: 40, date: "October", subject: "Throwing a set that matches", stories: [{ title: "Weighing the clay first", img: "studio-bowls.svg", min: 5 }, { title: "Lids that fit", img: "studio-jug.svg", min: 3 }] },
  { number: 39, date: "September", subject: "Why handles crack", stories: [{ title: "Pulling, not rolling", img: "studio-cups.svg", min: 4 }, { title: "Slow drying", img: "studio-plates.svg", min: 2 }] },
];
const NEXT: Issue = { number: 42, date: "December", subject: "Gifts you can still make by hand", stories: [{ title: "Three evenings, one set", img: "studio-cups.svg", min: 4 }, { title: "Wrapping fired work", img: "studio-flowers.svg", min: 2 }] };

type Placement = { depth: number; upcoming: boolean };
const stack: Variants = {
  placed: ({ depth }: Placement) => ({ opacity: 1, y: -depth * STEP, scale: 1 - depth * 0.045 }),
  away: ({ upcoming }: Placement) => (upcoming ? { opacity: 0, y: 56, scale: 1 } : { opacity: 0, y: -3 * STEP, scale: 1 - 3 * 0.045 }),
};

function IssueCard({ issue, to }: { issue: Issue; to?: string }) {
  return (
    <>
      <p className="nl-head">
        <span className="nl-mast">The Kiln Letter</span>
        <span>Issue {issue.number}</span>
      </p>
      <p className="nl-meta">{to ? `${issue.date}, to ${to}` : issue.date}</p>
      <p className="nl-subject">{issue.subject}</p>
      <ul className="nl-stories">
        {issue.stories.map((s) => (
          <li key={s.title}>
            <img src={`${S}/${s.img}`} alt="" width={28} height={28} />
            <span>{s.title}</span>
            <span className="nl-min">{s.min} min</span>
          </li>
        ))}
      </ul>
    </>
  );
}

/** Text that swaps in place: the old line lifts away and the new one rises out of a small blur. */
function Swap({ k, reduce, children }: { k: string; reduce: boolean; children: ReactNode }) {
  return (
    <AnimatePresence initial={false} mode="popLayout">
      <motion.span
        key={k}
        className="nl-swap"
        initial={reduce ? { opacity: 0 } : { opacity: 0, y: 6, filter: "blur(2px)" }}
        animate={{ opacity: 1, y: 0, filter: "blur(0px)" }}
        exit={reduce ? { opacity: 0, transition: { duration: 0 } } : { opacity: 0, y: -6, filter: "blur(2px)", transition: { duration: 0.15 } }}
        transition={{ duration: reduce ? 0 : 0.2, ease: EASE_ENTER }}
      >
        {children}
      </motion.span>
    </AnimatePresence>
  );
}

function NewsletterDemo() {
  const reduce = useSafeReducedMotion();
  const uid = useId();
  const [email, setEmail] = useState("");
  const [problem, setProblem] = useState<string | null>(null);
  const [tried, setTried] = useState(false);
  const [phase, setPhase] = useState<Phase>("idle");
  const [sentTo, setSentTo] = useState("");
  const input = useRef<HTMLInputElement>(null);
  const again = useRef<HTMLButtonElement>(null);
  const timer = useRef<number | undefined>(undefined);
  const [pill, animatePill] = useAnimate<HTMLDivElement>();

  useEffect(() => () => window.clearTimeout(timer.current), []);
  useEffect(() => {
    if (phase === "done") again.current?.focus({ preventScroll: true });
  }, [phase]);

  const check = (v: string) => (!v.trim() ? MSG.empty : EMAIL.test(v.trim()) ? null : MSG.format);
  const shake = () => {
    if (!reduce && pill.current) void animatePill(pill.current, { x: [0, -6, 5, -3, 1, 0] }, { duration: 0.36, ease: "easeOut" });
  };

  const submit = (e: FormEvent) => {
    e.preventDefault();
    if (phase !== "idle") return;
    setTried(true);
    const found = check(email);
    setProblem(found);
    if (found) {
      shake();
      input.current?.focus();
      return;
    }
    setPhase("sending");
    // Simulated send. A real build shows the next issue only once the server accepts the address.
    timer.current = window.setTimeout(() => {
      setSentTo(email.trim());
      setPhase("done");
    }, 1100);
  };
  const reset = () => {
    setEmail("");
    setProblem(null);
    setTried(false);
    setPhase("idle");
    window.setTimeout(() => input.current?.focus(), 0);
  };

  const done = phase === "done";
  const issues = (done ? [NEXT, ...RECENT] : RECENT).slice(0, 3);
  const label = done ? "done" : phase === "sending" ? "sending" : "idle";

  return (
    <div className="nl">
      <div className="nl-stack" aria-label="Recent issues">
        <div className="nl-issue nl-sizer" aria-hidden="true">
          <IssueCard issue={NEXT} to="someone@example.com" />
        </div>
        <AnimatePresence initial={false}>
          {issues.map((issue, depth) => (
            <motion.article
              key={issue.number}
              className="nl-issue"
              aria-hidden={depth > 0 || undefined}
              custom={{ depth, upcoming: issue === NEXT }}
              variants={stack}
              initial="away"
              animate="placed"
              exit="away"
              transition={reduce ? { duration: 0 } : { ...SETTLE, opacity: { duration: 0.1 } }}
              style={{ zIndex: 3 - depth }}
            >
              <IssueCard issue={issue} to={issue === NEXT ? sentTo : undefined} />
            </motion.article>
          ))}
        </AnimatePresence>
      </div>

      <form className="nl-form" noValidate onSubmit={submit} aria-labelledby={`${uid}-t`}>
        <h3 id={`${uid}-t`} className="nl-title">
          Get the studio letter
        </h3>
        <motion.div ref={pill} className="nl-pill" data-invalid={problem ? "" : undefined}>
          <label htmlFor={`${uid}-em`} className="sr-only">
            Email address
          </label>
          <input
            ref={input}
            id={`${uid}-em`}
            className="nl-input"
            type="email"
            inputMode="email"
            autoComplete="email"
            enterKeyHint="send"
            autoCapitalize="off"
            spellCheck={false}
            placeholder="you@studio.com"
            value={email}
            readOnly={phase !== "idle"}
            aria-invalid={problem ? true : undefined}
            aria-describedby={`${uid}-msg`}
            onBlur={() => {
              if (email.trim() && !tried && phase === "idle") {
                setTried(true);
                setProblem(check(email));
              }
            }}
            onChange={(e) => {
              setEmail(e.target.value);
              if (tried) setProblem(check(e.target.value));
            }}
          />
          <button type={done ? "button" : "submit"} className="nl-submit" data-state={label} aria-busy={phase === "sending" || undefined} aria-disabled={phase !== "idle" || undefined}>
            <Swap k={label} reduce={reduce}>
              {label === "sending" ? (
                <>
                  <span className="nl-spin" aria-hidden="true" />
                  Subscribing
                </>
              ) : label === "done" ? (
                <>
                  <svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
                    <motion.path d="M5 12.5l4.5 4.5L19 7.5" initial={reduce ? false : { pathLength: 0 }} animate={{ pathLength: 1 }} transition={{ duration: reduce ? 0 : 0.34, ease: EASE_ENTER, delay: 0.12 }} />
                  </svg>
                  Subscribed
                </>
              ) : (
                "Subscribe"
              )}
            </Swap>
          </button>
        </motion.div>
        <div className="nl-msg" aria-live="polite">
          {/* Every line the message can show, unseen, so the slot is always as tall as the longest. */}
          {[MSG.note, `${MSG.done} Use a different email`, MSG.format].map((t) => (
            <p key={t} className="nl-note nl-hold" aria-hidden="true">
              {t}
            </p>
          ))}
          <p id={`${uid}-msg`} className="nl-note" data-tone={problem ? "error" : done ? "done" : undefined} role={problem ? "alert" : undefined}>
            <Swap k={problem ?? (done ? "done" : "note")} reduce={reduce}>
              {problem ??
                (done ? (
                  <>
                    {MSG.done}{" "}
                    <button ref={again} type="button" className="nl-again" onClick={reset}>
                      Use a different email
                    </button>
                  </>
                ) : (
                  MSG.note
                ))}
            </Swap>
          </p>
        </div>
        <p className="nl-readers">
          <span className="nl-count">
            <Num value={READERS + (done ? 1 : 0)} />
          </span>{" "}
          readers
          <AnimatePresence initial={false}>
            {done && (
              <motion.span
                key="you"
                className="nl-you"
                initial={reduce ? { opacity: 0 } : { opacity: 0, width: 0 }}
                animate={reduce ? { opacity: 1 } : { opacity: 1, width: "auto" }}
                exit={{ opacity: 0, width: 0 }}
                transition={reduce ? { duration: 0 } : { width: SETTLE, opacity: { duration: 0.2, ease: EASE_ENTER, delay: 0.12 } }}
              >
                , including you
              </motion.span>
            )}
          </AnimatePresence>
          <span className="nl-sim">Sending is simulated in this preview.</span>
        </p>
      </form>
    </div>
  );
}

// What a hovered card shows: an address typed and sent, and the next issue arriving.
const TOUR: Tour = [
  { input: ".nl-input", value: "ana@studio.pt", wait: 600 },
  { click: ".nl-submit", wait: 2200 },
];

export default function NewsletterIssueEntry(props: LivePreviewProps) {
  return (
    <PieceStage {...props} label="Sign-up that hands over the next issue" tour={TOUR} sample>
      <NewsletterDemo />
    </PieceStage>
  );
}

More like this

File upload with progress per file

Loading, FeedbackForms and inputs

Form in named steps

Type, FeedbackForms and inputs

Several services in one field

Press, TypeForms and inputs

Cookie choice that folds away

FeedbackOverlays and toasts

Sign-up form that waits before it complains

Type, FeedbackForms and inputs

One question at a time

Layout AnimationForms and inputs