-
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
Router: Fix addition and removal of empty classnames #67378
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,13 +129,17 @@ export function useHistory() { | |
|
||
await new Promise< void >( ( resolve ) => { | ||
const classname = options.transition ?? ''; | ||
document.documentElement.classList.add( classname ); | ||
if ( classname ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's something I'm missing here. When can this happen? There's a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But that check doesn't prevent the transition from running! It's: if ( ! options.transition ) {
performPush();
}
await new Promise( ... ); There is missing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I swear in my mind, there was an early return haha :) |
||
document.documentElement.classList.add( classname ); | ||
} | ||
// @ts-expect-error | ||
const transition = document.startViewTransition( () => | ||
performPush() | ||
); | ||
transition.finished.finally( () => { | ||
document.documentElement.classList.remove( classname ); | ||
if ( classname ) { | ||
document.documentElement.classList.remove( classname ); | ||
} | ||
resolve(); | ||
} ); | ||
} ); | ||
|
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 we can probably remove the
?? ''
fallback hereThere 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 you added it mostly to satisfy types there. Without it, TS will not be happy because
classname
could beundefined
andclassList.add()
can't work withundefined
. Happy to keep it.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 thought the early return will tell typescript that the classname is not empty there anymore.
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.
It would have been the case if the calls were synchronous, but this is called in an async function.
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.
Currently TypeScript cannot narrow down the type from
string | undefined
toundefined
because theoptions.transition
usage is inside a nested function (the promise constructor). In such a case it's harder to guarantee in what order code is executed.But you could solve this by extracting most of the code out of the
new Promise
callback. The only part that needs to be async is the finaltransition.finished.finally
call. Everything before it is sync.