02. Restore color opacity support
š TL;DR
- We added themable colors that change fron theme to theme, butā¦
- We
borked
the color opacity support in the process š - We need to compose Tailwindās internal opacity CSS variable within our themable colors
ā¬ļø Skip to the challenge
š¢ Background
When implementing our themable colors, weāve accidentally thrown away an important feature of Tailwind CSS: Color Opacity management.
Oh no! š
How Tailwind CSS handles opacity
You may not even have thought about this, but there are no such properties as text-opacity
or background-opacity
in CSS.
Yet, Tailwind CSS still provides text-opacity
and bg-opacity
utility classes that work flawlessly.
How?
Letās take a look at a background-color
class for a default Tailwind CSS color, like bg-indigo-600
for example:
<p class="bg-indigo-600">I love this color</p>
Hereās the CSS generated for this utility class:
.bg-indigo-600 {
--tw-bg-opacity: 1;
background-color: rgb(79 70 229 / var(--tw-bg-opacity));
}
See that --tw-bg-opacity
CSS variable? Itās being used to compose the opacity into the background color, which is defined as an rgb()
color.
Nice!
CSS variable composition
By default, this opacity variable is set to 1
, or full opacity.
Letās add a background opacity utilty to our paragraph tag:
<p class="bg-indigo-600 bg-opacity-50">I love this color</p>
Can you guess what that bg-opacity-50
class is doing?
.bg-opacity-50 {
--tw-bg-opacity: 0.5;
}
Thatās right. It just redefines the value of the --tw-bg-opacity
variable. It doesnāt apply any CSS property or anything!
Since the opacity utilities are generated after the color utilities in the CSS file, the value of the --tw-bg-opacity
CSS variable is updated to 0.5
.
As a result, the background color is now a rgb()
color with a 0.5
alpha channel, or 50% opacity!
.bg-indigo-600 {
- --tw-bg-opacity: 1;
+ --tw-bg-opacity: 0.5;
background-color: rgb(79 70 229 / var(--tw-bg-opacity));
}
This is some pretty clever stuff, hopefully opening your eyes to what you can do with CSS variable composition.
Tailwind CSS is full of mind bending CSS variable composition tricks.
Tailwind CSS internally transform colors to RGB
While Tailwind CSS is capable of working its internal magic to transform different color formats like HEX, RGB or HSL, itās not able to do that when we define colors as CSS variables, as we currently do:
theme: {
extend: {
colors: {
base: 'var(--color-text-base)',
inverted: 'var(--color-text-inverted)',
}
}
}
When using CSS variables, hereās what CSS a similar background color utility would generate.
The following utility classā¦
<p class="bg-primary-600">I love this color</p>
⦠would generate this CSS:
.bg-primary-600 {
background-color: var(--primary-600);
}
No opacity composition anywhere.
Itās just using the CSS variable weāve passed to it.
Adding a background opacity would do nothing, since it just updates the --tw-bg-opacity
variable⦠which is never used in our bg-primary-600
class.
In other words, weāre stuck with using full opacity colors only with our current implementation.
Thatās a bummer!
<alpha-value>
to the rescue
Luckily, Tailwind gives us access to itās internal <alpha-value>
, which we can use to compose our own rgb()
colors when consuming CSS variables.
This is what youāll need to do in this challenge!
Hereās a link to a very useful page of the Tailwind CSS documentation website.
š Your challenge š
In the Tailwind Play below:
-
In the CSS tab, convert the HEX color values to
R
G
B
channels. Not thergb()
color, just the individual channels. -
In the JS tab, update the
primary
object to usergb()
color functions. -
Once again, donāt worry about code duplication just yet. Weāll get to that later.
Youāll find more hints within the Play.
āļø Good luck!
Start challenge