Material-UI v5 override MuiSwitch input - reactjs

I need to use Switch component from MUI v5 in a website with third party CSS that apply relative position to checkboxes and radios.
In v4 I was able to change this behaviour with an override:
overrides: {
MuiSwitch: {
input: { position: 'absolute !important' },
}
}
This was enough to set position to absolute.
Following the example provided by official documentation in order to migrate to v5 I changed the above snippet in:
components:{
MuiSwitch: {
styleOverrides: {
input: {
position: "absolute !important"
}
}
}
}
But MuiSwitch-input still has position relative.
Please take a look at this demo.
The only solution I found was to put style in CSS:
.MuiSwitch-input {
position: absolute !important;
}

I'm not sure why your code didn't work, but I've found a workaround by specifying the switch input className directly like this:
import Switch, { switchClasses } from "#mui/material/Switch";
import { createTheme, ThemeProvider } from "#mui/material/styles";
const theme = createTheme({
components: {
MuiSwitch: {
styleOverrides: {
root: {
[`& .${switchClasses.input}`]: {
position: "absolute"
}
},
switchBase: {
color: "red"
}
}
}
}
});
Live Demo

Related

How to change the styling of a selected row in MUI5?

I am fairly new to MaterialUI and I'm using V5. I have seen the code samples for this on v4 but not sure how to do the same on mui5. I have a data grid and I want to change the background color + font colour when selected. How do I do this?
My current table:
Update: Got a sandbox code demo to my query. This should be good
https://codesandbox.io/s/condescending-dijkstra-zc2rhs?file=/src/Demo.tsx
Edit: Attaching the code below
const myTheme = createTheme({
components: {
//#ts-ignore - this isn't in the TS because DataGird is not exported from `#mui/material`
MuiDataGrid: {
styleOverrides: {
row: {
"&.Mui-selected": {
backgroundColor: "rebeccapurple",
color: "yellow",
"&:hover": {
backgroundColor: "purple"
}
}
}
}
}
}
});
#Rick45888, now you can use the following for typescript support in pro components-
import type {} from "#mui/x-data-grid-pro/themeAugmentation";

Style Chakra UI FormControl and label at application theme level

Have been using Chakra for a while but I can not get my head around styling all my components, in this case, FormControl at a global level with the theme file.
For example if i want to add margin bottom to my FormControl and FormLabel elements I would add the components object to the theme file like so:
components: {
Form: {
parts: ['control', 'label', 'errorMessage'],
baseStyle: {
control: {
marginBottom: '2rem',
},
label: {
marginBottom: '3rem',
},
},
},
},
But this has no effect on the base style of the rendered FormControl or FormLabel.
Could someone please help me with what I am doing wrong here?
Having looked through the source code a bit more there is no parts array to FormControl as it is a Context rather than a component. Therefore, it cannot be styled!
This worked for me:
import type { ComponentStyleConfig } from '#chakra-ui/theme';
import { extendTheme } from '#chakra-ui/react'
const Form: ComponentStyleConfig = {
// The styles all button have in common
parts: ['container'],
baseStyle: {
/// The container styles the FormControl
container: {
marginY: '20px'
}
},
}
const theme = extendTheme({
components: {
Form,
},
})
export const formStyles = {
parts: ['container', 'requiredIndicator', 'helperText'],
baseStyle: {
container: {
label: {
fontSize: '14px',
fontWeight: 'bold',
},
},
},
};
Import it into your component styles
components: {
Form: formStyles,
}

Styling create-row input elements in Material Table

i wan to increase the with of a drop-down list "Birth Place" inside the Materialtable in reactjs
i want some thing like that and i dont know where can add this css code width: 60%; for this drop downlist
You can style those elements with the createMuiTheme of the material-ui lib:
const theme = createMuiTheme({
overrides: {
MuiTableRow: {
root: {
"&[mode=add]": {
"& .MuiInputBase-root": {
width: "90%",
background: '#dedede',
}
}
}
}
}
});
The problem starts when you need to style specific input in specific cell, because you can't control style/props of that element.
The solution is pretty ugly, but I couldn't find a better one (at least for version 1.57.2):
const theme = createMuiTheme({
overrides: {
MuiTableRow: {
root: {
"&[mode=add]": {
"& .MuiInputBase-root": {
width: "90%",
background: "#a1a1a1"
},
"& .MuiTableCell-body:nth-child(4)": {
"& .MuiInputBase-root": {
width: "100%",
background: "#d1d1d1"
}
}
}
}
}
}
});
You can find the complete working example here: https://codesandbox.io/s/material-table-style-create-row-cbpzk?file=/demo.js

