Reputation: 173
I am using a new library to create React Components, Styled-Components.
I want to apply an animation Tremble on my component via an onClick.
export class FormLoginTest extends React.Component { // eslint-disable-line react/prefer-stateless-function
static propTypes = {
isTrembling: React.PropTypes.bool
};
static defaultProps = {
isTrembling: true
};
onMakeTremble() {
alert("hello")
}
render() {
return (
<Form >
<ContainerFluid>
<H2>Login</H2>
</ContainerFluid>
<ContainerFluid>
<Label htmlFor="username">Username</Label>
<Input type="text" id="username" placeholder="bob" autoCorrect="off" autoCapitalize="off" spellCheck="false" />
</ContainerFluid>
<ContainerFluid>
<Label htmlFor="password">Password</Label>
<Input id="password" type="password" placeholder="••••••••••" />
</ContainerFluid>
<ContainerFluid>
<Button nature="success" onClick={this.onMakeTremble}>Hello</Button>
</ContainerFluid>
</Form>
);
}
}
So there is no Style.css sheet with Styled Components, all css is applied via javascript. Form has already been applied a css:
export class Form extends React.Component { // eslint-disable-line react/prefer-stateless-function
static propTypes = {
children: React.PropTypes.node.isRequired,
className: React.PropTypes.string
};
//
static defaultProps = {
isTrembling: true
};
render() {
return (
<form className={this.props.className}>
{React.Children.toArray(this.props.children)}
</form>
);
}
}
// eslint-disable-next-line no-class-assign
Form = styled(Form)`
max-width: 800px;
margin:0 auto;
display: block;
height: 100%;
border: 1px solid grey;
& h2{
text-align:center;
};
`;
export default Form;
And I have a component Tremble as well:
const shake = keyframes` 10%, 90% { transform: translate3d(-1px, 0, 0); }
20%, 80% {
transform: translate3d(2px, 0, 0);
}
30%, 50%, 70% {
transform: translate3d(-4px, 0, 0);
}
40%, 60% {
transform: translate3d(4px, 0, 0);
}
`;
const Tremble = styled.div`
display: inline-block;
&:hover {
animation: ${shake} 0.82s cubic-bezier(.36,.07,.19,.97) both;
transform: translate3d(0, 0, 0);
backface-visibility: hidden;
perspective: 1000px;
}
`;
export default Tremble;
Any clue as to how this may work?
Upvotes: 3
Views: 5501
Reputation: 1543
Check out the docs under the keyframes section. https://www.npmjs.com/package/styled-components
You might try importing keyframes from 'styled-components'
and use it like this:
Example
import styled, {keyframes} from 'styled-components';
const moveGradient = keyframes`
0%{background-position:38% 0%}
50%{background-position:63% 100%}
100%{background-position:38% 0%}
`;
const Wrapper = styled.div`
background-size: 800% 800%;
width: 100vw;
height: 100vh;
background: linear-gradient(${props => props.gradientRotation}, #cc6bbb, #ffa86d);
animation: ${moveGradient} 10s ease-out infinite;
`;
I am encountering a problem with keyframes myself, as this code doesnt work with my gradient animation, but it will with others.
I will link to my question/issue in a comment here later :)!
Upvotes: 2