Changing height of react-select component - reactjs

I am using the react-select component along with bootstrap v4
all of bootstraps stuff is based on 35px height it seems, the default height of the react-select component is 38px, which looks a little odd.
Any ideas how I can change the height of the component?
It is using some weird JS styling library I have never come across before. I have managed to get it to mimic the outline on focus using it, but the height escapes me, any help much appreceiated
You can play with it here

Spending hours, I end up with this to get exact 30px height of react select with border 1px:
const customStyles = {
control: (provided, state) => ({
...provided,
background: '#fff',
borderColor: '#9e9e9e',
minHeight: '30px',
height: '30px',
boxShadow: state.isFocused ? null : null,
}),
valueContainer: (provided, state) => ({
...provided,
height: '30px',
padding: '0 6px'
}),
input: (provided, state) => ({
...provided,
margin: '0px',
}),
indicatorSeparator: state => ({
display: 'none',
}),
indicatorsContainer: (provided, state) => ({
...provided,
height: '30px',
}),
};

You can add your styles to any part of the select components, take a look at the relevant docs
here is a working demo of what you ask for.
In your case the code that you need to add will look something like this:
const customStyles = {
control: base => ({
...base,
height: 35,
minHeight: 35
})
};
and in the select component:
<Select
className="basic-single"
classNamePrefix="select"
defaultValue={colourOptions[0]}
isDisabled={isDisabled}
isLoading={isLoading}
isClearable={isClearable}
isRtl={isRtl}
isSearchable={isSearchable}
name="color"
options={colourOptions}
styles={customStyles}
/>

The reason why you're not able to make it less than 36px is that the dropdownIndicator and indicatorContainer(clear icon is displayed) are taking 20px (icon) + 8px padding in all sides. If you reduce that padding, the minHeight will actually work.
dropdownIndicator: (styles) => ({
...styles,
paddingTop: 7,
paddingBottom: 7,
}),
clearIndicator: (styles) => ({
...styles,
paddingTop: 7,
paddingBottom: 7,
}),
You can play around with the padding of the dropdownIndicator and clearIndicator.
I noticed that you can't go under 30px in minHeight because of the valueContainer, unless you change its height/padding.

CSS Way
You can specify classNamePrefix and use it to override CSS styles.
<Select classNamePrefix="mySelect" />
.mySelect__value-container{
height: 35px;
}

I was barely able to make the Select component as small as 32px (in my browser) using the theme attribute. It works well when the height is greater than 45px. You can also omit the baseUnit attribute.
It didn't work for small sizes.
const theme = (theme: Theme) => ({
...theme,
spacing: {
...theme.spacing,
controlHeight: 30,
baseUnit: 0,
}
});
<Select options={props.options} theme={theme}/>

I was able to over write the menu-list's css style:
/* over write css in react-select module */
.Select__menu-list {
max-height: 120px !important;
}

If you only want to resize the box use this.
.create-select {
width: 160px;
float: right;
color: #000;
[class$="ValueContainer"] {
min-height: 28px !important;
max-height: 28px;
}
[class$="IndicatorsContainer"] {
min-height: 28px !important;
max-height: 28px;
}
[class$="-Input"] {
min-height: 28px !important;
max-height: 28px;
padding: 0px;
}
[class$="-control"] {
min-height: 28px !important;
max-height: 28px;
}
}

Cause
The .control has a min-height of 38px defined.
Fix
Change the min-height on the .control to your desired height.
const customStyles = {
control: (provided, state) => ({
...provided,
minHeight: '30px',
...additionalStyles
}),
};

In my case, I had to just set the css height property for the class
__value-container
Here is the code:
.react-select__value-container {
height: 3rem;
}

