How to change the colour of ReactSelect component? - css

Hello I am using the React select component, import ReactSelect, {ValueType} from 'react-select';
Currently my component looks like this:
This is the code for it:
<div>
<ReactSelect
className='react-select react-select-top'
classNamePrefix='react-select'
id='displayLanguage'
menuIsOpen={this.state.openMenu}
menuPortalTarget={document.body}
options={timeOptions}
clearable={false}
onChange={this.onChange}
onKeyDown={this.handleKeyDown}
value={this.state.selectedOption}
onMenuClose={this.handleMenuClose}
onMenuOpen={this.handleMenuOpen}
aria-labelledby='changeInterfaceLanguageLabel'
isDisabled={false}
/>
</div>
I want to change the colour of it to grey to look more like this but I am not sure how to:

Try this out,
found here https://react-select.com/styles
const customStyles = {
option: (provided, state) => ({
...provided,
borderBottom: '1px dotted pink',
color: grey,
padding: 20,
}),
control: () => ({
// none of react-select's styles are passed to <Control />
width: 200,
}),
singleValue: (provided, state) => {
const opacity = state.isDisabled ? 0.5 : 1;
const transition = 'opacity 300ms';
return { ...provided, opacity, transition };
}
}
const App = () => (
<Select
styles={customStyles}
options={...}
/>
);

React-select has a prop styles where you set an object that has a list of predefined properties for styling.
You can find more info here: https://react-select.com/styles
But to makes this more easy for you, in your situation you would write something like this.
<ReactSelect
className='react-select react-select-top'
classNamePrefix='react-select'
id='displayLanguage'
menuIsOpen={this.state.openMenu}
menuPortalTarget={document.body}
options={timeOptions}
clearable={false}
onChange={this.onChange}
onKeyDown={this.handleKeyDown}
value={this.state.selectedOption}
onMenuClose={this.handleMenuClose}
onMenuOpen={this.handleMenuOpen}
aria-labelledby='changeInterfaceLanguageLabel'
isDisabled={false}
styles={{
control: (styles) => ({
...styles,
background: '#eee'
})
}}
/>

Related

Adjust the width of the dropdown button

I'm using the preview Dropdown of Fluent UI.
I would like to reduce the width of the button. I tried to modify maxWidth: "500px", it did not work. I tried to add root={{ minWidth: "100px"}} in the <Dropdown, it did not work either.
Could anyone help?
CodeSandbox: https://codesandbox.io/s/distracted-gianmarco-51guru?file=/example.tsx
import { makeStyles, shorthands, useId } from "#fluentui/react-components";
import {
Dropdown,
Option,
OptionGroup,
DropdownProps
} from "#fluentui/react-components/unstable";
import * as React from "react";
const useStyles = makeStyles({
root: {
// Stack the label above the field with a gap
display: "grid",
gridTemplateRows: "repeat(1fr)",
justifyItems: "start",
...shorthands.gap("2px"),
maxWidth: "500px"
}
});
export const Grouped = (props: Partial<DropdownProps>) => {
const dropdownId = useId("dropdown-grouped");
const land = ["Cat", "Dog", "Ferret", "Hamster"];
const water = ["Fish", "Jellyfish", "Octopus", "Seal"];
const styles = useStyles();
return (
<div className={styles.root}>
<label id={dropdownId}>Best pet</label>
<Dropdown
aria-labelledby={dropdownId}
placeholder="Select an animal"
{...props}
>
<OptionGroup label="Land">
{land.map((option) => (
<Option key={option} disabled={option === "Ferret"}>
{option}
</Option>
))}
</OptionGroup>
<OptionGroup label="Sea">
{water.map((option) => (
<Option key={option}>{option}</Option>
))}
</OptionGroup>
</Dropdown>
</div>
);
};
Grouped.parameters = {
docs: {
description: {
story:
"Dropdown options can be semantically grouped with the `OptionGroup` element, with an optional group label."
}
}
};
Your issue it the "min-width:250px" value assigned to the button, I don't work with React but I did manage to do it this way, not sure if it is the best way but it does work :)
style={{minWidth:"auto"}}
Result:

Is there a way to style react select while keeping most of the original style?

