70

I am using the react-select library to create autocomplete drop-down. I have used 2 drop-down parallel if 2nd has some data & I open first one then the zIndex problem comes. see the imageenter image description here

Khushboo
  • 771
  • 1
  • 6
  • 13

9 Answers9

141

Add these lines in your all Select tag:

<Select 
    // other props
    menuPortalTarget={document.body} 
    styles={{ menuPortal: base => ({ ...base, zIndex: 9999 }) }}
/>
Akash Kumar Verma
  • 3,185
  • 2
  • 16
  • 32
hemant rao
  • 2,233
  • 2
  • 13
  • 14
  • Thank you so much! – Muslem Omar Mar 31 '21 at 18:22
  • 2
    Save my day! It should be the most robust solution. If the parent container has `overflow: hidden`, just setting `menu` z-index to 9999 cannot prevent the popup cropped by the parent container. – Renyuan wang Apr 27 '21 at 08:38
  • You made my day, this solution is for the case u have a parent div which set overflow: hidden. `menuPortalTarget={document.body} ` did the trick – Minh Kha Jul 21 '21 at 04:43
  • 2
    this is not working , Vicente's answer is working – Chuyi Huang Oct 13 '21 at 10:48
  • 1
    This solved my issue. I was opening it inside a small dialog, but I needed it outside. Thank you! – user.io Feb 09 '22 at 12:58
  • 1
    For some reason, this removes any font styles applied to custom components. Overriding the z-index of `menu` instead of `menuPortal` does not do this. – ADK Apr 19 '22 at 05:08
  • yey!!! i am using react-select-country-list which uses react-select and with this code added to the select component it worked!!! thanks so much – Fo Nko May 25 '22 at 12:15
  • This breaks when scrolling. The menu does not move with the parent element. – NickC Jun 15 '22 at 21:47
  • the doc is super large; thx for the hint – Ben Nov 23 '22 at 09:36
70

Try this hacky way of setting zIndex, and let me know if it worked :)

<Select
  styles={{
    // Fixes the overlapping problem of the component
    menu: provided => ({ ...provided, zIndex: 9999 })
  }}
  value={selectedOption}
  onChange={evt => onSelectChange(evt, index)}
  options={options}
/>
Vicente
  • 2,304
  • 11
  • 15
  • 3
    depending of @user11124592's markdown and CSS you're solution is completely valid and not necessary "hacky" :) – Laura Apr 24 '19 at 15:57
  • 4
    @Vicente thanks for your answer. If anyone else comes here and has trouble with this approach. You might be using the portal of react-select which in that case you need to use: ```menuPortal: provided => ({ ...provided, zIndex: 9999 })``` – Hudspeth Aug 26 '20 at 22:51
  • @Vicente, I have a similar problem, but I cannot display the zIndex. Do you have any other properties for the dropdown to overlap the other components? – Ruan Duarte Dec 03 '20 at 01:18
  • This is the superior solution. It's simpler and it can be applied to all Select components using a custom styles object. Additionally the other solution breaks when the page is scrolled as the portal is tied to the document body and not the menu element. Also it's not hacky, this is why z-index exists. – NickC Jun 15 '22 at 21:53
51

After seeing the half dozen related issues, yet not finding a resolution, I have finally found one.

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

Add these two options to your Select component.

It appears to make us of the new React Portal feature.

EDIT: If you do the above, you then run into this problem with the menu anchoring to the page due to position: fixed. Removing it may help. https://github.com/JedWatson/react-select/issues/4088

I Stand With Israel
  • 1,971
  • 16
  • 30
10

For me the solution was kind of the total of all the answers (thanks!);

 const customStyles = {
    ///.....
    menuPortal: provided => ({ ...provided, zIndex: 9999 }),
    menu: provided => ({ ...provided, zIndex: 9999 })
    ///.....
  }


 <Select
  //.....
  menuPortalTarget={document.body}
  menuPosition={'fixed'} 
  styles={customStyles}
  //.....
  />  
myuce
  • 1,321
  • 1
  • 19
  • 29
  • 1
    Adding yours "customStyles" only, works for me with "zIndex: 1000". But be careful with "Tooltips" or "Popovers" and add "zIndex" accordingly – michal Apr 06 '21 at 21:50
  • You can remove menuPosition={'fixed'} attribute. It prevents scrolling if menu list overflows page. – Ahmet Firat Keler May 25 '22 at 07:31
2

Another way is We can config the classNamePrefix and control it through the outer class.

import Select from "react-select";

<Select
   classNamePrefix={"my-custom-react-select"}
/>
.my-custom-react-select__menu {
  z-index: 2;
}

Bonus, We can re-style everything

.my-custom-react-select__control {
  border-width: 1px !important;
  border-color: #cbd5e0 !important;
  padding-top: 0.16rem;
  padding-bottom: 0.16rem;
}

.my-custom-react-select__control--is-focused {
  border-color: #746df7 !important;
  box-shadow: none !important;
}

.my-custom-react-select__indicator-separator {
   display: none;
}
Phat Tran
  • 3,404
  • 1
  • 19
  • 22
2

Just simply add the below two lines of code.

 const customStyles = {
    ///.....
    menuPortal: provided => ({ ...provided, zIndex: 5 }),
   
    ///.....
  }

 <Select
  //.....
  menuPosition={'fixed'} 
  styles={customStyles}
  //.....
  /> 
RAHUL RAJ
  • 91
  • 4
1

Only combination of those answers made the working solution for me on Next.js. menuPortalTarget={document.body} broke the application with the error ReferenceError: document is not defined, which makes sense on SSG/SSR :)

menuPosition={"fixed"} as suggested by @I Stand With Israel with combination of answer from @hemant rao: menuPortal: (base) => ({ ...base, zIndex: 4 }),.

Daniel Danielecki
  • 8,508
  • 6
  • 68
  • 94
0

This is my solution when using "react-select" (version 4.3.1)

import ReactSelect from 'react-select';
...
...
<ReactSelect
  className="custom_zindex"
  ...
  ...
/>

In the css file:

.custom_zindex {
   z-index: 100;  /* or 999 */
}

"className" is one of the props in react-select (https://react-select.com/props).

Brian Ho
  • 391
  • 4
  • 7
-5

Change the zIndex of the parent component of this select

<div style={{zIndex:1000}}>
  <React-Select-Component/>
</div>
Hatem
  • 285
  • 1
  • 8