简体   繁体   English

react-transition-group中的退出延迟动画

[英]Exit delay animation in react-transition-group

I am using react-transition-group in a ReactJS and GatsbyJS (V2) project. 我在ReactJSGatsbyJS (V2)项目中使用react-transition-group

I have my page transitions working with animation but when Link exit, the exiting animation is cut short because the next page is ready for entering. 我的页面过渡与动画一起使用,但是当退出Link ,由于准备好进入下一页, exiting动画被缩短。

I have tried delaying the Link action but whilst the page change was delayed, the exit animation is not triggered until the delay was over and the Link was actioned. 我尝试延迟“ Link操作,但是尽管页面更改被延迟,但直到delay结束并且Link已执行,才触发exit动画。

How can I delay the page change, whilst initiating the exiting animation onClick ? 在启动exiting动画onClick ,如何delay页面更改? Alternatively, is there a better way or props available? 或者,是否有更好的方法或props可用?

Here is my code 这是我的代码

Layout.js Layout.js

class Layout extends React.Component {
  ...
  return (
    <Transition>{children}</Transition>
  );
}

Transition.js Transition.js

class Transition extends React.Component {
constructor(props) {
    super(props);
    this.state = { exiting: false };
    this.listenerHandler = this.listenerHandler.bind(this);
  }

  listenerHandler() {
    this.setState({ exiting: true });
  }

  componentDidMount() {
    window.addEventListener(historyExitingEventType, this.listenerHandler);
  }

  componentWillUnmount() {
    window.removeEventListener(historyExitingEventType, this.listenerHandler);
  }

  static getDerivedStateFromProps({ exiting }) {
    if (exiting) {
      return { exiting: false };
    }
    return null;
  }

  render() {
    const transitionProps = {
      timeout: {
        enter: 0,
        exit: timeout
      },
      appear: true,
      in: !this.state.exiting
    };

    return (
      <ReactTransition {...transitionProps}>
        {status => (
          <div
            style={{
              ...getTransitionStyle({ status, timeout })
            }}
          >
            {this.props.children}
          </div>
        )}
      </ReactTransition>
    );
  }
}

export default Transition;

gatsby-config.js gatsby-config.js

import createHistory from 'history/createBrowserHistory';

const timeout = 1500;
const historyExitingEventType = `history::exiting`;

const getUserConfirmation = (pathname, callback) => {
  const event = new CustomEvent(historyExitingEventType, {
    detail: { pathname }
  });
  window.dispatchEvent(event);
  setTimeout(() => {
    callback(true);
  }, timeout);
};

let history;
if (typeof document !== 'undefined') {
  history = createHistory({ getUserConfirmation });
  history.block(location => location.pathname);
}

export const replaceHistory = () => history;

export { historyExitingEventType, timeout };

getTransitionStyle.js getTransitionStyle.js

const getTransitionStyles = timeout => {

return {
    entering: {
      transform: `scale(1.05) translateZ(0)`,
      opacity: 0
    },
    entered: {
      transition: `transform 750ms ease, opacity ${timeout}ms ease`,
      transitionDelay: `750ms`,
      transform: `scale(1) translateZ(0)`,
      opacity: 1
    },
    exiting: {
      transition: `transform 750ms ease, opacity ${timeout}ms ease`,
      transform: `scale(0.98) translateZ(0)`,
      opacity: 0
    }
  };
};

const getTransitionStyle = ({ timeout, status }) =>
  getTransitionStyles(timeout)[status];

export default getTransitionStyle;

Gatsby v2 is using Reach Router instead of React Router, so using getUserConfirmation with replaceHistory will not work anymore. Gatsby v2使用的是Reach Router而不是React Router,因此将getUserConfirmationreplaceHistory getUserConfirmation使用将不再起作用。 In the Gatsby v2 RC you can use react-pose to create page transitions a little more straightforward: 在Gatsby v2 RC中,您可以使用react-pose来创建页面转换,方法更为简单:

gatsby-browser.js and gatsby-ssr.js : gatsby-browser.jsgatsby-ssr.js

import React from "react"
import Transition from "./src/components/transition"
 export const wrapPageElement = ({ element, props }) => {
  return <Transition {...props}>{element}</Transition>
}

transition.js component: transition.js组件:

import React from "react"
import posed, { PoseGroup } from "react-pose"

const timeout = 250

class Transition extends React.PureComponent {
  render() {
    const { children, location } = this.props

    const RoutesContainer = posed.div({
      enter: { delay: timeout, delayChildren: timeout },
    })

    // To enable page transitions on mount / initial load,
    // use the prop `animateOnMount={true}` on `PoseGroup`.
    return (
      <PoseGroup>
        <RoutesContainer key={location.pathname}>{children}</RoutesContainer>
      </PoseGroup>
    )
  }
}

export default Transition

Inside your pages : 在您的页面内

// Use `posed.div` elements anywhere on the pages.

const Transition = posed.div({
  enter: {
    opacity: 1,
  },
  exit: {
    opacity: 0,
  },
})

// ...

<Transition>Hello World!</Transition>

Check the official example for a working demo. 查看官方示例以获取有效的演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 React 和 react-transition-group 卸载动画 - Animation on unmount with React and react-transition-group 根据用户交互更改react-transition-group退出动画? - Change react-transition-group exit animations depending on user interaction? 如何在反应过渡组中正确居中旋转轴 animation - How to center rotation axis properly in react-transition-group animation react-transition-group不与React Router一起使用 - react-transition-group not working with React Router 无法安装反应过渡组 - Can't install react-transition-group 使用 react-transition-group 可以吗? - Is it ok to use react-transition-group? 在组件中加载初始数据时,如何在react-transition-group中暂停动画? - How could you pause animation in react-transition-group while loading initial data in components? 如何使用 React-Transition-Group 显示和隐藏带有 animation 的模式/对话框? - How to use React-Transition-Group to show and hide modal/dialog with animation? 使用react-transition-group在转换之前元素值移位和更改 - Element values shifting and changing before transition with react-transition-group react-transition-group v2 和 react-router-dom v4。 为什么动画会同时更改内部和外部组件中的数据? - react-transition-group v2 and react-router-dom v4. Why animation change the data in both: inner and outer components?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM