-
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
Save post button: avoid extra re-renders when enablng/disabling tooltip #56502
Conversation
Size Change: +87 B (0%) Total Size: 1.7 MB
ℹ️ View Unchanged
|
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.
Reviewing the diff with the "Hide whitespace" option enabled can be quite helpful here
variant="tertiary" | ||
icon={ isLargeViewport ? undefined : cloudUpload } | ||
// Make sure the aria-label has always a value, as the default `text` is undefined on small screens. | ||
label={ text || label } |
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.
the label
prop has two functions in button:
- it serves as an aria-label, if the aria-label prop isn't explicitly set
- it's used as tooltip text
Swapping label
for aria-label
allows us to keep the button correctly labelled, without triggering the internal Button
's tooltip.
The same string of text is also being passed explicitly to the Tooltip
component via the text
prop.
* We want the tooltip to show the keyboard shortcut only when the | ||
* button does something, i.e. when it's not disabled. | ||
*/ | ||
shortcut={ isDisabled ? undefined : displayShortcut.primary( 's' ) } |
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.
This prop is now passed to the Tooltip
component instead
I think that we could merge this PR anyway, while we discuss whether the approach in #56490 is viable. |
That's correct. A more general fix is being discussed in #56490, although I'm not sure how easy it's going to be to find a solid solution that doesn't introduce regressions. This PR offers a fix specifically to the Save button, and shows an alternative workaround to setting the
Since the button is currently working as expected thanks to having the Regardless, I think that it would be better for #44735 to become an actual GH issue, rather than a discussion, |
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.
Looks good code-wise and tests well 👍
I think we can merge this one and keep the #44735 discussion going separately.
} ) | ||
: undefined | ||
} | ||
onClick={ isDisabled ? undefined : () => savePost() } |
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.
Just noticed we can shorten this to:
onClick={ isDisabled ? undefined : () => savePost() } | |
onClick={ isDisabled ? undefined : savePost } |
What?
Fixes #44735
Alternative approach to #56490
This PR makes changes to how the tooltip is rendered for the post save button, thus fixing an issue that was causing loss of keyboard focus on that button.
Why?
This change is necessary to fix the issue described in #44735, where basically the element rendered by
Button
would be removed and re-added to the DOM any time theButton
component switches from having to show a tooltip to not having to show it, and vice-versa.The fact that a new HTML element is re-created every time triggers a focus loss that can be very annoying, especially to assistive technology users.
How?
The approach is similar to what proposed in #56490 — although the fix is applied only for this specific instance of
Button
, instead of applying it directly to the button component (since that could have broader consequences).The issue is related to the fact that React can't reconciliate the "with tooltip" and "without tooltip" version of the
Button
component without re-creating the whole button every time.To solve this, I decided to tweak the code so that:
Button
component never renders a tooltip (thanks to removing theshowTooltip
andshortcut
props, and changing thelabel
prop toaria-label
)Button
component is always wrapped in aTooltip
component. The component receives a different set of props depending on whether a tooltip should be effectively shown.Note: this doesn't mean that the tooltip will always be shown, though — the original user-facing behaviour should have been retained by only passing props to the
Tooltip
component when needed.Testing Instructions
From #44735 (comment):
Screenshots or screencast
Kapture.2023-11-24.at.12.53.04.mp4