如何覆盖 material-ui 的选项卡选择颜色?
How do I override material-ui's tab selection color?
我正在构建一个具有 materialui-tabs 主题的 React 16.13.0 应用程序,https://material-ui.com/api/tab/。我在我的组件中创建了这些样式 ...
const theme = createMuiTheme({
overrides: {
MuiTab: {
root: {
"&.MuiTab-root": {
backgroundColor: "black",
border: 0,
borderBottom: "2px solid",
"&:hover": {
border: 0,
borderBottom: "2px solid",
},
},
"&.Mui-selected": {
backgroundColor: "none",
borderBottom: "2px solid #373985",
borderColor: "#373985",
}
}
}
}
});
const useStyles = makeStyles((theme) => ({
root: {
width: "100%",
flexGrow: 1,
color: "#3739B5",
backgroundColor: "white",
},
viewButtons: {
marginTop: theme.spacing(2),
marginBottom: theme.spacing(1),
},
}));
这些适用于
<ThemeProvider theme={theme}>
<AppBar position="static">
<Tabs
classes={classes}
value={value}
variant="fullWidth"
centered
onChange={handleChange}
aria-label="volunteer dashboard tabs"
>
<Tab label={proposedLabel} {...a11yProps(2)} />
<Tab label={planningLabel} {...a11yProps(1)} />
<Tab label={inProgressLabel} {...a11yProps(0)} />
<Tab label={completedLabel} {...a11yProps(3)} />
</Tabs>
</AppBar>
</ThemeProvider>
我正在尝试更改所选标签的背景颜色。基于devtools,检查,class列为
.PrivateTabIndicator-colorSecondary-267 {
background-color: #f50057;
}
.PrivateTabIndicator-root-265 {
width: 100%;
bottom: 0;
height: 2px;
position: absolute;
transition: all 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
}
然而,尽管我已经在我的主题中列出了这一点,但颜色显示为红色,尽管我在我的样式中指定了什么
如何覆盖所选选项卡的边框颜色?
你能试试这个对我有用的解决方案吗?我假设您想覆盖底部边框指示器颜色。
<Tabs value={0} TabIndicatorProps={{ style: { background: "#hex-color" } }}>
<Tab className={clasess.tab} label="Home" />
<Tab className={clasess.tab} label="Services" />
</Tabs>
您现在可以使用 TabIndicatorProps 为当前版本的 MUI (4.10.02) 设置活动指示器的样式。可用文档 here.
有两种方法可以做到这一点:
方法 1:使用样式:{}
import React from "react";
import PropTypes from "prop-types";
import { Tabs, Tab, makeStyles } from "@material-ui/core";
const TabsIndicator = () => {
const [value, setValue] = React.useState(0);
const handleChange = (event, newValue) => {
setValue(newValue);
};
return (
<React.Fragment>
<Tabs
value={value}
onChange={handleChange}
TabIndicatorProps={{
style: { background: "cyan", height: "10px", top: "35px" }
}}
>
<Tab label="TEST1" value={0} />
<Tab label="TEST2" value={1} />
<Tab label="TEST3" value={2} />
<Tab label="TEST4" value={3} />
</Tabs>
</React.Fragment>
);
};
export default TabsIndicator;
方法二:使用类名:{类}
import React from "react";
import PropTypes from "prop-types";
import { Tabs, Tab, makeStyles } from "@material-ui/core";
const useStyles = makeStyles(theme => ({
indicator: {
backgroundColor: "green",
height: "10px",
top: "45px"
}
}));
const TabsIndicator = () => {
const classes = useStyles();
const [value, setValue] = React.useState(0);
const handleChange = (event, newValue) => {
setValue(newValue);
};
return (
<React.Fragment>
<Tabs
value={value}
onChange={handleChange}
TabIndicatorProps={{ className: classes.indicator }}
>
<Tab label="TEST1" value={0} />
<Tab label="TEST2" value={1} />
<Tab label="TEST3" value={2} />
<Tab label="TEST4" value={3} />
</Tabs>
</React.Fragment>
);
};
export default TabsIndicator;
你也可以看看我的sandbox here。希望这对您有所帮助!
试试这个!
indicator: {
backgroundColor : 'your favorite color',
},
<Tabs classes={{ indicator: classes.indicator }}>
<Tab>....
</Tabs>
从 MUI v5 开始,可以使用 createTheme()]
API 轻松完成,如图 here.
您需要使用如下方式覆盖样式:
let theme = useTheme();
theme = createTheme(theme, {
components: {
MuiTab: {
styleOverrides: {
root:{
"&.Mui-selected": {
backgroundColor: theme.palette.secondary.main,
color: theme.palette.secondary.contrastText,
borderRadius: "25px"
}
}
}
}
}
})
可以将此主题传递给 <ThemeProvider />
组件,该组件将包含选项卡所在的部分。
注:
useTheme()
、createTheme()
和 <ThemeProvider />
都需要从 @mui/material/styles
而不是 @emotion/react
. 导入
- 从 v5 开始,
@mui/lab
提供 <TabContext />
、<TabList />
和 <TabPanel />
组件,docs 推荐使用这些组件,尽管您仍然可以使用旧的<Tabs />
. 的组成部分
我正在构建一个具有 materialui-tabs 主题的 React 16.13.0 应用程序,https://material-ui.com/api/tab/。我在我的组件中创建了这些样式 ...
const theme = createMuiTheme({
overrides: {
MuiTab: {
root: {
"&.MuiTab-root": {
backgroundColor: "black",
border: 0,
borderBottom: "2px solid",
"&:hover": {
border: 0,
borderBottom: "2px solid",
},
},
"&.Mui-selected": {
backgroundColor: "none",
borderBottom: "2px solid #373985",
borderColor: "#373985",
}
}
}
}
});
const useStyles = makeStyles((theme) => ({
root: {
width: "100%",
flexGrow: 1,
color: "#3739B5",
backgroundColor: "white",
},
viewButtons: {
marginTop: theme.spacing(2),
marginBottom: theme.spacing(1),
},
}));
这些适用于
<ThemeProvider theme={theme}>
<AppBar position="static">
<Tabs
classes={classes}
value={value}
variant="fullWidth"
centered
onChange={handleChange}
aria-label="volunteer dashboard tabs"
>
<Tab label={proposedLabel} {...a11yProps(2)} />
<Tab label={planningLabel} {...a11yProps(1)} />
<Tab label={inProgressLabel} {...a11yProps(0)} />
<Tab label={completedLabel} {...a11yProps(3)} />
</Tabs>
</AppBar>
</ThemeProvider>
我正在尝试更改所选标签的背景颜色。基于devtools,检查,class列为
.PrivateTabIndicator-colorSecondary-267 {
background-color: #f50057;
}
.PrivateTabIndicator-root-265 {
width: 100%;
bottom: 0;
height: 2px;
position: absolute;
transition: all 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
}
然而,尽管我已经在我的主题中列出了这一点,但颜色显示为红色,尽管我在我的样式中指定了什么
如何覆盖所选选项卡的边框颜色?
你能试试这个对我有用的解决方案吗?我假设您想覆盖底部边框指示器颜色。
<Tabs value={0} TabIndicatorProps={{ style: { background: "#hex-color" } }}>
<Tab className={clasess.tab} label="Home" />
<Tab className={clasess.tab} label="Services" />
</Tabs>
您现在可以使用 TabIndicatorProps 为当前版本的 MUI (4.10.02) 设置活动指示器的样式。可用文档 here.
有两种方法可以做到这一点:
方法 1:使用样式:{}
import React from "react";
import PropTypes from "prop-types";
import { Tabs, Tab, makeStyles } from "@material-ui/core";
const TabsIndicator = () => {
const [value, setValue] = React.useState(0);
const handleChange = (event, newValue) => {
setValue(newValue);
};
return (
<React.Fragment>
<Tabs
value={value}
onChange={handleChange}
TabIndicatorProps={{
style: { background: "cyan", height: "10px", top: "35px" }
}}
>
<Tab label="TEST1" value={0} />
<Tab label="TEST2" value={1} />
<Tab label="TEST3" value={2} />
<Tab label="TEST4" value={3} />
</Tabs>
</React.Fragment>
);
};
export default TabsIndicator;
方法二:使用类名:{类}
import React from "react";
import PropTypes from "prop-types";
import { Tabs, Tab, makeStyles } from "@material-ui/core";
const useStyles = makeStyles(theme => ({
indicator: {
backgroundColor: "green",
height: "10px",
top: "45px"
}
}));
const TabsIndicator = () => {
const classes = useStyles();
const [value, setValue] = React.useState(0);
const handleChange = (event, newValue) => {
setValue(newValue);
};
return (
<React.Fragment>
<Tabs
value={value}
onChange={handleChange}
TabIndicatorProps={{ className: classes.indicator }}
>
<Tab label="TEST1" value={0} />
<Tab label="TEST2" value={1} />
<Tab label="TEST3" value={2} />
<Tab label="TEST4" value={3} />
</Tabs>
</React.Fragment>
);
};
export default TabsIndicator;
你也可以看看我的sandbox here。希望这对您有所帮助!
试试这个!
indicator: {
backgroundColor : 'your favorite color',
},
<Tabs classes={{ indicator: classes.indicator }}>
<Tab>....
</Tabs>
从 MUI v5 开始,可以使用 createTheme()]
API 轻松完成,如图 here.
您需要使用如下方式覆盖样式:
let theme = useTheme();
theme = createTheme(theme, {
components: {
MuiTab: {
styleOverrides: {
root:{
"&.Mui-selected": {
backgroundColor: theme.palette.secondary.main,
color: theme.palette.secondary.contrastText,
borderRadius: "25px"
}
}
}
}
}
})
可以将此主题传递给 <ThemeProvider />
组件,该组件将包含选项卡所在的部分。
注:
useTheme()
、createTheme()
和<ThemeProvider />
都需要从@mui/material/styles
而不是@emotion/react
. 导入
- 从 v5 开始,
@mui/lab
提供<TabContext />
、<TabList />
和<TabPanel />
组件,docs 推荐使用这些组件,尽管您仍然可以使用旧的<Tabs />
. 的组成部分