Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prop to disappear to allow custom disappear func #26

Open
th3fallen opened this issue Apr 24, 2018 · 4 comments · May be fixed by #28
Open

Add prop to disappear to allow custom disappear func #26

th3fallen opened this issue Apr 24, 2018 · 4 comments · May be fixed by #28

Comments

@th3fallen
Copy link
Contributor

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
Copy link
Contributor Author

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>
    );
  }
}

@nitin42
Copy link
Owner

nitin42 commented Apr 25, 2018

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

@th3fallen
Copy link
Contributor Author

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

@nitin42
Copy link
Owner

nitin42 commented Apr 25, 2018

Haha no issues 😄

@th3fallen th3fallen linked a pull request Apr 25, 2018 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants