web.dev icon indicating copy to clipboard operation
web.dev copied to clipboard

<portal> blog post: include 'prefers-reduced-motion' in examples

Open Malvoz opened this issue 7 years ago • 7 comments

In https://web.dev/hands-on-portals/, please consider including prefers-reduced-motion, in respect of motion-triggered vestibular spectrum disorder. This helps create awareness of the prefers-reduced-motion media query, but also if devs resort to copy/pasting from the examples we'll just have a11y baked in for those sites.

Malvoz avatar May 10 '19 12:05 Malvoz

oh good point. I'm just about to head out on vacation but I can look into this when I come back.

robdodson avatar May 10 '19 16:05 robdodson

@uskay is this something you might be able to add?

robdodson avatar Jun 04 '19 00:06 robdodson

Yes I should be able to do that. Thank you for the heads up. I'll work on it as soon as I get a chance.

uskay avatar Jun 04 '19 00:06 uskay

rad thank you!

robdodson avatar Jun 04 '19 19:06 robdodson

Hey @uskay I wanted to check if you still think you'll have time to do this? If not I can see if someone else on our team is free to add it.

robdodson avatar Jul 15 '19 22:07 robdodson

Apologies. I looked into this and there are multiple way to implement this. The easiest way is using the prefers-reduced-motion media query in the CSS and setting the transition duration to 0.001s (which should be an instant transition for human eyes). The reason for not setting the transition-property value to none is that it'll be a lot more simpler to commonly use the transitionend event as the activate trigger.

  .portal-transition {
    transition:
      transform 0.4s,
      bottom 0.7s,
      left 0.7s,
      opacity 1.0s;
  }
  @media (prefers-reduced-motion: reduce) {
    .portal-transition {
      transition: all 0.001s;
    }
  }

If we strictly wanted to reduce the motion to none, I can think of a combination of both fixing the style and JavaScript logic. Having the JavaScript implementation is not enough since we need to control the opacity transition i.e. fade-in animation as well.

 .portal-transition {
    transition:
      transform 0.4s,
      bottom 0.7s,
      left 0.7s,
      opacity 1.0s;
  }
  @media (prefers-reduced-motion: reduce) {
    .portal-transition {
      transition-property: none;
    }
  }

and

portal.addEventListener('click', evt => {
  // Check if the user perferes reduced motion
  const mediaQuery = window.matchMedia('(prefers-reduced-motion: reduce)');
  if (mediaQuery.matches) {
    // Activate without transition
    portal.activate();
  } else {
    // Animate the portal once user interacts
    portal.classList.add('portal-reveal');
  }
}

As I'm not an expert in accessibility your advice is very much appreciated. I have a feeling I do want to stick with the first option to make the code cleaner IMO (as this should be a short snippet to understand the APIs of Portals).

uskay avatar Jul 17 '19 15:07 uskay

+1 for option one.

Malvoz avatar Jul 17 '19 15:07 Malvoz