MaterialUI theme styling for nested classes

I'm creating a theme for an app with createMuiTheme. I'm using material-table and I need to target to this icon of the column table header that is currently sorted:
Watching in developer tools it has the following CSS selectors:
.MuiTableSortLabel-root.MuiTableSortLabel-active.MuiTableSortLabel-root.MuiTableSortLabel-active .MuiTableSortLabel-icon {
color: rgba(0, 0, 0, 0.54);
opacity: 1;
}
How can i do that in the createMuiTheme object from this?
const theme = createMuiTheme({
overrides : {
MuiTableSortLabel: {
root: {
//i want to modify the icon color
color: blue
}
}
}
})
When you are uncertain how to override the default styles, the best resource is to look at how the default styles are defined. The browser developer tools will show you the end result, and the source code will show you the approach to use to generate CSS with similar specificity.
Below is the relevant code from TableSortLabel that controls the icon color:
export const styles = theme => ({
/* Styles applied to the root element. */
root: {
'&$active': {
// && instead of & is a workaround for https://github.com/cssinjs/jss/issues/1045
'&& $icon': {
opacity: 1,
color: theme.palette.text.secondary,
},
},
}
});
You can use very similar syntax for the theme override:
const theme = createMuiTheme({
overrides: {
MuiTableSortLabel: {
root: {
"&$active": {
"&& $icon": {
color: "blue"
}
}
}
}
}
});
Relevant JSS Documentation: https://cssinjs.org/jss-plugin-nested/?v=v10.0.3#use-rulename-to-reference-a-local-rule-within-the-same-style-sheet

How do you change the color of a stepper in material-ui in React

Referring to this:
https://material-ui.com/demos/steppers/
It is blue by default.
I want it to be material-ui's orange200
I tried the following code (from this stackoverflow answer) but it did not work.
import getMuiTheme from 'material-ui/styles/getMuiTheme'
const muiTheme = getMuiTheme({
stepper: {
iconColor: 'green' // or logic to change color
}
})
<MuiThemeProvider muiTheme={muiTheme}>
<Stepper>
...
</Stepper>
</MuiThemeProvider>
Use Chrome DevTools (or other browsers' dev tools) to find out a class that will give you an information about an element to override.
For example, assuming you found that the class name is
.MuiStepIcon-root-78. The formulae is Mui[component name]-[style rule name]-[UUID], where Mui is a default prefix and 78 is just an id. So, the element's name is MuiStepIcon and a subsequent attribute is root.
Say, you want to change the color. If you do the hierarchy MuiStepIcon -> root -> color, you will change the default color only. To change any other colors, watch out for pseudo classes. For example, using devtools, if the class is .MuiStepIcon-root-78.MuiStepIcon-active-80, then the pseudo class is active, and your code should be MuiStepIcon -> root -> '&$active' -> color. Look at the code below for references.
Look at the docs for more info https://material-ui.com/customization/overrides/#overriding-with-classes
You can also determine available elements to override by referring to createMuiTheme -> overrides, which will take you to overrides.d.ts file. There is an interface that lists all components names, like MuiStepIcon, though it won't give you other information as devtools do.
import React, { Component } from 'react';
import { MuiThemeProvider, createMuiTheme } from '#material-ui/core/styles';
const muiTheme = createMuiTheme({
overrides: {
MuiStepIcon: {
root: {
color: '#000000', // or 'rgba(0, 0, 0, 1)'
'&$active': {
color: '#000000',
},
'&$completed': {
color: '#000000',
},
},
},
}
});
const otherStyles = theme => ({
root: {
// Whatever needed
},
});
class MyComponent extends Component {
render(){
return (
<MuiThemeProvider theme={muiTheme}>
{
// Your stepper here, should be within MuiThemeProvider
}
</MuiThemeProvider>
);
}
};
export default withStyles(otherStyles, { withTheme: true })(MyComponent);
overrides: {
MuiStepIcon: {
root: {
color: '#000000', // or 'rgba(0, 0, 0, 1)'
'&$active': {
color: '#000000',
},
'&$completed': {
color: '#000000',
},
},
}
did work for me
One way to change the color and styling of icon of stepper material UI is to pass icon prop in StepLabel as:
<StepLabel
icon = <div style={{backgroundColor: 'orange', width:'11px', padding: '2px', textAlign: 'center', height: '11px', fontSize: '10px', borderRadius: '50%'}}>{index}</div>
>{label}</StepLabel>

Resources