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

Usage with react-router v4 #2

Closed
mjsisley opened this issue Mar 25, 2018 · 9 comments
Closed

Usage with react-router v4 #2

mjsisley opened this issue Mar 25, 2018 · 9 comments

Comments

@mjsisley
Copy link
Contributor

This library looks awesome!

Has this been tested with react-router v4 for route based transitions? It would be awesome if the docs included an example for this use case!

@drcmda
Copy link
Member

drcmda commented Mar 25, 2018

@mjsisley hey, thanks a lot! For sure it will work with React-router. I will make an example for it. Anything you look for specifically or just a plain transition between routes?

@drcmda
Copy link
Member

drcmda commented Mar 25, 2018

Happy with that one or need more?

https://codesandbox.io/embed/xo0lrqw2nz

@dylinmaust
Copy link

Thanks for the example! I'm looking forward to implementing this as I prefer it to Animated's imperative approach.

I realize Issues shouldn't be used for support questions, but it seems relevant to this particular issue. I'm trying to create an AnimatedSwitch component that I can re-use across multiple "Layouts" in my app. I'm not sure how to pass the style prop to the children prop. Is this is a valid approach?

import { Switch, withRouter } from 'react-router-dom';
import { Transition, config } from 'react-spring';

const AnimatedSwitch = ({ children, location }) => (
  <div className="content">
    <Transition
      config={config.slow}
      keys={location.pathname}
      from={{ opacity: 0, transform: 'scale3d(0.5,0.5,0.5)' }}
      enter={{ opacity: 1, transform: 'scale3d(1,1,1)' }}
      leave={{ opacity: 0, transform: 'scale3d(0.5,0.5,0.5)' }}
    >
      {style => (
        <Switch location={location}>
          {children}
        </Switch>
      )}
    </Transition>
  </div>
);

AnimatedSwitch.propTypes = {
  location: PropTypes.shape({
    pathname: PropTypes.string.isRequired,
  }).isRequired,
  children: PropTypes.node.isRequired,
};

export default withRouter(AnimatedSwitch);

Usage would look something like:

const Public = () => (
  <div>
	<AnimatedSwitch>
	  <Route path="/auth/login" render={props => Login({...props, style })} />
	  <Route path="/auth/signup" render={props => Signup({...props, style })} />
	</AnimatedSwitch>
  </div>
);

@drcmda
Copy link
Member

drcmda commented Mar 25, 2018

Passing props down like that is hard with react-router. There's been this issue: remix-run/react-router#4105 (comment) but for your use-case none of the examples would would, not even cloning. I've struggled with similar problems in the past. The only way i see would be allowing AnimatedSwitch itself to call a render child into which it can pass "styles", but that wouldn't be any different from how it is right now.

I'd probably leave Transition as it is, use it whenever it has to be used, but perhaps inject its props so that it becomes short to use, like so:

const props = {
    config: config.slow,
    from: { opacity: 0, transform: 'scale3d(0.5,0.5,0.5)' },
    enter: { opacity: 1, transform: 'scale3d(1,1,1)' },
    leave: { opacity: 0, transform: 'scale3d(0.5,0.5,0.5)' },
}

<Transition {...props} keys={location.pathname}>
    {styles => (
        <Switch location={location}>
            <Route path="/auth/login" render={props => Login({...props, style })} />
	    <Route path="/auth/signup" render={props => Signup({...props, style })} />
        </Switch>
    )}
</Transition>

@mjsisley
Copy link
Contributor Author

@drcmda Thanks a bunch for adding these examples! They cover the use cases I had in mind.

@drcmda drcmda closed this as completed Apr 6, 2018
@A7DC
Copy link

A7DC commented Apr 19, 2018

Hi, will this also work for react-router v3?

@drcmda
Copy link
Member

drcmda commented Apr 19, 2018

The spring is pretty forward in what it does, it simply moves props (or styles) from here to there. It should work with any react component.

@jgoux
Copy link

jgoux commented May 31, 2018

@dylinmaust @drcmda I implemented <AnimatedSwitch /> like this :

const AnimatedSwitch = withRouter(_AnimatedSwitch)

function _AnimatedSwitch({ location, children, ...transitionProps }) {
  return (
    <Transition native keys={location.pathname} {...transitionProps}>
      {style => (
        <Switch>
          {React.Children.map(children, child => {
            const { component: Component, ...childProps } = child.props
            const AnimatedComponent = animated(Component)
            return React.createElement(child.type, {
              ...childProps,
              render: props => <AnimatedComponent {...{ ...props, style }} />
            })
          })}
        </Switch>
      )}
    </Transition>
  )
}

