DEV Community

Cover image for A Headline That Swells Under Your Cursor (Variable Fonts + Motion)
Pooja Deliver Website
Pooja Deliver Website

Posted on Fully Autonomous

A Headline That Swells Under Your Cursor (Variable Fonts + Motion)

Most hover effects on hero headlines move the whole line: a gradient sweep, a tilt, a glow. This one works per letter. Each letter of the headline gets heavier and darker the closer your cursor is, so moving across the line feels like pressing into it.

It needs three things: a variable font, the pointer position as motion values, and one small calculation per letter.

Why a variable font

A variable font carries every weight between its lightest and heaviest in one file. That means font-weight can animate smoothly from 200 to 900 instead of jumping between a few static files. Geist, Inter, Roboto Flex and most modern system fonts support it.

Step 1: track the pointer without re-rendering

A minimal version: store the pointer in motion values, not React state. Motion values update outside React, so the headline doesn't re-render 60 times a second.

const ref = useRef<HTMLHeadingElement>(null);
const x = useMotionValue(-9999);
const y = useMotionValue(-9999);

function onPointerMove(e: React.PointerEvent) {
  const box = ref.current!.getBoundingClientRect();
  x.set(e.clientX - box.left);
  y.set(e.clientY - box.top);
}
Enter fullscreen mode Exit fullscreen mode

Starting at -9999 means every letter begins light until the pointer arrives.

Step 2: measure each letter once

Each letter needs to know where its centre is inside the headline. Measure on mount, when fonts finish loading, and on resize. Never per frame.

useEffect(() => {
  const measure = () => {
    const letter = letterRef.current?.getBoundingClientRect();
    const box = containerRef.current?.getBoundingClientRect();
    if (!letter || !box) return;
    center.current = {
      x: letter.left - box.left + letter.width / 2,
      y: letter.top - box.top + letter.height / 2,
    };
  };
  measure();
  document.fonts.ready.then(measure);
  window.addEventListener("resize", measure);
  return () => window.removeEventListener("resize", measure);
}, [containerRef]);
Enter fullscreen mode Exit fullscreen mode

document.fonts.ready matters: letters change width when the real font replaces the fallback, so a measurement taken too early is wrong.

Step 3: turn distance into pressure

const REACH_PX = 240;

const pressure = useTransform(() => {
  const d = Math.hypot(x.get() - center.current.x, y.get() - center.current.y);
  const closeness = Math.max(0, 1 - d / REACH_PX);
  return closeness * closeness; // squared: soft at the edge, strong at the centre
});
const fontWeight = useTransform(pressure, [0, 1], [200, 900]);
const opacity = useTransform(pressure, [0, 1], [0.35, 1]);

return (
  <motion.span ref={letterRef} style={{ fontWeight, opacity }} className="inline-block">
    {char}
  </motion.span>
);
Enter fullscreen mode Exit fullscreen mode

Squaring the closeness is the detail that makes it feel physical. A linear falloff makes a flat plateau of medium-weight letters; the squared curve gives a clear peak right under the cursor.

Step 4: split the text without breaking it

Splitting a headline into spans has two traps:

  1. Words breaking mid-word. Wrap each word in an inline-block whitespace-nowrap span so a word never splits across lines.
  2. Lost spaces. Browsers drop a trailing space inside an inline-block, which turns "Websites people" into "Websitespeople". Put the space between the word spans, not inside them.
<span className="sr-only">{text}</span>
<span aria-hidden>
  {words.map((word, i) => (
    <Fragment key={i}>
      {i > 0 && " "}
      <span className="inline-block whitespace-nowrap">
        {Array.from(word).map((c, j) => <PressureLetter key={j} char={c} ... />)}
      </span>
    </Fragment>
  ))}
</span>
Enter fullscreen mode Exit fullscreen mode

The sr-only copy means screen readers hear the headline once as normal text instead of letter by letter.

Phones and reduced motion

Phones have no hover, so the effect would never show. The full component feeds the letters a "wandering pointer" instead: when there's no cursor, a virtual point drifts across the section and rolls a slow wave through the headline, then hands control back the moment a real cursor arrives. For visitors with "reduce motion" on, a still headline at one comfortable weight is the safe fallback.

Performance

Animating font-weight triggers text layout, so keep it to the headline (a few dozen letters), not body copy. With motion values and measurements cached, it stays smooth on mid-range phones.

The full component

The finished version (React + Tailwind + Motion, with the wandering pointer for phones) is free on DeliverWebsite UI: https://ui.deliverwebsite.com/components/interactive-sections/pressure-type/

Top comments (0)