Skip to content

Commit 7945f0f

Browse files
authored
Merge pull request #2176 from tailwindlabs/gradients
Add background gradient support
2 parents 1d2dd1a + 4ca48db commit 7945f0f

11 files changed

+230957
-149148
lines changed

__tests__/fixtures/tailwind-output-flagged.css

Lines changed: 79978 additions & 57978 deletions
Large diffs are not rendered by default.

__tests__/fixtures/tailwind-output-important.css

Lines changed: 53409 additions & 33509 deletions
Large diffs are not rendered by default.

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

Lines changed: 44051 additions & 24151 deletions
Large diffs are not rendered by default.

__tests__/fixtures/tailwind-output.css

Lines changed: 53409 additions & 33509 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
@@ -9,6 +9,8 @@ import appearance from './plugins/appearance'
99
import backgroundAttachment from './plugins/backgroundAttachment'
1010
import backgroundClip from './plugins/backgroundClip'
1111
import backgroundColor from './plugins/backgroundColor'
12+
import backgroundImage from './plugins/backgroundImage'
13+
import gradientColorStops from './plugins/gradientColorStops'
1214
import backgroundPosition from './plugins/backgroundPosition'
1315
import backgroundRepeat from './plugins/backgroundRepeat'
1416
import backgroundSize from './plugins/backgroundSize'
@@ -118,6 +120,8 @@ export default function({ corePlugins: corePluginConfig }) {
118120
backgroundAttachment,
119121
backgroundClip,
120122
backgroundColor,
123+
backgroundImage,
124+
gradientColorStops,
121125
backgroundOpacity,
122126
backgroundPosition,
123127
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/gradientColorStops.js

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

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+
}

src/util/withAlphaVariable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function hasAlpha(color) {
1010
)
1111
}
1212

13-
function toRgba(color) {
13+
export function toRgba(color) {
1414
const [r, g, b, a] = createColor(color)
1515
.rgb()
1616
.array()

stubs/defaultConfig.stub.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ module.exports = {
151151
'64': '16rem',
152152
},
153153
backgroundColor: theme => theme('colors'),
154+
backgroundImage: {
155+
'gradient-to-t': 'linear-gradient(to top, var(--gradient-color-stops))',
156+
'gradient-to-tr': 'linear-gradient(to top right, var(--gradient-color-stops))',
157+
'gradient-to-r': 'linear-gradient(to right, var(--gradient-color-stops))',
158+
'gradient-to-br': 'linear-gradient(to bottom right, var(--gradient-color-stops))',
159+
'gradient-to-b': 'linear-gradient(to bottom, var(--gradient-color-stops))',
160+
'gradient-to-bl': 'linear-gradient(to bottom left, var(--gradient-color-stops))',
161+
'gradient-to-l': 'linear-gradient(to left, var(--gradient-color-stops))',
162+
'gradient-to-tl': 'linear-gradient(to top left, var(--gradient-color-stops))',
163+
},
164+
gradientColorStops: theme => theme('colors'),
154165
backgroundOpacity: theme => theme('opacity'),
155166
backgroundPosition: {
156167
bottom: 'bottom',
@@ -660,6 +671,8 @@ module.exports = {
660671
backgroundAttachment: ['responsive'],
661672
backgroundClip: ['responsive'],
662673
backgroundColor: ['responsive', 'hover', 'focus'],
674+
backgroundImage: ['responsive'],
675+
gradientColorStops: ['responsive', 'hover', 'focus'],
663676
backgroundOpacity: ['responsive', 'hover', 'focus'],
664677
backgroundPosition: ['responsive'],
665678
backgroundRepeat: ['responsive'],

0 commit comments

Comments
 (0)