Skip to content

Commit

Permalink
[Joy] Select popup should have max-height (#36156)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivek-Prajapatii authored Feb 22, 2023
1 parent 23556c6 commit 910d3a4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/data/joy/components/select/SelectMinWidth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as React from 'react';
import Box from '@mui/joy/Box';
import Select from '@mui/joy/Select';
import Option from '@mui/joy/Option';
import Typography from '@mui/joy/Typography';

export default function SelectMinWidth() {
return (
<Box sx={{ width: 100 }}>
<Select
defaultValue="German Shepherd"
slotProps={{
listbox: {
placement: 'bottom-start',
sx: { minWidth: 160 },
},
}}
>
<Option value="German Shepherd">German Shepherd</Option>
<Option value="Anatolian Shepherd Dog">Anatolian Shepherd Dog</Option>
<Option value="West Highland White Terrier">
West Highland White Terrier
</Option>
<Option value="Maltese dog">Maltese dog</Option>
</Select>
<Typography level="body3" textAlign="center" sx={{ mt: 1 }}>
Width is fixed at 100px
</Typography>
</Box>
);
}
40 changes: 40 additions & 0 deletions docs/data/joy/components/select/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,46 @@ const App = () => (
);
```
### Listbox
#### Maximum height
To change the listbox's maximum height, use `slotProps` prop to target listbox slot:
```jsx
<Select
slotProps={{
listbox: {
sx: {
maxHeight: '300px',
},
},
}}
>
...
</Select>
```
#### Minimum width
By default, the listbox's width is equal to Select's button or the maximum content of its children. You can control the minimum width by using `slotProps` prop to target listbox slot.
{{"demo": "SelectMinWidth.js"}}
:::success
To control the placement of the listbox, use `placement`:
```js
<Select
slotProps={{
// the left-edge of the listbox will align with button.
listbox: { placement: 'bottom-start' },
}}
>
```
:::
### `Option` component
The `Option` component is used for the chooseable options within the select.
Expand Down
3 changes: 3 additions & 0 deletions packages/mui-joy/src/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ const SelectButton = styled('button', {
fontFamily: 'inherit',
cursor: 'pointer',
whiteSpace: 'nowrap',
overflow: 'auto',
...((ownerState.value === null || ownerState.value === undefined) && {
opacity: 'var(--Select-placeholderOpacity)',
}),
Expand All @@ -239,6 +240,8 @@ const SelectListbox = styled(StyledList, {
'--List-item-stickyTop': 'calc(var(--List-padding, var(--List-divider-gap)) * -1)', // negative amount of the List's padding block
...scopedVariables,
minWidth: 'max-content', // prevent options from shrinking if some of them is wider than the Select's root.
maxHeight: '44vh', // the best value from what I tried so far which does not cause screen flicker when listbox is open.
overflow: 'auto',
outline: 0,
boxShadow: theme.shadow.md,
zIndex: theme.vars.zIndex.popup,
Expand Down

0 comments on commit 910d3a4

Please sign in to comment.