Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you do conditional hovering in styled components?

I have a component that based on certain props I want the ability to change the background on hover. but based on other props, hover should do nothing. is this possible?

export const Container = styled.div`

    &:hover {
        background: ${({ shouldHover }) => shouldHover ? 'red' : '' };
    }
`

however this does not work. any suggestions how this can be done?

like image 895
Red Baron Avatar asked Sep 16 '26 04:09

Red Baron


1 Answers

This will work:

export const Container = styled.div`
    ${ props => props.shouldHover 
        ? '&:hover { background: red }' 
        : ''
    }
`;
like image 140
dmraptis Avatar answered Sep 17 '26 18:09

dmraptis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!