Skip to content

Commit 7cbd9dc

Browse files
committed
Support using functions as colors when disabling color opacity utilities
1 parent 9daebe4 commit 7cbd9dc

File tree

4 files changed

+111
-47
lines changed

4 files changed

+111
-47
lines changed

src/corePlugins.js

+31-10
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,11 @@ export let divideColor = ({ matchUtilities, theme, corePlugins }) => {
10141014
{
10151015
divide: (value) => {
10161016
if (!corePlugins('divideOpacity')) {
1017-
return { ['& > :not([hidden]) ~ :not([hidden])']: { 'border-color': value } }
1017+
return {
1018+
['& > :not([hidden]) ~ :not([hidden])']: {
1019+
'border-color': toColorValue(value),
1020+
},
1021+
}
10181022
}
10191023

10201024
return {
@@ -1174,9 +1178,10 @@ export let borderStyle = ({ addUtilities }) => {
11741178

11751179
export let borderColor = ({ addBase, matchUtilities, theme, corePlugins }) => {
11761180
if (!corePlugins('borderOpacity')) {
1181+
let value = theme('borderColor.DEFAULT', 'currentColor')
11771182
addBase({
11781183
'@defaults border-width': {
1179-
'border-color': theme('borderColor.DEFAULT', 'currentColor'),
1184+
'border-color': toColorValue(value),
11801185
},
11811186
})
11821187
} else {
@@ -1193,7 +1198,9 @@ export let borderColor = ({ addBase, matchUtilities, theme, corePlugins }) => {
11931198
{
11941199
border: (value) => {
11951200
if (!corePlugins('borderOpacity')) {
1196-
return { 'border-color': value }
1201+
return {
1202+
'border-color': toColorValue(value),
1203+
}
11971204
}
11981205

11991206
return withAlphaVariable({
@@ -1213,7 +1220,9 @@ export let borderColor = ({ addBase, matchUtilities, theme, corePlugins }) => {
12131220
{
12141221
'border-t': (value) => {
12151222
if (!corePlugins('borderOpacity')) {
1216-
return { 'border-top-color': value }
1223+
return {
1224+
'border-top-color': toColorValue(value),
1225+
}
12171226
}
12181227

12191228
return withAlphaVariable({
@@ -1224,7 +1233,9 @@ export let borderColor = ({ addBase, matchUtilities, theme, corePlugins }) => {
12241233
},
12251234
'border-r': (value) => {
12261235
if (!corePlugins('borderOpacity')) {
1227-
return { 'border-right-color': value }
1236+
return {
1237+
'border-right-color': toColorValue(value),
1238+
}
12281239
}
12291240

12301241
return withAlphaVariable({
@@ -1235,7 +1246,9 @@ export let borderColor = ({ addBase, matchUtilities, theme, corePlugins }) => {
12351246
},
12361247
'border-b': (value) => {
12371248
if (!corePlugins('borderOpacity')) {
1238-
return { 'border-bottom-color': value }
1249+
return {
1250+
'border-bottom-color': toColorValue(value),
1251+
}
12391252
}
12401253

12411254
return withAlphaVariable({
@@ -1246,7 +1259,9 @@ export let borderColor = ({ addBase, matchUtilities, theme, corePlugins }) => {
12461259
},
12471260
'border-l': (value) => {
12481261
if (!corePlugins('borderOpacity')) {
1249-
return { 'border-left-color': value }
1262+
return {
1263+
'border-left-color': toColorValue(value),
1264+
}
12501265
}
12511266

12521267
return withAlphaVariable({
@@ -1272,7 +1287,9 @@ export let backgroundColor = ({ matchUtilities, theme, corePlugins }) => {
12721287
{
12731288
bg: (value) => {
12741289
if (!corePlugins('backgroundOpacity')) {
1275-
return { 'background-color': value }
1290+
return {
1291+
'background-color': toColorValue(value),
1292+
}
12761293
}
12771294

12781295
return withAlphaVariable({
@@ -1539,7 +1556,7 @@ export let textColor = ({ matchUtilities, theme, corePlugins }) => {
15391556
{
15401557
text: (value) => {
15411558
if (!corePlugins('textOpacity')) {
1542-
return { color: value }
1559+
return { color: toColorValue(value) }
15431560
}
15441561

15451562
return withAlphaVariable({
@@ -1583,7 +1600,11 @@ export let placeholderColor = ({ matchUtilities, theme, corePlugins }) => {
15831600
{
15841601
placeholder: (value) => {
15851602
if (!corePlugins('placeholderOpacity')) {
1586-
return { '&::placeholder': { color: value } }
1603+
return {
1604+
'&::placeholder': {
1605+
color: toColorValue(value),
1606+
},
1607+
}
15871608
}
15881609

15891610
return {

tests/opacity.test.css

-15
This file was deleted.

tests/opacity.test.html

-17
This file was deleted.

tests/opacity.test.js

+80-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import fs from 'fs'
22
import path from 'path'
33

4-
import { run } from './util/run'
4+
import { run, html, css } from './util/run'
55

66
test('opacity', () => {
77
let config = {
88
darkMode: 'class',
9-
content: [path.resolve(__dirname, './opacity.test.html')],
9+
content: [
10+
{
11+
raw: html`
12+
<div class="divide-black"></div>
13+
<div class="border-black"></div>
14+
<div class="bg-black"></div>
15+
<div class="text-black"></div>
16+
<div class="placeholder-black"></div>
17+
`,
18+
},
19+
],
1020
corePlugins: {
1121
backgroundOpacity: false,
1222
borderOpacity: false,
@@ -17,9 +27,74 @@ test('opacity', () => {
1727
}
1828

1929
return run('@tailwind utilities', config).then((result) => {
20-
let expectedPath = path.resolve(__dirname, './opacity.test.css')
21-
let expected = fs.readFileSync(expectedPath, 'utf8')
30+
expect(result.css).toMatchCss(css`
31+
.divide-black > :not([hidden]) ~ :not([hidden]) {
32+
border-color: #000;
33+
}
34+
.border-black {
35+
border-color: #000;
36+
}
37+
.bg-black {
38+
background-color: #000;
39+
}
40+
.text-black {
41+
color: #000;
42+
}
43+
.placeholder-black::placeholder {
44+
color: #000;
45+
}
46+
`)
47+
})
48+
})
2249

23-
expect(result.css).toMatchCss(expected)
50+
test('colors defined as functions work when opacity plugins are disabled', () => {
51+
let config = {
52+
darkMode: 'class',
53+
content: [
54+
{
55+
raw: html`
56+
<div class="divide-primary"></div>
57+
<div class="border-primary"></div>
58+
<div class="bg-primary"></div>
59+
<div class="text-primary"></div>
60+
<div class="placeholder-primary"></div>
61+
`,
62+
},
63+
],
64+
theme: {
65+
colors: {
66+
primary: ({ opacityValue }) =>
67+
opacityValue === undefined
68+
? 'rgb(var(--color-primary))'
69+
: `rgb(var(--color-primary) / ${opacityValue})`,
70+
},
71+
},
72+
corePlugins: {
73+
backgroundOpacity: false,
74+
borderOpacity: false,
75+
divideOpacity: false,
76+
placeholderOpacity: false,
77+
textOpacity: false,
78+
},
79+
}
80+
81+
return run('@tailwind utilities', config).then((result) => {
82+
expect(result.css).toMatchCss(css`
83+
.divide-primary > :not([hidden]) ~ :not([hidden]) {
84+
border-color: rgb(var(--color-primary));
85+
}
86+
.border-primary {
87+
border-color: rgb(var(--color-primary));
88+
}
89+
.bg-primary {
90+
background-color: rgb(var(--color-primary));
91+
}
92+
.text-primary {
93+
color: rgb(var(--color-primary));
94+
}
95+
.placeholder-primary::placeholder {
96+
color: rgb(var(--color-primary));
97+
}
98+
`)
2499
})
25100
})

0 commit comments

Comments
 (0)