How to reduce the size of React Select in v2 - react-select

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)?

Try passing in a value for the maxMenuHeight prop:
<Select
maxMenuHeight={190}
/>
see documentation

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;
}

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}/>

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',
}),
};

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={...}
/>
);

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.

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

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}
/>

Related

Style Drawer menu toggle button in react native

I'm trying to add some background shade to the toggle button and also increase the size a little more, but I'm unable to find the right prop that targets the button.
Here's my code.
<Drawer.Navigator screenOptions={(navigation) => ({
drawerStyle:{width:280},
drawerItemStyle: {
borderRadius: 0,
width: '100%',
marginVertical: 0,
marginLeft: 0,
},
drawerLabelStyle: {
fontFamily: 'sans-serif',
fontWeight:'100'
},
drawerActiveTintColor: '#288df9',
drawerInactiveTintColor: '#444'
})}>
...
</Drawer.Navigator>
Any help on how to style the toggle button will be rightly appreciated.
If you check the implementation of this Drawer you can find that this button is an Image hardcoded inside: https://github.com/react-navigation/react-navigation/blob/b91c9b05ff96727f5fa6ef0bec51b5d7eac06600/packages/drawer/src/views/DrawerToggleButton.tsx#L37
export default function DrawerToggleButton({ tintColor, ...rest }: Props) {
const navigation = useNavigation<DrawerNavigationProp<ParamListBase>>();
return (
<PlatformPressable
{...rest}
accessible
accessibilityRole="button"
android_ripple={{ borderless: true }}
onPress={() => navigation.dispatch(DrawerActions.toggleDrawer())}
style={styles.touchable}
hitSlop={Platform.select({
ios: undefined,
default: { top: 16, right: 16, bottom: 16, left: 16 },
})}
>
<Image
style={[styles.icon, tintColor ? { tintColor } : null]}
source={require('./assets/toggle-drawer-icon.png')}
fadeDuration={0}
/>
</PlatformPressable>
);
}
I think what you can do is replace the image in your node_modules and using patch-package save it as a patch in your local repository.
Another way is to implement your own Button and use openDrawer/closeDrawer methods to control the drawer.

PanGestureHandler with functional component react native

I am trying to use a Gesture handler with a functional component. The problem is when I drag for the second time it's dragging from initial position again
This is my code below
let translateXRef = useRef(new Animated.Value(0)).current;
const onGestureEvent = useCallback(
Animated.event(
[
{
nativeEvent: {
translationX: translateXRef,
},
},
],
{ useNativeDriver: true }
),
[]
);
<View
style={{
backgroundColor: '#FFFFFF80',
position: 'absolute',
top: 0,
bottom: 0,
left: 0,
right: 0,
height: 100,
}}
>
<PanGestureHandler
onGestureEvent={onGestureEvent}
onHandlerStateChange={onHandlerStateChange}
>
<Animated.View
// eslint-disable-next-line react-native/no-inline-styles
style={{
height: '100%',
width: 10,
backgroundColor: AppColors.buttonColor,
transform: [{ translateX: translateXRef }],
}}
/>
</PanGestureHandler>
</View>
You need to use the context in addition to the event in your callback.
I'm not sure why you're using the Animated.event. You should generate your callbacks using the useAnimatedGestureHandler.
Each of those callbacks onStart, onActive, onEnd, etc... take an event and context argument.
The context argument is an object that would let you set your previous position so that then next click would not reset the position.
Here's more info:
https://docs.swmansion.com/react-native-reanimated/docs/fundamentals/events/#using-context
Also, a pretty good video that explains it:
https://youtu.be/4HUreYYoE6U

How to remove red background on MultiValueRemove when focused

I want to style the MultiValueRemove container when it is focused (the one with the x inside to remove the chosen value). The backgroundColor changes to a red I don't want to have.
I can style the background when it is neither focused nor selected, but the "onHover" red background remains unaffected. Styling the background with the state isSelected, isFocused does not affect the red background when I hover on it.
multiValueRemove: (provided, state) => ({
...provided,
color: '#ffffff',
backgroundColor: '#6FC5C4',
borderRadius: 0,
}),
There's a trick for this one, isSelected and isFocused don't work in this case but you can use regular css hover state like this:
multiValueRemove: (base, state) => ({
...base,
color: "#fff",
backgroundColor: "#6FC5C4",
borderRadius: 0,
"&:hover": {
backgroundColor: "#6FC5C4",
color: "#fff"
}
})

V2 react-select css styling issues

when using react-select v2, I can't figure out how to set the font of the input field..
I have tried the following:
input: (base) => ({
...base,
fontFamily: 'Arial',
fontSize: 13,
fontWeight: 500,
color: 'green'
})
the color is working accordingly but none of the other font styles are applied.
Also is there a way of setting the color of the dropdownIndicator on hover of the control?
Like:
control: (base, { isFocused }) => ({
...base,
':hover': {
### Set the dropdownIndicator Color
},
}),
I know v2 is still in beta but I would really appreciate some help :)

Is there the way to change white color of a main page mask of "react-native-drawer" component?

As I see in android applications almost all drawers in open state makes a black "mask" above main content.
white mask in "react-native-drawer:
black color example:
It is possible to change the color of this "mask" in "react-native-drawer" component to black?
Was looking for the same thing and found a solution at GitHub.
tweenHandler={ratio => ({
main: {
opacity: 1,
},
mainOverlay: {
opacity: ratio / 2,
backgroundColor: 'black',
},
})}
Tested it out and here's what I got (made it pink for visibility):
<Drawer
tweenHandler={ratio => ({
main: {
opacity: 1,
},
mainOverlay: {
opacity: ratio / 2,
backgroundColor: 'pink',
},
})}
ref={(ref) => { this._drawer = ref; }}
content={ navigationView }
side="right"
panOpenMask={.25}
>
Screenshot: