Skip to content

Commit 71268de

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

File tree

4 files changed

+111
-50
lines changed

4 files changed

+111
-50
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-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import fs from 'fs'
2-
import path from 'path'
3-
4-
import { run } from './util/run'
1+
import { run, html, css } from './util/run'
52

63
test('opacity', () => {
74
let config = {
85
darkMode: 'class',
9-
content: [path.resolve(__dirname, './opacity.test.html')],
6+
content: [
7+
{
8+
raw: html`
9+
<div class="divide-black"></div>
10+
<div class="border-black"></div>
11+
<div class="bg-black"></div>
12+
<div class="text-black"></div>
13+
<div class="placeholder-black"></div>
14+
`,
15+
},
16+
],
1017
corePlugins: {
1118
backgroundOpacity: false,
1219
borderOpacity: false,
@@ -17,9 +24,74 @@ test('opacity', () => {
1724
}
1825

1926
return run('@tailwind utilities', config).then((result) => {
20-
let expectedPath = path.resolve(__dirname, './opacity.test.css')
21-
let expected = fs.readFileSync(expectedPath, 'utf8')
27+
expect(result.css).toMatchCss(css`
28+
.divide-black > :not([hidden]) ~ :not([hidden]) {
29+
border-color: #000;
30+
}
31+
.border-black {
32+
border-color: #000;
33+
}
34+
.bg-black {
35+
background-color: #000;
36+
}
37+
.text-black {
38+
color: #000;
39+
}
40+
.placeholder-black::placeholder {
41+
color: #000;
42+
}
43+
`)
44+
})
45+
})
46+
47+
test('colors defined as functions work when opacity plugins are disabled', () => {
48+
let config = {
49+
darkMode: 'class',
50+
content: [
51+
{
52+
raw: html`
53+
<div class="divide-primary"></div>
54+
<div class="border-primary"></div>
55+
<div class="bg-primary"></div>
56+
<div class="text-primary"></div>
57+
<div class="placeholder-primary"></div>
58+
`,
59+
},
60+
],
61+
theme: {
62+
colors: {
63+
primary: ({ opacityValue }) =>
64+
opacityValue === undefined
65+
? 'rgb(var(--color-primary))'
66+
: `rgb(var(--color-primary) / ${opacityValue})`,
67+
},
68+
},
69+
corePlugins: {
70+
backgroundOpacity: false,
71+
borderOpacity: false,
72+
divideOpacity: false,
73+
placeholderOpacity: false,
74+
textOpacity: false,
75+
},
76+
}
2277

23-
expect(result.css).toMatchCss(expected)
78+
return run('@tailwind utilities', config).then((result) => {
79+
expect(result.css).toMatchCss(css`
80+
.divide-primary > :not([hidden]) ~ :not([hidden]) {
81+
border-color: rgb(var(--color-primary));
82+
}
83+
.border-primary {
84+
border-color: rgb(var(--color-primary));
85+
}
86+
.bg-primary {
87+
background-color: rgb(var(--color-primary));
88+
}
89+
.text-primary {
90+
color: rgb(var(--color-primary));
91+
}
92+
.placeholder-primary::placeholder {
93+
color: rgb(var(--color-primary));
94+
}
95+
`)
2496
})
2597
})

0 commit comments

Comments
 (0)