Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for prefers-reduced-motion #8494

Merged
merged 12 commits into from
Jul 19, 2019
14 changes: 13 additions & 1 deletion src/ui/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ class Camera extends Evented {
easing: defaultEasing
}, options);

if (options.animate === false) options.duration = 0;
if (options.animate === false || browser.prefersReducedMotion) options.duration = 0;

const tr = this.transform,
startZoom = this.getZoom(),
Expand Down Expand Up @@ -857,6 +857,18 @@ class Camera extends Evented {
* @see [Fly to a location based on scroll position](https://www.mapbox.com/mapbox-gl-js/example/scroll-fly-to/)
*/
flyTo(options: Object, eventData?: Object) {
// Fall throwugh to jumpTo is user has set prefers-reduced-motion
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: typos

if (browser.prefersReducedMotion) {
this.jumpTo({
center: options.center,
zoom: options.zoom,
bearing: options.bearing,
pitch: options.pitch,
around: options.around
}, eventData);
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be return this (or return this.jumpTo(...)) for consistency/chaining.

}

// This method implements an “optimal path” animation, as detailed in:
//
// Van Wijk, Jarke J.; Nuij, Wim A. A. “Smooth and efficient zooming and panning.” INFOVIS
Expand Down
13 changes: 12 additions & 1 deletion src/util/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const cancel = window.cancelAnimationFrame ||

let linkEl;

let reducedMotionQuery: MediaQueryList;

/**
* @private
*/
Expand Down Expand Up @@ -53,7 +55,16 @@ const exported = {
},

hardwareConcurrency: window.navigator.hardwareConcurrency || 4,
get devicePixelRatio() { return window.devicePixelRatio; }

get devicePixelRatio() { return window.devicePixelRatio; },
get prefersReducedMotion(): boolean {
if (!window.matchMedia) return false;
//Lazily initialize media query
if (reducedMotionQuery == null) {
reducedMotionQuery = window.matchMedia('(prefers-reduced-motion: reduce)');
}
return reducedMotionQuery.matches;
},
};

export default exported;