Skip to content

Commit cca2231

Browse files
committed
Add support for radial gradients
1 parent 7ff8af8 commit cca2231

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,39 @@ Visual examples and online editor on [larsenwork.com/easing-gradients](https://l
9393
hsl(0, 100%, 50%)
9494
);
9595
}
96+
97+
.radial {
98+
background: radial-gradient(
99+
circle at top right,
100+
red,
101+
ease-in-out,
102+
blue
103+
);
104+
/* => */
105+
background: radial-gradient(
106+
circle at top right,
107+
hsl(0, 100%, 50%),
108+
hsl(353.5, 100%, 49.71%) 7.7%,
109+
hsl(347.13, 100%, 48.89%) 14.8%,
110+
hsl(341.1, 100%, 47.69%) 21%,
111+
hsl(335.24, 100%, 46.22%) 26.5%,
112+
hsl(329.48, 100%, 44.57%) 31.4%,
113+
hsl(323.63, 100%, 42.76%) 35.9%,
114+
hsl(317.56, 100%, 40.82%) 40.1%,
115+
hsl(310.92, 100%, 38.7%) 44.2%,
116+
hsl(303.81, 100%, 36.49%) 48.1%,
117+
hsl(296, 100%, 36.55%) 52%,
118+
hsl(288.73, 100%, 38.81%) 56%,
119+
hsl(282.14, 100%, 40.92%) 60.1%,
120+
hsl(276.09, 100%, 42.84%) 64.3%,
121+
hsl(270.27, 100%, 44.64%) 68.8%,
122+
hsl(264.54, 100%, 46.28%) 73.7%,
123+
hsl(258.7, 100%, 47.74%) 79.2%,
124+
hsl(252.68, 100%, 48.92%) 85.4%,
125+
hsl(246.32, 100%, 49.72%) 92.5%,
126+
hsl(240, 100%, 50%)
127+
);
128+
}
96129
```
97130

98131
<br>

demo/css/styles.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ body {
88
}
99

1010
.gradient {
11-
background-image: linear-gradient(to right, red, blue);
11+
background-image: radial-gradient(circle, red, blue);
1212
background-position: center;
1313
background-repeat: no-repeat;
1414
background-size: cover;
@@ -17,7 +17,7 @@ body {
1717
}
1818

1919
.gradient--easing {
20-
background: linear-gradient(to right, hsl(0, 100%, 50%), hsl(359.3, 100%, 49.42%) 7.7%, hsl(357.24, 100%, 47.8%) 14.8%, hsl(354.05, 100%, 45.49%) 21%, hsl(349.78, 100%, 42.72%) 26.5%, hsl(344.48, 100%, 39.72%) 31.4%, hsl(337.96, 100%, 36.57%) 35.9%, hsl(329.98, 100%, 33.33%) 40.1%, hsl(319.86, 100%, 29.96%) 44.2%, hsl(307.37, 100%, 26.64%) 48.1%, hsl(292.27, 100%, 26.72%) 52%, hsl(279.58, 100%, 30.13%) 56%, hsl(269.59, 100%, 33.49%) 60.1%, hsl(261.71, 100%, 36.71%) 64.3%, hsl(255.27, 100%, 39.86%) 68.8%, hsl(250.04, 100%, 42.84%) 73.7%, hsl(245.83, 100%, 45.57%) 79.2%, hsl(242.68, 100%, 47.86%) 85.4%, hsl(240.67, 100%, 49.45%) 92.5%, hsl(240, 100%, 50%));
20+
background: radial-gradient(circle at top right, hsl(0, 100%, 50%), hsl(353.5, 100%, 49.71%) 7.7%, hsl(347.13, 100%, 48.89%) 14.8%, hsl(341.1, 100%, 47.69%) 21%, hsl(335.24, 100%, 46.22%) 26.5%, hsl(329.48, 100%, 44.57%) 31.4%, hsl(323.63, 100%, 42.76%) 35.9%, hsl(317.56, 100%, 40.82%) 40.1%, hsl(310.92, 100%, 38.7%) 44.2%, hsl(303.81, 100%, 36.49%) 48.1%, hsl(296, 100%, 36.55%) 52%, hsl(288.73, 100%, 38.81%) 56%, hsl(282.14, 100%, 40.92%) 60.1%, hsl(276.09, 100%, 42.84%) 64.3%, hsl(270.27, 100%, 44.64%) 68.8%, hsl(264.54, 100%, 46.28%) 73.7%, hsl(258.7, 100%, 47.74%) 79.2%, hsl(252.68, 100%, 48.92%) 85.4%, hsl(246.32, 100%, 49.72%) 92.5%, hsl(240, 100%, 50%));
2121
width: 100vw;
2222
opacity: 1;
2323
}

demo/css/styles.pcss

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ body {
88
}
99

1010
.gradient {
11-
background-image: linear-gradient(to right, red, blue);
11+
background-image: radial-gradient(circle, red, blue);
1212
background-position: center;
1313
background-repeat: no-repeat;
1414
background-size: cover;
@@ -17,7 +17,7 @@ body {
1717
}
1818

1919
.gradient--easing {
20-
background: linear-gradient(to right, red, ease-in-out, blue);
20+
background: radial-gradient(circle at top right, red, ease-in-out, blue);
2121
width: 100vw;
2222
opacity: 1;
2323
}

index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ module.exports = postcss.plugin('easing-gradient', (options = {}) => {
1111
return function (css) {
1212
css.walkRules((rule) => {
1313
rule.walkDecls((decl) => {
14-
// If declaration value contains a linear-gradient.
15-
if (decl.value.includes('linear-gradient')) {
14+
// If declaration value contains a -gradient.
15+
if (decl.value.includes('-gradient')) {
1616
// Parse the declaration and walk through the nodes - https://github.com/TrySound/postcss-value-parser.
1717
const parsedValue = valueParser(decl.value)
1818
parsedValue.walk((node) => {
1919
// Only modify gradient as the value can contain more e.g. 'linear-gradient(black, pink) center'.
20-
if (node.value === 'linear-gradient') {
20+
if (node.value === 'linear-gradient' || node.value === 'radial-gradient') {
2121
// Get a sensible array of gradient parameters where e.g. a function is split into multiple array items
2222
const gradientParams = valueParser
2323
.stringify(helpers.divToSemiColon(node.nodes))
@@ -34,9 +34,9 @@ module.exports = postcss.plugin('easing-gradient', (options = {}) => {
3434
node.type = 'word'
3535
// Assume if it has 4 params it's because the first one is the direction
3636
if (gradientParams.length === 4) {
37-
node.value = `linear-gradient(${gradientParams[0]}, ${colorStops.join(', ')})`
37+
node.value = `${node.value}(${gradientParams[0]}, ${colorStops.join(', ')})`
3838
} else {
39-
node.value = `linear-gradient(${colorStops.join(', ')})`
39+
node.value = `${node.value}(${colorStops.join(', ')})`
4040
}
4141
// Update our declaration value
4242
decl.value = parsedValue.toString()

index.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ it('create an ease gradient with direction', () => {
3333
{ }
3434
)
3535
})
36+
it('create an ease radial-gradient', () => {
37+
return run(
38+
'a{ background: radial-gradient(circle at top right, red, ease-in-out, blue); }',
39+
'a{ background: radial-gradient(circle at top right, hsl(0, 100%, 50%), hsl(353.5, 100%, 49.71%) 7.7%, hsl(347.13, 100%, 48.89%) 14.8%, hsl(341.1, 100%, 47.69%) 21%, hsl(335.24, 100%, 46.22%) 26.5%, hsl(329.48, 100%, 44.57%) 31.4%, hsl(323.63, 100%, 42.76%) 35.9%, hsl(317.56, 100%, 40.82%) 40.1%, hsl(310.92, 100%, 38.7%) 44.2%, hsl(303.81, 100%, 36.49%) 48.1%, hsl(296, 100%, 36.55%) 52%, hsl(288.73, 100%, 38.81%) 56%, hsl(282.14, 100%, 40.92%) 60.1%, hsl(276.09, 100%, 42.84%) 64.3%, hsl(270.27, 100%, 44.64%) 68.8%, hsl(264.54, 100%, 46.28%) 73.7%, hsl(258.7, 100%, 47.74%) 79.2%, hsl(252.68, 100%, 48.92%) 85.4%, hsl(246.32, 100%, 49.72%) 92.5%, hsl(240, 100%, 50%)); }', // eslint-disable-line max-len
40+
{ }
41+
)
42+
})
3643

3744
/**
3845
* Output with custom settings

0 commit comments

Comments
 (0)