【问题标题】:Styled-components - Over 200 classes were generated, but cant get :hover to workStyled-components - 生成了 200 多个类,但无法 :hover 工作
【发布时间】:2022-01-02 07:46:52
【问题描述】:

我在 styled-components 中遇到了错误:

为组件生成了200多个类......

并从控制台进行了建议的修复,并且成功了,但是当我有一个容器组件“卡片”时,当悬停时应该更改另一个组件“数字”的文本颜色(应用了建议的修复,然后我无法更改颜色(我假设因为样式会覆盖悬停更改,因为它适用于不透明度)

提到的组件在 src/ProgressPieCard 中(前 2 个组件)

任何人有任何建议,在此先感谢:)

(抱歉样式/位置有点偏离)

CodeSandBox

const Number = styled.p.attrs<ColorProps>((props) => ({
  style: {
    color: props.color,
  },
}))`
  position: absolute;
  span {
    font-size: 1.5rem;
  }
`;

const Card = styled.div.attrs<ColorProps>((props) => ({
  style: {
    background: props.color,
  },
}))`
  position: relative;
  &:hover {
    ${Number} {
      opacity: 0.5;
      // color: red;  <-- this dont work
    }
  }
`;

【问题讨论】:

    标签: css reactjs styled-components


    【解决方案1】:

    来自 props 的值作为内联样式传递。他们有更高的优先级。我建议以不同的方式从 props 传递值。下面的示例现在可以按照您的意愿工作。

    const Number = styled.p<ColorProps>`
        position: absolute;
        color: ${p => p.color};
        span {
            font-size: 1.5rem;
        }
    `;
    
    const Card = styled.div<ColorProps>`
        position: relative;
        background: ${p => p.color};
        &:hover ${Number} {
            opacity: 0.5;
            color: red;  <-- this WILL work :)
        }
    `;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 2020-04-14
      • 2021-09-22
      • 1970-01-01
      • 2019-02-01
      • 2020-05-31
      • 1970-01-01
      相关资源
      最近更新 更多