Material-ui 中的高级样式

Far*_*rad 4 javascript reactjs material-ui jss

我开始研究material-ui并在SandBox中创建一个简单的应用程序: https: //codesandbox.io/s/eager-ride-cmkrc

jss 的风格对我来说很不寻常,但是如果你帮我做这两个简单的练习,那么我就会明白一切。

第一:我想要将ButtonLeft和的共同属性合并ButtonRight到新类中并扩展它:(https://github.com/cssinjs/jss-extend#use-rule-name-from-the-current-styles-object

ButtonControll: {
    display: "none",
    position: "absolute",
    fontSize: "24px"
},
ButtonLeft: {
    extend: 'ButtonControll',
    left: "0",
},
ButtonRight: {
    extend: 'ButtonControll',
    right: "0",
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用=(

第二:我希望当您将鼠标悬停在容器上时出现箭头,所以我这样写:

"&:hover .MuiIconButton-root": {
    display: "block"
}
Run Code Online (Sandbox Code Playgroud)

问题:MuiIconButton-root它是所有 IconButtons 的基类名称吗?但我想要这样的东西:

"&:hover ButtonLeft": {
    display: "block",
    backgroundColor: 'red' 
},
"&:hover ButtonRight": {
    display: "block",
    fontSize: '50px' 
}
Run Code Online (Sandbox Code Playgroud)

请帮助我完成这两个简单的任务,然后我就会明白一切=)

Rya*_*ell 5

jss-plugin-extend默认情况下不包含在 Material-UI 中。

您可以在此处找到包含的 JSS 插件列表: https: //material-ui.com/styles/advanced/#jss-plugins

您可以按照说明添加其他插件,但也可以通过其他方式达到相同的效果。

您可以将公共属性放入一个对象中:

const commonButton = {
  display: "none",
  position: "absolute",
  fontSize: "24px"
};
export default props => ({
  ButtonLeft: {
    ...commonButton,
    left: "0",
  },
  ButtonRight: {
    ...commonButton,
    right: "0",
  }
});
Run Code Online (Sandbox Code Playgroud)

或者,由于您有一个root规则,按钮在结构上,您可以通过嵌套规则应用所有常见样式root

export default props => ({
  root: {
    width: "250px",
    height: "182px",
    alignItems: "center",
    justifyContent: "space-between",
    border: "1px solid black",
    position: "relative",
    "& $ButtonLeft, & $ButtonRight": {
      display: "none",
      position: "absolute",
      fontSize: "24px"
    },
    "&:hover $ButtonLeft, &:hover $ButtonRight": {
      display: "block"
    }
  },
  ButtonLeft: { left: "0" },
  ButtonRight: { right: "0" }
});
Run Code Online (Sandbox Code Playgroud)

编辑jss嵌套

上面的示例利用了默认包含在 Material-UI 中的内容jss-plugin-nested。这也显示了引用的适当语法。hover

相关回答: