"Is there a way to modify the value of a CSS selector property dynamically within a React JS code

Is there a way to dynamically change the color of a specific selector in a React component? In my SCSS file, I have the following rule:

foo.bar.var * {
  color: blue;
}

But I want to change this color to yellow, red, or something else in my React code.

I can't use the style property for the element because I need the selector to apply to all subchilds.

Are there any native ways to achieve this, or should I use Radium or similar libraries? Maybe CSS-Next could help with this?

I have a color picker, so I can't write class styles for every color.

For those who can provide an answer, please note:

I have a selector in an SCSS file that is imported in a root JS file with .class * {color: $somecolor}, and I need to change the $somecolor in that selector while picking colors in a color picker.

Is there a way to set a selector for all items nested inside the style property, or is there a way to recursively apply CSS styles to every nested item from the style prop?

Answer №1

Can you explain

class NewComponent extends React.Component {
  render() {
    const green = true // Your condition
    
    return(
      <div className={`foo bar var ${green  && 'green'}`}
        New item
      </div>
    )
  }
}
.foo.bar.var {
  & * {
    color: red;
  }
  &.green * {
    color: green;
  }
}

Answer №2

To implement a custom CSS property (CSS variables), you can define it using the style attribute of an element and then assign the value to a prop or state.

<div className='custom-element' style={{ "--my-property": props.value }}></div>

This custom property can be used with any selector that applies to the component or its children. For example:

.custom-element * {
  property: var(--my-property);
}

Check out a similar code snippet here

Answer №3

Perhaps this question may come across as silly, but I am curious if this method is effective.

import customCss from './mydesign.css';

customCss.foo.bar.property = "your chosen color"

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

Is it possible to change a react-query query dynamically on the fly?

I am working on a next.js project that involves react-query. My goal is to showcase all the countries from the API on the homepage, along with functionalities to search by name and region using an input field and select dropdown. How can I dynamically chan ...

Bootstrap grid not maintaining consistent column widths

Currently, I am in the process of setting up a responsive bootstrap grid for the projects section on my portfolio page. My goal is to create a 3x3 grid that adjusts seamlessly for mobile devices and breaks down into a 2-column grid and eventually a 1x6 gri ...

Can a sophisticated matrix-like design be created using a CSS grid system?

I'm currently working on implementing a unique grid structure in Material-UI using a CSS grid: Browse the desired complex layout here Here's the current CSS grid layout I've created with Material-UI: View the existing layout here This is ...

center menu items will be displayed as a fallback in case flexbox is unavailable

The code I have creates space between menu items and centers them both horizontally and vertically within each item. This is achieved by using a wrapper for the menu items: .gel-layout { list-style: none; direction: ltr; text-align: left; ...

Resize images in PHP and MySQL with precision without distorting the image

I'm looking for a solution to resize images on upload without deforming them. The current code uploads the image and stores it in the database, but I need to add resizing functionality. I'd prefer not to use JavaScript, but if necessary, please i ...

Troubleshooting typescript error in styled-components related to Material-UI component

When using typescript and trying to style Material UI components with styled-components, encountering a type error with StyledComponent displaying Type '{ children: string; }' is missing the following properties import React, { PureComponent } f ...

Encountering the "processGoogleToken is not defined" error while using react-snap

When attempting to populate static pages from a react app using react-snap, I consistently encounter an error on each page. I have not been able to locate any information online regarding this error, particularly the "VM1516 integrator.js" reference. Doe ...

Redux Error: The function concat cannot be applied to state.favorites with TypeError

I encountered an issue while using redux toolkit. I am unable to push the results into an array that I have declared in my initial state: const initialState = { favorites: [], isError: false, isSuccess: false, isLoading: false, message: "&qu ...

Concealing overflow for text content through CSS styling

I am currently working with an element that contains both an image and text. View the Fiddle here Note: To see the full grid, resize the preview accordingly. The CSS I have written is as follows: .gridster .gs-w .item{ position: relative; wi ...

Guide on extracting unique key values from an array by utilizing a different key

I have an array containing the names of products along with their storage capacities. let products = [{name: "samsung A", storage: "128GB"}, {name: "samsung B", storage: "128GB"}, {name: "samsung C", storag ...

Eliminate whitespace on the right side of a webpage using Bootstrap

I am currently working on a form that will appear in a modal, but I need it to be centrally aligned so that I can reduce the size of the modal. Here is what I have implemented so far: It seems like there is some space being "reserved" for columns that are ...

The impact of CSS positioning relative on the height of an element

I need help with positioning elements in my HTML layout. I have one element stacked below another and am using relative positioning to nudge the bottom element up slightly so that it overlaps the top element. The paperOverlay element is positioned at the ...

Error encountered when starting Npm: events.js:292 triggers an unhandled exception

As I dive into learning React, I decided to create a new React app using the create-react-app tool. However, upon running npm start after successfully creating the app, I encountered the following error message: events.js:292 throw er; // Unhandled ...

Bootstrap's Vertical Margin Concept

Is there a way to include margin or a line similar to the image below? [![Please refer to the accompanying image][1]][1] ...

Show button image beyond the limits of the hyperlink

Is it possible to create a button with a background image that changes on hover and when active, while also having the active state display an image that extends beyond the anchor's bounds? Check out my sprite here: The top half of the sprite represe ...

Adjustable Text Size with HTML5 and CSS3

Looking to enhance the accessibility of a website by implementing text size adjustment buttons ' A A A ' using just HTML5 and CSS3. Avoiding the use of jQuery or Javascript for this task. Inspiration drawn from this link. Appreciate any assistan ...

How can I activate a disabled option number 2 after selecting option number 1?

I have encountered an issue with my JavaScript code. The second "select" element is supposed to be disabled by default, but I want it to become enabled when an option is selected from the first "select". However, this functionality is not working as expect ...

The dynamic value feature in Material UI React's MenuItem is currently experiencing functionality issues

Whenever I use Select in Material UI for React, I encounter an issue where I always receive undefined when selecting from the drop-down menu. This problem seems to occur specifically when utilizing dynamic values with the MenuItem component. If I switch to ...

The altered variable refuses to show up

Currently, I am working on a project to create a Skate Dice program that will select random numbers and display the results simultaneously. Unfortunately, I have encountered an issue where the randomly generated number is not being shown; instead, it dis ...

Is it possible to create this Mobile/Desktop design using Bootstrap (or another grid system)?

I am currently utilizing Twitter Bootstrap 3 to design a website that is optimized for mobile devices. I want two elements (#1 and #2 as shown in my sketch) to appear in a sidebar on the left side of the screen when viewed on a large display, while the mai ...