为什么 CSS/Sass 模块在与第三方库一起使用时无法工作?

huangapple go评论50阅读模式
英文:

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 &#39;./index.module.scss&#39;, 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 &#39;.my-item&#39; in your index.module.scss file, define it as &#39;:global(.my-item)&#39;. 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:

&lt;ScrollMenu
  selected={selected}
  scrollToSelected={true}
  data={items &amp;&amp; items.map((item: any) =&gt; renderItem(item))}
  styles={{ item: styles[&#39;my-item&#39;] }}
/&gt;

Then in your renderItem function, you can access the styles through the styles prop:

const renderItem = (item: any, styles: any) =&gt; (
  &lt;div
    onClick={() =&gt; {
      handleClickScroll(item.id);
    }}
    key={item.id}
    className={styles.item}
  &gt;
    &lt;img style={imgStyle} src={item.image} alt=&quot;loyalty-img&quot; /&gt;
    &lt;div className=&quot;category-container-items-title&quot;&gt;{item.name}&lt;/div&gt;
  &lt;/div&gt;
);

I hope this will help you.

huangapple
  • 本文由 发表于 2023年5月17日 11:41:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定