Skip to content
This repository was archived by the owner on Dec 15, 2024. It is now read-only.

Commit be1a653

Browse files
committed
Fix an error when using keywords for colors (fix #7)
1 parent 014276f commit be1a653

File tree

3 files changed

+124
-5
lines changed

3 files changed

+124
-5
lines changed

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project mostly adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [2.0.1] - 2019-05-23
9+
10+
### Fixed
11+
- Fixed an error when using keywords for colors such as `inherit` or `currentColor`
12+
813
## [2.0.0] - 2019-05-17
914

1015
### Added since 2.0.0-beta.2
@@ -56,7 +61,8 @@ and this project mostly adheres to [Semantic Versioning](https://semver.org/spec
5661

5762
Initial release
5863

59-
[Unreleased]: https://github.com/benface/tailwindcss-gradients/compare/v2.0.0...HEAD
64+
[Unreleased]: https://github.com/benface/tailwindcss-gradients/compare/v2.0.1...HEAD
65+
[2.0.1]: https://github.com/benface/tailwindcss-gradients/compare/v2.0.0...v2.0.1
6066
[2.0.0]: https://github.com/benface/tailwindcss-gradients/compare/v2.0.0-beta.2...v2.0.0
6167
[2.0.0-beta.2]: https://github.com/benface/tailwindcss-gradients/compare/v2.0.0-beta.1...v2.0.0-beta.2
6268
[2.0.0-beta.1]: https://github.com/benface/tailwindcss-gradients/compare/v1.1.0...v2.0.0-beta.1

index.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
const _ = require('lodash');
22
const Color = require('color');
33

4+
const colorsAreSupported = function(colors) {
5+
const unsupportedColorKeywords = ['inherit', 'initial', 'unset', 'revert'];
6+
return _.intersection(unsupportedColorKeywords, colors).length === 0;
7+
};
8+
49
const listColors = function(colors, transparentFirst = true) {
5-
if (!_.isArray(colors) || colors.length === 1) {
6-
const color = _.isArray(colors) ? colors[0] : colors;
7-
const parsedColor = Color(color);
8-
const transparentColor = parsedColor.alpha(0).rgb().string();
10+
if (colors.length === 1) {
11+
const color = colors[0];
12+
let transparentColor = 'transparent';
13+
try {
14+
const parsedColor = Color(color);
15+
transparentColor = parsedColor.alpha(0).rgb().string();
16+
}
17+
catch (e) {
18+
}
919
colors = transparentFirst ? [transparentColor, color] : [color, transparentColor];
1020
}
1121
return colors.join(', ');
@@ -57,6 +67,10 @@ module.exports = function() {
5767
const linearGradientUtilities = (function() {
5868
let utilities = {};
5969
_.forEach(linearGradientColors, (colors, colorKey) => {
70+
colors = _.castArray(colors);
71+
if (!colorsAreSupported(colors)) {
72+
return; // continue
73+
}
6074
_.forEach(linearGradientDirections, (direction, directionKey) => {
6175
utilities[`.${e(`bg-gradient-${directionKey}-${colorKey}`)}`] = {
6276
backgroundImage: `linear-gradient(${direction}, ${listColors(colors, true)})`,
@@ -69,6 +83,10 @@ module.exports = function() {
6983
const radialGradientUtilities = (function() {
7084
let utilities = {};
7185
_.forEach(radialGradientColors, (colors, colorKey) => {
86+
colors = _.castArray(colors);
87+
if (!colorsAreSupported(colors)) {
88+
return; // continue
89+
}
7290
_.forEach(radialGradientPositions, (position, positionKey) => {
7391
_.forEach(radialGradientSizes, (size, sizeKey) => {
7492
_.forEach(radialGradientShapes, (shape, shapeKey) => {

test.js

+95
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,101 @@ test('colors can be referenced from the theme with a closure', () => {
355355
});
356356
});
357357

358+
test('color keywords are accepted', () => {
359+
return generatePluginCss({
360+
theme: {
361+
colors: {
362+
'white': 'white',
363+
'black': 'black',
364+
'transparent': 'transparent',
365+
'current': 'currentColor',
366+
},
367+
linearGradients: theme => ({
368+
directions: {
369+
't': 'to top',
370+
},
371+
colors: theme('colors'),
372+
}),
373+
radialGradients: theme => ({
374+
positions: {
375+
't': 'top',
376+
},
377+
colors: theme('colors'),
378+
}),
379+
},
380+
variants: {
381+
linearGradients: [],
382+
radialGradients: [],
383+
},
384+
}).then(css => {
385+
expect(css).toMatchCss(`
386+
.bg-gradient-t-white {
387+
background-image: linear-gradient(to top, rgba(255, 255, 255, 0), white)
388+
}
389+
.bg-gradient-t-black {
390+
background-image: linear-gradient(to top, rgba(0, 0, 0, 0), black)
391+
}
392+
.bg-gradient-t-transparent {
393+
background-image: linear-gradient(to top, rgba(0, 0, 0, 0), transparent)
394+
}
395+
.bg-gradient-t-current {
396+
background-image: linear-gradient(to top, transparent, currentColor)
397+
}
398+
.bg-radial-t-white {
399+
background-image: radial-gradient(ellipse closest-side at top, white, rgba(255, 255, 255, 0))
400+
}
401+
.bg-radial-t-black {
402+
background-image: radial-gradient(ellipse closest-side at top, black, rgba(0, 0, 0, 0))
403+
}
404+
.bg-radial-t-transparent {
405+
background-image: radial-gradient(ellipse closest-side at top, transparent, rgba(0, 0, 0, 0))
406+
}
407+
.bg-radial-t-current {
408+
background-image: radial-gradient(ellipse closest-side at top, currentColor, transparent)
409+
}
410+
`);
411+
});
412+
});
413+
414+
test('some keywords such as inherit are skipped', () => {
415+
return generatePluginCss({
416+
theme: {
417+
colors: {
418+
'inherit': 'inherit',
419+
'red': '#f00',
420+
'initial': 'initial',
421+
'unset': 'unset',
422+
'revert': 'revert',
423+
},
424+
linearGradients: theme => ({
425+
directions: {
426+
't': 'to top',
427+
},
428+
colors: theme('colors'),
429+
}),
430+
radialGradients: theme => ({
431+
positions: {
432+
't': 'top',
433+
},
434+
colors: theme('colors'),
435+
}),
436+
},
437+
variants: {
438+
linearGradients: [],
439+
radialGradients: [],
440+
},
441+
}).then(css => {
442+
expect(css).toMatchCss(`
443+
.bg-gradient-t-red {
444+
background-image: linear-gradient(to top, rgba(255, 0, 0, 0), #f00)
445+
}
446+
.bg-radial-t-red {
447+
background-image: radial-gradient(ellipse closest-side at top, #f00, rgba(255, 0, 0, 0))
448+
}
449+
`);
450+
});
451+
});
452+
358453
test('radial gradient shapes and sizes can be customized', () => {
359454
return generatePluginCss({
360455
theme: {

0 commit comments

Comments
 (0)