Review the failure mode before adding a reduced-motion rule
Prerequisites: a disclosure-like control with one source of truth for open or closed state, persistent UI outputs for that state, and CSS that owns the interaction transition.
Start with the failure assertions a robust component should satisfy. Under reduced motion, an open disclosure must still report an expanded value, reveal its panel, and change to an open-state label. After a second toggle, it must report the collapsed value and restore the closed-state label. Invalid preference values and unsupported commands should be rejected rather than silently selecting behavior or changing state.
Those checks reveal the real risk: disabling a transition alone can pass a visual inspection while leaving the control ambiguous. A panel that appears instantly is not necessarily a control that clearly communicates its current state.
The implementation decision is to derive both semantic and visual state from the same expanded flag. The button’s label, icon, active color, and expanded attribute change with that flag; panel visibility changes with it too. Motion is a separate policy. The normal branch supplies a transition, while the reduced-motion branch supplies no transition.
Expected result: activating the control still opens and closes the same content for every preference. Users who allow motion see the transition. Users who reduce motion get the end state immediately, with enough persistent evidence to understand it without having watched movement.
One proof limit matters: controller tests can verify the returned state and selected transition policy, but they cannot demonstrate that a real browser matched an operating-system preference. Test browser behavior separately if that integration is part of the requirement.
One limitation remains: do not use this as a blanket answer when animation communicates essential sequence or data. Provide an equivalent textual or structural representation instead of treating the motion as disposable.
Working example
// This controller models the state that a disclosure button exposes to its UI.
// The CSS below is meant to be applied in a browser; Node tests only prove the
// controller's state and motion-policy behavior, not browser media-query matching.
export const disclosureCss = `
.disclosure-panel {
max-height: 0;
opacity: 0;
overflow: clip;
transition: max-height 180ms ease, opacity 180ms ease;
}
.disclosure-panel[data-expanded="true"] {
max-height: 20rem;
opacity: 1;
}
.disclosure-button[data-expanded="true"] {
color: #075985;
}
@media (prefers-reduced-motion: reduce) {
.disclosure-panel {
transition: none;
}
}
`;
const labels = {
collapsed: { text: 'Show details', icon: '›' },
expanded: { text: 'Hide details', icon: '⌄' },
};
function validateMotionPreference(motionPreference) {
if (motionPreference !== 'no-preference' && motionPreference !== 'reduce') {
throw new RangeError(
'motionPreference must be "no-preference" or "reduce"',
);
}
}
/**
* Creates an interaction controller for a disclosure control.
* `motionPreference` represents the result of the browser/OS preference check.
*/
export function createDisclosure({ motionPreference = 'no-preference' } = {}) {
validateMotionPreference(motionPreference);
let expanded = false;
function view() {
const label = labels[expanded ? 'expanded' : 'collapsed'];
const motionReduced = motionPreference === 'reduce';
return {
button: {
ariaExpanded: String(expanded),
dataExpanded: String(expanded),
text: label.text,
icon: label.icon,
color: expanded ? '#075985' : '#1f2937',
},
panel: {
hidden: !expanded,
dataExpanded: String(expanded),
transition: motionReduced
? 'none'
: 'max-height 180ms ease, opacity 180ms ease',
},
};
}
return {
dispatch(command) {
if (command !== 'toggle') {
throw new RangeError('Unsupported command: ' + command);
}
expanded = !expanded;
return view();
},
view,
};
}
Read the full article: https://chriseugenerodriguez.com/blog/preserve-control-state-when-motion-is-reduced