forked from Andrey-1988-dev/snowfall-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefersReducedMotion.js
40 lines (37 loc) · 1.32 KB
/
prefersReducedMotion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { logWarn } from "./logger.js";
/**
* Checks the reduced motion configuration based on user preferences and plugin settings.
*
* @param {Object} configParams - Configuration parameters.
* @returns {boolean} - True if reduced motion is allowed, false otherwise.
* @see canRunAnimation.js
*/
export function checkReducedMotionConfig(configParams) {
const prefersReducedMotion = window.matchMedia(
"(prefers-reduced-motion)"
).matches;
if (prefersReducedMotion && configParams.setReducedMotion === "disable") {
return false;
} else {
return true; // case: setReducedMotion = "reduce" or no-preference set in browser configuration
}
}
/**
* Validates the motion reduce multiplier value and provides a fallback value if needed.
*
* @param {Object} configParams - Configuration parameters.
* @returns {number} - Validated motion reduce multiplier value.
* @see validateParams.js
*/
export function validateReduceMultiplier(configParams) {
const inRange =
configParams.reduceMultiplier > 0.1 && configParams.reduceMultiplier < 0.9;
if (inRange) {
return configParams.reduceMultiplier;
} else {
logWarn(
`Input ${configParams.reduceMultiplier} is an invalid motion reduce multiplier value. Use a number value between 0.1 and 0.9! Fallback value 0.5 will be used.`
);
return 0.5;
}
}