-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Fix Navigable Toolbar initialIndex #51181
Conversation
If the initialIndex was undefined, the toolbar would set the tabindex=0 to the last item in the toolbar, which was an unexpected behavior.
Size Change: +2.4 kB (0%) Total Size: 1.39 MB
ℹ️ View Unchanged
|
I would like it to reset after interacting with a new block. |
Me too. That feels like a more expected behavior, and what it originally did. I can update this tomorrow. |
Flaky tests detected in 9089681. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5156640998
|
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.
LGTM
useEffect( () => { | ||
// Resets the index whenever the active block changes so this is not | ||
// persisted. See https://github.com/WordPress/gutenberg/pull/25760#issuecomment-717906169 | ||
initialToolbarItemIndexRef.current = undefined; | ||
}, [ clientId ] ); |
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.
If we're switching back to using useEffect
for this, do we still need the key
prop for the BlockContextualToolbar
?
See https://github.com/WordPress/gutenberg/pull/29573/files#r620982401
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 not sure why, but I can only get the behavior to work if this useEffect
and the key
prop are set. Without one of them, the index is persisted across block changes.
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.
Changing the key
prop on a React element basically tells it that it's a different component now, triggering the component to re-mount. So the BlockContextualToolbar
gets re-mounted when you change blocks.
And refs, when used like this, can prevent a component from re-rendering. So it prevents SelectedBlockPopover
from re-rendering when the selected toolbar index changes.
So I guess it makes sense to me that they're both needed when I think through it. You can't move the state inside BlockContextualToolbar
unless the shouldShowContextualToolbar
state moves inside it too because the BlockContextualToolbar
also gets remounted when the toolbar is hidden and then shown.
Maybe it would make sense to move that state inside to clear up some of the weirdness, but this PR is probably good enough for now to get the fix in.
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.
@ajlende and I worked on this refactor and decided to leave it as is, as the refactor was getting too involved.
* Store navigableToolbarRef so we have access on useEffect cleanup * Always set initialIndex to 0 if undefined If the initialIndex was undefined, the toolbar would set the tabindex=0 to the last item in the toolbar, which was an unexpected behavior. * Reset toolbar focus index on clientId change
What and Why
There is a bug in the
<NavigableToolbar />
used in the block popover that incorrectly sets the active toolbar item to the last item in the toolbar. To reproduce this issue:The expected and intended behavior is that the toolbar will always return you to the last item you were interacting with. So, if you were at 0, it should return you to the 0 index item. If you're at 6, it should return you to the 6 index item. If you haven't interacted with it at all, you should go to the first (0) item.
How?
There are two issues:
Testing Instructions
Screenshots or screencast
Before
After