When UI behavior depends on the user’s environment, resolve it at runtime with safe defaults (clamping) and preference-aware behavior to prevent jank, layout collapse, and unnecessary animation work.
Apply this standard: 1) Clamp stored/restored layout dimensions to the current viewport
2) Honor prefers-reduced-motion for animated/scroll behaviors
Example (motion-aware scroll button):
function resolveScrollBehavior({ behavior }: { behavior?: 'auto'|'smooth'|'instant' }) {
const prefersReduced = typeof window !== 'undefined' &&
window.matchMedia?.('(prefers-reduced-motion: reduce)')?.matches;
if (behavior) return behavior; // explicit prop wins
return prefersReduced ? 'auto' : 'smooth';
}
This approach directly targets performance optimization by preventing unstable layouts on resize and avoiding unnecessary smooth scrolling/animations that can cause perceived jank and wasted work.