英文:
Why is css/ sass modules not working with third party library?
问题
I wonder why my sass modules is not affected when I use third party component. In particular, I use react-horizontal-scrolling-menu
<ScrollMenu
selected={selected}
scrollToSelected={true}
data={items && items.map((item: any) => renderItem(item))}
/>
import styles from './index.module.scss';
const renderItem = (item: any) => (
<div
onClick={() => {
handleClickScroll(item.id);
}}
key={item.id}
className={styles['my-item']}
>
<img style={imgStyle} src={item.image} alt="loyalty-img" />
<div className="item-title">{item.name}</div>
</div>
);
CSS is not applied to component in renderItem
function. But if I use css, it works perfectly.
import './index.css'
const renderItem = (item: any) => (
<div
onClick={() => {
handleClickScroll(item.id);
}}
key={item.id}
className="my-item"
>
<img style={imgStyle} src={item.image} alt="loyalty-img" />
<div className="category-container-items-title">{item.name}</div>
</div>
);
Does anyone know what reason behinds it? Is there any solution for me to use sass module in third party component? Thank in advance.
Note: Components or React elements outside ScrollMenu
work correctly with sass module.
英文:
I wonder why my sass modules is not affected when I use third party component. In particular, I use react-horizontal-scrolling-menu
<ScrollMenu
selected={selected}
scrollToSelected={true}
data={items && items.map((item: any) => renderItem(item))}
/>
import styles from './index.module.scss';
const renderItem = (item: any) => (
<div
onClick={() => {
handleClickScroll(item.id);
}}
key={item.id}
className={styles['my-item']}
>
<img style={imgStyle} src={item.image} alt="loyalty-img" />
<div className="item-title">{item.name}</div>
</div>
);
CSS is not applied to component in renderItem
function. But if I use css, it works perfectly.
import './index.css'
const renderItem = (item: any) => (
<div
onClick={() => {
handleClickScroll(item.id);
}}
key={item.id}
className="my-item"
>
<img style={imgStyle} src={item.image} alt="loyalty-img" />
<div className="category-container-items-title">{item.name}</div>
</div>
);
Does anyone know what reason behinds it? Is there any solution for me to use sass module in third party component? Thank in advance.
Note: Components or React elements outside ScrollMenu
work correctly with sass module.
答案1
得分: 1
这似乎可能与你的Sass模块的作用域问题有关。当你从'./index.module.scss'
导入样式时,这些样式仅适用于特定组件内的元素。然而,当你将一个函数作为属性传递给像ScrollMenu这样的第三方组件时,它会创建一个新的作用域,样式可能不会正确应用。
一个可能的解决方案是尝试在你的Sass模块中使用全局CSS类,而不是局部类。例如,不要在你的index.module.scss文件中定义类似于'.my-item'
的类,而是将其定义为:global(.my-item)
。这将确保该类全局应用,而不仅仅在组件作用域内。
另一种解决方案可能是将样式作为属性传递给ScrollMenu组件,以便它们可以直接在组件的作用域内应用。例如:
<ScrollMenu
selected={selected}
scrollToSelected={true}
data={items && items.map((item: any) => renderItem(item))}
styles={{ item: styles['my-item'] }}
/>
然后在你的renderItem函数中,你可以通过styles
属性访问样式:
const renderItem = (item: any, styles: any) => (
<div
onClick={() => {
handleClickScroll(item.id);
}}
key={item.id}
className={styles.item}
>
<img style={imgStyle} src={item.image} alt="loyalty-img" />
<div className="category-container-items-title">{item.name}</div>
</div>
);
希望这能帮助你。
英文:
It sounds like there might be a scoping issue with your Sass modules. When you import styles from './index.module.scss'
, those styles are only applied to elements within that specific component. However, when you pass a function as a prop to a third-party component like ScrollMenu, it creates a new scope and the styles may not be applied correctly.
One possible solution is to try using global CSS classes in your Sass modules instead of local ones. For example, instead of defining a class like '.my-item'
in your index.module.scss file, define it as ':global(.my-item)'
. This will ensure that the class is applied globally and not just within the scope of your component.
Another solution could be to pass the styles as props to the ScrollMenu component, so that they can be applied directly within the component's scope. For example:
<ScrollMenu
selected={selected}
scrollToSelected={true}
data={items && items.map((item: any) => renderItem(item))}
styles={{ item: styles['my-item'] }}
/>
Then in your renderItem function, you can access the styles through the styles
prop:
const renderItem = (item: any, styles: any) => (
<div
onClick={() => {
handleClickScroll(item.id);
}}
key={item.id}
className={styles.item}
>
<img style={imgStyle} src={item.image} alt="loyalty-img" />
<div className="category-container-items-title">{item.name}</div>
</div>
);
I hope this will help you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论