Skip to content

Commit 36b13fc

Browse files
authored
fix(SelectDropdown): adding optional zIndex prop
Adding an optional zIndex prop to SelectDropdown's menu to address zIndex collisions.
1 parent f0547a6 commit 36b13fc

File tree

5 files changed

+95
-15
lines changed

5 files changed

+95
-15
lines changed

packages/gamut/src/Form/SelectDropdown/SelectDropdown.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export const SelectDropdown: React.FC<SelectDropdownProps> = ({
122122
shownOptionsLimit = 6,
123123
size,
124124
value,
125+
zIndex,
125126
...rest
126127
}) => {
127128
const rawInputId = useId();
@@ -243,8 +244,8 @@ export const SelectDropdown: React.FC<SelectDropdownProps> = ({
243244

244245
const theme = useTheme();
245246
const memoizedStyles = useMemo((): StylesConfig<any, false> => {
246-
return getMemoizedStyles(theme);
247-
}, [theme]);
247+
return getMemoizedStyles(theme, zIndex);
248+
}, [theme, zIndex]);
248249

249250
return (
250251
<SelectDropdownContext.Provider

packages/gamut/src/Form/SelectDropdown/styles.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,16 @@ const dropdownBorderStates = states({
6161
error: { borderColorTop: 'feedback-error' },
6262
});
6363

64-
const dropdownBorderStyles = css({
65-
...formBaseComponentStyles,
66-
border: 1,
67-
borderColor: 'currentColor',
68-
position: 'absolute',
69-
marginTop: 0,
70-
borderRadius: 'none',
71-
zIndex: 2,
72-
});
64+
const dropdownBorderStyles = (zIndex = 2) =>
65+
css({
66+
...formBaseComponentStyles,
67+
border: 1,
68+
borderColor: 'currentColor',
69+
position: 'absolute',
70+
marginTop: 0,
71+
borderRadius: 'none',
72+
zIndex,
73+
});
7374

7475
const getOptionBackground = (isSelected: boolean, isFocused: boolean) =>
7576
css({
@@ -89,7 +90,8 @@ const placeholderColor = css({
8990
});
9091

9192
export const getMemoizedStyles = (
92-
theme: typeof GamutTheme
93+
theme: typeof GamutTheme,
94+
zIndex?: number
9395
): StylesConfig<any, false> => {
9496
return {
9597
clearIndicator: (provided) => ({
@@ -146,7 +148,7 @@ export const getMemoizedStyles = (
146148

147149
return {
148150
...provided,
149-
...dropdownBorderStyles({ theme }),
151+
...dropdownBorderStyles(zIndex)({ theme }),
150152
...dropdownBorderStates({ error: state.selectProps.error, theme }),
151153
...(dropdownWidth
152154
? {

packages/gamut/src/Form/SelectDropdown/types/styles.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export interface SharedProps extends InternalInputsProps, SelectDropdownSizes {
2323
* This is only relevant when the dropdown width is set to be larger or smaller than the input width.
2424
*/
2525
menuAlignment?: 'left' | 'right';
26+
/** Z-index for the dropdown menu */
27+
zIndex?: number;
2628
}
2729

2830
/**

packages/styleguide/src/lib/Atoms/FormInputs/SelectDropdown/SelectDropdown.mdx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export const parameters = {
2020
};
2121

2222
<Meta of={SelectDropdownStories} />
23-
2423
<ComponentHeader {...parameters} />
2524

2625
## Usage
@@ -199,6 +198,14 @@ When using different widths for the input and dropdown, you can control how the
199198

200199
<Canvas of={SelectDropdownStories.MenuAlignmentRight} />
201200

201+
### Selectdropdown options menu z-index
202+
203+
You can control the z-index of the dropdown menu using the `zIndex` prop. This is useful when you have other elements on the page that might overlap with the dropdown.
204+
205+
In the example below, the z-index menu is configured to the default value (`auto`) and `5` respectively. The DataTable below the `SelectDropdown` has a z-index of `2` since it's sticky and intended to float above other content.
206+
207+
<Canvas of={SelectDropdownStories.zIndexOnMenu} />
208+
202209
## Multiple select
203210

204211
<Canvas of={SelectDropdownStories.MultipleSelect} />

packages/styleguide/src/lib/Atoms/FormInputs/SelectDropdown/SelectDropdown.stories.tsx

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { Box, FlexBox, FormGroup, SelectDropdown } from '@codecademy/gamut';
1+
import {
2+
Box,
3+
DataList,
4+
FlexBox,
5+
FormGroup,
6+
SelectDropdown,
7+
Text,
8+
} from '@codecademy/gamut';
29
import { RadarIcon, ResponsiveIcon, RocketIcon } from '@codecademy/gamut-icons';
310
import type { Meta, StoryObj } from '@storybook/react';
411

@@ -995,3 +1002,64 @@ export const LongPlaceholderAgain: Story = {
9951002
</Box>
9961003
),
9971004
};
1005+
1006+
export const zIndexOnMenu: Story = {
1007+
render: (args) => (
1008+
<FlexBox column height="500px">
1009+
<Text mb={16}>Notice how the menu renders based on using zIndex</Text>
1010+
<FlexBox gap={16}>
1011+
<FormGroup
1012+
htmlFor="usesDefaultZIndex"
1013+
isSoloField
1014+
label="This menu is rendered behind the header"
1015+
>
1016+
<SelectDropdown
1017+
name="usesDefaultZIndex"
1018+
options={args.options}
1019+
placeholder="Uses the default zIndex of 2"
1020+
/>
1021+
</FormGroup>
1022+
<FormGroup
1023+
htmlFor="hasSetZIndex"
1024+
isSoloField
1025+
label="This menu floats above the table's header"
1026+
>
1027+
<SelectDropdown
1028+
name="hasSetZIndex"
1029+
options={args.options}
1030+
placeholder="Has a zIndex of 5"
1031+
zIndex={5}
1032+
/>
1033+
</FormGroup>
1034+
</FlexBox>
1035+
<DataList
1036+
columns={[
1037+
{
1038+
header: 'Name',
1039+
key: 'name',
1040+
size: 'lg',
1041+
type: 'header',
1042+
sortable: true,
1043+
},
1044+
{ header: 'Rank', key: 'role', size: 'lg', sortable: true },
1045+
{ header: 'Ship', key: 'ship', size: 'lg', sortable: true },
1046+
]}
1047+
disableContainerQuery
1048+
id="crew"
1049+
idKey="name"
1050+
rows={[
1051+
{
1052+
name: 'Jean Luc Picard',
1053+
role: 'Captain',
1054+
ship: 'USS Enterprise',
1055+
},
1056+
{
1057+
name: 'Wesley Crusher',
1058+
role: 'Deus Ex Machina',
1059+
ship: 'USS Enterprise',
1060+
},
1061+
]}
1062+
/>
1063+
</FlexBox>
1064+
),
1065+
};

0 commit comments

Comments
 (0)