Skip to content

Commit af096a3

Browse files
fix(ui): z-index on react-select menu (#9512)
The [previous fix](#8735) worked but was a breaking change because it set a `z-index` in the `.react-select` wrapper instead of the `.rs__menu`, creating a new stacking-context, therefore making any existing customizations to the menu's `z-index` not work. This was a way to fix a regression introduced by the css-layers, in which Payload's custom `z-index: 4` no longer took precedence over react-select's default `z-index: 1`. With this PR we remove the default `z-index: 1` applied by react-select, so that the `z-index: 4` set in the "payload-default" css layer can take effect. An alternative to this fix would be to use `z-index: 4 !important`, but this has the advantage of allowing the `z-index` to be easily customized by the consumers of the CMS, as with all the other styles. ### Screenshots ![2024-11-25 18 21 22](https://github.com/user-attachments/assets/3dc9a067-901a-41ea-b04e-c811788f8415)
1 parent 6ffd4c7 commit af096a3

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

packages/ui/src/elements/ReactSelect/index.scss

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
}
77

88
.react-select {
9-
&:focus-within {
10-
z-index: 3;
11-
}
129
.rs__control {
1310
@include formInput;
1411
height: auto;

packages/ui/src/elements/ReactSelect/index.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use client'
2-
import type { KeyboardEventHandler } from 'react'
2+
import type { CSSProperties, KeyboardEventHandler } from 'react'
33

44
import { arrayMove } from '@dnd-kit/sortable'
55
import { getTranslation } from '@payloadcms/translations'
@@ -67,6 +67,13 @@ const SelectAdapter: React.FC<ReactSelectAdapterProps> = (props) => {
6767
.filter(Boolean)
6868
.join(' ')
6969

70+
const styles = {
71+
// Remove the default react-select z-index from the menu so that our custom
72+
// z-index in the "payload-default" css layer can take effect, in such a way
73+
// that end users can easily override it as with other styles.
74+
menu: (rsStyles: CSSProperties): CSSProperties => ({ ...rsStyles, zIndex: undefined }),
75+
}
76+
7077
if (!hasMounted) {
7178
return <ShimmerEffect height="calc(var(--base) * 2 + 2px)" />
7279
}
@@ -106,6 +113,7 @@ const SelectAdapter: React.FC<ReactSelectAdapterProps> = (props) => {
106113
onMenuClose={onMenuClose}
107114
onMenuOpen={onMenuOpen}
108115
options={options}
116+
styles={styles}
109117
unstyled={true}
110118
value={value}
111119
/>
@@ -183,6 +191,7 @@ const SelectAdapter: React.FC<ReactSelectAdapterProps> = (props) => {
183191
onMenuClose={onMenuClose}
184192
onMenuOpen={onMenuOpen}
185193
options={options}
194+
styles={styles}
186195
unstyled={true}
187196
value={value}
188197
/>

0 commit comments

Comments
 (0)