Course contentsShow
← Web Development
Lesson 1714 of 2,98231. Error Handling and ResilienceFree lesson

Respecting User Preferences with Toasts

Implementing prefers-reduced-motion for animations and providing persistent alternatives for critical messages.

Respecting User Preferences with Toasts

You'll learn how to honor user accessibility preferences when displaying toast notifications, ensuring animations don't harm users with motion sensitivities while keeping critical information accessible.

Why User Preferences Matter

Not all users experience animations the same way. For some, motion can trigger vestibular disorders, causing dizziness or nausea. The browser provides prefers-reduced-motion, a media query that signals when users have requested minimal animation in their operating system settings. Respecting this preference isn't just good practice—it's essential accessibility.

Implementing Reduced Motion for Toasts

When a user prefers reduced motion, modify your toast animations from the previous lesson. Instead of sliding or fading dynamically, toasts should appear and disappear instantly or with minimal transition.

.toast {
  animation: slideIn 0.3s ease-out;
}

@media (prefers-reduced-motion: reduce) {
  .toast {
    animation: none; /* Disable animation entirely */
    transition: none;
  }
}

You can also check this preference in JavaScript:

const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;

if (!prefersReducedMotion) {
  // Apply animation classes
  toast.classList.add('animated');
}

Persistent Alternatives for Critical Messages

Auto-dismissing toasts work for informational messages, but critical errors or warnings shouldn't disappear on users with reduced motion preferences. They may miss important information if it vanishes instantly.

For critical toasts, disable auto-dismiss when reduced motion is active, or provide a persistent fallback UI element that remains visible until the user explicitly acknowledges it.

const duration = prefersReducedMotion && isCritical ? null : 5000;

Key Takeaway: Always check prefers-reduced-motion to disable or minimize toast animations, and ensure critical messages remain visible long enough (or permanently) for users who need reduced motion—accessibility means meeting users where they are.