Emotion JS:悬停父项时如何将样式应用于子项?

Emotion JS: How to apply styles to child when hover parent?

似乎这个问题已经被以多种不同的方式提出和回答,但我看到的答案要么不适用于情感,要么与情感相关的答案对我不起作用。我在@emtion/core@10.0.28 和@emtion/styled@10.0.27.

本质上,我想在父组件为 hovered/active/focused 时将样式应用于子组件。父项是一个按钮,子项是一个可选图标。以下样式通过 styled 语法添加到(父)按钮。

const iconWrapperStyles = (props) => {
  return css`
    ${props.IconWrapper} {
      width: ${iconSizeMedium};
      height: ${iconSizeMedium};
      margin-left: ${spacingSizeSmall};
      color: ${textColor};
      fill: ${textColor};
      background: ${backgroundColor};
      border-color: ${borderColor};
    }

    &:hover:not(:disabled),
    &:focus:not(:disabled),
    &:active:not(:disabled) ${props.IconWrapper} {
      outline: none;
      color: ${textColorHover};
      fill: ${textColorHover};
      background: ${backgroundColorHover};
      border-color: ${borderColorHover};
    }
  `;
};

第一个样式块已成功应用。因此,乍一看,按钮和子图标的样式看起来很合适。但是,当您 hover/focus/make 激活按钮时,图标不会改变。我已经尝试了上面的实现,以及 ... + ${IconWrapper}... & ${IconWrapper};这三个都不适合我。 Official docs 表示 & 应该有效。

无论使用何种 JS 框架,以下内容都应该始终有效。

button {
  background: darkblue;
  color: white;
  border: none;
  padding: 5px;
}

button:hover i {
  color: red;
}
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">

<button>
  <i class='icon-edit'></i> Click to edit
</button>

在你的情况下,它变成了

   ${props.IconWrapper} {
     width: ${iconSizeMedium};
     height: ${iconSizeMedium};
     margin-left: ${spacingSizeSmall};
     color: ${textColor};
     fill: ${textColor};
     background: ${backgroundColor};
     border-color: ${borderColor};
   }

   &:hover:not(:disabled) ${props.IconWrapper},
   &:focus:not(:disabled) ${props.IconWrapper},
   &:active:not(:disabled) ${props.IconWrapper} {
     outline: none;
     color: ${textColorHover};
     fill: ${textColorHover};
     background: ${backgroundColorHover};
     border-color: ${borderColorHover};
   }

我失败了,因为我的 CSS 很弱。逗号分隔的 CSS 装饰器不会迭代最终声明的元素。

由此...

&:hover:not(:disabled),
&:focus:not(:disabled),
&:active:not(:disabled) ${props.IconWrapper} {
  outline: none;
  color: ${textColorHover};
  fill: ${textColorHover};
  background: ${backgroundColorHover};
  border-color: ${borderColorHover};
}

为此...

&:hover:not(:disabled) ${props.IconWrapper}, // include child el
&:focus:not(:disabled) ${props.IconWrapper}, // include child el
&:active:not(:disabled) ${props.IconWrapper} {
  outline: none;
  color: ${textColorHover};
  fill: ${textColorHover};
  background: ${backgroundColorHover};
  border-color: ${borderColorHover};
}