如何在ReactJS中将具有属性的CSS转换为MaterialUI样式

时间:2019-12-13 16:38:20

标签: css reactjs material-ui

我有以下CSS:

[contentEditable=true]:empty:not(:focus):before{
    content:attr(data-text)
}

允许在不包含内容的div中显示占位符。我正在使用Material-UI样式,因此需要以下内容:

const styles = theme => ({
  div[contentEditable=true]:empty:not(:focus):before: {
    content:attr(data-text)
  }
});

我该如何实现?有想法吗?

谢谢。

1 个答案:

答案 0 :(得分:2)

以下是几个语法选项,具体取决于您是要直接在div(editableDiv)上还是在祖先元素(container)上指定类。两者之间的唯一区别是定位后代时&后面的空格。

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

import { makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles({
  container: {
    "& [contentEditable=true]:empty:not(:focus):before": {
      content: "attr(data-text)"
    }
  },
  editableDiv: {
    "&[contentEditable=true]:empty:not(:focus):before": {
      content: "attr(data-text)"
    }
  }
});
function App() {
  const classes = useStyles();
  return (
    <div>
      <div className={classes.container}>
        <div contentEditable data-text="Click here to edit div 1" />
        <div contentEditable data-text="Click here to edit div 2" />
      </div>
      <div
        className={classes.editableDiv}
        contentEditable
        data-text="Click here to edit div 3"
      />
    </div>
  );
}

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

Edit Editable div JSS

相关文档:https://cssinjs.org/jss-plugin-nested?v=v10.0.0#use--to-reference-selector-of-the-parent-rule