backpack-ui
Version:
Lonely Planet's Components
36 lines (30 loc) • 1.21 kB
JavaScript
/* eslint-disable */
// https://github.com/souporserious/react-motion-ui-pack/blob/master/src/config-to-style.js
const TRANSFORM = require('./get-prefix')('transform')
const UNIT_TRANSFORMS = ['translateX', 'translateY', 'translateZ', 'transformPerspective']
const DEGREE_TRANFORMS = ['rotate', 'rotateX', 'rotateY', 'rotateZ', 'skewX', 'skewY', 'scaleZ']
const UNITLESS_TRANSFORMS = ['scale', 'scaleX', 'scaleY']
const TRANSFORMS = UNIT_TRANSFORMS.concat(DEGREE_TRANFORMS, UNITLESS_TRANSFORMS)
export default function (configs) {
let styles = {}
Object.keys(configs).map(key => {
const isTransform = (TRANSFORMS.indexOf(key) > -1)
const value = Math.round(+configs[key] * 10000) / 10000
if (isTransform) {
let transformProps = styles[TRANSFORM] || ''
if (UNIT_TRANSFORMS.indexOf(key) > -1) {
transformProps += `${key}(${value}px) `
}
else if (DEGREE_TRANFORMS.indexOf(key) > -1) {
transformProps += `${key}(${value}deg) `
}
else if (UNITLESS_TRANSFORMS.indexOf(key) > -1) {
transformProps += `${key}(${value}) `
}
styles[TRANSFORM] = transformProps
} else {
styles[key] = value
}
})
return styles
}