Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mouse wheel and touch scroll not working over popover #2123

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

pa4080
Copy link

@pa4080 pa4080 commented Dec 17, 2023

This PR gracefully resolves #607, also resolves #542. These issues are caused by an issue with Radix-ui/primitives which still open for over a year and a half - it is described here: [Dialog][Popover] Scrolling issue when Popover inside Dialog #1159

The current solution simulates an arrow down/up key press when we use the mouse wheel over the problematic popover primitive component.

Copy link

vercel bot commented Dec 17, 2023

@pa4080 is attempting to deploy a commit to the shadcn-pro Team on Vercel.

A member of the Team first needs to authorize it.

@pa4080 pa4080 changed the title fix: mouse wheel not scroll over popover #607 #542 fix: mouse wheel not scroll over popover Dec 17, 2023
@pa4080 pa4080 closed this Dec 17, 2023
@pa4080 pa4080 deleted the fix/wheel-scroll-over-popover branch December 17, 2023 14:01
@pa4080 pa4080 restored the fix/wheel-scroll-over-popover branch December 17, 2023 14:02
@pa4080 pa4080 reopened this Dec 17, 2023
@shrikantkalar023
Copy link

Hi! I used your fix in my popover component & it worked. Thanks dude <3 . Much love <3

@NazariiShvets
Copy link

Hi! I also have same issue, and solution works well. Thanks, saved my day!

@ejscheepers
Copy link

This solution worked well in my project!

@NazariiShvets
Copy link

NazariiShvets commented Apr 4, 2024

Hi, coming back here to add suggestion
In our application with this impl we still had problems with scroll, but this time at touch devices

So our final fix is

const useFixScrollForModaledPopovers = () => {
   const touchStartRef = useRef<number | null>(null)

  const onScroll = (event: WheelEvent<HTMLDivElement>) => {
    event.stopPropagation();
    const isScrollingDown = event.deltaY > 0;

    if (isScrollingDown) {
      event.currentTarget.dispatchEvent(
        new KeyboardEvent('keydown', { key: 'ArrowDown' })
      );
    } else {
      event.currentTarget.dispatchEvent(
        new KeyboardEvent('keydown', { key: 'ArrowUp' })
      );
    }
  }
  
  const onTouchStart = (event : TouchEvent<HTMLDivElement>) => {
     touchPosRef.current = event.changedTouches[0]?.clientY ?? null
  } 
  
  const onTouchMove = (event: TouchEvent<HTMLDivElement>) => {
     const touchPos = e.changedTouches[0]?.clientY ?? null;
     if(touchPosRef.current === null || touchPos === null) return;
     
     e.stopPropagation();

     const isScrollingDown = touchPosRef.current < touchPos;

     if (isScrollingDown) {
       e.currentTarget.dispatchEvent(
         new KeyboardEvent('keydown', { key: 'ArrowDown' })
       );
     } else {
       e.currentTarget.dispatchEvent(
         new KeyboardEvent('keydown', { key: 'ArrowUp' })
       );
     }

     touchPosRef.current = touchPos;
  }
  
  return { onWheel, onTouchStart, onTouchMove }
}

@pa4080 pa4080 changed the title fix: mouse wheel not scroll over popover fix: mouse wheel and touch scroll not working over popover Apr 10, 2024
@pa4080
Copy link
Author

pa4080 commented Apr 10, 2024

Hi, @NazariiShvets, I've committed your solution to the PR. It works great. Thanks!

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.

ScrollArea doesn't work inside Combobox ScrollArea not working inside a Popover
4 participants