How to do it...

Follow these steps to create a React pose animation:

  1. First, let's create our component structure:
import React, { Component } from 'react';
import posed from 'react-pose';
import styled from 'styled-components';
import './Animations.css';

class Animations extends Component {
render() {
return (
<div class="Animations">

</div>
);
}
}

export default Animations;
File: src/components/Animations/index.jsx
  1. The second thing we need to do is to create our first posed div with the states of our animation (normal and hover) and create a styled div using styled-components:
import React, { Component } from 'react';
import posed from 'react-pose';
import styled from 'styled-components';
import './Animations.css';

// Creating our posed div
const Circle = posed.div({
normal: {
scale: 1 // Normal state
},
hover: {
scale: 3 // Hover state
}
});

// Creating styled component
const StyledCircle = styled(Circle)`
color: white;
cursor: pointer;
background: blue;
line-height: 80px;
border-radius: 50%;
height: 80px;
width: 80px;
`;

class Animations extends Component {
render() {
return (
<div class="Animations">

</div>
);
}
}

export default Animations;
File: src/components/Animations/index.jsx
  1. Now we need to add our StyledCircle component into our render method:
      render() {
return (
<div class="Animations">
<StyledCircle
pose={this.state.hover ? 'hover' : 'normal'}
onMouseEnter={this.handleMouseEnter}
onMouseLeave={this.handleMouseLeave}
onClick={this.handleClick}
style={{ background: this.state.bg }}
>
Click me!
</StyledCircle>
</div>
);
}
File: src/components/Animations/index.jsx
  1. As you can see, we need to create some event methods, and we are going to use the local state to change the size of the circle and the color when the user clicks:
import React, { Component } from 'react';
import posed from 'react-pose';
import styled from 'styled-components';
import './Animations.css';

const Circle = posed.div({
normal: {
scale: 1 // Normal state
},
hover: {
scale: 3 // Hover state
}
});

// Creating styled component
const StyledCircle = styled(Circle)`
color: white;
cursor: pointer;
background: blue;
line-height: 80px;
border-radius: 50%;
height: 80px;
width: 80px;
`;

class Animations extends Component {
state = {
bg: 'blue',
hover: false
};

handleMouseEnter = () => {
this.setState({
hover: true
});
}

handleMouseLeave = () => {
this.setState({
hover: false
});
}

handleClick = () => {
// Choosing a random color...
const colors = ['red', 'green', 'gray', 'orange', 'black', 'pink'];

this.setState({
bg: colors[Math.floor(Math.random() * colors.length)]
});
}

render() {
return (
<div class="Animations">
<StyledCircle
pose={this.state.hover ? 'hover' : 'normal'}
onMouseEnter={this.handleMouseEnter}
onMouseLeave={this.handleMouseLeave}
onClick={this.handleClick}
style={{ background: this.state.bg }}
>
Click me!
</StyledCircle>
</div>
);
}
}

export default Animations;
File: src/components/Animations/index.jsx

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset