I am unsure if this is the wrong way to do this, but it seems to be working for me. and it seems more simple than Sam’s solution.
Hi and Welcome to the community.
This will work but the code changes the colour twice. What you could do is move the selected check first, and on false, do the second branch check for hover.
As it stands, it may introduce a very slight flicker when hovering over a selected item.
I hope this helps.
That is good to know, and I think I have seen what you mean.