Animation React to write a component, in order to enter and leave the assembly plus animation / excessive

problem

In the single-page applications, we often need to mount and switching or routing elements uninstall add transition effects, the introduction of third-party framework for such a small feature, it is a little tangled. As their own package.

Thinking

principle

When to enter opacity: 0 --> opacity: 1, when to exit opacity: 0 --> opacity: 1an example

When the element mount

  1. Mount dom element
  2. Animatingopacity: 0 --> opacity: 1

Elements unloaded

  1. Animatingopacity: 0 --> opacity: 1
  2. After unloading the animation dom

Component Design

In order to make assembly easy to use, low coupling, we expect the following ways to invoke components:

Property name Types of description
isShow Boolean Show or hide the child elements of control
name String Specify a name, enter animation animation exit

In App.jsxcalling component inside:

By changing the value to specify whether to display the isShow

// App.jsx
// 其他代码省略
import './app.css';

<Animation isShow={isShow} name='demo'>
    <div class='demo'>
        demo
    </div>
</Animation>
// 通过改变isShow的值来指定是否显示

In App.cssspecified in effect into the left:

// 基础样式
.demo {
    width: 200px;
    height: 200px;
    background-color: red;
}

// 定义进出入动画
.demo-showing {
    animation: show 0.5s forwards;
}
.demo-fading {
    animation: fade 0.5s forwards;
}

// 定义动画fade与show
@keyframes show {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes fade {
    from {
        opacity: 1;
    }
    to {
        opacity: 0;
    }
}

According to the idea to write code

// Animation.jsx
import { PureComponent } from 'react';
import './index.css';

class Animation extends PureComponent {
    constructor(props) {
        super(props);
        this.state = {
            isInnerShow: false,
            animationClass: '',
        };
    }

    componentWillReceiveProps(props) {
        const { isShow } = props;
        if (isShow) {
            // 显示
            this.show().then(() => {
                this.doShowAnimation();
            });
        } else {
            // 隐藏
            this.doFadeAnimation();
        }
    }

    handleAnimationEnd() {
        const isFading = this.state.animationClass === this.className('fading');
        if (isFading) {
            this.hide();
        }
    }

    show() {
        return new Promise(resolve => {
            this.setState(
                {
                    isInnerShow: true,
                },
                () => {
                    resolve();
                }
            );
        });
    }

    hide() {
        this.setState({
            isInnerShow: false,
        });
    }

    doShowAnimation() {
        this.setState({
            animationClass: this.className('showing'),
        });
    }

    doFadeAnimation() {
        this.setState({
            animationClass: this.className('fading'),
        });
    }

    /**
     * 获取className
     * @param {string} inner 'showing' | 'fading'
     */
    className(inner) {
        const { name } = this.props;
        if (!name) throw new Error('animation name must be assigned');
        return `${name}-${inner}`;
    }

    render() {
        let { children } = this.props;
        children = React.Children.only(children);
        const { isInnerShow, animationClass } = this.state;
        const element = {
            ...children,
            props: {
                ...children.props,
                className: `${children.props.className} ${animationClass}`,
                onAnimationEnd: this.handleAnimationEnd.bind(this),
            },
        };
        return isInnerShow && element;
    }
}

export default Animation;

Demo sample

I direct point

Guess you like

Origin www.cnblogs.com/looyulong/p/11071478.html