Skip to content

Commit

Permalink
Pass opacityValue to gradient colors for transparency support, do not…
Browse files Browse the repository at this point in the history
… pass opacityVariable since unused
  • Loading branch information
adamwathan committed Aug 19, 2020
1 parent 28930f4 commit a7e19ca
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
37 changes: 28 additions & 9 deletions __tests__/plugins/gradientColorStops.test.js
Expand Up @@ -6,30 +6,49 @@ test('opacity variables are given to colors defined as closures', () => {
tailwind({
theme: {
colors: {
primary: ({ opacityVariable }) => `rgba(31,31,31,var(${opacityVariable},1))`,
primary: ({ opacityVariable, opacityValue }) => {
if (opacityValue !== undefined) {
return `rgba(31,31,31,${opacityValue})`
}

if (opacityVariable !== undefined) {
return `rgba(31,31,31,var(${opacityVariable},1))`
}

return `rgb(31,31,31)`
},
},
opacity: {
'50': '0.5',
},
},
variants: {
textColor: [],
textOpacity: [],
gradientColorStops: [],
},
corePlugins: ['gradientColorStops'],
corePlugins: ['textColor', 'textOpacity', 'gradientColorStops'],
}),
])
.process('@tailwind utilities', { from: undefined })
.then(result => {
const expected = `
.text-primary {
color: rgba(31,31,31,var(--text-opacity,1))
}
.text-opacity-50 {
--text-opacity: 0.5
}
.from-primary {
--gradient-from-color: rgba(31,31,31,var(--gradient-from-opacity,1));
--gradient-color-stops: var(--gradient-from-color), var(--gradient-to-color, rgba(255, 255, 255, 0))
--gradient-from-color: rgb(31,31,31);
--gradient-color-stops: var(--gradient-from-color), var(--gradient-to-color, rgba(31, 31, 31, 0))
}
.via-primary {
--gradient-via-color: rgba(31,31,31,var(--gradient-via-opacity,1));
--gradient-color-stops: var(--gradient-from-color), var(--gradient-via-color), var(--gradient-to-color, rgba(255, 255, 255, 0))
--gradient-via-color: rgb(31,31,31);
--gradient-color-stops: var(--gradient-from-color), var(--gradient-via-color), var(--gradient-to-color, rgba(31, 31, 31, 0))
}
.to-primary {
--gradient-to-color: rgba(31,31,31,var(--gradient-to-opacity,1))
--gradient-to-color: rgb(31,31,31)
}
`

Expand Down
10 changes: 6 additions & 4 deletions src/plugins/gradientColorStops.js
Expand Up @@ -12,17 +12,19 @@ export default function() {

const utilities = _(colors)
.map((value, modifier) => {
const getColorValue = (color, type) => {
const getColorValue = color => {
if (_.isFunction(color)) {
return value({
opacityVariable: `--gradient-${type}-opacity`,
})
return value({})
}

return color
}

const transparentTo = (() => {
if (_.isFunction(value)) {
return value({ opacityValue: 0 })
}

try {
const [r, g, b] = toRgba(value)
return `rgba(${r}, ${g}, ${b}, 0)`
Expand Down

0 comments on commit a7e19ca

Please sign in to comment.