Simple selector and nested selector in JSS

▼魔方 西西 提交于 2020-05-15 02:48:06

问题


I'm currently training on ReactJS. I'm using material-ui and JSS (totally new for me).

I don't understand how can I simply select my H6 element or my H6 children elements (dangerStyle).

Any idea ?

Thanks !

myJss.js

const myJss = theme => ({
    textCenter : {
        textAlign:'center'
    },
    dangerStyle: {
        fontWeight:'normal',
        color:"#FF0000"
    },
    h6: {
        color:"#00FF00",
        "&.dangerStyle" : {
            fontWeight:'bold',
        }
    }

});
export default myJss;

app.js

import React, { Component } from 'react'
import { withStyles } from "@material-ui/core/styles";
import Danger from 'components/danger'
import myJss from 'assets/jss/myJss.js';

class App extends Component {

    constructor (props) {
        super(props)
    }

    render () {
        const { classes } = this.props;
        return (
            <div>
                APP
                <h6>
                    <Danger>Error occured</Danger>
                </h6>
            </div>
        )
    }
}
export default withStyles(myJss)(App)

danger.js

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import myJss from 'assets/jss/myJss.js';
const useStyles = makeStyles(myJss);

export default function Danger(props) {
    const { children } = props;
    const classes = useStyles();
    return (
        <div className={classes.dangerStyle}>
            {children}
        </div>
    );
}

回答1:


Each key in your styles object is going to be used to generate a CSS class. A key like h6 does not target the h6 tag, it just allows you to reference classes.h6 as a class similar to classes.dangerStyle.

In order to have the dangerStyle class behave differently when nested within an h6 tag, you can do something like the following:

Danger.js

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
const useStyles = makeStyles({
  dangerStyle: {
    fontWeight: "normal",
    color: "#FF0000",
    "h6 &": {
      color: "#00FF00",
      fontWeight: "bold",
      fontSize: 24
    }
  }
});

export default function Danger(props) {
  const { children } = props;
  const classes = useStyles();
  return <div className={classes.dangerStyle}>{children}</div>;
}

index.js

import React from "react";
import ReactDOM from "react-dom";

import Danger from "./Danger";
function App() {
  return (
    <div className="App">
      <Danger>Danger not in h6</Danger>
      <h6>
        <Danger>Danger in h6</Danger>
      </h6>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Related answers and documentation:

  • jss-plugin-nested documentation
  • Using material ui createStyles
  • Advanced styling in material-ui
  • In Material UI, How can I override a selector selected component style?
  • how to use css in JS for nested hover styles, Material UI
  • Using createMuiTheme to override default styles on div's, p's, body


来源:https://stackoverflow.com/questions/58326638/simple-selector-and-nested-selector-in-jss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!