简体   繁体   English

如何使用CSS模块的全局父选择器

[英]How to use a global parent selector with CSS Modules

I'm using CSS Modules within a React application. 我在React应用程序中使用CSS模块 I also have a dropdown component with some global styles (which I'm happy with, as the general styles I want to re-use). 我还有一个带有一些全局样式的下拉组件 (我很满意,因为我想重复使用的一般样式)。

When the dropdown is active, a CSS class is applied ( .dropdown--active ). 当下拉列表处于活动状态时,将应用CSS类( .dropdown--active )。 Is there a way I can include that global class alongside my component's locally scoped styles? 有没有办法可以将全局类与我的组件的本地范围样式一起包含在内? ie, what I'd like is for this to work: 也就是说,我想要的是:

.myClass {
  color: red;
}

:global .dropdown--active .myClass {
  color: blue;
}

However, that syntax makes the entire selector global, which is not what I'm after: I want .myClass to be scoped to the component. 但是,该语法使整个选择器全局化,这不是我所追求的:我希望.myClass作用于组件。

just include the desired global class in parens: 只需在parens中包含所需的全局类:

:global(.dropdown--active) .myClass {
  color: blue;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM