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

Button creates new HTML elements every time it internally flips between showing tooltips or not #56522

Closed
5 tasks done
ciampo opened this issue Nov 24, 2023 Discussed in #44735 · 0 comments · Fixed by #56490 or #57202
Closed
5 tasks done

Button creates new HTML elements every time it internally flips between showing tooltips or not #56522

ciampo opened this issue Nov 24, 2023 Discussed in #44735 · 0 comments · Fixed by #56490 or #57202
Labels
[Feature] UI Components Impacts or related to the UI component system [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Type] Enhancement A suggestion for improvement.

Comments

@ciampo
Copy link
Contributor

ciampo commented Nov 24, 2023

As discussed originally in #44735

The problem

The Button component can, based on which props are passed, show a tooltip when hovered or focused.

This feature is implemented by wrapping conditionally the button's contents in a Tooltip component.

The current implementation causes React not to be able to reconciliate the Button's contents if the need for showing a tootlip changes at runtime. In return, this causes the current button (or a) element rendered in the DOM to be removed and replaced by a new element, which causes focus loss.

Known workarounds and potential solutions

The most clear way to avoid the re-render is to make sure that the Tooltip component is always rendered (instead of being rendered conditionally). This can be achieved in a few ways:

  1. by specifying the showTooltip prop on Button (although this will force the tooltip to be always visible) (example)
  2. by avoiding to trigger the internal tooltip in Button, and instead wrapping Button externally with a Tooltip (tested in Save post button: avoid extra re-renders when enablng/disabling tooltip #56502)
  3. by tweaking the Button component to internally always render a Tooltip component (proposed in Button: always render the Tooltip component even when a tooltip should not be shown #56490)

Approaches 1 and 2 can work well to fix specific instances of Button, while approach 3 would solve the problem fundamentally for all Button usages (although it has the potential to introduce a breaking change).

A 4th approach (yet to be tested) would be to try to use the key special prop as a way to flag to React that a given component is stable (and therefore doesn't need to be destroyed and re-created across re-renders).

Plan of action

@ciampo ciampo changed the title Button creates new HTML elements every time it internally flips between showing tooltips or not Button creates new HTML elements every time it internally flips between showing tooltips or not Nov 24, 2023
@jordesign jordesign added [Type] Enhancement A suggestion for improvement. [Feature] UI Components Impacts or related to the UI component system labels Nov 26, 2023
@afercia afercia added the [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). label Nov 27, 2023
@ciampo ciampo reopened this Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] UI Components Impacts or related to the UI component system [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Type] Enhancement A suggestion for improvement.
Projects
None yet
3 participants