css模块中未生成类名选择器

dkqlctbz  于 2022-11-26  发布在  其他
关注(0)|答案(2)|浏览(173)

假设我们有这样一个第三方CardComponent

<Card title="This is a title in the header">This is text in the body</Card>

这会以纯HTML呈现

<div class="Card">
  <div class="CardHeader"></div>
  <div class="CardBody"></div>
</div>

我使用css模块进行样式化(在本例中是scss)。

.customCard {
  .CardBody {
    color: red;
  }
}

然后将类添加到Card

import styles from ...
...
<Card className={styles.customCard} ...>...</Card>

上面的方法不起作用,因为它为CardBody创建了一个生成的类,比如CardBody_sjkdh43。有没有办法在模块中选择非生成的类名?或者这只可能在使用样式表的老式方法中实现?
Demo SandBox

5kgi1eie

5kgi1eie1#

这是一个老问题,但答案是css模块中的:global()选择器。
如需更多信息:异常- CSS模块
示例代码:

<div class={styles.myClass}>
    <SomeComponentWithOwnCss />
</div>

输出量:

<div class="myClass_s4jkdh3">
    <div class="SomeComponentWithOwnCss"></div>
</div>

以及CSS:

.myClass div:global(.SomeComponentWithOwnCss) {
    background-color: red;
}

工作示例如下:codesandbox

xxslljrj

xxslljrj2#

一个小补充@Closery的回答,你救了我的屁股男人️。
对于SCSS,您可以尝试以下更方便方法:
第一个
输出:

<style>
  .filename-parent__7MR41 .non-cssmodule-class { color: red; }
</style>
<div class="filename-parent__7MR41">
    <div class="non-cssmodule-class"></div>
</div>

相关问题