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

Trouble with basic JavaScript functionality in a React component

Here is a unique component code snippet: import React, {Component} from 'react'; import 'D:/School/Alta/interactiveweb/src/webapp/src/App.css' class Chat extends Component { test() { alert(); } render() { return <nav ...

Troubleshooting an Issue with CSS Sliding Doors

I'm having trouble getting the right image to show on my website. The left side of the image is fine, but I can't figure out how to make the right one appear. As a HTML/CSS beginner, I'm still learning the basics. For more information, vis ...

How to make Firefox create a new line in a contenteditable field

I am working with a contenteditable div that is within a parent div with a floating width of 50%. My goal is to set the max width of the content editable to match the container, even if the text spans multiple lines. Here is the CSS I tried: .editable-con ...

Exploring the use of React material-ui Drawer list to render various components onClick in your application

Working on designing a Dashboard with the React material-ui package involves implementing an App bar and a Drawer containing a List of various items. The objective is to render the corresponding component inside the "main" tag of the Drawer upon clicking a ...

Encountering an error when using setState with React.createElement: invalid type provided

https://i.sstatic.net/YHssU.png Anticipated Outcome Upon clicking the login button without inputting an email or password, the user is expected to view the Notification component. Actual Outcome Upon clicking the login button, the setState function is ...

Image alignment on a web page

I'm currently working on developing a website that features a unique wave effect at the bottom of the 'hero' image. From my research, I've learned that this can be achieved by cropping the bottom part of the image, making modifications, ...

Tips for implementing styled components alongside Material UI input components

I'm currently facing an issue with Material UI input and I'm looking to customize its design using styled components. However, I've encountered a problem with the following code: import React from "react"; import styled from " ...

Ways to trigger the FETCH API every time my Server-Side Component is loaded

I am looking for a way to re-fetch the data in my server-side component in Next.js every time the component is loaded (similar to how we make API calls using useEffect in client-side components). Currently, the API is only called again when I manually ref ...

Ways to update the color of the mat-dialog-title using the material theme

Currently, I am utilizing the Angular Material Dialog and have been attempting to dynamically change the title color of the dialog based on the material theme. <h1 mat-dialog-title color="primary">{{ title }}</h1> Even though setting ...

The default value state of an uncontrolled Slider is being altered by a component once it has been initialized. This Slider, integrated with Material UI and Redux

I am currently facing an issue with a redux action that fetches data from a database and stores it in a state variable. I am then using this variable as a default value for a Material UI Slider component. To avoid any null values, I have initialized the de ...

Creating a custom navigation bar that elegantly fades away with a smooth animation as you scroll down the page is a must-have

How can I create a navigation bar that disappears when scrolling, with a smooth animation? This is the progress I have made so far. HTML: <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="css/style.css" type="tex ...

Angular is unable to eliminate the focus outline from a custom checkbox created with Boostrap

Have you noticed that Angular doesn't blur out the clicked element for some strange reason? Well, I came up with a little 'fix' for it: *:focus { outline: none !important; } It's not a perfect solution because ideally every element sh ...

Guide to displaying a canvas as an image in a new tab using Reactjs

My current project involves using a canvas barcode generated by the react-barcode library. At the moment, I can only right-click on the canvas to open it as an image in a new tab. Is there a way to achieve this functionality with just a click of a button i ...

Using CSS to disable an image map in conjunction with dynamically adjusting CSS styling

On the desktop version of the website, there is an imagemap implemented. However, when the site transitions into tablet or mobile view, the imagemap should be disabled to allow for a simpler navigation area to be displayed. I attempted to use the following ...

Storing segments of URL data for future use in React applications

Is there a way to extract information from a URL? I wish to utilize the appended details in this URL http://localhost:3000/transaction?transactionId=72U8ALPE within a fetch API. My goal is to retrieve the value 72U8ALPE and store it either in a state var ...

Adjust the svg rate using jQuery or JavaScript

Seeking help with a gauge I found on CodePen - struggling to adjust bubble values... <path id="Fill-13" fill="#F8B50F" d="M3.7 88.532h26.535v-#.795H3.7z"/> Can change the bars in JS, but not the bubbles using jq/js. Adjust the gauge with values be ...

Is there a way to dynamically update one input field while another input field is being modified? [HTML / JS]

I have a form with 3 input fields. One of the inputs is disabled, while the other two are enabled. The goal is to combine the values entered in the enabled fields to populate the disabled field. How can this be achieved? Is it possible for the value in th ...

What are the steps for positioning tables using HTML?

As a newcomer to this field (literally just started two days ago), I have managed to generate tables from an XML file using my XSL file. However, aligning these tables properly has become a bit of a challenge. I initially used the align attribute of <ta ...

Looking for assistance: Connecting React application to a WordPress site hosted on AWS Lightsail

Hello everyone, I am in need of some assistance! I am currently running a WordPress site on AWS Lightsail with an Apache server that has my site accessible on my public address. I have a specific application, a React project hosted on localhost:3000 using ...

What is the best way to halt Keyframe Animation once users have logged in?

To enhance user engagement, I incorporated keyframe animation into my login icon on the website. The main objective is to capture the attention of visitors who are not registered users. However, once a user logs in, I intend for the keyframe animation to c ...