Hover to play, leave to pause
We removed hover-tracking hooks from three animated art components. CSS now controls when they play, pause, and resume.
Audrey KlammerCSSReactAnimation
Three of our animated art components had a React hook just to track whether the pointer was inside their container. It found the right parent, attached listeners, checked the initial hover state, and updated React state.
We replaced that tracking with CSS. With a mouse, the animation plays when you enter its container and pauses where you leave it.
Try the ring below. Move anywhere inside the outlined area, leave halfway through a turn, then return.
The hook we removed
PulseOrb, BeadRing, and UnderwaterShaftScene each had a copy of this hook. Here is the relevant part, shortened:
function useHoverPlay(enabled: boolean) {
const marker = useRef<HTMLSpanElement>(null)
const [hovered, setHovered] = useState(false)
useEffect(() => {
if (!enabled) return
let area = marker.current?.parentElement?.parentElement ?? null
while (area && getComputedStyle(area).pointerEvents === "none")
area = area.parentElement
// ...pointerenter and pointerleave listeners that skip touch, and,
// one frame later, area.matches(":hover") for a pointer already inside
}, [enabled])
return { marker, hovered }
}
Each component used that state to control its animation. Entering and leaving the area updated state and triggered a render.
CSS already tracks hover
A hovered descendant can also make its ancestors match :hover
(Selectors Level 4).
The container can control playback directly, without forwarding hover
state through React.
All three components use that pattern. This is PulseOrb's direct-parent version:
[data-slot="pulse-orb"][data-hover-play] {
--pulse-orb-play: paused;
}
@media (any-hover: hover) {
:hover > [data-slot="pulse-orb"][data-hover-play] {
--pulse-orb-play: running;
}
}
.pulse-orb-anim {
animation-play-state: var(--pulse-orb-play, running) !important;
}
@media (prefers-reduced-motion: reduce) {
.pulse-orb-anim {
animation: none !important;
}
}
The !important matters here: PulseOrb sets the
animation shorthand
inline, which also sets animation-play-state. The stylesheet needs to
override that inline value.
Pausing preserves the animation's position. Resuming continues from where it left off.
Mouse, touch, and reduced motion
- Hover capability.
any-hover: hoverasks whether any available input can hover. It does not identify which input caused an interaction.hover: hoverasks about the primary input. - Touch-only devices. When the browser reports
any-hover: none, the hover-only animation stays paused, including after a tap. - Hybrid devices. Allowing touch-triggered playback when a hover-capable input is available was an intentional change from the old hook's touch check. In Chromium input emulation, a touch tap with hover capability available started the art. Touch-generated hover varies by browser.
- Reduced motion. All three components suppress their animations when
reduced motion is requested.
animated="always"bypasses hover gating, but still respects that preference;animated={false}disables motion.
Implementation details: wrappers, nested rings, and shared styles
Click-through wrappers
Some of our art sits inside pointer-events-none wrappers. The property
is inherited, so descendants also let pointer events through unless they
override it. A descendant with pointer-events: auto can restore targeting
(MDN).
For our click-through layers, playback is controlled from a targetable ancestor above the wrappers. These extra selectors stay inside the same hover-capability query:
@media (any-hover: hover) {
:hover > .pointer-events-none > [data-slot="pulse-orb"][data-hover-play],
:hover
> .pointer-events-none
> .pointer-events-none
> [data-slot="pulse-orb"][data-hover-play] {
--pulse-orb-play: running;
}
}This supports up to two wrappers carrying the pointer-events-none
class. The old hook could climb any number of ancestors based on computed
styles. The CSS covers a documented structure, rather than reproducing
that traversal for arbitrary markup.
Nested rings
Custom properties inherit. BeadRing explicitly sets --bead-play: running
on a ring with animated="always", so it does not inherit a hover-only
outer ring's pause. Reduced motion still suppresses its animation.
Shared styles
Each component renders its keyframes and hover rules in a
<style href="…" precedence="medium"> element.
React moves it to the head and deduplicates styles with the same href.
Ten BeadRing instances share one copy of their stylesheet.
Removing hover tracking let us remove the hook, its listeners, and
"use client" from all three component files.