I have following code and I want to pass value of y
from react component to moveVertically
keyframe. Is it possible to do so ?
import React from 'react';
import styled, {keyframes} from 'styled-components';
const moveVertically = keyframes`
0% {
transform : translateY(0px)
}
100% {
transform : translateY(-1000px) //I need y here
}
`;
//I can access y in here via props but can't send it above
const BallAnimation = styled.g`
animation : ${moveVertically} ${props => props.time}s linear
`;
export default function CannonBall(props) {
const cannonBallStyle = {
fill: '#777',
stroke: '#444',
strokeWidth: '2px',
};
return (
<BallAnimation time = {4} y = {-1000}>
<circle cx = {0} cy = {0} r="25" style = {cannonBallStyle}/>
</BallAnimation>
);
}
You can make moveVertically a function. Please consider code below:
const moveVertically = (y) => keyframes`
0% {
transform : translateY(0px)
}
100% {
transform : translateY(${y}px)
}
`;
const BallAnimation = styled.g`
animation : ${props => moveVertically(props.y)} ${props => props.time}s linear
`;
Here you have y in props of BallAnimation. So you can extract it and pass it to moveVertically function which accepts y value as a parameter.