Skip to content

fix: stop the lenis scroll when autoscroll (mouse3) is pressed#527

Open
mihaicosmini wants to merge 1 commit into
darkroomengineering:mainfrom
mihaicosmini:fix/middle-click-autoscroll
Open

fix: stop the lenis scroll when autoscroll (mouse3) is pressed#527
mihaicosmini wants to merge 1 commit into
darkroomengineering:mainfrom
mihaicosmini:fix/middle-click-autoscroll

Conversation

@mihaicosmini

@mihaicosmini mihaicosmini commented Jun 26, 2026

Copy link
Copy Markdown

What & why

This PR resolves a conflict between native middle-click autoscroll (activated by pressing mouse3) and Lenis smooth wheel scrolling.

What changes:

  • Adds private tracking fields (_isAutoscrolling and _middleClickTime) to track the browser's native autoscroll lifecycle.
  • Binds pointerup and keydown event listeners to the window to detect when autoscrolling ends.
  • Returns early from onVirtualScroll when _isAutoscrolling is active, ignoring virtual scroll events (e.g., mouse wheel events) while autoscrolling.
  • Resets the _isAutoscrolling state back to false when calling reset().

Why it's needed:
When a user starts autoscrolling by middle-clicking while a previous mouse wheel smooth scroll animation is still running (or scrolls the wheel during autoscroll), the native browser scroll and the Lenis scroll engine have a conflict. This causes jitter and unexpected scroll behavior. Disabling virtual scroll during autoscrolling lets the browser handle native scrolling smoothly without conflicts.

Closes #528

How to verify

  1. Clone/pull the branch and run the playground locally using bun run dev.
  2. Scroll the page using the mouse wheel, then immediately middle-click to enter autoscroll and move the mouse around.
  3. Observe that the scroll is smooth and doesn't jitter or jump back to the previous target.
  4. Scroll the wheel while autoscrolling is active and verify that it doesn't conflict or trigger smooth scroll animations.

Demo

BEFORE FIX

lenis.before.fix.2.mp4

AFTER FIX

after.lenis.fix.1.mp4

Checklist

  • PR is focused on a single logical change
  • Linked the related issue (or explained why there isn't one)
  • Lint/format/tests pass locally
  • Updated docs/types where relevant

@mihaicosmini mihaicosmini requested a review from a team as a code owner June 26, 2026 23:03
@vercel

vercel Bot commented Jun 26, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the Darkroom Team on Vercel.

A member of the Team first needs to authorize it.

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.

Scroll and autoscroll conflict

1 participant