Writing · Accessibility · CSS · Web
Why focus() silently failed for every reduced-motion user
A blanket prefers-reduced-motion rule made visibility a transition, so focus() on a just-revealed menu did nothing and keyboards couldn't open any menu.
For a long time, keyboard users of my app who had "reduce motion" switched on in their operating system could not use a single dropdown menu. They could open one with Enter. The arrow keys then did nothing, because focus never moved into the menu. Nothing was logged, no error was thrown, and every automated test passed. The cause was two reasonable pieces of CSS and JavaScript that only fail in combination.
The two reasonable pieces
Piece one: the reduced-motion reset. Like many sites, mine carries the well-known blanket rule that switches off animation for people who ask for less motion:
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
The duration is a tiny value and not zero on purpose. With zero, no transition runs at all, so transitionend and animationend events never fire, and any code waiting for them hangs.
Piece two: the menu. A dropdown has to be measured before it can be placed, because its position depends on its own size and on how much room the viewport has. So the menu component rendered its panel off-screen and invisible, measured it, then moved it into place, revealed it, and focused the first item. All of that happened synchronously in one step:
// after measuring
panel.style.left = x + "px";
panel.style.top = y + "px";
panel.style.visibility = "visible";
firstItem.focus();
visibility: hidden is a natural choice for the unplaced state. It keeps the element's layout box, so it can be measured, and it hides it from sight and from assistive technology.
Why they fail together
Three facts combine.
1. Every element already has transition-property: all. That is the CSS default. Elements don't visibly transition only because the default duration is zero. The reset rule gives every element a non-zero duration, so under reduced motion every property change on every element becomes a real transition, 0.01 ms long. That includes properties nobody thinks of as animated.
2. visibility can be transitioned. It doesn't fade, but it does take part in transitions: the value switches between the two states over the transition's duration. At the instant a transition starts, the element's computed value is still the starting value.
3. focus() refuses hidden elements, quietly. An element whose computed visibility is hidden can't be focused. The method doesn't throw and returns nothing. Focus just stays where it was.
Put together: the script set visibility: visible, which under reduced motion started a 0.01 ms transition. On the very next line, with no time having passed, the first item's computed visibility was still hidden. focus() did nothing. A fraction of a millisecond later the menu was fully visible, with focus still on the button behind it. When I finally logged it on an affected machine, the result was exactly that:
{ rowVisibility: "hidden", tookFocus: false }
Without reduced motion, the change applied instantly and focus landed. So the bug existed only for people who had turned the setting on, and nobody testing with default settings could ever see it.
The fix
The menu no longer uses visibility at all for its unplaced state. The panel is rendered far off-screen (left: -9999px) until it has been measured, then moved into place. An off-screen element is still focusable, so there is no moment at which focus() can be refused, whatever the user's motion setting does to transitions.
While I was in there I fixed a second problem with the same component: a menu taller than the viewport had rows that could not be reached at all. Placement now gives the panel a maximum height and lets it scroll.
Other ways to solve it, if moving the element off-screen doesn't suit your component:
- Take visibility out of the transition. Give the panel an explicit
transition-propertythat lists only what you animate (for exampleopacity, transform), with enough specificity to hold under the reset. - Focus after the reveal has been committed. Wait one animation frame, or for
transitionend, before callingfocus(). - Scope the reset. Apply the reduced-motion rule to the properties that actually move things, not to every transition on the page.
The general lesson: check that focus landed
focus() is one of the few DOM calls that fails without a signal. It can be refused because the element is hidden, inert, disabled, not yet in the document, or inside a closed <details>. Any code that moves focus as part of opening something should be able to confirm it:
function focusOrReport(el) {
el.focus();
if (document.activeElement !== el) {
console.warn("focus was refused", el, getComputedStyle(el).visibility);
return false;
}
return true;
}
In a test suite this is a one-line assertion (expect(document.activeElement).toBe(firstItem)), and it would have caught this bug on the day it was written, provided the test ran with reduced motion emulated.
Test with reduced motion on
That last condition is the real gap. My tests and my manual checks all ran with default settings. Reduced motion is easy to emulate, and it changes more behaviour than its name suggests:
- In Chrome DevTools: open the Rendering panel and set Emulate CSS media feature prefers-reduced-motion to reduce.
- In Playwright:
page.emulateMedia({ reducedMotion: "reduce" }). - On your own machine: turn the system setting on for a week. I found a second issue that way. A loading overlay that fades out when the app is ready stayed fully opaque over the finished page until its (collapsed) animation ended, then cut away abruptly. With motion reduced it was a longer wait that ended in a harder flash, the opposite of what the setting asks for. That one animation is now exempt from the reset, because it only changes opacity and nothing moves.
Summary
- The common reduced-motion reset turns every property change into a very short transition, because
transition-propertydefaults toall. visibilitytakes part in transitions, so an element revealed that way is still hidden for an instant.focus()on a hidden element fails silently. Verify withdocument.activeElement.- Don't hide a to-be-focused element with
visibility, or else focus it a frame later. - Run your keyboard tests with reduced motion emulated.