Usage :

<AnimatedSwitch
  from={{ transform: 'translateY(100px)', opacity: 0 }}
  enter={{ transform: 'translateY(0px)', opacity: 1 }}
  leave={{ transform: 'translateY(100px)', opacity: 0 }}
>
  <Route
    exact
    path="/page-1"
    component={Page1}
  />
  <Route exact path="/page-2" component={Page2} />
</AnimatedSwitch>

It seems to work so far. Do you see anything wrong?

@mupkoo
Copy link

mupkoo commented Mar 29, 2019

Doing a search for reach router points to here as well, so I will leave a comment with a possible solution

import React from 'react';
import { Location, Router } from '@reach/router';
import { Transition } from 'react-spring/renderprops';
import styled from 'styled-components';

const StyledRouter = styled(Router)`
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
`;

function FadeInRouter({ children }) {
  return (
    <Location>
      {({ location }) => (
        <Transition
          items={location}
          keys={(location) => location.key}
          from={{ opacity: 0 }}
          enter={{ opacity: 1 }}
          leave={{ opacity: 0 }}
        >
          {(location) => (props) => (
            <StyledRouter location={location} style={props}>
              {children}
            </StyledRouter>
          )}
        </Transition>
      )}
    </Location>
  );
}

export default FadeInRouter;

and later you can use this router

<FadeInRouter>
  <ContactUs path="contact-us" />
  <About path="about />
</FadeInRouter>

szjemljanoj pushed a commit to szjemljanoj/react-spring that referenced this issue Mar 29, 2019
imgbot bot pushed a commit to Moussa-Kalam/react-spring that referenced this issue Apr 7, 2024
*Total -- 31,135.93kb -> 20,686.13kb (33.56%)

