Customize the Header Background Color in React Table

I'm working on customizing a re-useable table component using vanilla CSS. I have defined six color variables and want users to be able to select one of them to change the table header background color. However, I am unsure how to make this color choice a prop on the component.

Here are the color variables defined in my CSS file:

th {
 --deq-primary: #0080b7;
  --daq-primary: #1ab7da;
  --ddw-primary: #147995;
  --dwq-primary: #034a64;
  --derr-primary: #05a68b;
  --dwmrc-primary: #f26322;
}

th {
  background-color: var(--deq-primary);
  color: white;
  border: 1px solid #cecece;
}

Is it possible to set a prop on my table component to allow users to choose one of these colors for the header background?

For instance:

<MyTable headerColor="ddw-primary" />

If implemented, this would change the header color to ddw-primary.

UPDATE: I have successfully incorporated Hugo Elhaj-Lahsen's suggestion. The variable name is passed as a string and I adjusted the example code provided to include the necessary -- before the variable name. Therefore, the final style tag now looks like this:


return <table style={{backgroundColor: `var(--${headerColor})` }}>
  {// ...}
</table>


....
>

When using the component, I pass the variable name as a string:

<MyTable
        columns={columns}
        data={data}
        headerColor="my-color-variable-name"
        />

Answer №1

An alternative way to handle styling in React without external libraries is by passing the CSS variable name as a prop and utilizing it within your component's style. For example:

<MyTable headerColor="ddw-primary" />

If you have CSS variables defined within the MyTable component, you can incorporate them using the style= property like this:

return <table style={{backgroundColor: `var(${headerColor})` }}>
  {// ...}
</table>

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

Create a pop-up box that appears beneath the anchor elements within Tiptap

I am currently in the process of developing an editor using Tiptap and I am facing challenges with implementing a notion-style anchor element in my rich text editor. Essentially, I need to display a tooltip below the links containing the href and an optio ...

Enhance video playback by eliminating accidental scrolling through the space bar

When using a video player, I always prefer to pause it by pressing the spacebar. However, this function is not working as expected. The issue at hand is that instead of pausing the video, pressing the space bar causes the page to scroll down unexpectedly ...

Having trouble positioning the button on the right side of the page and aligning the text in the center of the page

Having trouble positioning the button to the right of the page and aligning the text in the center, despite adding CSS properties such as float: right. <div id="project-heading" style = "margin-top: 0px; padding-bottom: 0px;margin-bottom: 0px ; padd ...

The issue of the Dropdown Menu Not Remaining Fixed While Scrolling

There is a challenge with a Datetime Picker Dropdown Menu not staying in place when the user scrolls. Previous attempts to use .daterangepicker {position: fixed !important;} have been unsuccessful, causing the Datetime Picker to not stay fixed in its posit ...

The key used with react-native-router-flux Actions is invalid and causes an error when invoked

The previous code was functioning properly on RN 0.57.4, but in order to publish the app on the play store, I had to upgrade it to version 0.61.4. Below is the main router being used: <Router sceneStyle={styles.allSceneStyle} navigationBarStyle={styles ...

Adjust the CSS styling for a newly added row in a JavaFX TableView

Apologies for any errors, I am French. I am currently facing an issue with an empty tableView in my project. There is a button labeled "Add" that adds a row to the tableView when clicked. Another button labeled "Cancel" appears when a row in the tableView ...

External CSS stylesheets

I have encountered the following HTML code: <div class="entry"> <p></p> //text-indent here <blockquote> <p></p> //no text-indent here </blockquote> </div> I am trying to apply a ...

What is the best way to position three DIVs next to each other within another DIV while aligning the last DIV to the right?

I need help formatting a simple list item with three DIVs. The first DIV should be left justified, the second should be able to grow as needed, and the third should be right justified. I currently have them stacked side by side, but can't get the last ...

Step-by-step guide on integrating StyleX into your fresh React project

As I delve into my new project, incorporating StyleX has proven to be a bit challenging especially when working with NextJS. I find myself grappling with configuring the "next.config.js" file without causing conflicts with the existing "babel.config.js" f ...

Struggling with color styling within link_to tags in Rails 4 - having trouble getting inline styling to show up correctly

<%= link_to "Sign In", new_user_session_path, :method => :get, style: "color:white" %> <h3>Read More - <%= link_to 'Article', action: :articles, style: "color: purple" %></h3> <%= link_to 'Click to Read More Abo ...

Troubleshooting: npx create-react-app displaying errors during installation

Encountering an error while trying to install create-react-app using npx. Seeking assistance on resolving this issue. My Node.js version is v16.14.2 and npm version is 8.5.0 Installing packages. This may take a few minutes. Installing react, react-dom, a ...

In Reactjs, you can prevent users from selecting future dates and times by modifying the KeyboardDateTimePicker component

I am currently using the Material UI KeyboardDateTimePicker component and have successfully disabled future dates with the disabledFuture parameter. However, I am now looking for a way to disable future times as well. Any suggestions or solutions would b ...

Concealing a button once the collapse feature is toggled in Bootstrap

The following code snippet from the bootstrap website demonstrates how to use collapse functionality: <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample"> Link with href & ...

Confirming class Value with Selenium IDE

I am looking to confirm the value of a specific class within an HTML code using Selenium IDE. Specifically, I want to retrieve the value of: data-val located under the class named tgl. In my example, this value can be either 0 or 1. How can I achieve thi ...

Adjusting the size of absolutely positioned div elements

Currently, I am immersed in a project that involves a full-size slideshow background with content positioned above it. The header and footer are required to have fixed margins at the top and bottom, respectively. The central part needs to be resizable wit ...

modify input type in Reactjs based on boolean state dynamically

I am currently working on implementing a checkbox feature that allows users to toggle between viewing their password or keeping it hidden. I decided to use Material UI React for the user interface elements. The checkbox is set up and functioning properly. ...

Avoid using dispatch outside of a React component

I am currently working on enhancing my react app by incorporating a new feature. I want to enable logging from any part of the app, including non-react components, so I need a way to dispatch an action from anywhere within the application. However, I&apos ...

Please provide the necessary environment variable

While developing my ReactJS app, I have been pondering on the process of specifying the necessary environment variables for the application. Where should I define that "my app requires a DATABASE_URL variable with a string formatted like this, and a PORT v ...

Despite providing a type, Typescript continues to display an error claiming that the property 'children' does not exist on the type 'FC<ProvidersProps>'

I have set up the props interface, but I am still encountering an error. Property 'children' does not exist on type 'FC'. 'use clilent' import React, { FC, ReactNode } from 'react' import { Toaster } from 'rea ...

Utilizing AMAZON_COGNITO_USER_POOLS in conjunction with apollo-client: A comprehensive guide

Struggling to populate my jwtToken with the latest @aws-amplify packages, facing some challenges. Encountering an error when attempting to run a Query: Uncaught (in promise) No current user It seems that when using auth type AMAZON_COGNITO_USER_POOLS, I ...