JedWatson / JedWatson/react-select

Automatic menu placement sometimes not correct within a scroll container

Open
#5,502 6 comments 14 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

The placement calculations for automatic menu placement seem to be wrong for some cases since #5177 has been merged.

Example codesandbox:
https://codesandbox.io/s/react-select-v5-sandbox-forked-8sgyet

Steps to reproduce:

  1. Have a scroll container that does not span the full window/viewport height
  2. Render the near at the bottom of that scroll container
  3. If the menu is opened, the MenuPlacer should let the menu flip and show on top of the select

Instead, the menu opens below, sometimes even increasing the scrollable area.

Possible cause:
https://github.com/JedWatson/react-select/blob/master/packages/react-select/src/components/Menu.tsx#L94
The computation of viewSpaceBelow seems incorrect when the scroll container's top edge is different from the windows top edge: menuTop comes from a call to getBoundingClientRect(), which gives the top offset relative to the viewport. When located in a scroll container, menuTop should actually be the top offset of the menu relative to the scroll parent, so that the comparison with viewHeight (the scrollParent height) returns the true view space available below the select.

Not sure which computation would give better results, I think menuTop should be relative to the scrollParent, not the viewport so that other computations remain correct. So something like
const menuTop = isFixedPosition ? menuEl.getBoundingClientRect().top : menuEl.getBoundingClientRect().top - scrollParent.getBoundingClientRect().top

I'm also willing to contribute a PR, but a good solution should be discussed first.

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 in packages/react-select/src/components/Menu.tsx around line 94 and reproduce the issue using the linked CodeSandbox. Compare the viewport-based menuTop with the scroll parent coordinates described in the report. Done means the menu flips above the select near the bottom of a shorter scroll container without expanding its scrollable area.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.