Jump to content
Search Community

GSAP ScrollTrigger pinned section causes scroll position jump when opening Base UI modal dialog with Lenis smooth scrolling

Anand-00

Go to solution Solved by GreenSock,

Recommended Posts

  • Solution
GreenSock
Posted

What’s happening
Opening the modal locks scroll (overflow: hidden), which removes the scrollbar and slightly changes layout width.

 

That triggers SplitText autoSplit (your observeChanges: true) to re-split. In onSplit, you kill the pinned ScrollTrigger — which removes the pin spacer and collapses page height. The browser clamps scroll upward (~19750 → ~13564).

 

First open only: width changes once (scrollbar gone). Second open, width is already stable → no re-split → no jump.

 

Vite vs Next.js: likely different global CSS on html/body (scrollbar behavior).

 

Fix
1. Prevent the layout shift

html {
  scrollbar-gutter: stable;
}


2. Don’t kill ScrollTrigger in onSplit
Create the pinned ScrollTrigger once. On re-split, only update tweens:

let tl;
SplitText.create(storyRef.current.children, {
  autoSplit: true,
  onSplit: contextSafe((self) => {
    if (!tl) {
      tl = gsap.timeline({
        scrollTrigger: {
          trigger: pinRef.current,
          pin: pinRef.current,
          // ...your existing config
        },
      });
    } else {
      tl.clear();
    }
    tl.to(self.chars, { /* ... */ }, 0);
    tl.to(imageRef.current, { /* ... */ }, 0);
    ScrollTrigger.refresh(); // safe — pin stays alive
    return tl;
  }),
});


3. If you don’t need live re-splitting, use the public API

SplitText.create(elements, {
  autoSplit: false, // no ResizeObserver — split only when YOU call .split()
  onSplit: (self) => { /* create ST once, never kill it */ },
});


Manually call .split() only for real changes (font load, theme toggle) — not on every modal open.

 

Bottom line: scrollbar-gutter: stable + don’t kill pinned ScrollTriggers in onSplit. If re-split on resize isn’t required, autoSplit: false is the clean public escape hatch. 

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...