Skip to content

Commit 7fb5d4a

Browse files
committed
Add support for background images and 2/3-color-stop gradients
1 parent 5b11f9c commit 7fb5d4a

10 files changed

+236578
-149311
lines changed

__tests__/fixtures/tailwind-output-flagged.css

Lines changed: 81456 additions & 57976 deletions
Large diffs are not rendered by default.

__tests__/fixtures/tailwind-output-important.css

Lines changed: 54770 additions & 33540 deletions
Large diffs are not rendered by default.

__tests__/fixtures/tailwind-output-no-color-opacity.css

Lines changed: 45485 additions & 24255 deletions
Large diffs are not rendered by default.

__tests__/fixtures/tailwind-output.css

Lines changed: 54770 additions & 33540 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"postcss-js": "^2.0.0",
6666
"postcss-nested": "^4.1.1",
6767
"postcss-selector-parser": "^6.0.0",
68+
"postcss-value-parser": "^4.1.0",
6869
"pretty-hrtime": "^1.0.3",
6970
"reduce-css-calc": "^2.1.6",
7071
"resolve": "^1.14.2"

src/corePlugins.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import appearance from './plugins/appearance'
88
import backgroundAttachment from './plugins/backgroundAttachment'
99
import backgroundClip from './plugins/backgroundClip'
1010
import backgroundColor from './plugins/backgroundColor'
11+
import backgroundImage from './plugins/backgroundImage'
12+
import gradientColor from './plugins/gradientColor'
1113
import backgroundPosition from './plugins/backgroundPosition'
1214
import backgroundRepeat from './plugins/backgroundRepeat'
1315
import backgroundSize from './plugins/backgroundSize'
@@ -116,6 +118,8 @@ export default function({ corePlugins: corePluginConfig }) {
116118
backgroundAttachment,
117119
backgroundClip,
118120
backgroundColor,
121+
backgroundImage,
122+
gradientColor,
119123
backgroundOpacity,
120124
backgroundPosition,
121125
backgroundRepeat,

src/plugins/backgroundImage.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import _ from 'lodash'
2+
import usesCustomProperties from '../util/usesCustomProperties'
3+
4+
export default function() {
5+
return function({ addUtilities, e, theme, variants, target }) {
6+
const utilities = _.fromPairs(
7+
_.map(theme('backgroundImage'), (value, modifier) => {
8+
if (target('backgroundImage') === 'ie11' && usesCustomProperties(value)) {
9+
return []
10+
}
11+
12+
return [
13+
`.${e(`bg-${modifier}`)}`,
14+
{
15+
'background-image': value,
16+
},
17+
]
18+
})
19+
)
20+
21+
addUtilities(utilities, variants('backgroundImage'))
22+
}
23+
}

src/plugins/gradientColor.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import _ from 'lodash'
2+
import flattenColorPalette from '../util/flattenColorPalette'
3+
4+
export default function() {
5+
return function({ addUtilities, e, theme, variants, target }) {
6+
if (target('gradientColor') === 'ie11') {
7+
return
8+
}
9+
10+
const colors = flattenColorPalette(theme('gradientColor'))
11+
12+
const utilities = _(colors)
13+
.map((value, modifier) => {
14+
return [
15+
[
16+
`.${e(`gradient-from-${modifier}`)}`,
17+
{
18+
'--gradient-from-color': value,
19+
'--gradient-color-stops': 'var(--gradient-from-color)',
20+
},
21+
],
22+
[
23+
`.${e(`gradient-to-${modifier}`)}`,
24+
{
25+
'--gradient-to-color': value,
26+
'--gradient-color-stops': 'var(--gradient-from-color), var(--gradient-to-color)',
27+
},
28+
],
29+
[
30+
`.${e(`gradient-mid-${modifier}`)}`,
31+
{
32+
'--gradient-mid-color': value,
33+
'--gradient-color-stops':
34+
'var(--gradient-from-color), var(--gradient-mid-color), var(--gradient-to-color)',
35+
},
36+
],
37+
]
38+
})
39+
.unzip()
40+
.flatten()
41+
.fromPairs()
42+
.value()
43+
44+
addUtilities(utilities, variants('gradientColor'))
45+
}
46+
}

src/util/usesCustomProperties.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import valueParser from 'postcss-value-parser'
2+
3+
export default function usesCustomProperties(value) {
4+
let foundCustomProperty = false
5+
6+
valueParser(value).walk(node => {
7+
if (node.type === 'function' && node.value === 'var') {
8+
foundCustomProperty = true
9+
}
10+
return !foundCustomProperty
11+
})
12+
13+
return foundCustomProperty
14+
}

stubs/defaultConfig.stub.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ module.exports = {
151151
'64': '16rem',
152152
},
153153
backgroundColor: theme => theme('colors'),
154+
backgroundImage: {
155+
'gradient-up': 'linear-gradient(0deg, var(--gradient-color-stops))',
156+
'gradient-right': 'linear-gradient(90deg, var(--gradient-color-stops))',
157+
'gradient-down': 'linear-gradient(180deg, var(--gradient-color-stops))',
158+
'gradient-left': 'linear-gradient(270deg, var(--gradient-color-stops))',
159+
},
160+
gradientColor: theme => theme('colors'),
154161
backgroundOpacity: theme => theme('opacity'),
155162
backgroundPosition: {
156163
bottom: 'bottom',
@@ -660,6 +667,8 @@ module.exports = {
660667
backgroundAttachment: ['responsive'],
661668
backgroundClip: ['responsive'],
662669
backgroundColor: ['responsive', 'hover', 'focus'],
670+
backgroundImage: ['responsive'],
671+
gradientColor: ['responsive', 'hover', 'focus'],
663672
backgroundOpacity: ['responsive', 'hover', 'focus'],
664673
backgroundPosition: ['responsive'],
665674
backgroundRepeat: ['responsive'],

0 commit comments

Comments
 (0)