Skip to content

Commit

Permalink
Merge pull request #2176 from tailwindlabs/gradients
Browse files Browse the repository at this point in the history
Add background gradient support
  • Loading branch information
adamwathan committed Aug 18, 2020
2 parents 1d2dd1a + 4ca48db commit 7945f0f
Show file tree
Hide file tree
Showing 11 changed files with 230,957 additions and 149,148 deletions.
137,956 changes: 79,978 additions & 57,978 deletions __tests__/fixtures/tailwind-output-flagged.css

Large diffs are not rendered by default.

86,918 changes: 53,409 additions & 33,509 deletions __tests__/fixtures/tailwind-output-important.css

Large diffs are not rendered by default.

68,202 changes: 44,051 additions & 24,151 deletions __tests__/fixtures/tailwind-output-no-color-opacity.css

Large diffs are not rendered by default.

86,918 changes: 53,409 additions & 33,509 deletions __tests__/fixtures/tailwind-output.css

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -65,6 +65,7 @@
"postcss-js": "^2.0.0",
"postcss-nested": "^4.1.1",
"postcss-selector-parser": "^6.0.0",
"postcss-value-parser": "^4.1.0",
"pretty-hrtime": "^1.0.3",
"reduce-css-calc": "^2.1.6",
"resolve": "^1.14.2"
Expand Down
4 changes: 4 additions & 0 deletions src/corePlugins.js
Expand Up @@ -9,6 +9,8 @@ import appearance from './plugins/appearance'
import backgroundAttachment from './plugins/backgroundAttachment'
import backgroundClip from './plugins/backgroundClip'
import backgroundColor from './plugins/backgroundColor'
import backgroundImage from './plugins/backgroundImage'
import gradientColorStops from './plugins/gradientColorStops'
import backgroundPosition from './plugins/backgroundPosition'
import backgroundRepeat from './plugins/backgroundRepeat'
import backgroundSize from './plugins/backgroundSize'
Expand Down Expand Up @@ -118,6 +120,8 @@ export default function({ corePlugins: corePluginConfig }) {
backgroundAttachment,
backgroundClip,
backgroundColor,
backgroundImage,
gradientColorStops,
backgroundOpacity,
backgroundPosition,
backgroundRepeat,
Expand Down
23 changes: 23 additions & 0 deletions src/plugins/backgroundImage.js
@@ -0,0 +1,23 @@
import _ from 'lodash'
import usesCustomProperties from '../util/usesCustomProperties'

export default function() {
return function({ addUtilities, e, theme, variants, target }) {
const utilities = _.fromPairs(
_.map(theme('backgroundImage'), (value, modifier) => {
if (target('backgroundImage') === 'ie11' && usesCustomProperties(value)) {
return []
}

return [
`.${e(`bg-${modifier}`)}`,
{
'background-image': value,
},
]
})
)

addUtilities(utilities, variants('backgroundImage'))
}
}
54 changes: 54 additions & 0 deletions src/plugins/gradientColorStops.js
@@ -0,0 +1,54 @@
import _ from 'lodash'
import flattenColorPalette from '../util/flattenColorPalette'
import { toRgba } from '../util/withAlphaVariable'

export default function() {
return function({ addUtilities, e, theme, variants, target }) {
if (target('gradientColorStops') === 'ie11') {
return
}

const colors = flattenColorPalette(theme('gradientColorStops'))

const utilities = _(colors)
.map((value, modifier) => {
const transparentTo = (() => {
try {
const [r, g, b] = toRgba(value)
return `rgba(${r}, ${g}, ${b}, 0)`
} catch (_error) {
return `rgba(255, 255, 255, 0)`
}
})()

return [
[
`.${e(`from-${modifier}`)}`,
{
'--gradient-from-color': value,
'--gradient-color-stops': `var(--gradient-from-color), var(--gradient-to-color, ${transparentTo})`,
},
],
[
`.${e(`via-${modifier}`)}`,
{
'--gradient-via-color': value,
'--gradient-color-stops': `var(--gradient-from-color), var(--gradient-via-color), var(--gradient-to-color, ${transparentTo})`,
},
],
[
`.${e(`to-${modifier}`)}`,
{
'--gradient-to-color': value,
},
],
]
})
.unzip()
.flatten()
.fromPairs()
.value()

addUtilities(utilities, variants('gradientColorStops'))
}
}
14 changes: 14 additions & 0 deletions src/util/usesCustomProperties.js
@@ -0,0 +1,14 @@
import valueParser from 'postcss-value-parser'

export default function usesCustomProperties(value) {
let foundCustomProperty = false

valueParser(value).walk(node => {
if (node.type === 'function' && node.value === 'var') {
foundCustomProperty = true
}
return !foundCustomProperty
})

return foundCustomProperty
}
2 changes: 1 addition & 1 deletion src/util/withAlphaVariable.js
Expand Up @@ -10,7 +10,7 @@ function hasAlpha(color) {
)
}

function toRgba(color) {
export function toRgba(color) {
const [r, g, b, a] = createColor(color)
.rgb()
.array()
Expand Down
13 changes: 13 additions & 0 deletions stubs/defaultConfig.stub.js
Expand Up @@ -151,6 +151,17 @@ module.exports = {
'64': '16rem',
},
backgroundColor: theme => theme('colors'),
backgroundImage: {
'gradient-to-t': 'linear-gradient(to top, var(--gradient-color-stops))',
'gradient-to-tr': 'linear-gradient(to top right, var(--gradient-color-stops))',
'gradient-to-r': 'linear-gradient(to right, var(--gradient-color-stops))',
'gradient-to-br': 'linear-gradient(to bottom right, var(--gradient-color-stops))',
'gradient-to-b': 'linear-gradient(to bottom, var(--gradient-color-stops))',
'gradient-to-bl': 'linear-gradient(to bottom left, var(--gradient-color-stops))',
'gradient-to-l': 'linear-gradient(to left, var(--gradient-color-stops))',
'gradient-to-tl': 'linear-gradient(to top left, var(--gradient-color-stops))',
},
gradientColorStops: theme => theme('colors'),
backgroundOpacity: theme => theme('opacity'),
backgroundPosition: {
bottom: 'bottom',
Expand Down Expand Up @@ -660,6 +671,8 @@ module.exports = {
backgroundAttachment: ['responsive'],
backgroundClip: ['responsive'],
backgroundColor: ['responsive', 'hover', 'focus'],
backgroundImage: ['responsive'],
gradientColorStops: ['responsive', 'hover', 'focus'],
backgroundOpacity: ['responsive', 'hover', 'focus'],
backgroundPosition: ['responsive'],
backgroundRepeat: ['responsive'],
Expand Down

0 comments on commit 7945f0f

Please sign in to comment.