This repository was archived by the owner on Dec 15, 2018. It is now read-only.
ReactCSSTransitionGroup support for route transition animations #117
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It would be great to be able to perform route transition animations using
ReactCSSTransitionGroup.Especially for hybrid mobile apps, animated route transitions are an important feature for a router. Some examples:
Initial attempt
This doesn't seem to work.
Rendered output
Here's the rendered output for the
/first-pageroute (the<span>elements are rendered byReactCSSTransitionGroup):and for the
/second-pageroute:The problem
I think the
ReactCSSTransitionGroupfails to animate itsRelativeFragmentchildren on route changes because theRelativeFragmentchildren are not actually being added or removed; they're just changing their rendered output.When the
Fragmentcomponent rendersnullfor non-matching routes, I believe theRelativeFragmentcomponent gets rendered as aReactDOMEmptyComponent(notice thereact-emptyelements above).For
ReactCSSTransitionGroupanimations to work, I believe its child fragments must actually be removed for non-matching routes (rather than being replaced byReactDOMEmptyComponents). I don't think this is possible with the current functionality of Redux Little Router.A possible solution
This pull request is an experiment to make it possible to use
ReactCSSTransitionGroup.My idea is to use a new component which sits in-between the low-level
Fragmentand the high-levelAbsoluteFragment/RelativeFragment. I've called it theWrappedFragment. It does the following:Fragment(which will either benullor a React element).Wrappercomponent (which can be aReactCSSTransitionGroupor any other suitable component). This is what enablesReactCSSTransitionGroupto work correctly. When the rendered output fromFragmentbecomesnull,ReactCSSTransitionGroupwill correctly perform the "leave" animation. See the React docs: "Animating One or Zero Items".The user-specified
Wrappercomponent is supplied via an optionalwrapperprop. If the user doesn't supply that prop, then by default theWrappedFragmentcomponent will simply render theFragmentwithout any wrapping (just as if it had been rendered directly byAbsoluteFragmentorRelativeFragment).Example usage
First we create our
Wrappercomponent:Then we pass our
Wrappercomponent in thewrapperprop of theRelativeFragments:Example rendered output
Here's the rendered output for the
/first-pageroute (again, the<span>elements are rendered byReactCSSTransitionGroup):and during a transition from
/first-pageto/second-page:and once the transition has completed:
You can see that
ReactCSSTransitionGroupcorrectly applies theexample-leave example-leave-activeCSS classes to the outgoing fragment, and theexample-enter example-enter-activeclasses to the incoming fragment. 🎉Feedback/discussion appreciated!
This is a non-breaking (backwards-compatible) change to Redux Little Router's API (the new
wrapperprop onAbsoluteFragmentandRelativeFragmentis optional).This may not be the best way to make
ReactCSSTransitionGroupwork with Redux Little Router, and so I haven't updated the README or Mocha tests yet, but I wanted to submit this pull request as a starting point for the discussion.Some questions:
ReactCSSTransitionGroupwith Redux Little Router's current API. Does anyone know if this is possible? If so, then this pull request may not be necessary :)ReactCSSTransitionGroupin which a user might want to wrap a fragment's contents in a custom component. Does this seem reasonable? If not, should the "wrapper" functionality be renamed to something more specific like "transition"?Many thanks to @tptee and everyone else for creating such a great router ☀️