The new v2 react-select control is great, but by default is too large. Is there a (preferably) simple way to reduce the height to the same as a standard select control (using Bootstrap v3)?
8 Answers
-
This should be the accepted answer. Thank you! – Marcin Wanago Apr 28 '21 at 13:29
-
no `menuWidth`?? Seesh...I think I'm up to an hour trying to just reduce the size of my React select without adding ``s around it.– velkoon Jun 03 '21 at 17:02
-
3This reduces the size of the menu, not the control. This is not a correct answer. – Joe W Sep 22 '21 at 15:14
The answer below was given when react-select v2 was in beta, much has changed since then.
Read the react-select docs HERE
React-Select v2 uses emotion CSS-in-JS so the new way to control style over the select components and sub-components is by passing a style object to the styles prop. You can also set a className to style with an external stylesheet.
CSS-in-JS way
const customControlStyles = base => ({
height: 20,
minHeight: 20
});
<Select ... styles={{control: customControlStyles}} ... />
CSS Way <Select ... className="myClassName" ... />
.myClassName__control {
height: 20px;
min-height: 20px;
}
- 197
- 1
- 6
-
1is there a way to get the component to autosize? I have fiddled with many of the css style keys and have come up empty on the relationship between the parts of the component to get it to size width wise based on the contents in the options. – tcoulson May 22 '18 at 14:49
-
6FYI, there is a min-height set on `control` of 38px. So if you want to go smaller, you need to provide a `minHeight` and not just a `height`. Eg: `styles={{ control: (base, _state) => ({...base, minHeight: '20px', height: '20px'})}}` – craigmichaelmartin Sep 19 '18 at 19:38
-
1oh my g*d why can't I change `width` or `min-width` with `className`?? Nor with `classNamePrefix`?? I'm going crazy!!!! – velkoon Jun 03 '21 at 17:05
-
1This answer does not work and does not answer the question, which was to reduce the control size, not increase it. If you specify a smaller height, all it will do is reduce the size of the outer div and nothing else inside the control so it will overflow. – Joe W Sep 22 '21 at 15:16
Setting the height property looks like it retains that height even when you have overflow (from multiple selected values spilling onto the next line) so you end up with the values falling outside the box.
I solved this issue by setting the padding top and bottom on the dropdownIndicator and the clearIndicator and setting minHeight on control like so:
const styles = {
control: (base) => ({
...base,
minHeight: 32,
}),
dropdownIndicator: (base) => ({
...base,
paddingTop: 0,
paddingBottom: 0,
}),
clearIndicator: (base) => ({
...base,
paddingTop: 0,
paddingBottom: 0,
}),
};
<Select styles={styles}/>
- 380
- 3
- 13
-
1Brilliant. Worked perfect. Made this an exported const, and also baked it into a reusable component. Thanks! – Boyardee Nov 09 '20 at 14:23
-
This answer does not work. All it will do is reduce the size of the outer div and nothing else inside the control so it will overflow. – Joe W Sep 22 '21 at 15:23
Adding onto what @craigmichaelmartin commented, the minHeight on control is important to overwrite, and it needs to be set at a bunch of places in order to really overcome it.
Here's what worked for me to get it to match the height of a 36px text input element (These settings also work in css, of course)
const customStyles = {
container: (provided) => ({
...provided,
display: 'inline-block',
width: '250px',
minHeight: '1px',
textAlign: 'left',
border: 'none',
}),
control: (provided) => ({
...provided,
border: '2px solid #757575',
borderRadius: '0',
minHeight: '1px',
height: '42px',
}),
input: (provided) => ({
...provided,
minHeight: '1px',
}),
dropdownIndicator: (provided) => ({
...provided,
minHeight: '1px',
paddingTop: '0',
paddingBottom: '0',
color: '#757575',
}),
indicatorSeparator: (provided) => ({
...provided,
minHeight: '1px',
height: '24px',
}),
clearIndicator: (provided) => ({
...provided,
minHeight: '1px',
}),
valueContainer: (provided) => ({
...provided,
minHeight: '1px',
height: '40px',
paddingTop: '0',
paddingBottom: '0',
}),
singleValue: (provided) => ({
...provided,
minHeight: '1px',
paddingBottom: '2px',
}),
};
- 141
- 3
- 8
-
1I also had to set the indicatorsContainer's height in order to make it smaller than the default – Marisol Gutiérrez Apr 05 '19 at 18:47
You can also try to style the input field of your react-select component, as it can impact the height of the whole react-select component. In my case, this was happening through interference from materializecss.
const customStyles = {
input: styles => {
return {
...styles,
height: '1.8em'
};
}
const App = () => (
<Select
styles={customStyles}
options={...}
/>
);
- 351
- 3
- 11
From a similar question also on SO, you can modify the react-select theme to change the height of the control. Seems easier to me than the other answers.
const customThemeFn = (theme) => ({
...theme,
spacing: {
...theme.spacing,
controlHeight: 30,
baseUnit: 2
}
})
<Select theme={customThemeFn}> ... </Select>
For more, see Theme modifier method at this page of the docs.
- 602
- 6
- 11
-
Great answer. This works if using multi-select and want the control height to auto icrease as more options are selected. – James Feb 11 '23 at 19:23
Yet another CSS Way
You can also specify classNamePrefix and use it to override CSS styles.
<Select classNamePrefix="mySelect" />
.mySelect__value-container{
height: 35px;
}
- 119
- 1
- 2
I got the answer. I think the following is the minimal setting. I could able to reduce the hight of the react select.
This is the code, I used TypeScript for this code.
const targetHeight = 30;
const styles = {
control: (base: any) => ({
...base,
minHeight: 'initial',
}),
valueContainer: (base: any) => ({
...base,
height: `${targetHeight - 1 - 1}px`,
padding: '0 8px',
}),
clearIndicator: (base: any) => ({
...base,
padding: `${(targetHeight - 20 - 1 - 1) / 2}px`,
}),
dropdownIndicator: (base: any) => ({
...base,
padding: `${(targetHeight - 20 - 1 - 1) / 2}px`,
}),
};
<Select
styles={styles}
/>
- 346
- 4
- 10
