Problem in Increasing Label in Material-Ui in React - css

I have successfully increased the Label in the TextField of my React app.
My problem is that when its on shrink, it just overlaps some line on its right.
Click Here
import React from "react";
import TextField from "#material-ui/core/TextField";
import { makeStyles } from "#material-ui/core/styles";
const useStyles = makeStyles((theme) => ({
root: {
"& .MuiInputLabel-shrink": {
fontSize: "24px"
}
}
}));
export default function CustomTextField({ InputLabelProps = {}, ...props }) {
const classes = useStyles();
return <TextField {...props} className={classes.root} />;
}

I would encourage you to always use the DevTools when it comes to applying customizations. The size of the gap it determined by the <legend> element:
The element's font size has a font-size: 0.75em to account for the CSS transformation.
So you can simply apply the same font size to its parent:
import React from "react";
import TextField from "#material-ui/core/TextField";
import { makeStyles } from "#material-ui/core/styles";
const useStyles = makeStyles((theme) => ({
root: {
"& .MuiInputLabel-shrink, & fieldset": {
fontSize: "24px"
}
}
}));
export default function CustomTextField({ InputLabelProps = {}, ...props }) {
const classes = useStyles();
return <TextField {...props} className={classes.root} />;
}
https://codesandbox.io/s/material-ui-custom-textfield-composition-forked-g5co7?file=/src/CustomTextField.js

