CSS模块,React和覆盖CSS类

乔·布鲁诺:

我正在使用React组件库(React Toolbox),该库使用CSS模块和Webpack在其Tab组件中输出此类:label___1nGy active___1Jur tab___Le7Ntab是我正在传递的className 属性labelactive类是从图书馆来了。我想在上应用不同的样式集active,例如.tab.active在何处tab引用我创建的样式,并active与库创建的生成的选择器匹配,但是无法弄清楚如何使用css-modules。我需要覆盖此动态选择器:.label___1nGy.active___1Jur

浏览器

[ 的CSS]] 2 [ 反应] 3

alechill:

Old post but still relevant, so adding an answer to help those with similar issue

While not inherently possible in CSS modules alone, the author of the react-toolbox library has actually solved this particular problem very nicely

Read their github docs which are more in depth on the subject at https://github.com/react-toolbox/react-toolbox#customizing-components

A list of the themeable classes for a particular component is given on the component demo page on their site too

In your case as well as passing a className for tab, you would also create a theme with classes that overrides that desired parts of the default theme and pass that as the theme prop. For example something alog the lines of...

MyComponentWithTabs.css

.tab {
  color: white;
}

MyTabTheme.css

.active {
  color: hotpink;
}

MyComponentWithTabs.js

import styles from './MyComponentWithTabs.css'
import tabTheme from './MyTabTheme.css'

// blah blah...

return <Tab key={index} className={styles.tab} theme={tabTheme} />

在表面之下,它使用了一个装饰器,它们已经抽象到单独的库react-css-themr中,我建议您也读一读,因为它解释了默认值如何与您的重写组成的更深层次,包括它们使用的不同合并策略

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章