Target another styled component on hover

后端 未结 5 914
长情又很酷
长情又很酷 2020-12-12 11:42

What is the best way to handle hovers in styled-components. I have a wrapping element that when hovered will reveal a button.

I could implement some state on the com

5条回答
  •  天命终不由人
    2020-12-12 11:59

    Similarly to mxstbr's answer, you can also use interpolation to reference a parent component. I like this route because it encapsulates a component's styling a little better than referencing the child component in the parent.

    const Button = styled.button`
      ${Wrapper}:hover & {
        display: none;
      }
    `;
    

    I couldn't tell you when this feature was introduced but this works as of v3.

    Relevant link: https://www.styled-components.com/docs/advanced#referring-to-other-components

提交回复
热议问题