You should use the Shrink as prop for TextField
Remove fontSize: "24px" let it resize by Material-UI
Make sure your TextField as:
<TextField InputLabelProps={{shrink: true}} .../>
if you want to customize the size of the label:
fontSize: 30,
color: "red",
"&$labelFocused": {
color: "purple"
}
},
labelFocused: {}
};
function App({ classes }) {
return (
<div className="App">
<TextField
id="standard-with-placeholder"
label="Your Label"
InputLabelProps={{
classes: {
root: classes.labelRoot,
focused: classes.labelFocused
}

Related

How to change label transform in TextField (MUI package)

When clicking the text area, I want to put the label on top right of text box with transparent border, so it will be like this:
But instead, I received something strange:
import TextField from "#mui/material/TextField";
import { createTheme, ThemeProvider } from "#mui/material/styles";
import { makeStyles } from "#material-ui/core/styles";
const theme = createTheme({
direction: "rtl"
});
const useStyles = makeStyles((theme) => ({
root: {
"& label": {
width: "100%",
textAlign: "right",
transformOrigin: "center",
"&.Mui-focused": {
transform: "translate(-5, -5px) scale(0)",
transformOrigin: "center",
},
},
},
}));
export default function CatalogTextBox(params) {
const classesStyles = useStyles();
return (
<ThemeProvider theme={theme}>
<div className="pt-2 col-sm" dir="rtl">
<TextField
className={classesStyles.root}
type={params.type}
id="outlined-basic"
label={params.text}
variant="outlined"
/>
</div>
</ThemeProvider>
);
}

Material UI: How can I change the border color of the Select component?

I am trying to customize the Select component from Material UI.
This is what it looks like:
However, when the select component is focused, I want to change the border-color from material UI's blue to a custom red color.
I tried setting the styles but it doesn't do anything at all
import FormControl from '#material-ui/core/FormControl';
import InputLabel from '#material-ui/core/InputLabel';
import MuiSelect from '#material-ui/core/Select';
import MenuItem from '#material-ui/core/MenuItem';
import { makeStyles } from '#material-ui/core/styles';
const useStyles = makeStyles((theme) => ({
formControl: {
margin: theme.spacing(1),
minWidth: 120,
},
select: {
borderColor: '#FF0000', //<------------ this does nothing
},
}));
const Select = () => {
const classes = useStyles();
return (
<FormControl variant="outlined" className={classes.formControl}>
<InputLabel>Months</InputLabel>
<MuiSelect label="Months" className={classes.select}>
<MenuItem value="1">January</MenuItem>
<MenuItem value="2">February</MenuItem>
<MenuItem value="3">March</MenuItem>
<MenuItem value="4">April</MenuItem>
</MuiSelect>
</FormControl>
);
};
Select.propTypes = {};
export default Select;
Try this:
const useStyles = makeStyles((theme) => ({
formControl: {
margin: theme.spacing(1),
minWidth: 120,
},
select: {
'&.Mui-focused .MuiOutlinedInput-notchedOutline': {
borderColor: 'red',
},
},
}));
If someone is interested in the latest version of MUI 5.
import {
Select,
} from '#mui/material';
import { SxProps } from '#mui/material/styles';
import classes from './component.module.css';
const styles: SxProps = {
select: {
'.MuiOutlinedInput-notchedOutline': {
borderColor: '#color',
},
'&:hover .MuiOutlinedInput-notchedOutline': {
borderColor: '#color',
borderWidth: '0.15rem',
},
},
};
<Select
variant="outlined"
sx={styles.select}
inputProps={{
classes: {
icon: classes.icon,
},
}}
>
For the icon, you can create a separate file component.module.css
.icon {
fill: #color;
}

How to change border color of material ui Rating stars

I need to change the border color of stars in material ui Rating cause my background color is black and I can't see nothing when the star is empty !
code:
import Rating from '#material-ui/lab/Rating';
import { makeStyles } from '#material-ui/core/styles';
const useStyles = makeStyles((theme) => ({
root: {
display: 'flex',
flexDirection: 'column',
'& > * + *': {
marginTop: theme.spacing(1),
},
},
}));
in the functional component :
<div className={classes.root}>
<Rating name="half-rating-read" defaultValue={finalAverage} precision={0.5} readOnly />
</div>
you need to import an icon with border for empty one like StarBorderIcon and add it like this:
import Rating from "#material-ui/lab/Rating";
import StarBorderIcon from "#material-ui/icons/StarBorder";
import { makeStyles } from "#material-ui/core/styles";
const useStyles = makeStyles((theme) => ({
root: {
display: "flex",
flexDirection: "column",
"& > * + *": {
marginTop: theme.spacing(1)
}
},
emptyStar: {
color: "white"
}
}));
const Star = () => {
const classes = useStyles();
return (
<div className={classes.root}>
<Rating
name="half-rating-read"
defaultValue={3.5}
precision={0.5}
readOnly
emptyIcon={
<StarBorderIcon fontSize="inherit" className={classes.emptyStar} />
}
/>
</div>
);
};
export default Star;

Material UI using tabs panel and CSS

I can not change the color and CSS from the TAB PANEL using material-ui Some ideas? :(
Looks like useStyle and theme are not working. I could change some other properties like scrollable but not the colors. I wonder if there is some conflict con other CSS, but I don't think so because the colors I see from the TABs are blue, I'm not using blue in my Web-App.
import PropTypes from 'prop-types';
import { makeStyles } from '#material-ui/core/styles';
import AppBar from '#material-ui/core/AppBar';
import Tabs from '#material-ui/core/Tabs';
import Tab from '#material-ui/core/Tab';
import Typography from '#material-ui/core/Typography';
import Box from '#material-ui/core/Box';
function TabPanel(props) {
const { children, value, index, ...other } = props;
return (
<div
role="tabpanel"
hidden={value !== index}
id={`scrollable-auto-tabpanel-${index}`}
aria-labelledby={`scrollable-auto-tabpanel-${index}`}
{...other}
>
{value === index && (
<Box p={3}>
<Typography>{children}</Typography>
</Box>
)}
</div>
);
}
TabPanel.propTypes = {
children: PropTypes.node,
index: PropTypes.any.isRequired,
value: PropTypes.any.isRequired,
};
function a11yProps(index) {
return {
id: `scrollable-auto-tabpanel-${index}`,
'aria-controls': `scrollable-auto-tabpanel-${index}`,
};
}
function LinkTab(props) {
return (
<Tab
component="a"
onClick={(event) => {
event.preventDefault();
}}
{...props}
/>
);
}
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
margin: 0,
background: 'white',
},
}));
export default function NavTabs() {
const classes = useStyles();
const [value, setValue] = React.useState(0);
const handleChange = (event, newValue) => {
setValue(newValue);
};
return (
<AppBar position="static">
<Tabs
variant="container-fluid"
value={value}
onChange={handleChange}
variant="scrollable"
scrollButtons="auto"
aria-label="scrollable auto tabs example"
centered
>
It's actually the AppBar that has that blue color. Upon reviewing the stylesheet, the individual tab items actually have transparent as a default value for background-color. So to solve this, just override the background of the root element of AppBar
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
margin: 0,
background: "white"
}
}));
export default function NavTabs() {
const classes = useStyles();
<AppBar position="static" classes={{ root: classes.root }}>
...

How to add linear-gradient color to Slider?

I want to add linear-gradient to Material-UI Slider as color. Is it possible? I try everything.
color: 'linear-gradient(180deg, #29ABE2 0%, #00EAA6 100%)'
linear-gradient creates an image not a color. So you need to use it in CSS that specifies an image (e.g. background-image).
Below is an example of a Slider using a gradient.
import React from "react";
import { makeStyles, withStyles } from "#material-ui/core/styles";
import Slider from "#material-ui/core/Slider";
const useStyles = makeStyles({
root: {
width: 200
}
});
const CustomSlider = withStyles({
rail: {
backgroundImage: "linear-gradient(.25turn, #f00, #00f)"
},
track: {
backgroundImage: "linear-gradient(.25turn, #f00, #00f)"
}
})(Slider);
export default function ContinuousSlider() {
const classes = useStyles();
const [value, setValue] = React.useState(30);
const handleChange = (event, newValue) => {
setValue(newValue);
};
return (
<div className={classes.root}>
<CustomSlider
value={value}
onChange={handleChange}
aria-labelledby="continuous-slider"
/>
</div>
);
}

Resources