I am using https://react-select.com/home#getting-started and I am trying to create something as close as possible to this:
The original style of select that I coded (without customStyles function from the select library) gave me something like this:
But as soon as I start adding the styles (customStyles function from the select library) as showed here below:
import { useEffect, useState } from 'react';
// styled components
import styled from 'styled-components';
// icons
import { IoIosArrowDown } from 'react-icons/io';
// select input
import Select from 'react-select';
const ContextItem = ({ text, options }) => {
const [treeNodeNames, setTreeNodeNames] = useState();
// THIS IS FROM THE LIBRARY
const customStyles = {
option: (provided, state) => ({
...provided,
// borderBottom: '1px dotted pink',
color: state.isSelected ? 'white' : 'black',
}),
control: () => ({
// none of react-select's styles are passed to <Control />
width: 200,
}),
singleValue: (provided, state) => {
const opacity = state.isDisabled ? 0.5 : 1;
const transition = 'opacity 300ms';
return { ...provided, opacity, transition };
},
};
useEffect(() => {
if (options) {
const treeNodes = options.data.findTreeNodesByTree.map((element) => {
return {value: element.name, label: element.name };
});
setTreeNodeNames(treeNodes);
}
}, [options]);
return (
<ContextContainer>
<ContextTitle>
<p> {text}</p>
</ContextTitle>
{treeNodeNames && (
<SearchInput>
<Select options={treeNodeNames} styles={customStyles} />
</SearchInput>
)}
</ContextContainer>
);
};
// STYLES
const ContextContainer = styled.div`
display: flex;
align-items: center;
justify-content: space-between;
`;
const ContextTitle = styled.div`
display: flex;
width: 30%;
align-items: center;
justify-content: space-between;
`;
const SearchInput = styled.div`
width: 60%;
padding: 5px;
outline: black;
`;
export default ContextItem;
I get something like this:
So the question is: how do I achieve the desired results showed in the first photo using the customStyles from select without destroying everything? is there a way to have the original react-select's style passed to and modify them accordingly. At the end I just need the:
No border around the select option
The select option moved to the right
same color
But different background on when selected or hovered
and no changed border color when clicked on the selected input field
I hope makes sense, let me know if the question is not clear:) I appreciate any sort of help and clues.
This react-select component is composed of multiple smaller parts.
You're on the right track to apply custom styles. Here is a list of various parts of react-select that you can customize.
The syntax:
const customStyles = {
option: (provided, state) => ({
...provided,
color: state.isSelected ? 'red' : 'blue',
// rest of styling
})
}
For example, if you want to change the text for no options available, you'll style the noOptionsMessage.
Other things to note:
To access the pseudo-classes like :hover for the option component, CSS-in-JS uses this syntax: "&:hover": {...}.
More details on how to inspect and style a component's pseudostates here.
Here is a codesandbox which has the react-select styled close to your first picture.
You can use styled-components then use the base component as the Select you want then change specific things about it as you wish
Here's another SO thread on the matter
Styled-components docs on the matter https://styled-components.com/docs/advanced#issues-with-specificity

How override Material UI Popover CSS classes in Select component in React

I am using Material UI Select component inside my React project.
I am trying to override the CSS class .MuiPaper-root and or .MuiMenu-list.
My Select component:
<Select
value={selectValue}
disableUnderline
onChange={handleChange}
css={styles.select}
>
{cities?.map((city) => {
return (
<MenuItem
key={city.value}
value={city.value}
css={styles.selectItem}
>
{city.label}
</MenuItem>
);
})}
</Select>
Below isn't working?
export default ({ theme }: StylesProps) => ({
select: css`
.MuiPaper-root {
background-color: red;
}
`,
});
According to the doc, there are several ways that we can modify styles in MUI. In order to change MuiPaper, we can take advantage of createMuiTheme and create a theme as below to override MuiPaper:
const theme = createMuiTheme({
overrides: {
MuiPaper: {
root: {
color: "white"
}
}
}
});
Then, we need to pass it as a theme prop to the ThemeProvider component:
<ThemeProvider theme={theme}>
//***Other part of your code***//
</ThemeProvider>
when it comes to changing MenuProps in the Select component, we can use a property called MenuProps in the Select component(description in doc)
First, I created a list style in useStyles:
const useStyles = makeStyles((theme) => ({
//other classes//
list: {
backgroundColor: "blue"
}
}));
and then passed it as a MenuProp property to the select component:
<Select
labelId="demo-simple-select-label"
id="demo-simple-select"
value={age}
onChange={handleChange}
MenuProps={{ classes: { list: classes.list } }}
>
//***other part of your code***//
</Select>
Here is a codesandbox example that I've created for this example. In the Muipaper modification, I changed the color of the text to white. And in the MenuProps changed the background color to blue.

