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

Commit f28ba23

Browse files
committed
Add ability to generate radial gradients (fix #2)
1 parent c3d7158 commit f28ba23

File tree

4 files changed

+354
-42
lines changed

4 files changed

+354
-42
lines changed

CHANGELOG.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ 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-
## [Unreleased]
8+
## [2.0.0] - 2019-05-17
9+
10+
### Added since 2.0.0-beta.2
11+
- Added radial gradient utilities (see `README` for more info)
912

1013
### Changed since 2.0.0-beta.2
14+
- Renamed the `gradients` theme and variants keys to `linearGradients`
1115
- Added support for global variants thanks to Tailwind’s `variants()` helper function
1216

1317
### Added since 1.x
@@ -52,7 +56,8 @@ and this project mostly adheres to [Semantic Versioning](https://semver.org/spec
5256

5357
Initial release
5458

55-
[Unreleased]: https://github.com/benface/tailwindcss-gradients/compare/v2.0.0-beta.2...HEAD
59+
[Unreleased]: https://github.com/benface/tailwindcss-gradients/compare/v2.0.0...HEAD
60+
[2.0.0]: https://github.com/benface/tailwindcss-gradients/compare/v2.0.0-beta.2...v2.0.0
5661
[2.0.0-beta.2]: https://github.com/benface/tailwindcss-gradients/compare/v2.0.0-beta.1...v2.0.0-beta.2
5762
[2.0.0-beta.1]: https://github.com/benface/tailwindcss-gradients/compare/v1.1.0...v2.0.0-beta.1
5863
[1.1.0]: https://github.com/benface/tailwindcss-gradients/compare/v1.0.1...v1.1.0

README.md

+41-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ npm install tailwindcss-gradients
1212
// tailwind.config.js
1313
{
1414
theme: {
15-
gradients: {
15+
linearGradients: {
1616
directions: { // defaults to these values
1717
't': 'to top',
1818
'tr': 'to top right',
@@ -29,9 +29,34 @@ npm install tailwindcss-gradients
2929
'red-green-blue': ['#f00', '#0f0', '#00f'],
3030
},
3131
},
32+
radialGradients: {
33+
shapes: { // defaults to this value
34+
'default': 'ellipse',
35+
},
36+
sizes: { // defaults to this value
37+
'default': 'closest-side',
38+
},
39+
positions: { // defaults to these values
40+
'default': 'center',
41+
't': 'top',
42+
'tr': 'top right',
43+
'r': 'right',
44+
'br': 'bottom right',
45+
'b': 'bottom',
46+
'bl': 'bottom left',
47+
'l': 'left',
48+
'tl': 'top left',
49+
},
50+
colors: { // defaults to {}
51+
'red': '#f00',
52+
'red-blue': ['#f00', '#00f'],
53+
'red-green-blue': ['#f00', '#0f0', '#00f'],
54+
},
55+
},
3256
},
3357
variants: {
34-
gradients: ['responsive'], // defaults to ['responsive']
58+
linearGradients: ['responsive'], // defaults to ['responsive']
59+
radialGradients: ['responsive'], // defaults to ['responsive']
3560
},
3661
plugins: [
3762
require('tailwindcss-gradients')(),
@@ -46,10 +71,16 @@ or you can reference your existing theme colors:
4671
'red': '#f00',
4772
'blue': '#00f',
4873
},
49-
gradients: theme => ({
74+
linearGradients: theme => ({
5075
// directions: { ... },
5176
colors: theme('colors'),
5277
}),
78+
radialGradients: theme => ({
79+
// shapes: { ... },
80+
// sizes: { ... },
81+
// positions: { ... },
82+
colors: theme('colors'),
83+
}),
5384
},
5485
plugins: [
5586
require('tailwindcss-gradients')(),
@@ -60,8 +91,14 @@ or you can reference your existing theme colors:
6091
This plugin generates the following utilities:
6192

6293
```css
63-
/* configurable with the "gradients" theme object */
94+
/* configurable with the "linearGradients" theme object */
6495
.bg-gradient-[direction-key]-[color-key] {
6596
background-image: linear-gradient([direction-value], [color-value-1], [color-value-2], [...]);
6697
}
98+
99+
/* configurable with the "radialGradients" theme object */
100+
/* note that the "default" [shape-key], [size-key], and [position-key] are omitted from the class */
101+
.bg-radial-[shape-key]-[size-key]-[position-key]-[color-key] {
102+
background-image: radial-gradient([shape-value] [size-value] at [position-value], [color-value-1], [color-value-2], [...]);
103+
}
67104
```

index.js

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

4+
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();
9+
colors = transparentFirst ? [transparentColor, color] : [color, transparentColor];
10+
}
11+
return colors.join(', ');
12+
};
13+
414
module.exports = function() {
515
return ({ theme, variants, e, addUtilities }) => {
6-
const defaultGradientDirections = {
16+
const defaultLinearGradientDirections = {
717
't': 'to top',
818
'tr': 'to top right',
919
'r': 'to right',
@@ -13,30 +23,66 @@ module.exports = function() {
1323
'l': 'to left',
1424
'tl': 'to top left',
1525
};
16-
const defaultGradientColors = {};
17-
const defaultGradientVariants = ['responsive'];
26+
const defaultLinearGradientColors = {};
27+
const defaultLinearGradientVariants = ['responsive'];
28+
const defaultRadialGradientShapes = {
29+
'default': 'ellipse',
30+
};
31+
const defaultRadialGradientSizes = {
32+
'default': 'closest-side',
33+
};
34+
const defaultRadialGradientPositions = {
35+
'default': 'center',
36+
't': 'top',
37+
'tr': 'top right',
38+
'r': 'right',
39+
'br': 'bottom right',
40+
'b': 'bottom',
41+
'bl': 'bottom left',
42+
'l': 'left',
43+
'tl': 'top left',
44+
};
45+
const defaultRadialGradientColors = {};
46+
const defaultRadialGradientVariants = ['responsive'];
1847

19-
const gradientDirections = theme('gradients.directions', defaultGradientDirections);
20-
const gradientColors = theme('gradients.colors', defaultGradientColors);
21-
const gradientVariants = variants('gradients', defaultGradientVariants);
48+
const linearGradientDirections = theme('linearGradients.directions', defaultLinearGradientDirections);
49+
const linearGradientColors = theme('linearGradients.colors', defaultLinearGradientColors);
50+
const linearGradientVariants = variants('linearGradients', defaultLinearGradientVariants);
51+
const radialGradientShapes = theme('radialGradients.shapes', defaultRadialGradientShapes);
52+
const radialGradientSizes = theme('radialGradients.sizes', defaultRadialGradientSizes);
53+
const radialGradientPositions = theme('radialGradients.positions', defaultRadialGradientPositions);
54+
const radialGradientColors = theme('radialGradients.colors', defaultRadialGradientColors);
55+
const radialGradientVariants = variants('radialGradients', defaultRadialGradientVariants);
2256

23-
const gradientUtilities = (function() {
57+
const linearGradientUtilities = (function() {
2458
let utilities = {};
25-
_.forEach(gradientColors, (colors, colorKey) => {
26-
if (!_.isArray(colors) || colors.length === 1) {
27-
let color = _.isArray(colors) ? colors[0] : colors;
28-
let parsedColor = Color(color);
29-
colors = [parsedColor.alpha(0).rgb().string(), color];
30-
}
31-
_.forEach(gradientDirections, (direction, directionKey) => {
59+
_.forEach(linearGradientColors, (colors, colorKey) => {
60+
_.forEach(linearGradientDirections, (direction, directionKey) => {
3261
utilities[`.${e(`bg-gradient-${directionKey}-${colorKey}`)}`] = {
33-
backgroundImage: `linear-gradient(${direction}, ${colors.join(', ')})`,
62+
backgroundImage: `linear-gradient(${direction}, ${listColors(colors, true)})`,
3463
};
3564
});
3665
});
3766
return utilities;
3867
})();
3968

40-
addUtilities(gradientUtilities, gradientVariants);
69+
const radialGradientUtilities = (function() {
70+
let utilities = {};
71+
_.forEach(radialGradientColors, (colors, colorKey) => {
72+
_.forEach(radialGradientPositions, (position, positionKey) => {
73+
_.forEach(radialGradientSizes, (size, sizeKey) => {
74+
_.forEach(radialGradientShapes, (shape, shapeKey) => {
75+
utilities[`.${e(`bg-radial${shapeKey === 'default' ? '' : `-${shapeKey}`}${sizeKey === 'default' ? '' : `-${sizeKey}`}${positionKey === 'default' ? '' : `-${positionKey}`}-${colorKey}`)}`] = {
76+
backgroundImage: `radial-gradient(${shape} ${size} at ${position}, ${listColors(colors, false)})`,
77+
};
78+
});
79+
});
80+
});
81+
});
82+
return utilities;
83+
})();
84+
85+
addUtilities(linearGradientUtilities, linearGradientVariants);
86+
addUtilities(radialGradientUtilities, radialGradientVariants);
4187
};
4288
};

0 commit comments

Comments
 (0)