I'm working on a text highlight component, the idea is for paragraph of text, you might highlight a word or a sentence and this in the page margin some comment corresponding to that highlight will be displayed.
I'm now wanting to add some hover functionality, where if you mouse over the highlight, both the highlight and the comment will be emphasised, and vice versa.
For regular css, hover styling can easily be achieved with the :hover
selector.
Currently I'm going down the path of adding onMouseEnter and onMouseLeave listeners to each of the highlight and the comment, and then using that do add a '.hover' class that I'll target.
Is there a much easier way to do this?
What I'm thinking of is how elements can similarly be bound, for example how the for
attribute will allow clicking a label in one place to focus on a different element.
:has()
, but you'd have to write a selector for every single highlight/comment combo. Or it could perhaps also be done by wrapping the highlight into an extra container, and then putting the comment into that container as well - and using anchor positioning to somehow get them to display in the desired location. (Browser support for anchor positioning isn't fully there yet though.)<fn>
proposal?)