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

Question about giving the layout component information about the page component for per page transitions. #8659

Closed
TylerBarnes opened this issue Oct 1, 2018 · 2 comments
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@TylerBarnes
Copy link
Contributor

TylerBarnes commented Oct 1, 2018

Summary

I spent some time a couple weeks ago figuring out how to get a different page transition for each page (as specified by the page component) and I came up with what seems to me to be a reliable enough solution. It feels hacky though and I'm wondering if there's a better way to do this in gatsby.

I'm using gatsby-plugin-layout to create my own layout component that doesn't unmount. I'm then adding default props to my page components with a transition component as a prop. The layout component then checks the props of the page component since it has access to it and it wraps the page component in the specified transition component.

example site
example repo

In my page component:

import TransitionElement from './transitions';

const PageName = () => (
    <div><h1>This is my page</h1></div>
);

PageName.defaultProps = {
  transitionComponent: TransitionElement
};

export default PageName;

In my layout component:

export const WrapPageComponent = ({ element, props }) => {
  const { key } = props.location;
  const { transitionComponent: TransitionComponent } = element.props;

  return (
      <PoseGroup>
        <TransitionComponent key={key} {...props}>
          {element}
        </TransitionComponent>
      </PoseGroup>
  );
};

Relevant information

This seems to work well because the layout component can just check the props of the page component but I have a feeling this is really hacky. Is there a better way for the layout component to know about the page component?
I thought maybe I could use the context api for this but I couldn't get it to actually work.

@TylerBarnes
Copy link
Contributor Author

This could be the foundation for a gatsby transition component plugin #5213 if for example the page component could just have a named export that the layout component could pick up.

export default PageComponent;
export { TransitionComponent };

@kakadiadarpan kakadiadarpan added the type: question or discussion Issue discussing or asking a question about Gatsby label Oct 1, 2018
@kakadiadarpan
Copy link
Contributor

@TylerBarnes let's continue this conversation in #5213

Closed in favor of #5213

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

2 participants