Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

javascript - Advanced styling in material-ui

I start to study material-ui and create a simple app in the SandBox: https://codesandbox.io/s/eager-ride-cmkrc

The style of jss is unusual for me, but if you help me with these two simple exercises, then I will understand everything.

First: I want union common properties of ButtonLeft and ButtonRight into new class and extend it: (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",
}

But it's doesn't work =(

Second: I want the arrows to appear when you hover over the container, so I wrote this:

"&:hover .MuiIconButton-root": {
    display: "block"
}

Problem: MuiIconButton-root it is base className for all IconButtons? but I want something like this:

"&:hover ButtonLeft": {
    display: "block",
    backgroundColor: 'red' 
},
"&:hover ButtonRight": {
    display: "block",
    fontSize: '50px' 
}

Please, help me with these two simple tasks and then I will understand everything =)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

jss-plugin-extend is not included by default in Material-UI.

You can find the list of included JSS plugins here: https://material-ui.com/styles/advanced/#jss-plugins.

You can follow the instructions for adding additional plugins, but you can also achieve the same effect in other ways.

You can put the common properties in an object:

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

Or since you have a root rule that the buttons are structurally within, you could apply all the common styles via nested rules in 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" }
});

Edit jss-nested

The example above leverages jss-plugin-nested which is included in Material-UI by default. This also shows the appropriate syntax for the hover references.

Related answers:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...