Skip to content

Conversation

@jon-neher
Copy link
Contributor

When using anchor links, Docusaurus doesn't always load the elements in time.

I created a component that should help with this, but it didn't work the first time.

I got Cursor to help with:

  1. Event-Driven Approach
    Added a hashchange event listener to handle both programmatic navigation and direct browser navigation

The component now responds to browser-native hash changes, not just React router changes

  1. Better DOM Detection
    Added getBoundingClientRect() check to ensure the target element is actually visible and has dimensions before trying to scroll
    Increased retry attempts from 3 to 5 for better reliability

  2. Improved Timing
    Added a DOM_READY_DELAY (50ms) for initial load to ensure DOM is fully ready
    Used shorter retry intervals (100ms) for better responsiveness

  3. Proper Cleanup
    Added proper event listener cleanup to prevent memory leaks
    Used refs to track current hash and initialization stat

@jon-neher jon-neher requested a review from a team October 17, 2025 14:10
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.

2 participants