How to convert CSS with properties to MaterialUI Styles in ReactJS - css

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.

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);
Related documentation: https://cssinjs.org/jss-plugin-nested?v=v10.0.0#use--to-reference-selector-of-the-parent-rule

Related

Is it possible to use linear-gradient on top of a landing image in react?

I am wishing to add a gradient to a landing page image.
I see examples in CSS using background (Example) but I am curious if it is possible to add it inline.
I appreciate any critiques, thank you!
import React from 'react';
import city from '../images/city-buildings.jpg'
function Home() {
return (
<div className='home'>
<img
src={city}
alt="city buildings looking up"
width="100%"
height="100%"
backgroundImage="linear-gradient(to right, #4880EC, #019CAD)". <------------- this line
/>
</div>
);
}
export default Home;
To apply a linear gradient over an image this code worked for me:
Component
import React, { Component } from 'react';
import city from '../images/city-buildings.jpg'
const style = {
width: '100%',
height: '100%',
opacity: '0.8'
}
export default class MyComponent extends Component {
render() {
return (
<div className="home">
<img
src={city}
style={style}
alt="city buildings looking up"
/>
</div>
)
}
}
Component.css
.home {
background: linear-gradient(#e66465, #9198e5);
}

How do I override Material-UI with css modules in create-react-app and Typescript?

For example I want to add flex my h1 with class pageTitle
import AppBar from '#material-ui/core/AppBar';
const styles = require('./Header.module.css');
<div className={styles.header}>
<AppBar>
<Typography>
<h1 className={styles.pageTitle}>HELLO</h1>
</Typography>
</AppBar>
</div>
In my Header.module.css file
.pageTitle {
display: flex;
border-top: 10px;
}
Is this possible to do? Because its not changing anything on my end, again using Typescript, but following the same process as the docs that Material UI shows for css modules
Probably material-ui selectors are "stronger".
Material-ui supply several ways to override their styles.
For example, use classes. In your case:
import React from "react";
import ReactDOM from "react-dom";
import { makeStyles } from '#material-ui/core/styles';
import AppBar from "#material-ui/core/AppBar";
import Typography from "#material-ui/core/Typography";
const useStyles = makeStyles({
root: {
display: 'flex',
borderTop: '10px solid orange'
}
});
function App() {
const classes = useStyles();
return (
<AppBar classes={classes}>
<Typography>
<h1>HELLO</h1>
</Typography>
</AppBar>
);
}
ReactDOM.render(<App />, document.querySelector("#app"));
https://codesandbox.io/s/material-ui-override-styles-classes-v8nhl
BTW, <AppBar /> already has display: flex and you set borderTop in a css file which is invalid (it should be border-top) so maybe if it was valid, it was working.

How to apply style for css class using Material UI withStyle. WithStyle is not working

Here is My Code
import { withStyles, MuiThemeProvider, createMuiTheme } from '#material-ui/core/styles';
import classNames from 'classnames';
const styles = theme => ({
myGridStyle:{
'&:.my-row-selected':{
backgroundColor:"#ff9900 !important"
}
},
});
<div className={myGridStyle}>
<div role="row" class="my-row-selected" style="height: 30px;"></div>
</div>
I am using non material ui component for grid I want to apply style through Material UI withstyle and theme, so I have added withStyle for my parent div and i want to apply style for child div and child CSS classes.
You have several options, the newest is using the makeStyles hook, but you can use the withStyles HOC.
const styles = theme => ({
myGridStyle: {
'&:.my-row-selected':{
backgroundColor:"#ff9900 !important"
}
},
});
function myGrid(props) {
const { classes } = props;
return (
<div className={classes.myGridStyle}>
<div role="row" class="my-row-selected" style="height: 30px;"></div>
</div>
);
}
export default withStyles(styles)(myGrid);
https://material-ui.com/styles/basics/

Extending styles with styled-components not working

I'm trying to extend styles for a react component using styled-components but is not working.
AFAIK, I'm doing it the right way, but perhaps I'm missing something...
Here is what I have:
import React from "react";
import styled from "styled-components";
const TextContainer = ({ text }) => {
return <p dangerouslySetInnerHTML={{ __html: text }} />;
};
const Paragraph = styled(TextContainer)`
background: red;
`;
class Home extends React.Component {
render() {
const { t } = this.props;
return <Paragraph text="This is a test" />;
}
}
export default Home;
Of course, the expected result is to have a red background on p, but right now the output looks like this:
Any idea on how to solve this? Probably I'm missing something, but I can't realize what.
Thanks is advance!
As stated in documentation:
The styled method works perfectly on all of your own or any
third-party components, as long as they attach the passed className
prop to a DOM element.
Example
// This could be react-router-dom's Link for example, or any custom component
const Link = ({ className, children }) => (
<a className={className}>
{children}
</a>
);
const StyledLink = styled(Link)`
color: palevioletred;
font-weight: bold;
`;
render(
<div>
<Link>Unstyled, boring Link</Link>
<br />
<StyledLink>Styled, exciting Link</StyledLink>
</div>
);
Ref: https://www.styled-components.com/docs/basics#styling-any-component
I didn't know that was a way to do it.
I would do:
const Link = styled.a`
..put you css styles here (className styles)
`
const StyledLink = styled(Link) `
color: palevioletred;
font-weight: bold;
`
render(){
return(
<div>
<Link>Unstyled, boring Link</Link>
<br />
<StyledLink>Styled, exciting Link</StyledLink>
</div>
)
}

I cannot center the Button component in Material-UI using Styled Components

I am quite new to Material UI # next, and I like the fact that it supports Styled Components.
However, I am struggling in aligning the Button component to the center through Styled Components. I only manage to align it by using the styles technique such as this:
const styles = {
container: {
textAlign: "center"
}
};
class Landing extends Component {
render() {
return (
...
<div style={styles.container}>
<Button variant="raised" color="primary">
I am a button
</Button>
</div>
);
}
}
This is something that I like to avoid, and instead use the Styled Components such as:
const Container = styled.div`
text-align: "center";
`;
However, for some reason it is not working although they look exactly identical. Can someone explain whether text-align and textAlign are pointing to the same CSS property?
It works using styled component here is the code
import React from 'react';
import Button from "react-bootstrap/es/Button";
import styled from 'styled-components';
const Container = styled.div`
text-align: center;
`;
class Landing extends React.Component {
render() {
return (
<Container>
<div>
<Button color="primary">
I am a button
</Button>
</div>
</Container>
)};
};
You have to wrap the styled component which you have taken. Here I've taken container and then inside the container, I've added the needed CSS.
For more information and usage about the styled component, you can visit this URL - https://www.styled-components.com/docs/basics
Thanks

Resources