-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Trigger sidebar animations only on cross-route navigations #61402
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative fix would be to set the initial direction to
forward
, which would be a one-line change intrunk
. What would you think of this?I can't think of any example where setting the initial direction would break: because we'd always change the
routeKey
for different screens the sidebar will always be animated. We just needdirection
for determining how to animate it (right-to-left or the other way around).The closure created based on an initializator function for
useState
is a nice trick, though I find it less readable than the current code intrunk
. Unless I'm missing an use case, from a code maintenance POV I'd favor just setting the initial direction.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would trigger the
forward
animation also on initial load, and we don't want that. That's the reason whynull
, or "no animation" also needs to be one of the states. We setforward
orback
only on actual navigation, triggered by a click.I don't agree it's a trick, here we're simply creating a
NavState
object and storing it permanently in local state. It's a Gutenberg convention that we avoid "real" classes (new NavState()
) and instead prefercreate*
functions that store the object private data in local variables (a closure) and return an interface object.Maybe it's the
useState
without anysetState
that is confusing? An alternative way to write this is:This does the same thing, it only has the theoretical disadvantage that React is allowed to "forget" the memoized values when it feels it has too little memory or whatever.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that's true. Thanks, I had missed that.
I haven't come across this pattern until now. Of course, being such a big project it may be used somewhere and I just didn't come across it.
The essence of what we want to do is to only recompute the
wrapperCls
when therouteKey
actually changes. What if we are explicit about that? We could do that by memoizingwrapperCls
and making it dependant onrouteKey
, for example. Patch to be applied totrunk
:Would that work for you? Is there scenario where this approach would break?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I tried exactly this when working on this PR, and it works, but unfortunately it also triggers ESLint warnings:
The code I propose is basically an attempt to avoid these warnings:
wrapperCls
calculation to a child component that getsrouteKey
askey
, I achieve the "recompute whenrouteKey
changes" behavior without ever usingrouteKey
in a hook dependency array. Because that's always going to be a problem becauserouteKey
is not used inside the hook.navState
object and callingnavState.get()
inside the hook, I avoid having to add the actualnavState.direction
value as a hook dependency. I only add the constantnavState
one.So, I see your point, maybe I overcomplicated things just by trying to avoid
eslint-disable
. But I'm not sure if there is a better solution that satisfies all the constraints.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to merge the PR now as is. If we arrive at some actionable suggestion as how to improve the code (
navState
,routeKey
) I'm happy to do it as a followup.