What could be the reason for the compatibility issues between CSS/SASS modules and a third-party library?

I am puzzled by the fact that my sass modules do not affect the third-party component I am using, specifically 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>
  );

The CSS is not being applied to the component in the renderItem function. However, if I use regular CSS, it works fine.

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>
  );

Is there anyone who knows why this is happening? Is there a solution for me to use sass modules in a third-party component? Thank you in advance.

Note: Components or React elements outside of ScrollMenu are working correctly with the sass module.

Answer №1

It appears that there may be an issue related to scoping with your Sass modules. When importing styles from './index.module.scss', those styles are confined to elements within that specific component only. However, when passing a function as a prop to a third-party component like ScrollMenu, it introduces a new scope which could prevent the styles from being applied as intended.

One potential solution is to consider using global CSS classes in your Sass modules instead of local ones. For instance, rather than defining a class such as '.my-item' in your index.module.scss file, define it as ':global(.my-item)'. This will ensure that the class is applied universally and not restricted to the component's scope.

Alternatively, you could pass the styles as props to the ScrollMenu component so that they can be directly applied 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'] }}
/>

Subsequently, 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 trust this guidance proves beneficial for your situation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm feeling lost when it comes to rendering these components using styled-components in reactjs

Include the BarItem and BarItemSelect components inside the link when the condition is true, both styled with a specific CSS class. Currently, these are two separate components... I'm unsure how to achieve this const S = { BarItem: styled.a` pos ...

What is the best approach to animating a specified quantity of divs with CSS and javascript?

How neat is this code snippet: <div class="container"> <div class="box fade-in one"> look at me fade in </div> <div class="box fade-in two"> Oh hi! i can fade too! </div> <div class="box fade-in three"& ...

What methods can I use to make sure the right side of my React form is properly aligned for a polished appearance?

Trying to create a React component with multiple input field tables, the challenge is aligning the right side of the table correctly. The issue lies in inconsistent alignment of content within the cells leading to disruption in overall layout. Experimente ...

Sending file data to an API endpoint in JSON format with React

I have a feature where users can upload product details via an excel/csv file, and I can extract this information to display it in a table. Here is the table: However, I also need to send this data to the backend in the following format: { "bp_produc ...

"Enhance your browsing experience with Chrome by automatically changing the background when

I am currently using Chrome with a CSS3 background. <h4 class="title-name clearfix"><a href="****************">Fairy Tail - LUCY</a> </h4> My issue is that when I hover my mouse over the title, the ba ...

Issue encountered on Vercel when using a custom path for a folder inside the components directory in Next.js version 13 with js

Currently, my setup in Next.js is as follows: Version: 13.4.4 ESLint: enabled App directory: yes Server components: yes Using JavaScript (not TypeScript) In the jsconfig.json file, I have set a path shortcut for the components folder located within /src: ...

Ways to retrieve arrow information in JavaScript?

I'm currently retrieving tabular data in Arrow format from an API where the response content-type is application/vnd.apache.arrow.stream. Within my React code, I am attempting to parse this response. However, I'm uncertain if this approach is the ...

What is preventing me from accessing my session array in this.state.props from my mapStateToProps in React-Native Redux?

I am currently facing an issue with my Redux store setup. I am attempting to store an array of Session objects, where each Session object contains an array of Hand objects. However, when trying to access my store using `mapStateToProps`, none of the option ...

It appears that Typescript mistakenly interprets a class or type as a value, indicating that "'Classname' is being referred to as a value but used as a type here."

I want to pass an Object of a React.Component as "this" to a Child React.Component in the following way: component 1 file: class Comp1 extends React.Component<...,...> { ... render() { return (<Comp2 comp1={this}/> ...

When using express and passport-local, the function `req.isAuthenticated()` will typically return a

I'm seeking some insight into my current situation. I've noticed that whenever I call req.isAuthenticated() in an app.router endpoint, running on port 3001 via the fetch API, it always returns false. It seems like the connect.sid is not being pro ...

What are some strategies for enhancing the accessibility of options in a react-select dropdown?

Currently, I am in the process of developing a reactjs application and have incorporated a library known as react-select for creating a searchable dropdown feature. However, I have encountered an issue where the options within the dropdown are not being re ...

Tips for effectively showcasing specific variables in React

I am trying to implement a solution in Reactjs that displays the current year, but I encounter an error when running the code: Error: dispatch must be called inside of an event handler or within the function arguments of useAction, useState or useContent ...

Tips for handling null input values when saving to a database

<!DOCTYPE html> <html> <head> <title>Data</title> </head> <body> /* Enter your data here */ <form action="this.php" method="post"> <input type='text&a ...

An incorrect object was removed from the array

Having an issue where the wrong item is getting deleted from an array in my component's state. Here is a simplified version of my parent Component: export default class BankList extends Component { state = { banks: [new Bank("Name1"), new ...

"Enhance your website with a sleek Bootstrap navigation system featuring dividers and

Need help with creating a Bootstrap nav menu similar to this design made in Photoshop? Wondering about the best way to position the logo and create dividers between menu items like shown in this image. Here's the HTML code currently being used: & ...

What is the correct way to call upon Bootstrap's CSS that was obtained through a Gradle dependency?

I'm having trouble referencing the Bootstrap library from the 'External Libraries' section in my project. In my build.gradle file, I included: compile group: 'org.webjars', name: 'bootstrap', version: '3.3.7' ...

Tips for creating Material UI elements using React and Typescript

I am looking to extend a component from the Material UI library by creating a custom component that encapsulates specific styles, such as for a modal wrapper. However, I feel that my current solution involving the props interface may not be ideal. Is the ...

I encountered an issue while making customizations to my default next.config.js file. Despite attempting various solutions, I consistently encountered an error regarding the invalid src property

I'm currently trying to introduce some custom configurations into the next.config.js file. However, I keep encountering an error regarding an invalid src prop. Despite my attempts to troubleshoot in various ways, the error persists. // ...

Conflicting Transformation Properties Causing CSS Issues Within a Single Element

I'm currently working on a user interface where users can drag and drop a box with a red outline to position it on the screen within a black box. See the UI here Alternatively, users can also move the box by adjusting the inputs on the right side. ...

Is there a way I can create a slight gap between the icon and the text in this area?

After learning Reactjs and JavaScript, I have encountered a problem that I need advice on. In the image provided, the "Sign in" text does not have proper spacing between the icon and the text, unlike the other menu items. https://i.stack.imgur.com/b663b. ...