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

Nesting maps in JavaScript is a powerful way to transform

I'm in the process of developing a budgeting app using React and JavaScript. At the moment, I have successfully generated a table displaying various costs. Name Budget Used $ Used % Available Food 300 300 100 0 Streaming services 600 600 100 ...

Exploring ways to customize the date display in chart.js

Need help with adjusting the date display on my chart? See the chart below: Is there a way to customize how dates are displayed, like showing them horizontally and only every third date? Check out my code snippet below: import { ChartProps } from " ...

Adjust the CSS of a dynamically generated jQuery checkbox button in real-time

I am working on a project where I am creating a series of jQuery checkboxes dynamically within a loop. Here is how I am doing it: var checkbox = $('<input>').attr({type: 'checkbox', id: checkbox_id); panel.append(checkbox); panel ...

Error encountered when using Symfony 2 command line interface

Just started learning Symfony2 and encountered a problem. I made changes to my CSS and when I try to re-install them in Eclipse shell, I get this error message: 'C:\wamp\www\Symfony' is not recognized as an internal or external ...

What is the best way to align an html table to the top of the page?

Can anyone help me with positioning an HTML table at the top of the screen in IE7 without the automatic top border? I am using HTML and CSS for this. I'm currently working on development in VS2008. ...

What could be causing my Reanimated (React Native) code to fail on Android devices?

Check out my React Native code using Reanimated and react-native-gesture-handler: import React, { Component } from 'react'; import { View, Text, StyleSheet, Image, Dimensions } from 'react-n ...

TailwindCSS: Footer component overlaps central panel when scrolling on mobile devices

https://play.tailwindcss.com/notWWuhDpr?size=546x752 My project consists of three key components: The header A scrollable main panel The footer While it was relatively easy to implement this layout on the web using the code provided in the link above, I ...

What is the process for setting up both an open and closed status for my accordion?

After browsing through multiple threads on accordions, none seem to match my current structure which I believed was effective. In an attempt to learn and build elements from scratch, I created the following accordion with some help from Google and experi ...

Display a custom toast from a list using Bootstrap 5

I've been utilizing the toast feature from Bootstrap, and it works perfectly when displaying a single toast. However, I encountered an issue when trying to display a specific toast, like an error toast, from a list of predefined toasts. It ended up sh ...

What is the best way to update the state by either adding to it or replacing it if the key

I'm currently working on a project involving radio buttons and I need to create dynamic state by setting the initial state on the first click, then updating it with subsequent clicks by either concatenating the new value onto the existing state or rep ...

How can I switch between different images using jQuery?

HTML <div class="image_rollover"> <img id="image_one" src=image_one.png"> <img id="image_two" src="image_two.png"> <img id="image_three" src="image_three.png"> <img id="image_four" src="image_four.png"> ...

Why isn't the hover function working on the table row element?

When I try to apply a hover effect on tbody and td elements, it works perfectly. However, when I apply the same effect to the tr element, it doesn't work as expected. I am using inline style (js pattern) rather than CSS code and incorporating Radium i ...

What is the best way to assign a starting value to the array within the `<FieldArray/>` component?

While working with react-final form, I have been building a form that includes conditional fields that are displayed when a user clicks a checkbox. If the checkbox is checked, a component is rendered. The implementation is similar to the example provided ...

Manipulating divs by positioning them at the top, left, right, bottom, and center to occupy the entire visible portion of the page

Many suggest avoiding the use of table layouts and opting for divs and CSS instead, which I am happy to embrace. Please forgive me for asking a basic question. I am looking to create a layout where the center content stretches out to cover the entire visi ...

Attempting to reach <p> from numerous web pages

Currently, I am working on a project where I need to extract text data from various websites. I have successfully managed to retrieve the text data using <p> tags. I would really appreciate any assistance with this task. Thank you! ...

Floating color problem

Do you know why the social icons turn black when hovered over? Could it be related to the navbar buttons? Is there a way to change the hover color to blue or red only? Link to code snippet <body> <nav class="navbar navbar-default navbar-fixed-t ...

Customize the progress bar color in Bootstrap by setting a unique color to it

Is it possible to apply a custom color to the progress bar in Bootstrap that is not one of the standard options like success, info, warning or danger? I attempted adding the following code snippet to my bootstrap-theme.css file: .progress-bar-custom { b ...

What is the best way to position an image beside a jquery dialog box?

Apologies for not finding a suitable topic to discuss. Is it possible to place an image next to a jQuery dialog, similar to the following example? If so, how can this be achieved? ...

A guide on aligning a CSS item perfectly in the center using absolute positioning

I have been searching for a solution to my problem, but all I found were answers that addressed similar issues in a slightly different way. My challenge involves placing multiple smaller images on top of a larger background image. To achieve this, I utili ...

How can I effectively incorporate this SVG file into my React component without encountering any errors?

I am attempting to utilize JavaScript SVG as an icon, but I keep encountering this error. I managed to resolve one issue by changing xmls:xlink to xmlsXlink. However, the same fix did not work for xml:space. https://i.stack.imgur.com/Obwfj.png SVG code & ...