animate-components icon indicating copy to clipboard operation
animate-components copied to clipboard

Add prop to disappear to allow custom disappear func

Open th3fallen opened this issue 7 years ago • 4 comments

Disppear component is fantastic but it would be great if there was a way to call a function after the animation is complete.

I.e. i use this for a Notice and i need to trigger a redux action once it's closed to keep the store in sync.

th3fallen avatar Apr 24 '18 18:04 th3fallen

something like this may do it just fine... not sure since cloning this repo is... interesting

import PropTypes from 'prop-types';
import { fadeIn } from 'animate-keyframes';
import { getElementType, avoidNest } from 'element-utils';

export default class Disappear extends PureComponent {
  constructor(props: Object) {
    super(props);
  };

  static defaultProps = {
    name: fadeIn,
    duration: '2s',
    as: 'div',
    timingFunction: 'ease',
+    onDisappear: () => {}
  };

  static propTypes = {
    name: PropTypes.string,
    duration: PropTypes.string,
    as: PropTypes.string,
    timingFunction: PropTypes.string,
    component: PropTypes.func,
+    onDisappear: PropTypes.func
  };

  componentWillMount = () => {
    this.timeouts = null;
  };

  componentDidMount = () => {
    this.performAndDisapper(this.props);
  };

  componentWillUnmount = () => {
    clearTimeout(this.timeouts);
  };

  performAndDisapper = (props) => {
    const element = document.getElementById('animation-root');
    element.style = `animation: ${props.name} ${props.duration} ${props.timingFunction}`; // start on initial render
    element.addEventListener('animationend', () => {
      element.style =
        'visibility: \'hidden\'; opacity: 0; transition: visibility 0s 2s, opacity 2s linear;';
      this.timeouts = setTimeout(() => {
        element.remove();
+        props.onDisappear()
      }, 2000); // Sync with fadeOut
    });
  };

  render() {
    const { name, duration, children, as, timingFunction, component, ...rest } = this.props;
    const ElementType = getElementType(Disappear, this.props);
    const NormalizedComponent = avoidNest(ElementType, this.props, 'Disappear');
    const Wrapper = this.props.component;

    return (
      <NormalizedComponent id='animation-root' {...rest}>
        { Wrapper ? React.createElement(Wrapper, children) : children }
      </NormalizedComponent>
    );
  }
}

th3fallen avatar Apr 24 '18 19:04 th3fallen

Similar to what I was about to suggest. Can you create a PR for this ?

nitin42 avatar Apr 25 '18 18:04 nitin42

crap i had one but forgot to push it (facepalm)

th3fallen avatar Apr 25 '18 18:04 th3fallen

Haha no issues 😄

nitin42 avatar Apr 25 '18 18:04 nitin42