export const customStyles = {
control: (provided: Record<string, unknown>, state: any) => ({
...provided,
height: 42,
boxShadow: 'none',
borderColor: 'none',
'&:hover': {
color: '#60B3D1'
},
border: state.isFocused ? '1.5px solid #60B3D1' : '1.5px solid #cbd5e1'
}),
option: (styles: any, state: any) => ({
...styles,
color: state.isSelected ? '#FFF' : styles.color,
backgroundColor: state.isSelected ? '#60B3D1' : styles.color,
borderBottom: '1px solid rgba(0, 0, 0, 0.125)',
'&:hover': {
color: '#FFF',
backgroundColor: '#60B3D1'
}
}),
input: (base: any) => ({
...base,
'input:focus': {
boxShadow: 'none',
border: '1px solid #60B3D1'
}
}),
menuPortal: (base: any) => ({ ...base, zIndex: 9999 })
}
<Select styles={customStyles } />
This really works well for me

Related

React-Select: Unable to set default value and changing border

I'm having issues with setting the default value and changing border color of the select when it's click already tried with :active,:blur,:focus and it's not working.
Current code:
const controlStyles = {
control: (styles: any) => ({
...styles,
fontFamily: `Gilroy`,
fontStyle: `normal`,
fontWeight: `700`,
fontSize: `18px`,
lineHeight: `22px`,
color: `#979B9B`,
width: `280px`,
height: `57px`,
border: `1px solid #007749`,
background: `#FFFFFF`,
":hover": {
border: `1px solid #007749`,
},
/*":active": {
border: `1px solid #007749`,
},
":focus": {
border: `1px solid #007749`,
},
":blur": {
border: `1px solid #007749`,
},*/
}),
};
And in setting the default value, already tried defaultValue and inputDefaultValue still not working
Current code:
<Select
styles={controlStyles}
//value={badgeData[0]}
//defaultValue={badgeData[0]}
//defaultInputValue={badgeData[0]}
//getOptionLabel={(e) => e.badge_name}
getOptionValue={(e) => e.id}
options={badgeData}
formatOptionLabel={(badgeData) => (
<div className="badge-option">
<img
src={badgeData.imgBase64}
alt={badgeData.badge_name}
className="me-4"
/>
<span>{badgeData.badge_name}</span>
</div>
)}
/>
Sample data for options:
Any ideas what Am I missing here? Thank you!

How to group CSS selectors with Material-UI?

I am wondering if there is a way to group CSS selectors with Material-UI to avoid repetition, from something like this:
const useStyles = makeStyles(() => ({
root: {
backgroundColor: '#000000',
color: '#ffffff',
'&::before': {
content: '""',
position: 'absolute',
borderTop: '1px solid white',
},
'&::after': {
content: '""',
position: 'absolute',
borderTop: '1px solid white',
}
},
}));
To something that would look more or less like this?
const useStyles = makeStyles(() => ({
root: {
backgroundColor: '#000000',
color: '#ffffff',
'&::before',
'&::after': {
content: '""',
position: 'absolute',
borderTop: '1px solid white',
}
},
}));
Thanks for your help!
I actually found the solution, it was as simple as using a comma between the 2 CSS selectors:
const useStyles = makeStyles(() => ({
root: {
backgroundColor: '#000000',
color: '#ffffff',
'&::before, &::after': {
content: '""',
position: 'absolute',
borderTop: '1px solid white',
}
},
}));

cant override styles in modal component?

