How to convert CSS with properties to MaterialUI Styles in ReactJS
Asked Answered
K

1

10

I have the following CSS:

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

which allows to show a placeholder inside a content-editable div when it has no content. I am using Material-UI Styles, so I need something like:

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

How could I achieve this? Any idea?

Thank you.

Kirov answered 13/12, 2019 at 16:38 Comment(0)
E
8

Below are a couple syntax options depending on whether you want to specify the class directly on the div (editableDiv) or on an ancestor element (container). The only difference between the two is the space after & when targeting descendants.

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

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

Exclusive answered 13/12, 2019 at 16:58 Comment(1)
Thank you, It is working. Also thank you for the documentation, I will take a look.Kirov

© 2022 - 2024 — McMap. All rights reserved.