Customize Material UI components by incorporating SASS classes

Currently, I am using Material UI components but I want to streamline my styles by moving them all into a .scss file. Right now, I have a significant styles object within the same JavaScript file where I am utilizing the Material UI components. This styles object is passed to the Material UI components through the style prop, essentially resulting in inline styling. My aim is to eliminate this practice. Additionally, I have nested components (both Material UI and custom React ones) within these Material UI components.

 const styles= {//my css styles go here}

<TableRowColumn key={index} style={styles.column}>
     <span className="checkbox-thing">
       <input
         ....
       />
     </span>
</TableRowColumn>

I have looked at the override documentation: Material UI Next Customization Overrides

I also came across this Stack Overflow question: How to style material ui next components with styled components and SASS

In my opinion, neither of these resources provides clear guidance on how to use an external .scss file to store styles and then reference those class names within the Material UI component. Ideally, I would like to achieve something similar to working with normal HTML elements:

<input   
    type="checkbox"
    checked={
      this.props.isChecked
    }
    className="someClassInTheSCSSFile"
/>

To sum up, my goals are:

  • Move my large styles object into separate classes in a .scss file

  • Reference a class from the .scss file and apply it to a Material UI component

Answer №1

It's a mystery why this approach is suddenly working now when it didn't before, but using

className="someClassInTheSCSSFile"
seems to do the trick as long as my .scss file includes something like:

//.scss file

.someClassInTheSCSSFile {
   color: blue;
   //rest of the styles go here
}

So, in short, utilizing the className prop directly inside MATERIAL UI components does work. For example, my code snippet below functions as intended:

//Javascript file

<TableRowColumn key={index} className="someClassInTheSCSSFile">
 <span className="checkbox-thing">
   <input
     ....
   />
 </span>
</TableRowColumn>

The real challenge arises when trying to dynamically pass a value to the CSS property. Let's say the width attribute of the TableRowColumn component shouldn't be hardcoded in the .scss file. When I attempt to achieve this without using inline styling, it becomes quite problematic. If the colWidth parameter needs to be passed into the css width attribute of the TableRowColumn component, it seems nearly impossible at the moment. The workaround involves:

//Javascript File

<TableRowColumn key={index} style={{width:`${coldWidth}`}} className="someClassInTheSCSSFile">
 <span className="checkbox-thing">
   <input
     ....
   />
 </span>
</TableRowColumn>

This leads to inline styling, which goes against my initial goal. Thus, I end up with both inline styling and a className that references my .scss file, making the solution seem clunky and cumbersome. Can the attr() function within the .scss file offer a better alternative? Unfortunately, according to MDN (https://developer.mozilla.org/en-US/docs/Web/CSS/attr), only strings can be supported by this method, while other data types are not widely recognized in mainstream browsers. Take a look at this codepen (https://jsfiddle.net/hmr0hckf/131/) for a demonstration. Ultimately, it seems that inline styling is unavoidable if dynamic changes to css attributes are required.

Answer №2

Implementing Webpack with node-sass and sass-loader

Getting the hang of it! Just gather all your styles in one or multiple .scss files, business as usual. Let's say you have a class called "nodeContent."

.nodeContent {
   position: absolute;
   top: 0;
   bottom: 0;
   display: flex;
   align-items: center;
}

All you have to do is import your styles and access the class names like properties of your styles object. For instance, if I have a Sass file named styles.scss in the same directory as my component, I can import them this way:

import * as styles from './styles.scss'

The 'styles' variable is an object that allows me to refer to my classes similar to any other object property. So, when I want to apply the nodeContent class on a material component, I can simply do this:

<Card className={styles.nodeContent} />

If You're Not Using Webpack (not entirely certain on this)

You may compile your .scss files into .css format and link them as a stylesheet in your index.html file. Subsequently, you should be able to use the classNames as strings as demonstrated above. Here's a related article I came across where they import the css file within their component:

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

Switch the scroll direction in the middle of the page and then switch it back

Yesterday, while browsing online, I stumbled upon this website and was amazed by the unique scroll direction change from vertical to horizontal mid-page. I'm curious about how they managed to achieve that effect. Does anyone have insight into the pro ...

creating an HTML document with 2 interactive elements

In my app.js file, I have defined routes using Angular and included references to HTML files. However, when I run the file, it only displays two controls instead of what is expected. Below is a snippet from my login.html file: ![<!DOCTYPE html> < ...

Is it possible to execute functions inline with conditional logic in react?

Is there a way to shorten the long conditions inside an inline if-else statement in React by putting a function inside it? I attempted to do this but encountered an error stating that "discount is not defined." function getDiscount(props) { const d ...

What is the function of this.handleClick that is positioned on the LEFT side of the = operator?

I'm struggling to understand the usage of this in an ES6 constructor. Initially, I grasped the following concepts (see example below): - We utilize this.height = height; to introduce a new instance variable. - Adding a new instance method with cl ...

Click here to open in a new tab and experience the ever-changing dynamic

Imagine a scenario where a property site displays the main viewed property ad at the top, with smaller ads in square divs below. When a user clicks on one of the smaller ads, it dynamically swaps places with the main ad thanks to JavaScript. All ad data is ...

How to fix the issue of the mobile Chrome bar occupying part of the screen height in CSS

I am encountering a well-known issue with my collapsible scrollable bar implemented in Bootstrap. The problem occurs when scrolling on mobile devices, as the search bar takes up space from the sidebar's 100% height, causing the scrollbar to not reach ...

How should one properly utilize the app and pages directories in a next.js project?

For my current next.js 13 project, I have decided to utilize the /pages directory instead of /app. Nonetheless, I recently included the app directory specifically for its new features related to dynamic sitemap rendering. Do you think this approach is app ...

Error message encountered with React.js and Next.js: Attempting to iterate through a null object, resulting in a TypeError (property Symbol(Symbol.iterator

Having trouble accessing state's values using [...state_name,{}]. I'm facing an issue while trying to implement this algorithm for a dynamic menu. The data is fetched from the List action of the useFetch method. The data retrieval is successful ...

Guide to displaying API data in HTML format

This university assignment involves working on a homework project where I need to utilize a public API in HTML. So far, I have successfully called the public API to display a list of radio channels in the left menu (without adding any click functionality). ...

Issues with Vue enter transitions not functioning as expected

I am currently involved in a project where I need to implement enter and exit animations for some components. When a component enters the screen, it should come from the bottom, and when it leaves, it should go upwards. The intended functionality is that w ...

When incorporating a background-image into my Vue.js project, I encountered this error

I encountered an issue after adding a background-image to my vue.js project: ./src/assets/scss/argon.scss (./node_modules/css-loader??ref--8-oneOf-3-1!./node_modules/postcss-loader/src??ref--8-oneOf-3-2!./node_modules/sass-loader/dist/cjs.js??ref--8-oneOf ...

Inform the user that an error has occurred when attempting to perform an invalid

While using redux promise middleware for my front end, I am wondering about the correct status code to throw from my backend in case of an error. I know that I can use res.status(500).json(something), but is 500 the appropriate code for all types of erro ...

Electronic circuit embedded within a material-textured text field offering multiline functionality

While experimenting with TagsInput, I came across this helpful snippet on codesandbox that you can check out here. The challenge I encountered is when there are numerous chips, they extend beyond the boundaries of the text field. My goal is to implement ...

Froala text area is unexpectedly visible when I attempt to cover it with a partially see-through mask

The website I'm currently developing features a semi-transparent overlay that dims the screen with a light-colored message displayed on top. This overlay and message are shown whenever there is a background process running. Everything was running smoo ...

Error found in the HTML tag data when viewing the page source for an issue

I am displaying some data from my express to ejs in HTML tag format. It appears correctly on the ejs template page and the web page. However, when I check the page source, the HTML tags are encoded and displayed as unescaped characters. Is there a solution ...

Tips for retaining directories in build when running npm run build

I have a unique setup for my SPA react app where each personal project is stored as a directory with normal index.html, .js, and .css files, while the layout is created with react. Currently, I am facing an issue where I am unable to use <Link> to r ...

Activate the Alert (https://material-ui.com/components/alert/#alert) starting from the React component at the bottom of the hierarchy

When it comes to alerts, a normal alert is typically used like alert("message to be displayed");. However, I prefer using material UI Alerts which return a JSX component. For example: <Alert severity="success">This is a success alert — check it out ...

What is the best way to determine the component type of a React lazy-loaded component?

Using Next.js 13 and React Server Components has presented me with a challenge. I have a special component called Block, which serves as the parent to other similar child components. This is how it manages them: export function Block({ children }) { con ...

Having trouble importing partials with Sass @import?

I'm new to utilizing professional npm / gulp workflows and I've been using a repository called react-starterify to construct a basic web application. I am attempting to incorporate partial SCSS files by using @import "variables"; in my main.scss ...

Display more/hide less form using div elements in a NextJS project

Looking to create a hidden block of amenities on my hotel website that can be expanded and collapsed with buttons in NextJS using tailwind-css. How can I achieve this functionality? Example 1: https://i.stack.imgur.com/BbrUj.png Example-2: https://i.stac ...