/demo/src/sandboxes/wordle/thumbnail.png -- 44.81kb -> 5.11kb (88.6%)
/demo/src/sandboxes/smile-grid/thumbnail.png -- 114.00kb -> 19.07kb (83.28%)
/demo/src/sandboxes/css-variables/thumbnail.png -- 71.26kb -> 14.28kb (79.97%)
/demo/src/sandboxes/css-keyframes/thumbnail.png -- 45.24kb -> 12.32kb (72.77%)
/demo/src/sandboxes/parallax/thumbnail.png -- 538.96kb -> 161.65kb (70.01%)
/demo/src/sandboxes/list-reordering/thumbnail.png -- 112.72kb -> 33.93kb (69.89%)
/demo/src/sandboxes/draggable-list/thumbnail.png -- 112.72kb -> 33.93kb (69.89%)
/demo/src/sandboxes/animating-auto/thumbnail.png -- 33.91kb -> 10.33kb (69.54%)
/demo/src/sandboxes/css-gradients/thumbnail.png -- 2,359.90kb -> 721.76kb (69.42%)
/cypress/snapshots/All Integration Specs/horizontal pmndrs#2.snap.png -- 15.97kb -> 5.07kb (68.29%)
/cypress/snapshots/All Integration Specs/horizontal pmndrs#3.snap.png -- 15.97kb -> 5.07kb (68.29%)
/cypress/snapshots/All Integration Specs/vertical pmndrs#2.snap.png -- 15.97kb -> 5.07kb (68.29%)
/demo/src/sandboxes/noise/thumbnail.png -- 1,751.61kb -> 556.77kb (68.21%)
/demo/src/sandboxes/chain/thumbnail.png -- 33.61kb -> 10.70kb (68.18%)
/demo/src/sandboxes/popup-modal/thumbnail.png -- 84.03kb -> 28.13kb (66.52%)
/demo/src/sandboxes/trail/thumbnail.png -- 76.91kb -> 25.77kb (66.5%)
/demo/src/sandboxes/svg-filter/thumbnail.png -- 42.20kb -> 14.20kb (66.34%)
/assets/projects/next.png -- 23.57kb -> 8.46kb (64.1%)
/cypress/snapshots/All Integration Specs/horizontal #1.snap.png -- 16.51kb -> 6.33kb (61.68%)
/cypress/snapshots/All Integration Specs/vertical #1.snap.png -- 16.51kb -> 6.33kb (61.68%)
/assets/projects/aragon.png -- 27.59kb -> 10.81kb (60.82%)
/demo/src/sandboxes/parallax-sticky/thumbnail.png -- 107.66kb -> 43.11kb (59.95%)
/demo/src/sandboxes/macos-dock/thumbnail.png -- 177.95kb -> 73.71kb (58.58%)
/demo/src/sandboxes/simple-transition/thumbnail.png -- 166.75kb -> 79.62kb (52.25%)
/demo/src/sandboxes/scrolling-wave/thumbnail.png -- 119.52kb -> 57.53kb (51.86%)
/assets/projects/csb.png -- 30.14kb -> 14.70kb (51.22%)
/demo/src/sandboxes/rocket-decay/thumbnail.png -- 34.78kb -> 17.70kb (49.11%)
/cypress/snapshots/All Integration Specs/vertical pmndrs#3.snap.png -- 16.00kb -> 8.24kb (48.52%)
/demo/src/sandboxes/springy-boxes/thumbnail.png -- 570.88kb -> 295.11kb (48.31%)
/demo/src/sandboxes/notification-hub/thumbnail.png -- 80.27kb -> 42.60kb (46.92%)
/demo/src/sandboxes/multistage-transition/thumbnail.png -- 62.53kb -> 33.83kb (45.9%)
/demo/src/sandboxes/slide/thumbnail.png -- 83.89kb -> 46.47kb (44.6%)
/demo/src/sandboxes/webgl-switch/thumbnail.png -- 345.12kb -> 192.32kb (44.27%)
/demo/src/sandboxes/floating-button/thumbnail.png -- 3,141.47kb -> 1,764.19kb (43.84%)
/demo/src/sandboxes/tree/thumbnail.png -- 47.16kb -> 26.87kb (43.03%)
/demo/src/sandboxes/parallax-vert/thumbnail.png -- 42.17kb -> 24.33kb (42.32%)
/demo/src/sandboxes/goo-blobs/thumbnail.png -- 110.36kb -> 70.29kb (36.31%)
/demo/src/sandboxes/cards-stack/thumbnail.png -- 694.04kb -> 485.62kb (30.03%)
/demo/src/sandboxes/card/thumbnail.png -- 266.36kb -> 192.83kb (27.61%)
/demo/src/sandboxes/masonry/thumbnail.png -- 1,548.59kb -> 1,181.22kb (23.72%)
/demo/src/sandboxes/image-fade/thumbnail.png -- 577.86kb -> 442.00kb (23.51%)
/demo/src/sandboxes/flip-card/thumbnail.png -- 577.86kb -> 442.00kb (23.51%)
/demo/src/sandboxes/viewpager/thumbnail.png -- 3,348.35kb -> 2,568.20kb (23.3%)
/demo/src/sandboxes/exit-before-enter/thumbnail.png -- 3,193.60kb -> 2,454.14kb (23.15%)
/docs/public/images/quotee/arzafran.png -- 57.51kb -> 46.26kb (19.56%)
/docs/public/spring-icon.png -- 121.24kb -> 98.10kb (19.09%)
/docs/public/share.jpg -- 303.25kb -> 246.24kb (18.8%)
/demo/public/bright-rain.png -- 4,843.07kb -> 3,988.58kb (17.64%)
/demo/src/sandboxes/floating-button/public/bright-rain.png -- 4,843.07kb -> 3,988.58kb (17.64%)
/demo/public/cross.jpg -- 12.42kb -> 10.65kb (14.27%)
/demo/src/sandboxes/webgl-switch/public/cross.jpg -- 12.42kb -> 10.65kb (14.27%)
/.github/publish-ci/vite/public/vite.svg -- 1.46kb -> 1.42kb (2.81%)
/demo/favicon.svg -- 0.39kb -> 0.38kb (2.48%)
/packages/parallax/test/src/favicon.svg -- 1.49kb -> 1.46kb (2.17%)
/docs/public/icon-link.svg -- 0.47kb -> 0.46kb (1.86%)
/docs/public/favicon-32x32.png -- 2.46kb -> 2.44kb (0.71%)
/docs/public/images/quotee/bruno_lemos.jpeg -- 21.33kb -> 21.19kb (0.65%)
/docs/public/images/quotee/alexander_prinzhorn.jpeg -- 15.88kb -> 15.82kb (0.42%)
/.github/publish-ci/next/public/logo.svg -- 1.10kb -> 1.09kb (0.09%)
/.github/publish-ci/vite/src/logo.svg -- 1.10kb -> 1.09kb (0.09%)

Signed-off-by: ImgBotApp <ImgBotHelp@gmail.com>
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

6 participants