fix: wrap tether instance creation and updates in requestAnimationFrame #236
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When using react-tether in complex UI scenarios, positioning issues and flickering can occur during rapid component mounting/updating. This happens because tether attempts to position elements before the browser has fully completed its layout calculations.
Solution
This PR wraps tether instance creation and updates in
requestAnimationFramecallbacks. This defers the positioning calculations until after the browser has completed its current layout work, ensuring accurate positioning.Changes
_createTetherInstance: Wrapsnew Tether()and_registerEventListeners()inrequestAnimationFrame_updateTether: Wraps_addContainerToDOM(),setOptions(),_createTetherInstance(), andposition()inrequestAnimationFrameTest Updates
Tests have been updated to use
async/awaitwithwaitForfrom@testing-library/reactto accommodate the now-asynchronous tether initialization. Added proper cleanup inafterEachto ensure test isolation.Background
This fix has been battle-tested in production at Datadog, where we've been maintaining a local patch for react-tether v2.0.8 with this same approach. We're now looking to upgrade to v3 and would like to contribute this fix upstream.
Testing
All existing tests pass with the updated async patterns.