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

[1.0] build example site with fancy page transitions #925

Closed
KyleAMathews opened this issue May 5, 2017 · 26 comments
Closed

[1.0] build example site with fancy page transitions #925

KyleAMathews opened this issue May 5, 2017 · 26 comments

Comments

@KyleAMathews
Copy link
Contributor

Something like this would be 💯

https://codepen.io/sdras/full/gWWQgb/?utm_source=reactnl&utm_medium=email

@anthonysapp
Copy link

I'm actually working on implementing something with
https://github.com/reactjs/react-transition-group#low-level-api-transitiongroup

Any ideas on how to do that?
I've tried a couple of approaches so far in fiddling around:

  1. Something like this in the default layout, in the render method:
<TransitionGroup component="div" className="page">
    {children}
</TransitionGroup>
  1. Wrapping the root component in using the gatsby-browser.js wrapRootComponent lifecycle hook.

For both, the pages render fine, but I haven't been able to get TransitionGroup's lifecycle hooks to work.

@KyleAMathews
Copy link
Contributor Author

I'm not really sure how TransitionGroup works but I'd love to get things working such that there could be plugins which auto add transition effects (perhaps have 5-6 diff options) to a site or to individual pages.

@KyleAMathews
Copy link
Contributor Author

Also this will effect things #940

@KyleAMathews
Copy link
Contributor Author

*affect

@anthonysapp
Copy link

Ah crap. I for some reason thought Gatsby had already changed to RR V4. That's probably part of the problem :)

I will keep plugging away and report back findings. The TransitionGroup functionality seems to have changed in a recent update to React, and it's been moved to a new repo with new maintainers, so I'm sure that's also part of my misunderstanding.

@KyleAMathews
Copy link
Contributor Author

KyleAMathews commented May 9, 2017 via email

@anthonysapp
Copy link

@KyleAMathews so after some more digging I've definitely figured out what needs to be done to implement this type of transition at the page level. I think an elegant way to do this would be to wrap the component for each page you want to transition in a TransitionGroup component. I'm having a crack at doing this using a HOC at the moment.

@anthonysapp
Copy link

anthonysapp commented May 12, 2017

After reading the plan for #940 , I think it's best to wait to do any further implementation of this. I did figure it out, though. I'll have a crack at explaining it, so perhaps it will help in some way with the changes that are coming.

What I ultimately found is that I needed to interact with the components that are generated for each page in the .cache/child-routes.js file. SInce it is those components that are ultimately rendered as the children of your layout eg: src/layouts/default.js. This was obviously a bit problematic - maybe due to something I'm missing. What I ended up doing in my default.js layout is wrapping the rendered child component in a <TransitionGroup> component:

<div className="page-container">
    <TransitionGroup>
        <children.type {...this.props} key={this.props.location.pathname} />
    </TransitionGroup>
</div>

I know that <children.type/> is the component Gatsby derives, and the default.js layout component doesn't get removed from the DOM, so it seems to be the place you want to control page transitions from (unless the layout changes, I suppose).

So the problem from there was, TransitionGroup calls lifecycle methods in the component it's wrapping... so it's going to try to call them in the derived component in child-routes.js, NOT in my actual template component for each page. So from there, I wrote a little utility to map the lifecycle methods up the chain to the page template component, but it seems very hack-ish to me.

Helper script src/transitions/bindTransitions.js

const lifeCycleHooks = [
  "componentWillAppear",
  "componentWillEnter",
  "componentWillLeave",
  "componentDidAppear",
  "componentDidEnter",
  "componentDidLeave"
];

const bindTransitions = instance => {
  const parentPrototype = instance.props.children.type.prototype;
  lifeCycleHooks.forEach(methodName => {
    if (instance[methodName] !== undefined) {
      instance[methodName] = instance[methodName].bind(instance);
      parentPrototype[methodName] = instance[methodName];
    }
  });
};
export default bindTransitions;

Then, in src/templates/page-template.js:

import { bindTransitions } from "../transitions";
class PageTemplate extends React.Component {
  constructor(props) {
    super(props);
    
    // call helper script to map TransitionGroup lifecycle hooks
    bindTransitions(this);
  }

  componentWillAppear(cb) {
    this.animateIn(cb);
  }

  componentWillEnter(cb) {
    this.animateIn(cb);
  }

  componentWillLeave(cb) {
    this.animateOut(cb);
  }

  animateIn(cb){
      // do animation and trigger cb when done
      cb()
  }
   animateOut(cb){
      // do animation and trigger cb when done
      cb()
  }

  render(){
      ...
  }
}

TransitionGroup now works as expected, with the ability to map its lifecycle hooks to any page template component, and do custom animations with EG gsap tweening engine.

Hopefully that makes some kind of sense :)

@anthonysapp
Copy link

In messing around with this some more this morning, I realized this method doesn't work when doing a production build... I still think the above post might be useful though, hopefully.

@KyleAMathews
Copy link
Contributor Author

#940 is merged now btw. Since this.props.children is a function now for layouts, perhaps that can be leveraged for this?

@anthonysapp
Copy link

Cool, I will have a look.

@anthonysapp
Copy link

anthonysapp commented May 16, 2017

So much better! I was able to get something workable going.
One remaining issue in my work in progress: on a production build, if I load the site on any route that isn't the main "/" route I get this error:

screen shot 2017-05-16 at 4 12 08 pm

If I go to the root route first, everything works fine.

Seems like it might have something to do with code splitting not loading a required file, but I'm not 100% sure.

@0x80
Copy link
Contributor

0x80 commented May 16, 2017

I've been experiencing this issue too, but hadn't investigated enough to report it yet. Not sure how long has been there but I suspect since the router v4 update.

@KyleAMathews Should we maybe make a separate issue for it? Or do you have this on your radar already?

@KyleAMathews
Copy link
Contributor Author

KyleAMathews commented May 16, 2017 via email

@0x80
Copy link
Contributor

0x80 commented May 16, 2017

@KyleAMathews Yes

@anthonysapp
Copy link

Yes, at layouts/index.js

@KyleAMathews
Copy link
Contributor Author

KyleAMathews commented May 16, 2017 via email

@anthonysapp
Copy link

@KyleAMathews of course, apologies for being vague. I will have a closer look tomorrow and report back. Worth opening a new issue?

@KyleAMathews
Copy link
Contributor Author

Yeah sounds like it

@0x80
Copy link
Contributor

0x80 commented May 17, 2017

#981

@jbolda jbolda added the v1 label Jun 3, 2017
@muhajirdev
Copy link
Contributor

Any progress on this? is it possible now?

@yunda
Copy link

yunda commented Aug 20, 2017

☝️

@sebastienfi
Copy link
Contributor

https://github.com/gatsbyjs/gatsby/tree/master/examples/using-page-transitions

@KyleAMathews
Copy link
Contributor Author

Per @sebastienfi comment, we've got solid support now for page transitions — would love a really fancy example site too someday but this will suffice for now.

@thebarty
Copy link

thebarty commented Apr 9, 2018

Hi guys,

is anyone using scroll-to-anchor animations?

I am having weird rendering-issues with https://www.npmjs.com/package/react-anchor-link-smooth-scroll

@mvasin
Copy link

mvasin commented Apr 17, 2018

Did somebody use react-spring / react-motion / pose for more sophisticated transitions between pages? Are there any examples on integration gatsby with those libraries?

Ideally I'd morph one page into another like this.

Transition-In is no-brainer, but I'm not sure how can I transition-Out or cross-fade.

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

No branches or pull requests

9 participants