VB-1556: Scroll iframe to #anchor on initial load (Visual Builder)#601
Closed
karancs06 wants to merge 1 commit into
Closed
VB-1556: Scroll iframe to #anchor on initial load (Visual Builder)#601karancs06 wants to merge 1 commit into
karancs06 wants to merge 1 commit into
Conversation
Adds useScrollToHashAnchor in the Visual Builder SDK: reads window.location.hash, ignores hash-router URLs (#/, #!/) and percent-decodes the id. If the anchor element is in the DOM, scrollIntoView fires immediately; otherwise a MutationObserver waits up to 5s for it to appear (SPA hydration), then scrolls. Wired into the VisualBuilder constructor alongside useScrollToField. Ticket: VB-1556
🔒 Security Scan Results
⏱️ SLA Breach Summary
✅ BUILD PASSED - All security checks passed |
Coverage Report
File Coverage
|
||||||||||||||||||||||||||||||||||||||||||||
Contributor
Author
|
this is a csr feature request not required currently |
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.
VB-1556: Scroll iframe to
#anchoron initial load (Visual Builder)Ticket: VB-1556
Paired with: Visual Builder PR contentstack/visual-builder#2234
Why
When a user opens Visual Builder with a target URL containing a fragment (e.g.
host/page#footer), the Visual Builder PR ensures the iframesrcends up ashost/page?entry_uid=…&builder=true#footer. The browser, however, only attempts a native anchor scroll at initial document parse time — and for SPA target sites (the common case), the#footerelement doesn't yet exist in the DOM at that instant. The browser's native scroll silently no-ops and never retries.The fix has to live inside the iframe, which is what this SDK PR adds.
Screen.Recording.2026-05-26.at.2.46.24.PM.mov
What this PR adds
src/visualBuilder/eventManager/useScrollToHashAnchor.ts(new file)window.location.hash.#/route,#!/route) — those are routes, not anchors.#contact%20us→contact us).document.getElementById(id), falling back todocument.querySelector('[name="…"]')for legacy anchors.scrollIntoView({ behavior: "smooth", block: "start" })immediately.MutationObserverondocument.body(childList,subtree) so the scroll fires the moment the element is inserted (SPA hydration). A 5-second safety timeout disconnects the observer if the element never appears.src/visualBuilder/index.tsuseScrollToHashAnchor()in the existingBUILDERwindow-type block of theVisualBuilderconstructor, next touseScrollToField().How it works end-to-end
Why scroll/parse logic lives inside the SDK and not in Visual Editor
Visual Editor is in a different window/context than the iframe. It cannot directly observe or scroll DOM nodes inside the user's page. The hook has to run in the same realm as the user's document — i.e. the SDK that's already bundled into that page.
Why the
#fragmentis appended after the query string in the iframe URL(Repeated here from the paired VE PR for reviewers landing on this one first.)
URL spec (RFC 3986):
scheme://host/path?query#fragment. The fragment is always last.If
#footerwere placed before?entry_uid=…, the?would become part of the fragment, breaking everything that readswindow.location.search(this SDK, the user's analytics, their router) and native anchor scrolling (browser would look forid="footer?entry_uid=…"). The current order is the only standards-compliant one.Type of Change
Testing
Unit tests
Added
src/visualBuilder/eventManager/__test__/useScrollToHashAnchor.test.tscovering:#/routeignored.#!/routeignored.scrollIntoViewcalled immediately with{ behavior: "smooth", block: "start" }.scrollIntoViewcalled, observer disconnects.<a name="…">anchors found via fallback selector.#contact%20us) decoded before lookup.Run from
live-preview-sdk/:All 8 tests pass.
Manual testing
Verified against
test-resources/csr(CSR SPA target site):#footer(after temporarily settingid="footer"on a footer-like element in the CSR app for local testing).#footerelement after entry load.#/dashboard) are not affected — no spurious scroll.