如何在悬停内容之前更改样式组件 [emotion.js ,样式组件 ]

how to change a styled component's before content on hover [emotion.js , styled components ]

在按钮悬停时我也想更改 :before 内容,我在官方文档中找不到答案我想知道这是否可能。 这是 css 选择器,我想将其转换为带样式的组件语法是:

.button:hover::before{}

这里是样式组件:

import styled from '@emotion/styled'

export const Button =styled.button(props=>{
   return`
    padding: .75rem 2rem ;
    color: #fff;
    background-color: transparent;
    border: 1px solid #000;
    position: relative;
    z-index: 1;
    transition: all .5s cubic-bezier(0.785, 0.135, 0.15, 0.86);
    cursor: pointer;
    width: ${props.width ? props.width:'fit-content'};
    margin-top:  ${props.marginTop ? props.marginTop :0}rem;

    &:before{
         content: '';
         position: absolute;
         z-index: -1;
         height: 100%;
         width: 100%;
         top: 0;
         left: 0;
         ${props.collection?"background-color: #fff;":"background-color: var(--colorGrey);"}
         transition: all .5s cubic-bezier(0.785, 0.135, 0.15, 0.86);
         transform-origin: left center;

         //I want to change style on hover 
    }
    
    &:hover{
      color:var(--colorGrey);
    }
`})

只需在 :hover 伪 class.

中嵌套一个 :before pseudo-element

此外,考虑使用模板字面量,而不是采用 props 的函数。

import styled from '@emotion/styled';

export const Button = styled.button`
  padding: 0.75rem 2rem;
  color: #fff;
  background-color: transparent;
  border: 1px solid #000;
  position: relative;
  z-index: 1;
  transition: all 0.5s cubic-bezier(0.785, 0.135, 0.15, 0.86);
  cursor: pointer;
  width: ${(props) => (props.width ? props.width : 'fit-content')};
  margin-top: ${(props) => (props.marginTop ? props.marginTop : 0)}rem;

  &:before {
    content: '';
    position: absolute;
    z-index: -1;
    height: 100%;
    width: 100%;
    top: 0;
    left: 0;
    ${(props) => (props.collection ? 'background-color: #fff;' : 'background-color: var(--colorGrey);')}
    transition: all .5s cubic-bezier(0.785, 0.135, 0.15, 0.86);
    transform-origin: left center;
  }

  &:hover {
    color: var(--colorGrey);

    &:before {
      content: 'Hovered';
    }
  }
`;