I am trying to change the colour of my Buttons nested inside a ButtonGroup. I want to change the text colour as well as the "|" lines in between the buttons. I have tried overriding every class that I could find using the developer's console through styled but none of them had any effect.
const SortOptionButtonGroup = styled(ButtonGroup)({
'& .MuiButtonGroup-root': { color: pageColor, borderColor: pageColor },
'& .MuiButtonGroup-text': { color: pageColor, borderColor: pageColor },
});
const SortOptionButton = styled(Button)({
'& .MuiButton-root': { color: pageColor, borderColor: pageColor },
'& .MuiButton-text': { color: pageColor, borderColor: pageColor },
'& .MuiButton-textPrimary': { color: pageColor, borderColor: pageColor },
'& .MuiButton-sizeMedium': { color: pageColor, borderColor: pageColor },
'& .MuiButton-textSizeMedium': { color: pageColor, borderColor: pageColor },
'& .MuiButtonBase-root': { color: pageColor, borderColor: pageColor },
'& .MuiButtonGroup-grouped': { color: pageColor, borderRight: pageColor },
'& .MuiButtonGroup-groupedHorizontal ': { color: pageColor, borderColor: pageColor },
'& .MuiButtonGroup-groupedText': { color: pageColor, borderColor: pageColor },
'& .MuiButtonGroup-groupedTextHorizontal': { color: pageColor, borderColor: pageColor },
'& .MuiButtonGroup-groupedTextPrimary': { color: pageColor, borderColor: pageColor },
});
I am using v5.4.1.
You want to do it like so:
use .MuiButtonGroup-grouped to control the style the ButtonGroup children, regardless of the variant. If you'd like to be more specific, you can use a class based on the current variant instead. For example,
.MuiButtonGroup-groupedText when variant is text.
use not(:last-of-type) pseudo class to customize style of all but last buttons borders (i.e the buttonGroup dividers).
The final code look like this:
import * as React from "react";
import Button from "#mui/material/Button";
import ButtonGroup from "#mui/material/ButtonGroup";
import { styled } from "#mui/material/styles";
const StyledButtonGroup = styled(ButtonGroup)({
// change the text color for all buttons
'& .MuiButtonGroup-grouped': {
color: "green",
},
// change the button group dividers color
'& .MuiButtonGroup-grouped:not(:last-of-type)': {
borderColor: "red"
}
});
export default function VariantButtonGroup() {
return (
<StyledButtonGroup variant="text">
<Button>One</Button>
<Button>Two</Button>
<Button>Three</Button>
</StyledButtonGroup>
);
}
You can see a demo of it in this codesandbox.
You can change inner button styles (color, background-color, etc) directly through styled:
const StyledButton = styled(Button)({
color: '#ff0000',
backgroundColor: '#00ff00'
})
The divider line between buttons is implemented as buttons' border, so to change it, you should style button border inside ButtonGroup. You can do something like this:
const StyledButtonGroup = styled(ButtonGroup)({
"& .MuiButtonGroup-grouped:not(:last-of-type)": {
borderColor: "#ebc200"
}
});
Here's an example sandbox
Related
I have been trying to change the color of the default label for a textfield in mui v5. I haven't had any luck despite trying many things.
Here is what my textfield looks like:
<TextField
sx={{
input: {
color: 'blue'
},
// fontStyle: 'italic'
// MuiOutlinedInput-input MuiInputBase-input MuiInputBase-inputSizeSmall css-pfigon-MuiInputBase-input-MuiOutlinedInput-input
//
// "&:hover input": { color: 'red' },
"&:hover .MuiOutlinedInput-root": {
"& > fieldset": { border: '2px solid #40D61A'},
},
"& .MuiOutlinedInput-root": {
"& > fieldset": { border: "solid 1px green"},
},
"& .MuiOutlinedInput-root.Mui-focused": {
"& > fieldset": {
borderColor: "blue",
},
},
"& .MuiInputBase-root": {
color: 'darkBlue',
},
"& .MuiInputBase-root.Mui-focused": {
color: 'blue',
},
"& .MuiOutlinedInput-root.Mui-focused .MuiOutlinedInput-input": {
color: 'blue"'
},
"& .MuiInputLabel-outlined .MuiInputLabel-sizeSmall": {
color: 'red'
},
"& .MuiInputLabel-animated.Mui-focused": {
color: 'black'
},
}}
I am able to get the borders to change, I am able to get the text to change while focused... but I am unable to get the initial labels to change. Any help would be greatly appreciated thank you.
You can create your own custom theme and provide it to your app.
Your Theme
import { createTheme } from "#mui/material";
const myTheme = createTheme({
palette:{
light: "#abffbb",
main: "#00ffbb",
dark: "#ffffbb",
}
});
You can also only add main. light and dark will be taken accordingly by MUI itself.
Providing Theme
<ThemeProvider theme={MyTheme}>
<TextField variant='outlined' label='Text field' />
<TextField variant='filled' label='Text field' />
<TextField variant='standard' label='Text field' />
</ThemeProvider>
Now the borderColor and the color of label text will be same as main color
Here is the codesandbox link you can play with
I have created a brand new NextJs project with typescript to play a little with Chakra UI
I made some custom styles to the Default Button component but it doesn't seem to hit the small size styles or apply some base styles I have defined such as backgroundColor
Here is the code that I wrote to Customize the default Button:
NOTE: the background color does change but you have to specify both bg and background or backgroundColor properties which is kinda weird
export const Button: ComponentStyleConfig = {
baseStyle: {
borderRadius: "10px",
fontWeight: "Regular",
fontSize: "10pt",
bg: "red",
background: "red",
_focus: {
boxShadow: "none",
},
},
sizes: {
sm: {
fontSize: "8pt",
bg: "red",
background: "red",
backgroundColor: "red",
color: "white",
textColor: "white",
width: "100px",
w: "100px",
},
md: {
bg: "blue",
background: "blue",
backgroundColor: "blue",
fontSize: "10pt",
width: "200px",
},
},
variants: {
filled: {
color: "#F8F8F8",
_hover: {
backgroundColor: "#2B8CFF",
},
},
},
};
Try creating breakpoints and set them to the theme object:
// 1. Import the utilities
import { createBreakpoints } from '#chakra-ui/theme-tools';
// 2. Update the breakpoints as key-value pairs
export const breakpoints = createBreakpoints({
sm: '320px',
md: '768px',
lg: '960px',
xl: '1200px',
});
Then include it on the theme:
export default extendTheme({ breakpoints })
I cannot change font-color in TextField Material UI component. I try to do it using createTheme() and when I add class '& .MuiOutlinedInput-input' in code sandbox font-color changes as it should, but when I apply it to the app code it doesn't work.
Will appreciate Your support.
Below is the implementation:
const theme = createTheme({
components: {
MuiOutlinedInput: {
styleOverrides: {
root: {
backgroundColor: `#EDEDED`,
'& .MuiOutlinedInput-notchedOutline': {
border: 'none',
},
'&.Mui-focused': {
'& .MuiOutlinedInput-notchedOutline': {
border: 'none',
},
},
'& .MuiOutlinedInput-input': {
padding: '10px',
fontSize: '13px',
color: 'red',
},
},
},
},
},
});
<ThemeProvider theme={theme}>
<InputForm
name={'phoneNumber'}
id={'phoneNumber'}
label={phoneNumberLabelTxt}
disabled={isDisabledInputs}
/>
</ThemeProvider>
Your code worked in my local repo. however if its not working for you then try below code which also worked for me.
'&.MuiOutlinedInput-root': { // no space between & and .
padding: '10px',
fontSize: '13px',
color: 'red',
},
I have this MUI codes that are applied to multiple components. I want to separate it into a single file like 'MuiSetup.js' and then import the file to the component that I am using. I've tried with as React component, however, it does not work and I have zero clues how can I do this.
// Styling TextField
const ValidationTextField = withStyles({
root: {
'& input:valid + fieldset': {
borderColor: '#ff9800',
borderWidth: 1,
},
'& .MuiOutlinedInput-root': {
'&:hover fieldset': {
borderColor: '#ff9800',
},
'&.Mui-focused fieldset': {
borderColor: '#ff9800',
},
},
'& input:invalid + fieldset': {
borderColor: '#ff9800',
borderWidth: 1,
backgroundColor: 'black',
},
'& input:valid:focus + fieldset': {
borderColor: '#ff9800',
borderLeftWidth: 5,
padding: '4px !important', // override inline-style
},
},
})(TextField);
//Style MUI
const useStyles = makeStyles((theme) => ({
root: {
height: '100vh',
backgroundColor: 'black',
},
input: {
color: '#ff9800',
},
formBackground: {
background: 'black',
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
},
paper: {
margin: theme.spacing(8, 4),
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
},
avatar: {
margin: theme.spacing(1),
},
form: {
width: '100hv',
height: '100%', // Fix IE 11 issue.
marginTop: theme.spacing(0),
color: '#ff9800',
},
submit: {
margin: theme.spacing(3, 0, 2),
},
}));
//Label Style
const useLabelStyles = makeStyles({
root: {
color: '#ff9800',
'&.Mui-focused': {
color: '#ff9800',
},
fontSize: '14px',
},
});
-----------
const App = () => {......}
export default App
How can I take the MUI part to the separated file?
In one of my projects I actually used createMuiTheme() in another file then added my own classes under the overrides. Then the entire app is wrapped in the ThemeProvider which you pass in your custom theme. Then you can just give the component a className.
So my MuiSetup.js equivalent file looks something like this:
import { createMuiTheme } from '#material-ui/core';
// Describe material ui theme
export const customTheme = (isDark = false) => {
return createMuiTheme({
palette: isDark
? {
common: {...},
background: {...},
primary: {...},
secondary: {...},
error: {...},
text: {...},
}
: {// Light theme stuff},
overrides: {
MuiAppBar: {...},
MuiButton: {
// Button example...
root: {
'&.CustomButton': {
margin: 0,
},
'&.OtherCustomButton': {
width: '100%',
minHeight: 'inherit',
},
'&$disabled': {
color: grey[600],
},
},
outlinedSecondary: {
'&$disabled': {
border: `1px solid ${grey[600]}`,
},
},
},
// Other mui components...
},
});
};
You'll need to read the API docs to find your inputs.
Then the ThemeProvider:
import React, { Component } from 'react';
import { ThemeProvider } from '#material-ui/core';
import { customTheme } from './MuiSetup.js';
class App extends Component {
render() {
const currentTheme = customTheme(true);
return (
<ThemeProvider theme={currentTheme}>
<h1>My app</h1>
// Usage
<Button className='CustomButton'/>
</ThemeProvider>
);
}
}
export default App;
Then from any component within the app, you can do: <Button className='CustomButton'/> as I've done above.
Not sure if this is the best way, might be a bit tricky to make multiple updates however you could do something similar in your situation and create a function in another file that returns your custom makeStyles.
HTH
Ciao, to put these customizations in a separated file is just necessary to define them in aseparated file with the key "export const ..." and then, when you want to import them, just write:
import { ValidationTextField } from "./muiCustomizations"; // supposing that you defined your customizations in a file called muiCustomizations.js
and then you can use them:
function MyComponent() {
return (
<div>
<ValidationTextField />
</div>
);
}
Here a codesandbox example.
I'm trying to make my entire material UI TextField tag white. The label and the border, both off focus and on focus.
The on focus is all white, but I can't get the off-focus to be white. I see it in the dev tools as, but it must not be specific enough. I've even tried !important, but that still doesn't take priority over the original color.
I've tried about half a dozen methods and only have been able to get the on focus to work. Not sure what I'm doing wrong here.
import React from 'react';
import { withStyles } from '#material-ui/styles';
import TextField from '#material-ui/core/TextField';
const styles = theme => ({
eventWrap: {
width: '90%',
margin: '0 auto',
'$ eventInput': {
color: 'white',
},
},
eventExplaination: {
fontSize: '25px',
marginBottom: '50px',
},
root: {
color: "white !important",
borderColor: 'white !important',
},
input: {
color: "white !important",
borderColor: 'white !important',
}
});
const CssTextField = withStyles({
root: {
'& label.Mui-focused': {
color: 'white',
},
'& .MuiInput-underline:after': {
borderBottomColor: 'white',
},
},
})(TextField);
class Event extends React.Component {
nextSection = () => {
if(this.props.event !== '') {
this.props.nextSection( 'emotions' )
} else {
alert('you must write in a situation or event')
}
}
render() {
const { classes } = this.props;
return(
<div className={classes.eventWrap}>
<p className={classes.eventExplaination}>Write out an event or situation that made you feel anxious. Keep it factual, leave out all feelings about it.</p>
<CssTextField
id="custom-css-standard-input"
label="Type in Event/Situation"
value={this.props.event}
onChange={(e) => this.props.updateEvent( e.target.value )}
placeholder="Event/Situation"
fullWidth
className={classes.root}
InputProps={{ className: classes.input }}
color="primary"
/>
<button onClick={() => this.nextSection()}>Next</button>
</div>
)
}
}
export default withStyles(styles)(Event);
const WhiteTextField = withStyles({
root: {
'& .MuiInputBase-input': {
color: '#fff', // Text color
},
'& .MuiInput-underline:before': {
borderBottomColor: '#fff8', // Semi-transparent underline
},
'& .MuiInput-underline:hover:before': {
borderBottomColor: '#fff', // Solid underline on hover
},
'& .MuiInput-underline:after': {
borderBottomColor: '#fff', // Solid underline on focus
},
},
})(TextField);