@atlaskit/motion
Version:
A set of utilities to apply motion in your application.
36 lines (35 loc) • 1.39 kB
JavaScript
import { convertToMs } from './convert-to-ms';
var cssTimePattern = /^-?(?:\d+\.?\d*|\.\d+)(?:ms|s)$/;
var toCssList = function toCssList(value) {
return value.split(',').map(function (item) {
return item.trim();
}).filter(Boolean);
};
var toTimeList = function toTimeList(value) {
return toCssList(value).filter(function (time) {
return cssTimePattern.test(time);
}).map(convertToMs);
};
/**
* Returns the longest total runtime from computed CSS animation name, duration, and delay lists.
* `animation-name` determines the number of animations. CSS repeats shorter duration and delay
* lists and ignores timing values that do not correspond to an animation name.
*/
export var getComputedAnimationDurationMs = function getComputedAnimationDurationMs(animationName, animationDuration, animationDelay) {
var names = toCssList(animationName);
var durations = toTimeList(animationDuration);
var delays = toTimeList(animationDelay);
if (names.length === 0 || durations.length === 0) {
return 0;
}
var longestDuration = 0;
for (var index = 0; index < names.length; index++) {
if (names[index] === 'none') {
continue;
}
var duration = durations[index % durations.length];
var delay = delays.length > 0 ? delays[index % delays.length] : 0;
longestDuration = Math.max(longestDuration, duration + delay);
}
return longestDuration;
};