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

Router: Fix addition and removal of empty classnames #67378

Merged
merged 2 commits into from
Nov 28, 2024

Conversation

tyxla
Copy link
Member

@tyxla tyxla commented Nov 28, 2024

What?

Fixes an error that occurs when navigating between different blocks in global styles in the site editor.

Why?

Just fixing an error, seems like it was introduced in #67199.

Found while late testing #67199.

How?

We're making sure that before adding or removing a classname during router navigation, there's actually one to be added or removed.

Testing Instructions

  • Open /wp-admin/site-editor.php?p=%2Fstyles&section=%2Fblocks
  • Navigate to a few blocks.
  • Verify you no longer see the error.

Testing Instructions for Keyboard

Same

Screenshots or screencast

Screenshot 2024-11-28 at 11 08 33

@tyxla tyxla added [Type] Bug An existing feature does not function as intended [Package] Edit Site /packages/edit-site [Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") labels Nov 28, 2024
@tyxla tyxla self-assigned this Nov 28, 2024
Copy link

github-actions bot commented Nov 28, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: tyxla <tyxla@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@@ -129,13 +129,17 @@ export function useHistory() {

await new Promise< void >( ( resolve ) => {
const classname = options.transition ?? '';
document.documentElement.classList.add( classname );
if ( classname ) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ! options.transition check above that is meant to prevent using a transition entirely no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a ! options.transition check above

But that check doesn't prevent the transition from running! It's:

if ( ! options.transition ) {
  performPush();
}

await new Promise( ... );

There is missing return after performPush(). That's probably the actual bug!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, nice find @jsnajdr. Updated in bb9b71f

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I swear in my mind, there was an early return haha :)

@tyxla tyxla requested a review from youknowriad November 28, 2024 10:12
Copy link

Flaky tests detected in bb9b71f.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12066792436
📝 Reported issues:

Copy link
Member

@jsnajdr jsnajdr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a perfect fix :shipit:

@@ -125,6 +125,7 @@ export function useHistory() {
! options.transition
) {
performPush();
return;
}

await new Promise< void >( ( resolve ) => {
Copy link
Contributor

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 here

Copy link
Member Author

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 be undefined and classList.add() can't work with undefined. Happy to keep it.

Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Member

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 to undefined because the options.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 final transition.finished.finally call. Everything before it is sync.

@tyxla tyxla merged commit c845cda into trunk Nov 28, 2024
69 of 72 checks passed
@tyxla tyxla deleted the fix/router-history-empty-classname branch November 28, 2024 12:17
@github-actions github-actions bot added this to the Gutenberg 19.9 milestone Nov 28, 2024
im3dabasia pushed a commit to im3dabasia/gutenberg that referenced this pull request Dec 4, 2024
* Router: Fix addition and removal of empty classnames

* Actually prevent transition

Co-authored-by: tyxla <tyxla@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>
michalczaplinski pushed a commit that referenced this pull request Dec 5, 2024
* Router: Fix addition and removal of empty classnames

* Actually prevent transition

Co-authored-by: tyxla <tyxla@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") [Package] Edit Site /packages/edit-site [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants