JedWatson / JedWatson/react-select

Combination of menuPortalTarget and menuShouldBlockScroll causes click on document

Open
#5,020 2 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

issue/bug-unconfirmed
Dominant language
TypeScript
Stars
28k
Forks
4.1k
PR merge metrics
No merged PRs in 30d

Description

Are you reporting a bug or runtime error?

Bug

I set up the dev server with the docs on my machine to see if this was my setup, but it happens there too.

When creating a Select component with menuPortalTarget={document.body} and menuShouldBlockScroll={true} and opening the select, a click event is propagated to the document, even when I've wrapped the select component in a div with onClick={e => e.stopPropagation()}.

This would be logical when clicking on the menu itself (which is rendered in a portal so outside of my stopPropagation), but not when clicking on the input itself, which is definitely still a child of the propagation-stopping div.

I need the combination of both of these props to fix other issues in my app (mainly z-index issues and this one: https://github.com/JedWatson/react-select/issues/4088)

In my app, I'm rendering the select in a dropdown that listens to a click event on document to close the dropdown, opening the select instantly closes the dropdown because of this bug.

I've narrowed it down to this fixed div being rendered over the screen (which I believe is all menuShouldBlockScroll does):
https://github.com/JedWatson/react-select/blob/4d3b1aaf96777285e6881c74391ada637fcaaef8/packages/react-select/src/internal/ScrollManager.tsx#L45-L50

Even when wrapping the ScrollManager in a div that stops propagation of the click event, this still happens.

The click event has a srcElement of body for some reason. Could this mean this is being caused by a strange combination of a mouseUp and mouseDown event being triggered on 2 separate elements?

Any idea what I could do differently to get this to work, or any pointers to how I could better debug this?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with packages/react-select/src/internal/ScrollManager.tsx at lines 45-50 and reproduce a Select using menuPortalTarget={document.body} with menuShouldBlockScroll={true} inside a propagation-stopping wrapper. Trace the click target and mouse event sequence in the local docs setup; done means opening the select no longer triggers the surrounding document click handler while the portal and scroll-blocking behavior remain available.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.