fix: wrap tether instance creation and updates in requestAnimationFrame#236
Open
SBoudrias wants to merge 1 commit intodanreeves:mainfrom
Open
fix: wrap tether instance creation and updates in requestAnimationFrame#236SBoudrias wants to merge 1 commit intodanreeves:mainfrom
SBoudrias wants to merge 1 commit intodanreeves:mainfrom
Conversation
This prevents positioning issues that can occur when the DOM hasn't fully settled during rapid updates. By deferring tether operations to the next animation frame, the browser has time to complete layout calculations before positioning is computed. The fix addresses flickering and incorrect positioning that can happen when components mount or update quickly, particularly in complex UI scenarios with nested tethered elements. Tests have been updated to use async/await with waitFor to accommodate the now-asynchronous tether initialization.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.