getAnimations Finds Running CSS Transitions

List active CSS transitions with getAnimations and await their finished promises instead of hardcoding durations; snapshot after the new style has rendered.

JavascriptResilience

Code

javascriptsnippet
41 lines
// CSS on the element: transition: opacity 240ms ease;
const panel = document.createElement('div');
panel.style.width = '40px';
panel.style.height = '40px';
panel.style.transition = 'opacity 240ms ease';
panel.style.opacity = '0';
document.body.append(panel);

function nextFrame() {
  return new Promise((resolve) => requestAnimationFrame(resolve));
}

function waitForTransitions(el) {
  // getAnimations({ subtree: true }) also collects descendant animations
  const running = el.getAnimations({ subtree: true });
  console.log('running:', running.map((a) => a.constructor.name || a.playState));
  return Promise.all(running.map((a) => a.finished));
}

// A CSS transition only exists once the browser has (1) rendered the old
// value and (2) put the new value through style recalc. Calling
// getAnimations earlier — same frame, or before the change is rendered —
// returns an empty list and Promise.all resolves immediately.
// Frame 1 paints the panel at opacity 0; frame 2 carries the change (the
// transition starts in that frame's style recalc); frame 3 is the first
// moment the snapshot reliably sees the running CSSTransition.
nextFrame()
  .then(() => nextFrame())
  .then(() => {
    panel.style.opacity = '1';
    return nextFrame();
  })
  .then(() => waitForTransitions(panel))
  .then(() => {
    console.log('opacity settled:', getComputedStyle(panel).opacity);
  });

setTimeout(() => {
  // By now the 240ms transition is done; a new snapshot finds none.
  console.log('later snapshot:', panel.getAnimations().length === 0);
}, 400);

How the snippet works

Everything happens inside the functions nextFrame and waitForTransitions.

Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed.

Returns a newly built value, leaving the inputs as they were.

Points of care

  • The snippet leans on document, requestAnimationFrame — browser-only surfaces a bare Node process cannot provide.

Using it

Signature: function nextFrame()

The demo output is labeled (running:, opacity settled:, later snapshot:); each label names the call that printed it.

Key statements

return new Promise((resolve) => requestAnimationFrame(resolve));
return Promise.all(running.map((a) => a.finished));
.then(() => {
  panel.style.opacity = '1';
  return nextFrame();
})

Interactive Tools for This Topic

FAQ

Does `nextFrame` modify its input?
No. The code builds and returns a new value and leaves the arguments untouched, so it is safe to call on shared or frozen data structures.
Does getAnimations Finds Running CSS Transitions run in plain Node.js?
No — it touches `document` and `requestAnimationFrame`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.