React JS : React Select Dropdown position to top if there is no space below
Asked Answered
T

7

35

I am using React Select

I want its Dropdown position to be dynamic. let's say if there is no space below it, then the dropdown should be opened to above select box.

<Select
  value={selectedOption}
  onChange={this.handleChange}
  options={options}
/>

Is there any option to make dropdown position dynamic?

Any help would be great.

Thank You.

Tuscany answered 25/5, 2020 at 6:17 Comment(2)
Which version of react-select are you using? v3?Ulita
I am using Version 3.1.0Tuscany
C
47

You can use menuPlacement option on your Select component with "bottom" (default), "top" or "auto".

You can see other props here ;)

Chantress answered 30/10, 2020 at 13:42 Comment(3)
It's usually fine to make the cheeky "check the docs ;)" comment, but the React Select docs are honestly terrible to search through. It took me hours to find this propManaging
That prop doesn't work, at least for asyncs. It's an unresolved issue on their end.Lymphoid
That doesn't make it dynamic based on "fitting the screen"Ineluctable
D
23
<Select
  menuPlacement="top"
  ...
/>

You can use menuPlacement prop top, bottom or auto

Daryl answered 6/1, 2021 at 6:55 Comment(0)
T
17

<Select menuPortalTarget={document.body} menuPosition={'fixed'} />

After hours of finding the solution.This perfectly worked for me.

Teacart answered 2/9, 2022 at 15:33 Comment(2)
Wow, perfect. This answer should be the accepted one :)Ineluctable
What an Elegant solution! Thank you!Fontanez
R
3

If you are already using menuPlacement="auto" and the dropdown does not drop up as you would expect, you can additionally use minMenuHeight to inform the dropdown when it should change, which solved this issue for me:

<Select 
  menuPlacement="auto"
  minMenuHeight={300}
/>
Radiotransparent answered 14/2, 2023 at 21:7 Comment(0)
C
1

Inside component:

render() {
    const className = this.state.dropUp ? 'drop-up' : '';

    return (
      <Select {...this.props} className={className} />
    );
  }

And in css file:

.drop-up .Select-menu-outer {
  top: auto;
  bottom: 100%;
}
Catadromous answered 25/5, 2020 at 6:39 Comment(1)
side note: you can make state value of dropup false by window sizeCatadromous
T
1

If you want a native solution for this problem, use styles prop like below

<ReactSelect
  id="my-select"
  name="my-select"
  instanceId="my-select"
  options={mySelectOptions}
  value={mySelectValue}
  onChange={handleMySelectChange}
  
  // ATTENTION
  menuPortalTarget={typeof window !== 'undefined' ? document.body : null}
  styles={{
    menuPortal: (provided: React.CSSProperties) => ({
      ...provided,
      zIndex: 9999,
    }),
    menu: (provided: React.CSSProperties) => ({
      ...provided,
      zIndex: 9999,
      top: 'auto',
      bottom: '100%',
    })
  }}
  //

/>

Read more on https://react-select.com/styles

Thromboplastic answered 16/11, 2023 at 13:58 Comment(0)
W
0

I investigated the source code of the library and found that to make menuPosition="absolute" and menuPlacement="auto" work the closest scrollable parent (relative to which we want to position menu) must have position: relative. It fixed all the issues with dropdown menu positioning in my case.

It works at least in the latest version of the library (5.8.0).

Would answered 22/3 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.