Library of React components together with SASS modules

I'm currently in the process of developing a React components library and I am looking to incorporate some shared SCSS code from other non-react projects.

In order to achieve this, I am exploring the use of SASS modules within my react component.

While the basic use case is functioning well, I am facing a challenge with the Button component. The component itself is straightforward, but it requires multiple style variations for certain components like buttons.

The issue lies specifically with the Button component. It's a simple component with 3 different variant values.

Below is the content of Button.tsx:

import React from "react";
import styles from "./Button.module.scss";

type Variant = "default" | "primary" | "tertiary";

interface Props {
  children: String;
  variant: Variant;
}

export const Button: React.FC<Props> = ({ children, variant }) => {
  return <button className={`${styles.button} ${variant}`}>{children}</button>;
};

And here is the content of Button.module.scss:

.button {
  border: none;
  padding: 0.5rem 1.5rem;
  border-radius: 0.25rem;
  background-color: grey;
  color: white;

  &.default {
    background-color: green;
  }
  &.primary {
    background-color: red;
  }
}

My expectation is to have a green button when using the component as

<Button variant="default">I'm green</Button>
, however I am seeing a grey button instead.

You can view a live example on codesandbox

I am currently stuck on this issue and would appreciate any assistance on applying different styles based on prop values.

Answer №1

<button className={`${styles.btn} ${styles[customize]}`}>

Answer №2

For this task, it is recommended to utilize the classnames npm package.

import cn from 'classnames';

<button className={cn(styles.button, styles[variant])}>

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

Transferring information from ReactJs via the fetch API to a nodeJs server

I've encountered an issue where I am sending JSON data from my React application using the Fetch API, but on the server side, the req.body is showing up as empty. I'm not sure what the problem could be. Here is a snippet of my React code: impor ...

How can the table header be displayed only once within the map function in React JS?

I am currently working on displaying tabular data in the UI using React and TailwindCSS. The issue I am facing is that when I use a map function to iterate over the data, the header is being displayed after each row in the table. How can I ensure that the ...

Showcasing a JSON file in the interface using $http in AngularJS

I am a beginner in angularjs (and programming). I am attempting to showcase a json file on my view (home.html) using $http and ngRepeat, but unfortunately, it is not working as expected. Upon inspecting the angular responses, I noticed that there are numer ...

Column with a set width in Bootstrap

Is it possible to create a fixed width right column in Bootstrap while keeping the left column responsive? I am currently working with Bootstrap 2.3.2 https://i.stack.imgur.com/xOhQo.png (source: toile-libre.org) I want the right column to maintain it ...

Access real-time information via JSON

I am facing a logical thinking challenge. Successfully retrieving data from a PHP file via JSON, but now encountering a slight issue. My goal is to retrieve various headlines - main and sub headlines. Each main headline may contain an unknown number of su ...

CSS for the root folder of an ASP.NET website

After deciding to venture into ASP.NET, I quickly encountered my first obstacle. The folder structure is as follows: \ ->Admin -->Admin.Aspx ->CSS -->Style.css ->Images -->bg.gif Default.aspx Default.master Both admin.aspx and ...

Highest Positioned jQuery Mobile Section

Requesting something a bit out of the ordinary, I understand. I currently have a jQueryMobile page set up with simplicity: <div data-role="page" class="type-home" id="home"> <div data-role="header" data-theme="b"> <h1>Our To ...

AngularJS - Shared service object mistakenly removed in error

When I call the removeQuestion() function for the second time, 2 questions are being deleted instead of one. Any suggestions on what might be causing this issue? Let me know if you require additional code snippets. controller.js crtPromoCtrl.controller(& ...

Trouble with predefined JavaScript in Mongodb situation

Encountering the error "Missing ";" before statement" in Mongodb Atlas Online is frustrating for me as a newbie. Despite my efforts, I can't seem to figure out why the following code snippets are causing this issue: const counter = await counterCollec ...

How to Add Functionality to Angular Apps Without Defining a Route

One unique aspect of my website is the navigation bar. It appears on some pages but not others, so I've created a controller specifically for it. Here's how the navigation bar markup looks: <html ng-app="myApp"> <head> <title& ...

How can I access the attributes of items stored in an array in JavaScript?

Input was being retrieved from an HTML text box and stored in an object with multiple properties. Two functions, addAddress and displayAddressDetails, were created for adding data and displaying it. However, there seems to be an issue with displaying the d ...

Step-by-step guide on visually comparing two iframes for differences

Scenario : In my project, I am dealing with 2 iframes that contain a significant number of divs and other controls, making both iframes similar in size to complete HTML websites. My goal is to compare these two iframes and identify any differences. Consi ...

Tips on avoiding asynchronous issues in NodeJS: Ensure task a is finished before initiating task b

I have a situation where I need to ensure that task B code only executes after task A has completed. Task A involves converting an audio file, while Task B relies on the converted audio for further processing. The issue arises because Task A saves the n ...

employing JavaScript to present an image

Here is the image code I have: <img id="imgId" src="img/cart.png" style="display: none"/> After clicking a button, it triggers a JavaScript function to show the image document.getElementById("imgId").style.display = "inline" The image display ...

Adding up characteristics within an array of objects using Angular

As someone who is relatively new to Angular and Javascript, I am reaching out for some assistance. My goal is to calculate the sum and average of values within an object array. The objects are added to the array via input boxes, and here is the code I have ...

It can be challenging to determine whether a variable is set when selections are not calculating correctly

Check out my website at When choosing an option from the drop-down menu for attribute19k, the price value changes. However, selecting other options does not trigger any change. The JavaScript code at the bottom of the source is supposed to calculate and a ...

Is it secure to use a unique, static HTML/CSS website?

As a web developer, I am currently working on hosting a Wordpress-website for a client. However, my frontend development skills have improved significantly and now I feel inspired to create a simpler and more customizable version on my own. This website i ...

Issue with exchanging columns in Bootstrap version 5.0

Seeking assistance! I am a beginner with bootstrap and struggling to figure out an issue I'm experiencing. In the attached image(s), my goal is to have the coin image positioned above its corresponding text on the left side when the screen size is red ...

I am unable to connect an external CSS file when Bootstrap is already linked

Having trouble with linking my external CSS file while also linking bootstrap. The CSS file doesn't seem to load when the bootstrap link is included, but it works if I comment out the bootstrap link. body { background-color: black; font-size: 2 ...

What could be the reason for this simple sails socket not functioning properly?

Just curious why the simple web socket code below isn't functioning? io.socket.on('user', function(event){ console.log("RECEIVED EVENT:",event); }) I have included sails.io.js in my index file and the above code is located in a test.js ...