motion
Version:
The Motion library for the web
83 lines (78 loc) • 2.73 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var array = require('../../../utils/array.cjs.js');
var noop = require('../../../utils/noop.cjs.js');
var data = require('../data.cjs.js');
/**
* A list of all transformable axes. We'll use this list to generated a version
* of each axes for each transform.
*/
const axes = ["", "X", "Y", "Z"];
/**
* An ordered array of each transformable value. By default, transform values
* will be sorted to this order.
*/
const order = ["translate", "scale", "rotate", "skew"];
const transformAlias = {
x: "translateX",
y: "translateY",
z: "translateZ",
};
const rotation = {
syntax: "<angle>",
initialValue: "0deg",
toDefaultUnit: (v) => v + "deg",
};
const baseTransformProperties = {
translate: {
syntax: "<length-percentage>",
initialValue: "0px",
toDefaultUnit: (v) => v + "px",
},
rotate: rotation,
scale: {
syntax: "<number>",
initialValue: 1,
toDefaultUnit: noop.noopReturn,
},
skew: rotation,
};
const transformPropertyDefinitions = new Map();
const asTransformCssVar = (name) => `--motion-${name}`;
/**
* Generate a list of every possible transform key
*/
const transforms = ["x", "y", "z"];
order.forEach((name) => {
axes.forEach((axis) => {
transforms.push(name + axis);
transformPropertyDefinitions.set(asTransformCssVar(name + axis), baseTransformProperties[name]);
});
});
/**
* A function to use with Array.sort to sort transform keys by their default order.
*/
const compareTransformOrder = (a, b) => transforms.indexOf(a) - transforms.indexOf(b);
/**
* Provide a quick way to check if a string is the name of a transform
*/
const transformLookup = new Set(transforms);
const isTransform = (name) => transformLookup.has(name);
const addTransformToElement = (element, name) => {
const { activeTransforms } = data.getAnimationData(element);
array.addUniqueItem(activeTransforms, name);
element.style.transform = buildTransformTemplate(activeTransforms);
};
const buildTransformTemplate = (activeTransforms) => activeTransforms
.sort(compareTransformOrder)
.reduce(transformListToString, "")
.trim();
const transformListToString = (template, name) => `${template} ${name}(var(${asTransformCssVar(name)}))`;
exports.addTransformToElement = addTransformToElement;
exports.asTransformCssVar = asTransformCssVar;
exports.axes = axes;
exports.buildTransformTemplate = buildTransformTemplate;
exports.compareTransformOrder = compareTransformOrder;
exports.isTransform = isTransform;
exports.transformAlias = transformAlias;
exports.transformPropertyDefinitions = transformPropertyDefinitions;