Skip to content

Commit ee42258

Browse files
committed
add fallback support for variables
1 parent 22ce041 commit ee42258

File tree

4 files changed

+119
-1
lines changed

4 files changed

+119
-1
lines changed

__tests__/fallback.test.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;
4+
5+
@layer components {
6+
.component .header {
7+
@apply text-red-400;
8+
}
9+
}

__tests__/fallback.test.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div class="bg-red-400">tailwindcss-variables</div>
2+
<div class="bg-red-500">tailwindcss-variables</div>
3+
<div class="bg-red-600">tailwindcss-variables</div>
4+
<div class="bg-red-700">tailwindcss-variables</div>
5+
<div class="text-red-800">tailwindcss-variables</div>
6+
<div class="bg-gray bg-opacity-50">tailwindcss-variables</div>
7+
<div class="component">
8+
<div class="header">tailwindcss-variables</div>
9+
</div>

__tests__/fallback.test.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const tailwindcssVariables = require('../src/index')
2+
const utils = require('./util/_utils')(__filename)
3+
const colorVariable = require('../colorVariable')
4+
5+
test('fallback', async () => {
6+
expect(
7+
await utils.diffOnly({
8+
corePlugins: ['textColor', 'textOpacity', 'backgroundColor', 'backgroundOpacity'],
9+
content: [utils.content()],
10+
darkMode: false,
11+
theme: {
12+
screens: false,
13+
colors: {
14+
red: {
15+
400: 'var(--colors-red-400 red)',
16+
500: 'var(--colors-red red)',
17+
600: colorVariable('var(--header-color, black)'),
18+
700: colorVariable('var(--header-color, black)', true),
19+
800: colorVariable('var(--header-color, black)', false),
20+
},
21+
22+
gray: 'var(--header-color, blue)',
23+
},
24+
25+
variables: {
26+
DEFAULT: {
27+
header: {
28+
color: '#ffffff',
29+
},
30+
31+
colors: {
32+
red: {
33+
DEFAULT: '#ff0000',
34+
400: '#ff3f3f',
35+
},
36+
},
37+
},
38+
},
39+
},
40+
41+
plugins: [
42+
tailwindcssVariables({
43+
colorVariables: true,
44+
}),
45+
],
46+
})
47+
).toMatchInlineSnapshot(`
48+
"
49+
50+
-
51+
+ :root {
52+
+ --header-color: #ffffff;
53+
+ --header-color-rgb: 255,255,255;
54+
+ --colors-red-400: #ff3f3f;
55+
+ --colors-red: #ff0000;
56+
+ --colors-red-400-rgb: 255,63,63;
57+
+ --colors-red-rgb: 255,0,0
58+
+ }
59+
+ .bg-red-400 {
60+
+ background-color: var(--colors-red-400 red)
61+
+ }
62+
+ .bg-red-500 {
63+
+ background-color: var(--colors-red red)
64+
+ }
65+
+ .bg-red-600 {
66+
+ --tw-bg-opacity: 1;
67+
+ background-color: rgba(var(--header-color-rgb, black), var(--tw-bg-opacity))
68+
+ }
69+
+ .bg-red-700 {
70+
+ --tw-bg-opacity: 1;
71+
+ background-color: rgba(var(--header-color, black), var(--tw-bg-opacity))
72+
+ }
73+
+ .bg-gray {
74+
+ background-color: var(--header-color, blue)
75+
+ }
76+
+ .bg-opacity-50 {
77+
+ --tw-bg-opacity: 0.5
78+
+ }
79+
+ .text-red-800 {
80+
+ --tw-text-opacity: 1;
81+
+ color: rgba(var(--header-color-rgb, black), var(--tw-text-opacity))
82+
+ }
83+
+ .text-gray {
84+
+ color: var(--header-color, blue)
85+
+ }
86+
87+
"
88+
`)
89+
})

src/helpers.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
const startsWith = require('lodash/startsWith')
22
const mapValues = require('lodash/mapValues')
33
const isPlainObject = require('lodash/isPlainObject')
4+
const includes = require('lodash/includes')
5+
const split = require('lodash/split')
6+
const trim = require('lodash/trim')
47

8+
const withFallback = (variable, startsWithVar = false) => {
9+
if (includes(variable, ',')) {
10+
variable = variable.replace(',', '-rgb,')
11+
return startsWithVar ? variable : variable + ')'
12+
} else {
13+
return startsWithVar ? variable.replace(')', '-rgb)') : variable + '-rgb'
14+
}
15+
}
516
const withRgb = (variable, forceRGB) => {
617
if (forceRGB) {
718
return startsWith(variable, 'var') ? variable : 'var(' + variable + ')'
819
}
9-
return startsWith(variable, 'var') ? variable.replace(')', '-rgb)') : 'var(' + variable + '-rgb)'
20+
return startsWith(variable, 'var') ? withFallback(variable, true) : 'var(' + withFallback(variable) + ')'
1021
}
1122

1223
const colorVariable = (variable, forceRGB = false) => {

0 commit comments

Comments
 (0)