I am using the modal component in material ui and I am having trouble overriding the auto display styles they have for the component.
Here is my use styles
const useStyles = makeStyles((theme) => ({
modal: {
inset: '1px 1px 0 0',
width: '40vw',
},
})
<Modal
aria-labelledby='transition-modal-title'
aria-describedby='transition-modal-description'
className={classes.modal}
>
the default css properties are overriding the styles I wrote in classes.modal, why is this happening and how can I fix it? I need to change the inset property completely
You can use linear styles if you can't override it with a class.
<Modal style={{inset:'1px 1px 0 0',width:'40vw'}}>
try to use (paper and <Fade/>):
const useStyles = makeStyles((theme) => ({
modal: {
inset: "1px 1px 0 0",
width: "40vw"
},
paper: {
backgroundColor: theme.palette.background.paper,
border: "2px solid gray",
boxShadow: theme.shadows[5],
padding: theme.spacing(2, 4, 3)
}
}));
sandbox

How to style react-select options?

I have used a react-select for multiselect. Now I want to style it but not getting it.
const selectStyles = {
control: (base) => ({
...base,
fontSize: '16px',
fontWeight: 'bold',
borderRadius: '8px',
padding: '6px 5px',
border: '1px solid #21274F !important',
boxShadow: 'none',
'&:focus': {
border: '0 !important',
},
}),
}
<Select
placeholder='Type Team Name...'
value={getOptions(value)}
options={getOptions(data)}
onChange={(data) => setValue(data || [])}
styles={selectStyles}
isMulti
isClearable
isSearchable
/>
Its looking like this. I want to change the background-color to blue and text = white. How can I achieve this? please help.
EDIT: After applied #Manish Jangir code. It looking like this.
But I want the text ie. 'leadership' to appear white in color and on hover the appering red color on cross to be removed.
There are a lot of custom custom components to style the entire select. Have a look at this. You need to use multiValue component to style to change the styles of a single tag:
const selectStyles = {
control: (base) => ({
...base,
fontSize: '16px',
fontWeight: 'bold',
borderRadius: '8px',
padding: '6px 5px',
border: '1px solid #21274F !important',
boxShadow: 'none',
'&:focus': {
border: '0 !important',
},
}),
multiValue: (base) => ({
...base,
backgroundColor: 'blue',
color: 'white',
}),
}
It's been a while, but adding this here for who would have the same problem and wants to tackle it with custom components and custom styling.
I had the similar issue as well. Creating custom component like in docs and wrapping it around components.Option didn't give me the desired result in terms of styling.
After digging through, I found an example in react.select issues. So instead of creating custom component that wraps components.Option, you can now create your own component and pass in the props as you needed. This would also allow you to style the component with classes easily (in my case, it was Tailwind).

stop make react-select width expanding while user typing

i'm using react-select. But, somehow, when user typing so much character, the input container is growing horizontally and even its break the screen (the width over the screen). How to make the width is static in every condition? example condition is when user typing or when the value that displayed in the option is too long, I want it to only display some of the character, for example:
real string: 'hello this is option one dude',
displayed in the option and in the input container: 'hello this is option.....'
is it achievable? how to do it? I've try this but not working.
here is the full code of styling for the react-select:
const styles = {
option: (base, state) => ({
...base,
backgroundColor: state.isSelected ? 'grey' : 'grey',
color: state.isSelected ? 'white' : 'black',
':active': {
backgroundColor: state.isSelected ? 'grey' : 'grey',
color: state.isSelected ? 'white' : 'white',
},
}),
control: (base, state) => ({
...base,
background: 'white',
borderRadius: 0,
borderTop: 0,
borderLeft: 0,
borderRight: 0,
borderColor: state.isFocused ? 'black' : 'black', // disable blue color in the box when input focused
boxShadow: state.isFocused ? 0 : 0,
whiteSpace: 'nowrap',
width: '100%',
}),
menu: base => ({
...base,
borderRadius: 0,
hyphens: 'auto', // beautify the word cut by adding a dash see https://caniuse.com/#search=hyphens for the compatibility
marginTop: 0, // kill the gap
textAlign: 'left',
}),
menuList: base => ({
...base,
padding: 0, // kill the white space on first and last option
backgroundColor: 'grey',
maxHeight: '80px',
overflowY: 'auto',
}),
indicatorSeparator: base => ({
...base,
display: 'none',
}),
dropdownIndicator: (base, state) => ({
...base,
transition: 'all .2s ease',
transform: state.isFocused ? 'rotate(180deg)' : null,
}),
noOptionsMessage: base => ({
...base,
color: 'white',
}),
valueContainer: base => ({
...base,
overflowX: 'hidden',
display: 'inline-block',
}),
input: base => ({
...base,
display: 'inline-block',
}),
};
thank you!
can you add below styles to your input element (assuming it is valueContainer and check if it hides the overflow
{
display: block;
width: 100px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}

Resources