I'm experimenting with JSS to see if it is realistic to migrate a Sass code base. I have a very basic example of a CSS style that, when hovered, modifies the style of a child node.
span {
color: red;
}
button:hover span {
color: blue;
}
<button>
<span>Click Me</span>
</button>
I am unsure how to write this in JSS. Something I have tried looks like:
const styles = {
button: {
'&:hover': {
span: {
color: 'blue',
}
}
},
span: {
color: 'red',
}
}
const { classes } = jss.createStyleSheet(styles).attach()
document.body.innerHTML = `
<button class=${classes.button}>
<span class=${classes.span}>Click Here</span>
</button>
`
Thanks!
If, x, y and z are three HTML elements and z resides within start and end tag of y, and y resides within start and end tag of x; then y is called as a child of x; and z is called as a child of y. While writing child selectors, selectors must be separated with ">" combinator.
JSS is a JavaScript library that allows you to create objects that follow a CSS-like syntax for styling components. More technically, from the JSS docs, JSS lets you “use JavaScript to describe styles in a declarative, conflict-free and reusable way”. Many JavaScript libraries make use of JSS (also known as CSS-In-JS).
The child combinator ( > ) is placed between two CSS selectors. It matches only those elements matched by the second selector that are the direct children of elements matched by the first. Elements matched by the second selector must be the immediate children of the elements matched by the first selector.
Definition and UsageThe :nth-child(n) selector matches every element that is the nth child of its parent. n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-of-type() selector to select the element that is the nth child, of the same type (tag name), of its parent.
Have you tried doing:
const styles = {
button: {
'&:hover span': {
color: 'blue',
}
},
span: {
color: 'red',
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With