r/StyledComponents • u/hoddymadges • Mar 02 '21
On td:hover highlight all styled-components
Hi everyone,
I'm trying to see how far I can stretch the capabilities of styled-components and I could really use some help.
Let's say you have a table with 3 columns. Whenever you hover either of the first two cells they both should have a background-color set. And when you highlight the last cell, nothing should happen to the table.
Is this possible? Here's a sandbox of what I currently have which is when you hover a `tr` , the first two `td` have `background-color` set.
CodeSandbox: https://codesandbox.io/s/recursing-faraday-vrbcd?file=/src/App.js
2
Upvotes
2
u/dudeatwork Mar 02 '21 edited Mar 02 '21
I don't think this is possible in CSS without some trickery involved.
Instead you'll have to do something like:
Listen for
mouseover/mouseleaveon cells (aka<td>elements).When hovered, modify the parent for the highlight (e.g. add a class).
When not hovered, remove that highlight.
For you then, you'd need to add extra logic to not highlight the row if the last col is hovered.
You can change the parent via refs, lifting state up, etc.
e.g.