Doist / reactist

Open source React components made with ❤️ by Doist
http://doist.github.io/reactist
MIT License
250 stars 22 forks source link

fix: Tooltip no longer re-created anchor when tooltip content changes #773

Closed gnapse closed 1 year ago

gnapse commented 1 year ago

Short description

The design team recently suggested that they are considering using tooltips to convey that a field has an invalid value. Normally we'd use the message prop, but in the case they're looking into, a tooltip would be convenient.

When exploring if that would work (given that it sounds unconventional to have a tooltip attached to a text field) it was all mostly good. For instance, the tooltip is correctly associated as the accessible description of the field. However, there was a problem with the field loosing focus as soon as I typed the character that made the tooltip appears due to the value being invalid.

When investigating why, I found that it was caused by the tooltip anchor element being re-created (re-mounted a new instance of the component, in React lingo). But we really wanted the element to stay the same, and just update its state. This was caused by how the Tooltip component changes its behavior based on whether the tooltip content was empty/null or not.

This PR fixes that, so that the anchor element is always present and stays the same component instance all the time.

PR Checklist

Versioning

Patch release.

Demo

https://github.com/Doist/reactist/assets/15199/a61c3851-7d6d-4bc2-8d47-8c2d497db22c