Different ways to dynamically change tailwind colors during execution

Utilizing tailwind v3, it's feasible to customize existing colors by modifying the tailwind.config file.

https://tailwindcss.com/docs/customizing-colors

module.exports = {
    theme: {
        extend: {
            colors: {
                gray: {
                    "50": "#fcefed",
                    "100": "#fadedb",
                    "200": "#f7ceca",
                    "300": "#f5bdb8",
                    "400": "#f2ada6",
                    "500": "#ef9d94",
                    "600": "#ed8c82",
                    "700": "#ea7c71",
                    "800": "#e86b5f",
                    "900": "#e55b4d"
                }
            }
        }
}

When a user clicks on a button, I aim to change the color palette dynamically. Is there a way to override it at runtime? Could I follow a similar convention as the tailwind config file, like this:

    //user click on a button
    const onColorsPicked = () => {
        const colors = {
            "50": "#fcefed",
            "100": "#fadedb",
            "200": "#f7ceca",
            "300": "#f5bdb8",
            "400": "#f2ada6",
            "500": "#ef9d94",
            "600": "#ed8c82",
            "700": "#ea7c71",
            "800": "#e86b5f",
            "900": "#e55b4d"
        }
        //what would be the best approach to implement this function? it's even possible?
        overrideTailwindColor("gray", colors)
    }

I am uncertain if the just-in-time-mode could be beneficial ()

What would be the ideal method to achieve this? Can CSS variables be overridden instead? Or would I have to override all possible classes generated by tailwind? (seems excessive)

Answer №1

A better approach to avoid hardcoding color variables in tailwind.conf.js is to utilize CSS variables as demonstrated below:

module.exports = {
    theme: {
        extend: {
            colors: {
                gray: {
                    "50": "var(--gray-50)",
                    "100": "var(--gray-100)",
                    .
                    .
                    . 
                }
            }
        }
}
 

This method allows you the flexibility to update these CSS variables using JavaScript whenever necessary.

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

The power of Ng-show and filtering

What I am aiming for is to display a complete list of cities as soon as the page is loaded. Once a user clicks on a checkbox next to a city, my goal is to utilize ng-show/ng-hide in order to display the results specific to that city while hiding those of ...

The JSP page does not redirect after an Ajax post request has been made

When submitting a form with basic AJAX post, I am facing an issue where the redirection to the JSP does not occur upon success. Setting the redirect programmatically seems to create a new JSP instead of utilizing the existing one with POST data. After debu ...

Ways to retrieve the upcoming possible date

I'm currently working on a project using express and I need to implement a scheduling calendar. My goal is to provide users with the next available day in the format YYYY-MM-DD. Here are the rules for determining the next available day: The default ...

Error in calculation of $.post function in JQuery

I am facing an issue with my file delete.php, which contains the following code: <?php $folder = "./fak/"; $filename = $_POST['name']; unlink($folder.$filename); ?> Additionally, I have an index.html file with the below code: <html ...

Refreshing the page causes ngRoute to malfunction or results in an unknown route error

I am encountering an issue with my code. Whenever I refresh the page on localhost:portnumber/home or /todos or /contacts, it crashes -> showing an error "Cannot GET /home" or /todos or /contacts. The same problem occurs if I just enter localhost:port ...

How can I turn off a state property?

My goal is to detect if the user is using a device with a screen width smaller than 768px, and if they are, I want to set isTop to false. However, I am encountering some difficulties. Everything seems fine, but I keep receiving this error: TypeError: _t ...

Custom JavaScript clock designed for the Lockscreen of jailbroken iPhones

I am struggling with getting my JavaScript code to display the time correctly on my HTML Lockscreen. After looking at a few examples, I noticed that others are using document.getElementById() instead of document.write() and pushing it to a div. I attempt ...

Discovering the oldest date in an array with JavaScript

Is there a way to identify the earliest date, also known as the minimum date, within an array using JavaScript? For instance: ["10-Jan-2013", "12-Dec-2013", "1-Sep-2013", "15-Sep-2013"] The desired output would be: ...

Guide to creating 2D point shapes with three.js

Is there a way to render a 2D shape of points in three.js? I have been struggling to find a working geometry for this task. I simply want to create a polygon of points that lie on the same plane. The 'Shape' object doesn't seem to be suitab ...

Display confirmation pop-up when clicking outside of the modal window in Bootstrap

I am currently utilizing AngularJS (1.5.9) and ui-bootstrap in my application. I am looking to display a confirmation popup if the user clicks outside of an already open modal. I have attempted using both controlled exit with $uibModalInstance.close() and ...

Is it possible to incorporate Nth child into JavaScript?

Is it feasible to implement an Nth Child in the following code snippet? $(function() { var count = $(".parent a").length; $(".parent div").width(function(){ return ($(".parent").width()/count)-5; }).css("margin-right","5px"); }); ...

Promise rejection not handled: Trying to modify headers after they have already been sent to the client

I can't seem to figure out why these errors keep popping up. I've tried looking for solutions online but haven't had any luck. Here is the node function I'm using for an API call: exports.GetEmployeeConfirmationList = function (req, res ...

What confusion do I have regarding resolving promises in Angular's state management?

Here is the state defined in appRouteConfig.js, where I am using $promise to verify userList: .state('userAccounts',{ url:'/userAccounts', controller:'UserAccount', resolve:{ registerService: "registerServ ...

What is the best way to retrieve the second to last element in a list

When using Protractor, you have convenient methods like .first() and .last() on the ElementArrayFinder: var elements = element.all(by.css(".myclass")); elements.last(); elements.first(); But what about retrieving the element that comes right before the ...

What methods are available to test my website across different browsers such as outdated versions of Internet Explorer like IE8?

Currently, I am working on Chrome with Windows 7 OS installed. However, Windows 7 does not support Internet Explorer 8. This is causing issues when trying to view my web pages in the IE8 version and older. How can I resolve this problem? I have attempted ...

Focus issue with MUI Custom Text Field when state changes

Currently, I've integrated the MUI library into my React Js application. In this project, I'm utilizing the controlled Text Field component to establish a basic search user interface. However, an unexpected issue has surfaced. Following a chang ...

Encountering a Node Js post handling error with the message "Cannot GET /url

This is my ejs file titled Post_handling.ejs: <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>POST-Handling Page</title> </head> <body& ...

Deactivate text/input field in React and Next.js based on Dropdown Selection

As a newcomer to React, I am facing an issue with disabling and enabling an input field based on the selected value in a dropdown. My tech stack includes React, Next Js, React-bootstrap, and formik. function Input(){ const [disabled, setDisabled] = useStat ...

Using Javascript Timers in an ASP.NET AJAX application with the pageLoad() function

function initiatePageLoad() { clearTimeout("MessagesTimer"); clearTimeout("NotificationsTimer"); var MessagesTimer = setTimeout("CheckMessages()", 15000); var NotificationsTimer = setTimeout("CheckNotifications()", 15000); } I've be ...

Firebase ref.on('value') will repeatedly trigger, even if no changes have occurred

My current setup involves using an event listener to monitor changes in my real-time database. const [chats, setChats] = useState({}); const ref = db.ref(`users/${sender}/chat/`); ref.on('value', (snapshot) => { const data = snapshot ...