Skip to content

Conversation

@SBoudrias
Copy link

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 requestAnimationFrame callbacks. This defers the positioning calculations until after the browser has completed its current layout work, ensuring accurate positioning.

Changes

  1. _createTetherInstance: Wraps new Tether() and _registerEventListeners() in requestAnimationFrame
  2. _updateTether: Wraps _addContainerToDOM(), setOptions(), _createTetherInstance(), and position() in requestAnimationFrame

Test Updates

Tests have been updated to use async/await with waitFor from @testing-library/react to accommodate the now-asynchronous tether initialization. Added proper cleanup in afterEach to 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant