Try 'magnetic button'
View code

Specification

Catalogue
No. 52 of 110
Interaction
Hover
Element
Chart
Preview
Live component
Imports
react 19.3.1
Added

Notes

Lines from a business's tools into its hub draw themselves once, and again for any tool you point at.

An automation or integration understood at a glance. Each line is drawn once on arrival, and pointing at a tool redraws its line and says what it hands over.

Each line draws in 0.7s, 120ms apart, cubic-bezier(.65, 0, .35, 1), once when 60% is in view. Pointing at or pressing a tool redraws its line in the brand colour and says what it does. Changed from the library: a plain stroke drawn once, no gradient light looping for ever. A real build uses the client's real tools with their marks, grey until hover.

The names, figures and pictures in the demo are sample content; a real build uses the client's own.

Code

React and plain CSS
"use client";

import { useLayoutEffect, useRef, useState } from "react";
import { layoutScale, useArrived } from "@/components/pieces/motion-helpers";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import "./animated-beam.css";

const NODES = [
  { id: "form", label: "Booking form", side: "in", says: "A new booking lands in your CRM with the client's details." },
  { id: "calendar", label: "Calendar", side: "in", says: "Free times come from your calendar, so nobody double-books." },
  { id: "payments", label: "Card payments", side: "in", says: "Deposits are matched to the booking they belong to." },
  { id: "email", label: "Email", side: "out", says: "The CRM sends the confirmation and the reminder." },
  { id: "accounts", label: "Accounts", side: "out", says: "Paid bookings go to your accounts as sales." },
] as const;

type Line = { id: string; d: string };

function AnimatedBeamDemo() {
  const box = useRef<HTMLDivElement>(null);
  const hub = useRef<HTMLDivElement>(null);
  const nodes = useRef<Record<string, HTMLButtonElement | null>>({});
  const arrived = useArrived(box, 0.6);
  const [lines, setLines] = useState<Line[]>([]);
  const [focus, setFocus] = useState<string | null>(null);
  const [redraw, setRedraw] = useState<Record<string, number>>({});

  useLayoutEffect(() => {
    const b = box.current;
    const h = hub.current;
    if (!b || !h) return;
    const measure = () => {
      const br = b.getBoundingClientRect();
      const hr = h.getBoundingClientRect();
      // Rects are on-screen sizes; the paths are drawn in the box's own units.
      const s = layoutScale(b);
      const hx = (hr.left - br.left) / s;
      const hy = (hr.top - br.top + hr.height / 2) / s;
      const out: Line[] = [];
      for (const n of NODES) {
        const el = nodes.current[n.id];
        if (!el) continue;
        const r = el.getBoundingClientRect();
        const y = (r.top - br.top + r.height / 2) / s;
        if (n.side === "in") {
          const x = (r.right - br.left) / s;
          const mid = (x + hx) / 2;
          out.push({ id: n.id, d: `M${x} ${y} C${mid} ${y} ${mid} ${hy} ${hx} ${hy}` });
        } else {
          const x = (r.left - br.left) / s;
          const hxr = hx + hr.width / s;
          const mid = (x + hxr) / 2;
          out.push({ id: n.id, d: `M${hxr} ${hy} C${mid} ${hy} ${mid} ${y} ${x} ${y}` });
        }
      }
      setLines(out);
    };
    measure();
    const ro = new ResizeObserver(measure);
    ro.observe(b);
    return () => ro.disconnect();
  }, []);

  const ask = (id: string) => {
    setFocus(id);
    setRedraw((r) => ({ ...r, [id]: (r[id] ?? 0) + 1 }));
  };
  const current = NODES.find((n) => n.id === focus);

  return (
    <div className="dm">
      <div className="bm" ref={box} data-drawn={arrived || undefined}>
        <svg className="bm-lines" aria-hidden="true">
          {lines.map((l, i) => (
            <path key={`${l.id}-${redraw[l.id] ?? 0}`} d={l.d} pathLength={1} data-on={focus === l.id || undefined} data-again={redraw[l.id] ? true : undefined} style={{ animationDelay: redraw[l.id] ? "0ms" : `${i * 120}ms` }} />
          ))}
        </svg>
        <div className="bm-col">
          {NODES.filter((n) => n.side === "in").map((n) => (
            <button
              key={n.id}
              ref={(el) => {
                nodes.current[n.id] = el;
              }}
              type="button"
              className="bm-node"
              aria-pressed={focus === n.id}
              onClick={() => ask(n.id)}
              onPointerEnter={(e) => e.pointerType === "mouse" && ask(n.id)}
            >
              {n.label}
            </button>
          ))}
        </div>
        <div ref={hub} className="bm-hub">
          Your CRM
        </div>
        <div className="bm-col">
          {NODES.filter((n) => n.side === "out").map((n) => (
            <button
              key={n.id}
              ref={(el) => {
                nodes.current[n.id] = el;
              }}
              type="button"
              className="bm-node"
              aria-pressed={focus === n.id}
              onClick={() => ask(n.id)}
              onPointerEnter={(e) => e.pointerType === "mouse" && ask(n.id)}
            >
              {n.label}
            </button>
          ))}
        </div>
      </div>
      <p className="bm-say" aria-live="polite">
        {current ? current.says : "Point at or press any tool to see what it hands over"}
      </p>
    </div>
  );
}

export default function AnimatedBeamEntry(props: LivePreviewProps) {
  return (
    <PieceStage {...props} label="How the tools connect" sample replay>
      <AnimatedBeamDemo />
    </PieceStage>
  );
}

More like this

Slope chart for before and after

Hover, PressCharts and numbers

Context window meter

Hover, PressCharts and numbers

Donut that changes period

Hover, PressCharts and numbers

Steps that scroll past a picture that holds

ScrollPage sections

Cards that stack as you scroll

ScrollCards and accordions

Balanced logo wall

Hover, PressPage sections