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-page
route (the<span>
elements are rendered byReactCSSTransitionGroup
):and for the
/second-page
route:The problem
I think the
ReactCSSTransitionGroup
fails to animate itsRelativeFragment
children on route changes because theRelativeFragment
children are not actually being added or removed; they're just changing their rendered output.When the
Fragment
component rendersnull
for non-matching routes, I believe theRelativeFragment
component gets rendered as aReactDOMEmptyComponent
(notice thereact-empty
elements above).For
ReactCSSTransitionGroup
animations to work, I believe its child fragments must actually be removed for non-matching routes (rather than being replaced byReactDOMEmptyComponent
s). 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
Fragment
and the high-levelAbsoluteFragment
/RelativeFragment
. I've called it theWrappedFragment
. It does the following:Fragment
(which will either benull
or a React element).Wrapper
component (which can be aReactCSSTransitionGroup
or any other suitable component). This is what enablesReactCSSTransitionGroup
to work correctly. When the rendered output fromFragment
becomesnull
,ReactCSSTransitionGroup
will correctly perform the "leave" animation. See the React docs: "Animating One or Zero Items".The user-specified
Wrapper
component is supplied via an optionalwrapper
prop. If the user doesn't supply that prop, then by default theWrappedFragment
component will simply render theFragment
without any wrapping (just as if it had been rendered directly byAbsoluteFragment
orRelativeFragment
).Example usage
First we create our
Wrapper
component:Then we pass our
Wrapper
component in thewrapper
prop of theRelativeFragment
s:Example rendered output
Here's the rendered output for the
/first-page
route (again, the<span>
elements are rendered byReactCSSTransitionGroup
):and during a transition from
/first-page
to/second-page
:and once the transition has completed:
You can see that
ReactCSSTransitionGroup
correctly applies theexample-leave example-leave-active
CSS classes to the outgoing fragment, and theexample-enter example-enter-active
classes to the incoming fragment. 🎉Feedback/discussion appreciated!
This is a non-breaking (backwards-compatible) change to Redux Little Router's API (the new
wrapper
prop onAbsoluteFragment
andRelativeFragment
is optional).This may not be the best way to make
ReactCSSTransitionGroup
work 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:
ReactCSSTransitionGroup
with Redux Little Router's current API. Does anyone know if this is possible? If so, then this pull request may not be necessary :)ReactCSSTransitionGroup
in 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 ☀️