Tailwind CSS custom gradient with React state and FastAverageColor

Cătălin Avasiloaie

I'm trying to add a custom gradient over an image using React state, Tailwind CSS and the FastAverageColor package (https://www.npmjs.com/package/fast-average-color) into my Next JS app. I'm using an useEffect hook for this:

const [avgColor, setAvgColor] = useState("")
useEffect(() => {
        const fac = new FastAverageColor()
        fac.getColorAsync(songData.track.album.images[0].url, { algorithm: 'dominant' }).then(result => {
            setAvgColor(`w-full h-full absolute bg-gradient-to-tr from-[${result.hex}] to-transparent`)
        })
}, [avgColor, songData.track.album.images])

The JSX is presented below:

        <div className="relative w-full h-full">
            <Image priority alt="test" layout='fill' objectFit='cover' src={songData.track.album.images[0].url} />
            {avgColor ? <div className={avgColor}></div> : null}
        </div>

The problem is that my gradient doesn't appear over the image. Do you know maybe why this happens?

doğukan

It's not possible to do with JIT classes but you can use from-current and set an inline color to set the from color.

So, try this:

setAvgColor(result.hex)

and

<div className="w-full h-full absolute bg-gradient-to-tr from-current to-transparent" style={{color: avgColor}}></div>

basic demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Tailwind CSS how to style a href links in React?

Tailwind css background gradient not applying

Why is my custom Tailwind CSS utility not applying to React elements?

Understanding npm install command of Tailwind CSS for React

Importing Custom Fonts Flask and Tailwind CSS

Tailwind css - use className custom spacing value as variable for tailwind config

Tailwind CSS does not work with React App

Tailwind css don't show hover state when input is in focus

Text is not centering in React JS (and Tailwind CSS)

Using material ui and Tailwind css in react projects

Tailwind CSS: Referencing to custom color in tailwind.config.js

Does Tailwind CSS remove unused custom CSS classes in production?

Tailwind css 3.0.5 classes is not working with react

React tailwind, cannot pass tailwind css from parent to child

Is there a way to adjust the **angle** of the *linear gradient* in Tailwind CSS?

How to add custom drop-shadow with linear-gradient in tailwind css?

React App adding Pure CSS to Tailwind CSS

how to make focus state working on div in tailwind-CSS

Tailwind CSS does not work with react app - no affect

Custom group "states" in tailwind css

How can I use tailwind-css to style the active state of the Navlink(react-router-dom)?

Tailwind CSS Dynamic State Updates But Not Rendered Correctly

Tailwind CSS not loading custom classes on React app

make gradient background with tailwind css

Navbar in React and Tailwind CSS

How to make a button a linear gradient color border in tailwind css?

Tailwind CSS not displaying using Create React App

How to align the color with React and tailwind.css

Tailwind CSS installed in react (vite) but not working