// Text & type
007 / 112
Variable-weight type that thickens under the pointer and thins behind it.
Loading…
The code
All of it, in one file: the component above, restyle included. Nothing imported but React.
"use client";
import { useRef } from "react";
/** WeightHover — variable-weight type thickens under the pointer and thins behind it, falling off with distance. Needs a variable font. React only. */
export default function WeightHover({ text = "Weight under the pointer" }: { text?: string }) {
const box = useRef<HTMLParagraphElement>(null);
const apply = (px: number | null) => {
box.current?.querySelectorAll<HTMLElement>("i").forEach((el) => {
const b = el.getBoundingClientRect();
const d = px === null ? 999 : Math.abs(px - (b.left + b.width / 2));
el.style.fontWeight = String(Math.round(200 + 700 * Math.max(0, 1 - d / 140)));
});
};
return (
<p ref={box} aria-label={text} onPointerMove={(e) => apply(e.clientX)} onPointerLeave={() => apply(null)}
style={{ margin: 0, fontFamily: "var(--f-display,system-ui)", fontStretch: "87.5%", fontSize: "clamp(34px,5vw,68px)", letterSpacing: "-.03em", lineHeight: 1.05, textTransform: "uppercase" }}>
{[...text].map((c, i) => (
<i key={i} aria-hidden="true" style={{ fontStyle: "normal", fontWeight: 200, transition: "font-weight .25s" }}>{c === " " ? " " : c}</i>
))}
</p>
);
}
Props
| Prop | Default |
|---|---|
text | "Weight under the pointer" |
Theme tokens it reads
Set these on :root to re-skin it. Each has a fallback, so it also works with none of them defined.
| Variable | Fallback | What it does |
|---|---|---|
--f-display | system-ui | Display face for big type (a condensed variable font reads best). |
Or install it
Published to a shadcn-style registry, so one command fetches the file straight into your project.
npx shadcn@latest add https://your-domain.com/r/weight-hover.jsonPlaceholder domain — set NEXT_PUBLIC_SITE_URL to your deployed origin and the command updates.
// Next up