@csstools/postcss-gradients-interpolation-method 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changes to PostCSS Gradients Interpolation Method
2
+
3
+ ### 1.0.0 (March 4, 2022)
4
+
5
+ - Initial version
package/LICENSE.md ADDED
@@ -0,0 +1,108 @@
1
+ # CC0 1.0 Universal
2
+
3
+ ## Statement of Purpose
4
+
5
+ The laws of most jurisdictions throughout the world automatically confer
6
+ exclusive Copyright and Related Rights (defined below) upon the creator and
7
+ subsequent owner(s) (each and all, an “owner”) of an original work of
8
+ authorship and/or a database (each, a “Work”).
9
+
10
+ Certain owners wish to permanently relinquish those rights to a Work for the
11
+ purpose of contributing to a commons of creative, cultural and scientific works
12
+ (“Commons”) that the public can reliably and without fear of later claims of
13
+ infringement build upon, modify, incorporate in other works, reuse and
14
+ redistribute as freely as possible in any form whatsoever and for any purposes,
15
+ including without limitation commercial purposes. These owners may contribute
16
+ to the Commons to promote the ideal of a free culture and the further
17
+ production of creative, cultural and scientific works, or to gain reputation or
18
+ greater distribution for their Work in part through the use and efforts of
19
+ others.
20
+
21
+ For these and/or other purposes and motivations, and without any expectation of
22
+ additional consideration or compensation, the person associating CC0 with a
23
+ Work (the “Affirmer”), to the extent that he or she is an owner of Copyright
24
+ and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and
25
+ publicly distribute the Work under its terms, with knowledge of his or her
26
+ Copyright and Related Rights in the Work and the meaning and intended legal
27
+ effect of CC0 on those rights.
28
+
29
+ 1. Copyright and Related Rights. A Work made available under CC0 may be
30
+ protected by copyright and related or neighboring rights (“Copyright and
31
+ Related Rights”). Copyright and Related Rights include, but are not limited
32
+ to, the following:
33
+ 1. the right to reproduce, adapt, distribute, perform, display, communicate,
34
+ and translate a Work;
35
+ 2. moral rights retained by the original author(s) and/or performer(s);
36
+ 3. publicity and privacy rights pertaining to a person’s image or likeness
37
+ depicted in a Work;
38
+ 4. rights protecting against unfair competition in regards to a Work,
39
+ subject to the limitations in paragraph 4(i), below;
40
+ 5. rights protecting the extraction, dissemination, use and reuse of data in
41
+ a Work;
42
+ 6. database rights (such as those arising under Directive 96/9/EC of the
43
+ European Parliament and of the Council of 11 March 1996 on the legal
44
+ protection of databases, and under any national implementation thereof,
45
+ including any amended or successor version of such directive); and
46
+ 7. other similar, equivalent or corresponding rights throughout the world
47
+ based on applicable law or treaty, and any national implementations
48
+ thereof.
49
+
50
+ 2. Waiver. To the greatest extent permitted by, but not in contravention of,
51
+ applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
52
+ unconditionally waives, abandons, and surrenders all of Affirmer’s Copyright
53
+ and Related Rights and associated claims and causes of action, whether now
54
+ known or unknown (including existing as well as future claims and causes of
55
+ action), in the Work (i) in all territories worldwide, (ii) for the maximum
56
+ duration provided by applicable law or treaty (including future time
57
+ extensions), (iii) in any current or future medium and for any number of
58
+ copies, and (iv) for any purpose whatsoever, including without limitation
59
+ commercial, advertising or promotional purposes (the “Waiver”). Affirmer
60
+ makes the Waiver for the benefit of each member of the public at large and
61
+ to the detriment of Affirmer’s heirs and successors, fully intending that
62
+ such Waiver shall not be subject to revocation, rescission, cancellation,
63
+ termination, or any other legal or equitable action to disrupt the quiet
64
+ enjoyment of the Work by the public as contemplated by Affirmer’s express
65
+ Statement of Purpose.
66
+
67
+ 3. Public License Fallback. Should any part of the Waiver for any reason be
68
+ judged legally invalid or ineffective under applicable law, then the Waiver
69
+ shall be preserved to the maximum extent permitted taking into account
70
+ Affirmer’s express Statement of Purpose. In addition, to the extent the
71
+ Waiver is so judged Affirmer hereby grants to each affected person a
72
+ royalty-free, non transferable, non sublicensable, non exclusive,
73
+ irrevocable and unconditional license to exercise Affirmer’s Copyright and
74
+ Related Rights in the Work (i) in all territories worldwide, (ii) for the
75
+ maximum duration provided by applicable law or treaty (including future time
76
+ extensions), (iii) in any current or future medium and for any number of
77
+ copies, and (iv) for any purpose whatsoever, including without limitation
78
+ commercial, advertising or promotional purposes (the “License”). The License
79
+ shall be deemed effective as of the date CC0 was applied by Affirmer to the
80
+ Work. Should any part of the License for any reason be judged legally
81
+ invalid or ineffective under applicable law, such partial invalidity or
82
+ ineffectiveness shall not invalidate the remainder of the License, and in
83
+ such case Affirmer hereby affirms that he or she will not (i) exercise any
84
+ of his or her remaining Copyright and Related Rights in the Work or (ii)
85
+ assert any associated claims and causes of action with respect to the Work,
86
+ in either case contrary to Affirmer’s express Statement of Purpose.
87
+
88
+ 4. Limitations and Disclaimers.
89
+ 1. No trademark or patent rights held by Affirmer are waived, abandoned,
90
+ surrendered, licensed or otherwise affected by this document.
91
+ 2. Affirmer offers the Work as-is and makes no representations or warranties
92
+ of any kind concerning the Work, express, implied, statutory or
93
+ otherwise, including without limitation warranties of title,
94
+ merchantability, fitness for a particular purpose, non infringement, or
95
+ the absence of latent or other defects, accuracy, or the present or
96
+ absence of errors, whether or not discoverable, all to the greatest
97
+ extent permissible under applicable law.
98
+ 3. Affirmer disclaims responsibility for clearing rights of other persons
99
+ that may apply to the Work or any use thereof, including without
100
+ limitation any person’s Copyright and Related Rights in the Work.
101
+ Further, Affirmer disclaims responsibility for obtaining any necessary
102
+ consents, permissions or other rights required for any use of the Work.
103
+ 4. Affirmer understands and acknowledges that Creative Commons is not a
104
+ party to this document and has no duty or obligation with respect to this
105
+ CC0 or use of the Work.
106
+
107
+ For more information, please see
108
+ http://creativecommons.org/publicdomain/zero/1.0/.
package/README.md ADDED
@@ -0,0 +1,164 @@
1
+ # PostCSS Gradients Interpolation Method [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]
2
+
3
+ [<img alt="npm version" src="https://img.shields.io/npm/v/@csstools/postcss-gradients-interpolation-method.svg" height="20">][npm-url]
4
+ [<img alt="CSS Standard Status" src="https://cssdb.org/images/badges/TODO.svg" height="20">][css-url]
5
+ [<img alt="Build Status" src="https://github.com/csstools/postcss-plugins/workflows/test/badge.svg" height="20">][cli-url]
6
+ [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
7
+
8
+ [PostCSS Gradients Interpolation Method] lets you use different interpolation methods in CSS gradient functions following [CSS Specification].
9
+
10
+ ```pcss
11
+ .oklch {
12
+ background: linear-gradient(in oklch 90deg, black 25% , blue 75%);
13
+ }
14
+
15
+ :root {
16
+ --background: linear-gradient(in oklch 90deg, black 25%, blue 75%);
17
+ }
18
+
19
+ /* becomes */
20
+
21
+ .oklch {
22
+ background: linear-gradient(90deg ,black 25%,color-mix(in oklch, black 90%, blue 10%) calc(25% + ((75% - 25%) * 0.1)),color-mix(in oklch, black 80%, blue 20%) calc(25% + ((75% - 25%) * 0.2)),color-mix(in oklch, black 70%, blue 30%) calc(25% + ((75% - 25%) * 0.3)),color-mix(in oklch, black 60%, blue 40%) calc(25% + ((75% - 25%) * 0.4)),color-mix(in oklch, black 50%, blue 50%) calc(25% + ((75% - 25%) * 0.5)),color-mix(in oklch, black 40%, blue 60%) calc(25% + ((75% - 25%) * 0.6)),color-mix(in oklch, black 30%, blue 70%) calc(25% + ((75% - 25%) * 0.7)),color-mix(in oklch, black 20%, blue 80%) calc(25% + ((75% - 25%) * 0.8)),color-mix(in oklch, black 10%, blue 90%) calc(25% + ((75% - 25%) * 0.9)),blue 75%);
23
+ background: linear-gradient(in oklch 90deg, black 25% , blue 75%);
24
+ }
25
+
26
+ :root {
27
+ --background: linear-gradient(90deg ,black 25%,color-mix(in oklch, black 90%, blue 10%) calc(25% + ((75% - 25%) * 0.1)),color-mix(in oklch, black 80%, blue 20%) calc(25% + ((75% - 25%) * 0.2)),color-mix(in oklch, black 70%, blue 30%) calc(25% + ((75% - 25%) * 0.3)),color-mix(in oklch, black 60%, blue 40%) calc(25% + ((75% - 25%) * 0.4)),color-mix(in oklch, black 50%, blue 50%) calc(25% + ((75% - 25%) * 0.5)),color-mix(in oklch, black 40%, blue 60%) calc(25% + ((75% - 25%) * 0.6)),color-mix(in oklch, black 30%, blue 70%) calc(25% + ((75% - 25%) * 0.7)),color-mix(in oklch, black 20%, blue 80%) calc(25% + ((75% - 25%) * 0.8)),color-mix(in oklch, black 10%, blue 90%) calc(25% + ((75% - 25%) * 0.9)),blue 75%);
28
+ }
29
+
30
+ @supports (background: linear-gradient(in oklch, red 0%, red 0% 1%, red 2%)) {
31
+ :root {
32
+ --background: linear-gradient(in oklch 90deg, black 25%, blue 75%);
33
+ }
34
+ }
35
+ ```
36
+
37
+ ## Warnings
38
+
39
+ ⚠️ This plugin assumes you have a separate plugin to transform `color-mix()` to something older browsers can understand.
40
+
41
+ ⚠️ Color stops with only a color and Interpolation hints are not supported.
42
+ We can not statically check if a certain value is a single color or an interpolation hint.
43
+
44
+ These are equivalent in PostCSS :
45
+
46
+ ```pcss
47
+ --red: red;
48
+ /* Color stop variable */
49
+ background-image: linear-gradient(90deg, black, var(--red), blue);
50
+
51
+ --perc-10: 10%;
52
+ /* Interpolation hint */
53
+ background-image: linear-gradient(90deg, black, var(--perc-10), blue);
54
+ ```
55
+
56
+ ## Usage
57
+
58
+ Add [PostCSS Gradients Interpolation Method] to your project:
59
+
60
+ ```bash
61
+ npm install postcss @csstools/postcss-gradients-interpolation-method --save-dev
62
+ ```
63
+
64
+ Use it as a [PostCSS] plugin:
65
+
66
+ ```js
67
+ const postcss = require('postcss');
68
+ const postcssGradientsInterpolationMethod = require('@csstools/postcss-gradients-interpolation-method');
69
+
70
+ postcss([
71
+ postcssGradientsInterpolationMethod(/* pluginOptions */)
72
+ ]).process(YOUR_CSS /*, processOptions */);
73
+ ```
74
+
75
+ [PostCSS Gradients Interpolation Method] runs in all Node environments, with special
76
+ instructions for:
77
+
78
+ | [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
79
+ | --- | --- | --- | --- | --- | --- |
80
+
81
+ ## Options
82
+
83
+ ### preserve
84
+
85
+ The `preserve` option determines whether the original notation
86
+ is preserved. By default, it is not preserved.
87
+
88
+ ```js
89
+ postcssGradientsInterpolationMethod({ preserve: true })
90
+ ```
91
+
92
+ ```pcss
93
+ .oklch {
94
+ background: linear-gradient(in oklch 90deg, black 25% , blue 75%);
95
+ }
96
+
97
+ :root {
98
+ --background: linear-gradient(in oklch 90deg, black 25%, blue 75%);
99
+ }
100
+
101
+ /* becomes */
102
+
103
+ .oklch {
104
+ background: linear-gradient(90deg ,black 25%,color-mix(in oklch, black 90%, blue 10%) calc(25% + ((75% - 25%) * 0.1)),color-mix(in oklch, black 80%, blue 20%) calc(25% + ((75% - 25%) * 0.2)),color-mix(in oklch, black 70%, blue 30%) calc(25% + ((75% - 25%) * 0.3)),color-mix(in oklch, black 60%, blue 40%) calc(25% + ((75% - 25%) * 0.4)),color-mix(in oklch, black 50%, blue 50%) calc(25% + ((75% - 25%) * 0.5)),color-mix(in oklch, black 40%, blue 60%) calc(25% + ((75% - 25%) * 0.6)),color-mix(in oklch, black 30%, blue 70%) calc(25% + ((75% - 25%) * 0.7)),color-mix(in oklch, black 20%, blue 80%) calc(25% + ((75% - 25%) * 0.8)),color-mix(in oklch, black 10%, blue 90%) calc(25% + ((75% - 25%) * 0.9)),blue 75%);
105
+ background: linear-gradient(in oklch 90deg, black 25% , blue 75%);
106
+ }
107
+
108
+ :root {
109
+ --background: linear-gradient(90deg ,black 25%,color-mix(in oklch, black 90%, blue 10%) calc(25% + ((75% - 25%) * 0.1)),color-mix(in oklch, black 80%, blue 20%) calc(25% + ((75% - 25%) * 0.2)),color-mix(in oklch, black 70%, blue 30%) calc(25% + ((75% - 25%) * 0.3)),color-mix(in oklch, black 60%, blue 40%) calc(25% + ((75% - 25%) * 0.4)),color-mix(in oklch, black 50%, blue 50%) calc(25% + ((75% - 25%) * 0.5)),color-mix(in oklch, black 40%, blue 60%) calc(25% + ((75% - 25%) * 0.6)),color-mix(in oklch, black 30%, blue 70%) calc(25% + ((75% - 25%) * 0.7)),color-mix(in oklch, black 20%, blue 80%) calc(25% + ((75% - 25%) * 0.8)),color-mix(in oklch, black 10%, blue 90%) calc(25% + ((75% - 25%) * 0.9)),blue 75%);
110
+ }
111
+
112
+ @supports (background: linear-gradient(in oklch, red 0%, red 0% 1%, red 2%)) {
113
+ :root {
114
+ --background: linear-gradient(in oklch 90deg, black 25%, blue 75%);
115
+ }
116
+ }
117
+ ```
118
+
119
+ ### enableProgressiveCustomProperties
120
+
121
+ The `enableProgressiveCustomProperties` option determines whether the original notation
122
+ is wrapped with `@supports` when used in Custom Properties. By default, it is enabled.
123
+
124
+ ⚠️ We only recommend disabling this when you set `preserve` to `false` or if you bring your own fix for Custom Properties. See what the plugin does in its [README](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-progressive-custom-properties#readme).
125
+
126
+ ```js
127
+ postcssGradientsInterpolationMethod({ enableProgressiveCustomProperties: false })
128
+ ```
129
+
130
+ ```pcss
131
+ .oklch {
132
+ background: linear-gradient(in oklch 90deg, black 25% , blue 75%);
133
+ }
134
+
135
+ :root {
136
+ --background: linear-gradient(in oklch 90deg, black 25%, blue 75%);
137
+ }
138
+
139
+ /* becomes */
140
+
141
+ .oklch {
142
+ background: linear-gradient(90deg ,black 25%,color-mix(in oklch, black 90%, blue 10%) calc(25% + ((75% - 25%) * 0.1)),color-mix(in oklch, black 80%, blue 20%) calc(25% + ((75% - 25%) * 0.2)),color-mix(in oklch, black 70%, blue 30%) calc(25% + ((75% - 25%) * 0.3)),color-mix(in oklch, black 60%, blue 40%) calc(25% + ((75% - 25%) * 0.4)),color-mix(in oklch, black 50%, blue 50%) calc(25% + ((75% - 25%) * 0.5)),color-mix(in oklch, black 40%, blue 60%) calc(25% + ((75% - 25%) * 0.6)),color-mix(in oklch, black 30%, blue 70%) calc(25% + ((75% - 25%) * 0.7)),color-mix(in oklch, black 20%, blue 80%) calc(25% + ((75% - 25%) * 0.8)),color-mix(in oklch, black 10%, blue 90%) calc(25% + ((75% - 25%) * 0.9)),blue 75%);
143
+ background: linear-gradient(in oklch 90deg, black 25% , blue 75%);
144
+ }
145
+
146
+ :root {
147
+ --background: linear-gradient(90deg ,black 25%,color-mix(in oklch, black 90%, blue 10%) calc(25% + ((75% - 25%) * 0.1)),color-mix(in oklch, black 80%, blue 20%) calc(25% + ((75% - 25%) * 0.2)),color-mix(in oklch, black 70%, blue 30%) calc(25% + ((75% - 25%) * 0.3)),color-mix(in oklch, black 60%, blue 40%) calc(25% + ((75% - 25%) * 0.4)),color-mix(in oklch, black 50%, blue 50%) calc(25% + ((75% - 25%) * 0.5)),color-mix(in oklch, black 40%, blue 60%) calc(25% + ((75% - 25%) * 0.6)),color-mix(in oklch, black 30%, blue 70%) calc(25% + ((75% - 25%) * 0.7)),color-mix(in oklch, black 20%, blue 80%) calc(25% + ((75% - 25%) * 0.8)),color-mix(in oklch, black 10%, blue 90%) calc(25% + ((75% - 25%) * 0.9)),blue 75%);
148
+ --background: linear-gradient(in oklch 90deg, black 25%, blue 75%);
149
+ }
150
+ ```
151
+
152
+ _Custom properties do not fallback to the previous declaration_
153
+
154
+ [cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
155
+ [css-url]: https://cssdb.org/#TODO
156
+ [discord]: https://discord.gg/bUadyRwkJS
157
+ [npm-url]: https://www.npmjs.com/package/@csstools/postcss-gradients-interpolation-method
158
+
159
+ [Gulp PostCSS]: https://github.com/postcss/gulp-postcss
160
+ [Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
161
+ [PostCSS]: https://github.com/postcss/postcss
162
+ [PostCSS Loader]: https://github.com/postcss/postcss-loader
163
+ [PostCSS Gradients Interpolation Method]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-gradients-interpolation-method
164
+ [CSS Specification]: https://drafts.csswg.org/css-images-4/#linear-gradients
@@ -0,0 +1,8 @@
1
+ import type { Node } from 'postcss-value-parser';
2
+ export declare type ColorStop = {
3
+ color: string;
4
+ colorStopLength: string;
5
+ colorHintBetween: Array<ColorStop>;
6
+ nodes: Array<Node>;
7
+ };
8
+ export declare function colorStopList(nodes: Array<Node>, interpolationArguments: string): Array<ColorStop> | false;
@@ -0,0 +1,2 @@
1
+ import type { Node } from 'postcss';
2
+ export declare function hasSupportsAtRuleAncestor(node: Node): boolean;
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";var e=require("@csstools/postcss-progressive-custom-properties"),t=require("postcss-value-parser");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=o(e),r=o(t);function s(e,t){const o=[];let n={color:"",colorStopLength:"",colorHintBetween:[],nodes:[]};for(let t=0;t<e.length;t++){const r=e[t];"div"!==r.type||","!==r.value?n.nodes.push(r):(o.push(n),n={color:"",colorStopLength:"",colorHintBetween:[],nodes:[]})}o.push(n);const s=[];for(let e=0;e<o.length;e++){const t=o[e];switch(t.nodes.length){case 0:break;case 1:return!1;case 2:t.color=r.default.stringify(t.nodes[0]),t.colorStopLength=r.default.stringify(t.nodes[1]),s.push(t);break;case 3:s.push({color:r.default.stringify(t.nodes[0]),colorStopLength:r.default.stringify(t.nodes[1]),colorHintBetween:[],nodes:[t.nodes[0],t.nodes[1]]}),s.push({color:r.default.stringify(t.nodes[0]),colorStopLength:r.default.stringify(t.nodes[2]),colorHintBetween:[],nodes:[t.nodes[0],t.nodes[2]]})}}for(let e=0;e<s.length;e++){const o=s[e];o.color||(o.color=`color-mix(in ${t}, ${s[e-1].color} 50%, ${s[e+1].color} 50%)`,o.colorHintBetween=[s[e-1],s[e+1]])}return s}function i(e){return e.includes("in ")&&(e.includes("conic-gradient(")||e.includes("linear-gradient(")||e.includes("radial-gradient(")||e.includes("repeating-conic-gradient(")||e.includes("repeating-linear-gradient(")||e.includes("repeating-radial-gradient("))}const l=["shorter","longer","increasing","decreasing","specified"],a=e=>({postcssPlugin:"postcss-gradients-interpolation-method",Declaration(t,{result:o}){if(!i(t.value))return;if(function(e){let t=e.parent;for(;t;)if("atrule"===t.type){if("supports"===t.name&&i(t.params))return!0;t=t.parent}else t=t.parent;return!1}(t))return;let n;try{n=r.default(t.value)}catch(e){t.warn(o,`Failed to parse value '${t.value}' as a CSS gradient. Leaving the original value intact.`)}if(void 0===n)return;let a=!1;if(n.walk((e=>{if("function"!==e.type||"conic-gradient"!==(t=e.value)&&"linear-gradient"!==t&&"radial-gradient"!==t&&"repeating-conic-gradient"!==t&&"repeating-linear-gradient"!==t&&"repeating-radial-gradient"!==t)return;var t;const o=e.nodes.filter((e=>"comment"!==e.type&&"space"!==e.type)),n={interpolationArguments:[],argumentsRemainder:[],colorStops:[]};for(let e=0;e<o.length;e++){const t=o[e];if("div"===t.type&&","===t.value){const t=n.interpolationArguments.map((e=>r.default.stringify(e))).join(" "),i=s(o.slice(e),t);if(i){n.colorStops=i;break}t&&(a=!0);break}if("word"===t.type&&"in"===t.value&&o[e+1]){if(n.interpolationArguments.push(o[e+1]),e++,o[e+1]&&"word"===o[e+1].type&&l.includes(o[e+1].value)){n.interpolationArguments.push(o[e+1]),e++;continue}}else n.argumentsRemainder.push(t)}if(!n.interpolationArguments.length||!n.colorStops.length)return;e.nodes=[],n.argumentsRemainder.length&&(e.nodes.push(...n.argumentsRemainder.flatMap((e=>[e,{type:"space",value:" "}]))),e.nodes.push({type:"div",value:","}));const i=n.interpolationArguments.map((e=>r.default.stringify(e))).join(" ");for(let t=0;t<n.colorStops.length;t++){const o=n.colorStops[t],s=n.colorStops[t+1];if(s)if(o.color!==s.color)for(let t=0;t<10;t++){if(0===t){e.nodes.push(r.default(`${o.color} ${o.colorStopLength}`),{type:"div",value:","});continue}const n=`color-mix(in ${i}, ${o.color} ${100-10*t}%, ${s.color} ${10*t}%)`,l=`calc(${o.colorStopLength} + ((${s.colorStopLength} - ${o.colorStopLength}) * ${t/10}))`;e.nodes.push(r.default(`${n} ${l}`),{type:"div",value:","})}else e.nodes.push(r.default(`${o.color} ${o.colorStopLength}`),{type:"div",value:","});else e.nodes.push(r.default(`${o.color} ${o.colorStopLength}`))}})),a)return void t.warn(o,`Failed to parse value '${t.value}' as a CSS gradient with interpolation. Leaving the original value intact.`);const c=r.default.stringify(n);c!==t.value&&(e.preserve?t.cloneBefore({prop:t.prop,value:c}):t.value=c)}});a.postcss=!0;const c=e=>{const t=Object.assign({enableProgressiveCustomProperties:!0,preserve:!0},e);return t.enableProgressiveCustomProperties&&t.preserve?{postcssPlugin:"postcss-gradients-interpolation-method",plugins:[n.default(),a(t)]}:a(t)};c.postcss=!0,module.exports=c;
@@ -0,0 +1,7 @@
1
+ import type { PluginCreator } from 'postcss';
2
+ declare type pluginOptions = {
3
+ preserve?: boolean;
4
+ enableProgressiveCustomProperties?: boolean;
5
+ };
6
+ declare const postcssPlugin: PluginCreator<pluginOptions>;
7
+ export default postcssPlugin;
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import e from"@csstools/postcss-progressive-custom-properties";import o from"postcss-value-parser";function t(e,t){const n=[];let r={color:"",colorStopLength:"",colorHintBetween:[],nodes:[]};for(let o=0;o<e.length;o++){const t=e[o];"div"!==t.type||","!==t.value?r.nodes.push(t):(n.push(r),r={color:"",colorStopLength:"",colorHintBetween:[],nodes:[]})}n.push(r);const s=[];for(let e=0;e<n.length;e++){const t=n[e];switch(t.nodes.length){case 0:break;case 1:return!1;case 2:t.color=o.stringify(t.nodes[0]),t.colorStopLength=o.stringify(t.nodes[1]),s.push(t);break;case 3:s.push({color:o.stringify(t.nodes[0]),colorStopLength:o.stringify(t.nodes[1]),colorHintBetween:[],nodes:[t.nodes[0],t.nodes[1]]}),s.push({color:o.stringify(t.nodes[0]),colorStopLength:o.stringify(t.nodes[2]),colorHintBetween:[],nodes:[t.nodes[0],t.nodes[2]]})}}for(let e=0;e<s.length;e++){const o=s[e];o.color||(o.color=`color-mix(in ${t}, ${s[e-1].color} 50%, ${s[e+1].color} 50%)`,o.colorHintBetween=[s[e-1],s[e+1]])}return s}function n(e){return e.includes("in ")&&(e.includes("conic-gradient(")||e.includes("linear-gradient(")||e.includes("radial-gradient(")||e.includes("repeating-conic-gradient(")||e.includes("repeating-linear-gradient(")||e.includes("repeating-radial-gradient("))}const r=["shorter","longer","increasing","decreasing","specified"],s=e=>({postcssPlugin:"postcss-gradients-interpolation-method",Declaration(s,{result:i}){if(!n(s.value))return;if(function(e){let o=e.parent;for(;o;)if("atrule"===o.type){if("supports"===o.name&&n(o.params))return!0;o=o.parent}else o=o.parent;return!1}(s))return;let l;try{l=o(s.value)}catch(e){s.warn(i,`Failed to parse value '${s.value}' as a CSS gradient. Leaving the original value intact.`)}if(void 0===l)return;let a=!1;if(l.walk((e=>{if("function"!==e.type||"conic-gradient"!==(n=e.value)&&"linear-gradient"!==n&&"radial-gradient"!==n&&"repeating-conic-gradient"!==n&&"repeating-linear-gradient"!==n&&"repeating-radial-gradient"!==n)return;var n;const s=e.nodes.filter((e=>"comment"!==e.type&&"space"!==e.type)),i={interpolationArguments:[],argumentsRemainder:[],colorStops:[]};for(let e=0;e<s.length;e++){const n=s[e];if("div"===n.type&&","===n.value){const n=i.interpolationArguments.map((e=>o.stringify(e))).join(" "),r=t(s.slice(e),n);if(r){i.colorStops=r;break}n&&(a=!0);break}if("word"===n.type&&"in"===n.value&&s[e+1]){if(i.interpolationArguments.push(s[e+1]),e++,s[e+1]&&"word"===s[e+1].type&&r.includes(s[e+1].value)){i.interpolationArguments.push(s[e+1]),e++;continue}}else i.argumentsRemainder.push(n)}if(!i.interpolationArguments.length||!i.colorStops.length)return;e.nodes=[],i.argumentsRemainder.length&&(e.nodes.push(...i.argumentsRemainder.flatMap((e=>[e,{type:"space",value:" "}]))),e.nodes.push({type:"div",value:","}));const l=i.interpolationArguments.map((e=>o.stringify(e))).join(" ");for(let t=0;t<i.colorStops.length;t++){const n=i.colorStops[t],r=i.colorStops[t+1];if(r)if(n.color!==r.color)for(let t=0;t<10;t++){if(0===t){e.nodes.push(o(`${n.color} ${n.colorStopLength}`),{type:"div",value:","});continue}const s=`color-mix(in ${l}, ${n.color} ${100-10*t}%, ${r.color} ${10*t}%)`,i=`calc(${n.colorStopLength} + ((${r.colorStopLength} - ${n.colorStopLength}) * ${t/10}))`;e.nodes.push(o(`${s} ${i}`),{type:"div",value:","})}else e.nodes.push(o(`${n.color} ${n.colorStopLength}`),{type:"div",value:","});else e.nodes.push(o(`${n.color} ${n.colorStopLength}`))}})),a)return void s.warn(i,`Failed to parse value '${s.value}' as a CSS gradient with interpolation. Leaving the original value intact.`);const c=o.stringify(l);c!==s.value&&(e.preserve?s.cloneBefore({prop:s.prop,value:c}):s.value=c)}});s.postcss=!0;const i=o=>{const t=Object.assign({enableProgressiveCustomProperties:!0,preserve:!0},o);return t.enableProgressiveCustomProperties&&t.preserve?{postcssPlugin:"postcss-gradients-interpolation-method",plugins:[e(),s(t)]}:s(t)};i.postcss=!0;export{i as default};
@@ -0,0 +1,2 @@
1
+ export declare function includesGradientsFunction(str: string): boolean;
2
+ export declare function isGradientsFunctions(str: string): boolean;
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@csstools/postcss-gradients-interpolation-method",
3
+ "description": "Use interpolation methods in CSS gradient functions",
4
+ "version": "1.0.0",
5
+ "author": "Jonathan Neal <jonathantneal@hotmail.com>",
6
+ "license": "CC0-1.0",
7
+ "engines": {
8
+ "node": "^12 || ^14 || >=16"
9
+ },
10
+ "main": "dist/index.cjs",
11
+ "module": "dist/index.mjs",
12
+ "types": "dist/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "import": "./dist/index.mjs",
16
+ "require": "./dist/index.cjs",
17
+ "default": "./dist/index.mjs"
18
+ }
19
+ },
20
+ "files": [
21
+ "CHANGELOG.md",
22
+ "LICENSE.md",
23
+ "README.md",
24
+ "dist"
25
+ ],
26
+ "peerDependencies": {
27
+ "postcss": "^8.3"
28
+ },
29
+ "devDependencies": {
30
+ "@csstools/postcss-progressive-custom-properties": "^1.1.0",
31
+ "postcss-value-parser": "^4.2.0"
32
+ },
33
+ "scripts": {
34
+ "build": "rollup -c ../../rollup/default.js",
35
+ "clean": "node -e \"fs.rmSync('./dist', { recursive: true, force: true });\"",
36
+ "docs": "node ../../.github/bin/generate-docs/install.mjs && node ../../.github/bin/generate-docs/readme.mjs",
37
+ "lint": "npm run lint:eslint && npm run lint:package-json",
38
+ "lint:eslint": "eslint ./src --ext .js --ext .ts --ext .mjs --no-error-on-unmatched-pattern",
39
+ "lint:package-json": "node ../../.github/bin/format-package-json.mjs",
40
+ "prepublishOnly": "npm run clean && npm run build && npm run test",
41
+ "test": "node .tape.mjs && npm run test:exports",
42
+ "test:exports": "node ./test/_import.mjs && node ./test/_require.cjs",
43
+ "test:rewrite-expects": "REWRITE_EXPECTS=true node .tape.mjs"
44
+ },
45
+ "homepage": "https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-gradients-interpolation-method#readme",
46
+ "repository": {
47
+ "type": "git",
48
+ "url": "https://github.com/csstools/postcss-plugins.git",
49
+ "directory": "plugins/postcss-gradients-interpolation-method"
50
+ },
51
+ "bugs": "https://github.com/csstools/postcss-plugins/issues",
52
+ "keywords": [
53
+ "color",
54
+ "conic",
55
+ "css",
56
+ "gradients",
57
+ "interpolation",
58
+ "linear",
59
+ "postcss",
60
+ "postcss-plugin",
61
+ "radial",
62
+ "repeating",
63
+ "syntax"
64
+ ],
65
+ "csstools": {
66
+ "cssdbId": "TODO",
67
+ "exportName": "postcssGradientsInterpolationMethod",
68
+ "humanReadableName": "PostCSS Gradients Interpolation Method",
69
+ "specUrl": "https://drafts.csswg.org/css-images-4/#linear-gradients"
70
+ },
71
+ "volta": {
72
+ "extends": "../../package.json"
73
+ }
74
+ }