Extracting Tailwind color palette through API request

After conducting some research, I have not been able to find a definitive answer to my query.

I possess a CMS API that supplies branding colors and other assets for a React application that can dynamically change its appearance using a combination of colors and images.

As I am currently working on version 2 of this application, I am contemplating switching from sass/scss to tailwind. Previously, with sass and scss, we simply overrode colors with CSS in JS, which worked effectively.

However, in tailwind, creating a custom color palette requires a configuration file, which may not be suitable for my intended use case as the colors will be changing frequently.

Consider a component:

import { FC } from 'react';

const MyComponent: FC = () => {
  return <div className='bg-blue-500'></div>;

};

This method works well, but if I wish to change the color temporarily, I could use a class like bg-[#f1f1f1], which is also effective!

However, an issue arises when attempting to pass the color as a template string in React, as shown below:

import { FC } from 'react';
import { useBranding } from '../some_path';

const MyComponent: FC = () => {
  const [branding] = useBranding();  // custom hook providing various colors
  
  return <div className={`bg-[${branding.colours.primary}]`}></div>;
};

Upon inspection, it appears that the color code is being injected correctly, yet it seems that Tailwind's preprocessing is potentially being bypassed in this scenario?

Does anyone have any suggestions on how to effectively incorporate colors in this manner with Tailwind?

Answer №1

After the build, Tailwind is not recognizing classes.

It's possible that I could be mistaken since you haven't shared how your hook functions, but it seems like you can utilize a style tag instead:

import { FC } from 'react';
import { useBranding } from '../some_path';

const MyComponent: FC = () => {
  const [branding] = useBranding();  // This custom hook generates colors
  
  return <div style={{backgroundColor: branding.colours.primary}}></div>;
};

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

Inaccurate rendering of border widths on IOS platform

After setting the border width of these input boxes to 1px, it appears that on iOS devices, the top border seems larger than intended (regardless of the browser being used). Interestingly, on any other type of device, the border looks perfectly fine. & ...

The combination of Inline CSS and Bootstrap classes does not seem to be functioning properly

I'm new to web design and currently working on a website project. My concept involves hiding the #login-box when the user clicks on the login button, and displaying another element (.dashboard) in its place. Initially, I set the .dashboard class to ha ...

A guide on dynamically adjusting text size based on viewport width and height in React with TailwindCSS

Utilizing React and Tailwind, I am working towards creating two overlapping rectangles with text. The top rectangle's text should be aligned to the left, while the bottom rectangle's text should be aligned to the right. Regardless of window size ...

Concentrate on the initial element once the focused element has been removed

In my code snippet below, I have a TextField with autoFocus set. Upon initial rendering, the focus is correctly on this field. However, if I navigate to the second "Del" button using the tab key and delete that row, the focus is lost upon re-rendering. How ...

What is the process for creating a hover effect on a button that is triggered when a user hovers over the button's parent container?

Visit this link to view the code My Goal: I want the hover effect that normally applies to buttons when hovered over, to instead be applied when a user hovers over the parent div containing those buttons. The parent divs in question have a class of "ser ...

An error will occur if you try to modify the state of a component from outside the component

Creating a modal component that triggers a bootstrap modal from any section of the application and then defines custom states for that component externally. It is functional, however, an error message consistently appears upon opening the modal, and I am u ...

tips on encrypting css/js file names

Can the filename of the css/js file be encrypted? I have noticed that when I view the source of the website, the filenames of the css and js files look encrypted like this. <link href="/assets/css/builds/73e15c8a3cf6409214bbf8a742e9b5d41403226617.css" ...

What is the best way to apply various styles using the ng-style directive in different scenarios?

When working in Angular, I am attempting to apply different style attributes based on varying conditions. However, the typical conditional expression method is limited to just two conditions and is not providing the desired results. <div ng-style=" ...

Should the header include individual CSS and JS files, or should all code be contained within a single CSS and JS file?

As I work on optimizing my website, I find myself juggling multiple plugins that include jQuery plugins with CSS along with my own JavaScript code. Currently, the CSS is spread across separate files for each plugin I have downloaded. When needed on a page ...

Error: The function `map` cannot be applied to this.props.comments

I am new to coding in React and have been trying to create a comment box. However, I keep encountering errors such as TypeError: this.props.comments.map is not a function and Uncaught TypeError: comments.concat is not a function. I am feeling lost and conf ...

Trouble with Next.js loading files efficiently

Exploring the realm of Next.js, I find myself diving into the world of JavaScript. This is where my directory resides. https://i.stack.imgur.com/GLEWR.png Within the 'counter-state.js' file: I have implemented the following code: import {useSt ...

Ways to dynamically implement a JSON configuration file in React JS during runtime

Looking to achieve text and image externalization in React? It's all about making changes in a JSON file that will reflect on your live Single Page Application (SPA). Here's an example of what the JSON file might look like: { "signup. ...

Can Keyboarddatepicker automatically format the date?

We are utilizing material ui Keyboarddatepicker in our React application and have specified the date format using props as format=DD/MMM/YYYY, resulting in dates being displayed like "10/12/2020". The date picker functions perfectly when selecting a date. ...

Upon hovering, icons for each project name are displayed when `mouseenter` event is triggered

My issue lies with the mouseenter function. I am trying to display icons specific to the project name I hover over, but currently, it displays icons for all projects at once. I want each project hovered over to show me its respective icons Below is some c ...

The npm installation failed to execute when using Node.js version 16.0.0

Hey, I just cloned my repository and attempted to run npm install but encountered an error npm install npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! Found: <a href="/cdn-cgi/l/email-protection" class= ...

Using React Hooks: Can you modify the useState hook from a separate .jsx file?

I have a dilemma with two separate components stored in their own files. One of them is meant to be reusable, so I want to keep them separate. However, I need to update a specific hook within the "child" component from the "parent" component. Passing a val ...

Is it possible to create a React app (using create-react-app) paired with an Express backend all within a single

I am currently working on a React front-end application with an Express backend setup that is functioning smoothly in my local environment as a proxy API server. During development, the React front-end is hosted on port 3001 while the Express server is ru ...

Unable to utilize React Icons component as an object value in typescript

Currently, as I develop my personal website using typescript and react, I am faced with an issue in the footer section. I have an array of objects with url and icon properties that I map through to display different icons on each iteration. Initially, this ...

How can we effectively create reusable modals in React with the most efficient approach?

Currently, I am developing an application using React and Material UI. In order to streamline the use of Modals across multiple pages, I have implemented a global modal context that dynamically populates the modal with different props based on state change ...

Tips for disabling selection when the enter key is pressed in react-select

I am facing an issue where the enter key selects the first value from the drop-down list in a react-select component even if the user has not typed anything. Does anyone have a solution to prevent this behavior? The selection should only happen when the u ...