Tooltip
A cursor tooltip should cost one render to open and none to follow the pointer.
The problem
The obvious cursor tooltip keeps the pointer position in component state. Every mouse move sets state, every state change re-renders, and sweeping across a chart means dozens of renders a second for a box that just slides along.
The obvious fix has a trap of its own. Write the position to CSS variables on :root and every element in the page inherits a value that changes on every move, so the browser rechecks styles for the whole tree.
What I concluded
Position isn’t state. It’s a style. The only React state is a boolean for “open.” After that, each pointer move writes two custom properties straight onto the tooltip element, and CSS places it.
stage.addEventListener("pointermove", (e) => {
tip.style.setProperty("--px", `${e.clientX - frame.left}px`);
tip.style.setProperty("--py", `${e.clientY - frame.top}px`);
}, { passive: true });
Registering those properties with @property and inherits: false keeps them from cascading even into the tooltip’s own children.
Edge flipping is CSS too. The tooltip prefers the right of the cursor. A min()/max() expression compares the room left against a boundary and, when it runs out, picks the flipped position on the other side. The boundary can be the viewport or a region, so a tooltip in one pane never crosses into the next.
Keyboard opens it too. With no pointer to follow, a focused trigger positions the tooltip against its own box, links it with aria-describedby, and closes it on Escape without moving focus. For that case, CSS anchor positioning can replace the math where it’s supported. It can’t follow a cursor, though, so the custom-property version stays for pointer tracking.
Rules
- One render to open. Visibility is state, position isn’t.
- Write variables on the tooltip. Never on
:root, and register them as non-inheriting. - Flip at a boundary you choose. Viewport by default, a region when the page is split.
- Stay mounted after first use. An unmounted node can’t play an exit animation, so only set
role="tooltip"while it’s open. - Don’t disable the trigger. A
disabledbutton gets no pointer events or focus, so its tooltip can never show. Usearia-disabledand ignore the click.
What you get
A tooltip that tracks the cursor smoothly however heavy the chart under it is. The demo counts React renders against pointer moves, and toggles flipping at the region’s edge. It doesn’t show the keyboard path. I use this for transient readouts like chart cursors and info icons, not for partial detail in table cells.
Related: Chart plumbing, Layout geometry