Trying to show tooltip in react-big-calendar month view but row covers it up?

I am trying to show a basic tooltip when the user clicks on an event in the calendar with a few event details but the tooltip gets covered by the next row.
Currently, I'm trying to use the slotPropGetter prop to change the style of a cell to include z-index: -1 and inline-styling the tooltip with z-index: 1000 but to no avail.
Here's my current component:
export default() =>{
const eventStyleGetter = ({ color }) => {
const style = {
backgroundColor: color,
opacity: 0.8,
zindex: "6",
position:"relative",
};
return {
style: style,
};
};
const slotStyleGetter = () =>{
const style = {
position: "relative",
zindex:0,
height:"100px",
}
return{
style: style
}
}
const CalendarElement = (
<Calendar
localizer={localizer}
defaultDate={new Date()}
events={events}
style={{ height: 500 }}
eventPropGetter={(event) => eventStyleGetter(event)}
slotPropGetter={(slot) => slotStyleGetter(slot)}
onSelectSlot={(e) => handleSlotSelect(e)}
selectable
popup
components={{
event: EventPopover,
}}
/>
);
return(
{CalendarElement}
)
}
The issue isn't the cell z-index, but that of your tooltip. What are you using to render your tooltip? Under the hood, RBC has react-bootstrap#^0.32.4, which uses react-overlay#^0.8.0. If you use react-overlay to create your tooltips, you can portal them, which should automatically handle your z-index issue.
The way to correctly implement this is to use Reactstrap/Bootstrap Popover (based on Popperjs) rather than plain CSS. It worked in this case.

Animated button with React Spring

I am coming from a React-pose background and like to try React-spring. I have a really simple case which I'd like to transfer to be used with React-spring.
I have the case written in a Codesanbox using React-pose, https://codesandbox.io/s/4wxzm60nk9
I've tried converting this myself, but I just end up confusing myself. Especially now when trying to do it with their hooks API. All help that I can get is super appriciated.
Thank you.
I just made an animated button yesterday, so I refactored it to change its size.
import React, {useState} from "react";
import { Spring, animated as a } from 'react-spring/renderprops';
const SpringButton = () => {
const [pressed, setPressed] = useState(false);
return (
<Spring native from={{scale: 1}} to={{scale: pressed? 0.8 : 1}}>
{({scale}) => (
<a.button
style={{
backgroundColor: 'red',
height: '100px',
width: '100px',
color: 'rgb(255, 255, 255)',
transform: scale.interpolate(scale => `scale(${scale})`)
}}
onMouseDown={() => setPressed(true)}
onClick={() => setPressed(false)}
onMouseLeave={() => setPressed(false)}
>
Click me
</a.button>
)}
</Spring>
);
}
Its not the hook interface yet, but I think it helps you to understand how it works. I t also uses the quicker native rendering. The event handling a bit different from react-pose. And you can tweek the config as well if you want the animation really springy.
import {config} from 'react-spring/renderprops';
<Spring config={config.wobbly} ...>
https://codesandbox.io/s/7zlrkv4kjj
Something like this probably: https://codesandbox.io/s/pyvo8mn5x0
function App() {
const [clicked, set] = useState(false)
const { scale } = useSpring({ scale: clicked ? 0.8 : 1 })
return (
<div className="App">
<animated.button
onMouseDown={() => set(true)}
onMouseUp={() => set(false)}
style={{
backgroundColor: 'red',
height: '100px',
width: '100px',
color: '#FFF',
transform: scale.interpolate(s => `scale(${s})`)
}}
children="Click me"
/>
</div>
)
}
You could also interpolate up-front if you like:
const props = useSpring({ transform: `scale(${clicked ? 0.8 : 1})` })
return <animated.button style={props} />
Unlike pose react-spring does not include gesture stuff, it choses to interface with 3rd party libs for that. For instance: https://github.com/react-spring/react-with-gesture

Resources