javascript - 如何将 MUI 主题颜色添加到内联样式?

标签 javascript reactjs material-ui

可以说我在下面有这些标签。如何使用 MUI primarysecondary内联 CSS 的颜色?我想使用我调色板中的颜色来自定义它。

            <Tabs
              value={value}
              onChange={handleChange}
              variant='fullWidth'
              aria-label="simple tabs example"
              className="tableViewTabs"
              centered
              color="primary"
              // inkBarStyle={{backgroundColor: '#68C222', width: '33.3%'}}
              // tabItemContainerStyle={{backgroundColor: '#FFFFFF', width: '30%'}} 
          >
                <Tab
                  icon={<GradeIcon/>}
                  {...a11yProps(0)}
                  disableFocusRipple={true}
                  disableRipple = {true}
                  color="primary"
                  style={{ textTransform: "none", fontSize: 16, color: value === 0 ? "" : "" }}
                />
                <Tab
                    icon={<ViewAgendaIcon />}
                    {...a11yProps(1)}
                    disableFocusRipple={true}
                disableRipple={true}
                style={{ textTransform: "none", fontSize: 16, fontWeight: value === 1 ? "bold" : "" }} 
                />
                <Tab
                    icon={<AppsIcon />}
                    {...a11yProps(2)}
                    disableFocusRipple = {true}
                    disableRipple = {true}
                    style={{ textTransform: "none", fontSize: 16, fontWeight: value === 2 ? "bold" : "" }} 
                />

最佳答案

如果需要使用内联样式,可以获取 theme对象来自 useTheme 钩。

const theme = useTheme()
主题对象包含有关 Material-UI 主题的所有数据,如调色板、断点、zIndex... 您可以检查 DefaultTheme对象 here .
return <Tabs {...props} style={{ backgroundColor: theme.palette.primary.main }} />
MUI v5 ,您可以使用 sx prop快速添加动态样式作为内联样式的替代方案。它还与 MUI 主题有更好的集成,如 following examples 所示。 :
<Box sx={{ color: 'text.secondary' }}>Text</Box>
<div style={{ color: theme.palette.text.secondary }}>Text</Box>
<Box sx={{ border: 1 }}>Text</Box>
<div style={{ border: '1px solid black' }}>Text</Box>
<Box sx={{ borderRadius: 2 }}>Text</Box>
<div style={{ borderRadius: theme.shape.borderRadius * 2 }}>Text</Box>
<Box sx={{ mx: 2 }}>Text</Box>
<div style={{ margin: `0 ${theme.spacing(2)}` }}>Text</Box>
<Box sx={{ fontWeight: 'fontWeightLight' }}>Text</Box>
<div style={{ fontWeight: theme.typography.fontWeightLight }}>Text</Box>
<Box sx={{ zIndex: 'tooltip' }}>Text</Box>
<div style={{ zIndex: theme.zIndex.tooltip }}>Text</Box>
<Box sx={{ boxShadow: 1 }}>Text</Box>
<div style={{ boxShadow: theme.shadows[1] }}>Text</Box>

关于javascript - 如何将 MUI 主题颜色添加到内联样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66912925/

相关文章:

node.js - ReactDOM.render : Unable to find node on an unmounted component after React upgrade from 16. 4.2 到 16.5.2

javascript - npm install Material-ui 没有安装最新版本

javascript - AngularJS ng-repeat 和 $index 提供解析错误

javascript - React native - x 秒后更改屏幕

reactjs - Material ui TextField 中的必需属性不适用于提交

ReactJS - 仅输入 PropType 设置为数字但初始化为零的数字

javascript - 使用 NPM 模块调用 API 'got'

javascript - 单击任意页面时如何关闭侧边栏导航?

javascript - 未捕获的安全错误 : Failed to set the 'domain' property on 'Document' : 'app.herokuapp.com' is not a suffix of '' .

javascript - Google 可视化双 Y 轴图表将线与特定条对齐