Created
May 18, 2022 13:39
-
-
Save beefchimi/4e637744635a619e3f32aff4303ac62f to your computer and use it in GitHub Desktop.
Framer Motion Transform Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { TransformProperties } from 'framer-motion/types/motion/types'; | |
// These functions would not be necessary if `framer-motion` | |
// resets `transform` back to `none` when a `translate` value === `0`. | |
function hasScale(scale: TransformProperties['scale']) { | |
return scale !== 1 && scale !== undefined; | |
} | |
function hasTranslateX(translateX: TransformProperties['scale']) { | |
const isNumberZero = typeof translateX === 'number' && translateX === 0; | |
const isStringZero = typeof translateX === 'string' && translateX.startsWith('0'); | |
return !isNumberZero && !isStringZero && translateX !== undefined; | |
} | |
export function iconTransformTemplate({ scale, translateX }: TransformProperties) { | |
const scaleValue = hasScale(scale) ? `scale(${scale})` : null; | |
const xValue = hasTranslateX(translateX) ? `translateX(${translateX})` : null; | |
return scaleValue || xValue ? [scaleValue, xValue].join(' ') : 'none'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment