postcss-ruler 1.4.0 → 2.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.
Files changed (3) hide show
  1. package/README.md +59 -0
  2. package/index.js +30 -0
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -33,6 +33,16 @@ module.exports = {
33
33
  minWidth: 320, // Default minimum viewport width
34
34
  maxWidth: 1760, // Default maximum viewport width
35
35
  generateAllCrossPairs: false,
36
+ // Optional: Pre-define scales for cross-file usage (Astro, Vite, etc.)
37
+ scales: {
38
+ space: {
39
+ pairs: {
40
+ xs: [8, 16],
41
+ sm: [16, 24],
42
+ md: [24, 32],
43
+ },
44
+ },
45
+ },
36
46
  },
37
47
  },
38
48
  };
@@ -332,6 +342,55 @@ You can override the global setting per utility by explicitly setting `lowSpecif
332
342
  | `maxWidth` | number | `1760` | Default maximum viewport width in pixels |
333
343
  | `generateAllCrossPairs` | boolean | `false` | Generate cross-combinations in scale mode |
334
344
  | `lowSpecificity` | boolean | `false` | Wrap utility selectors in `:where()` to lower specificity to 0 |
345
+ | `scales` | object | `{}` | Pre-defined scales for cross-file usage (see below) |
346
+
347
+ ### Pre-defined Scales (for Astro, Vite, etc.)
348
+
349
+ When using bundlers like Astro or Vite that process CSS files in unpredictable order, you may encounter issues where `@ruler utility()` in one file can't reference a scale defined with `@ruler scale()` in another file.
350
+
351
+ To solve this, define your scales in the plugin config:
352
+
353
+ ```javascript
354
+ // postcss.config.js
355
+ module.exports = {
356
+ plugins: {
357
+ "postcss-ruler": {
358
+ scales: {
359
+ space: {
360
+ pairs: {
361
+ xs: [8, 16],
362
+ sm: [16, 24],
363
+ md: [24, 32],
364
+ lg: [32, 48],
365
+ },
366
+ },
367
+ size: {
368
+ minWidth: 400,
369
+ maxWidth: 1200,
370
+ generateAllCrossPairs: true,
371
+ pairs: {
372
+ sm: [100, 200],
373
+ md: [200, 400],
374
+ },
375
+ },
376
+ },
377
+ },
378
+ },
379
+ };
380
+ ```
381
+
382
+ Now you can use `@ruler utility()` in any file without needing `@ruler scale()` first:
383
+
384
+ ```css
385
+ /* Component.astro or any CSS file */
386
+ @ruler utility({
387
+ selector: '.gap',
388
+ property: 'gap',
389
+ scale: 'space'
390
+ });
391
+ ```
392
+
393
+ **Note:** If you use `@ruler scale()` with the same prefix as a config-defined scale, the inline scale will override the config scale for that file.
335
394
 
336
395
  ### At-Rule Options
337
396
 
package/index.js CHANGED
@@ -9,6 +9,7 @@ module.exports = (opts) => {
9
9
  maxWidth: 1760,
10
10
  generateAllCrossPairs: false,
11
11
  lowSpecificity: false,
12
+ scales: {},
12
13
  };
13
14
  const config = Object.assign(DEFAULTS, opts);
14
15
 
@@ -112,6 +113,35 @@ module.exports = (opts) => {
112
113
  return clampScales;
113
114
  };
114
115
 
116
+ /**
117
+ * Pre-processes scales defined in plugin config
118
+ * @param {Object} configScales - Scales object from plugin options
119
+ */
120
+ const initializeConfigScales = (configScales) => {
121
+ Object.entries(configScales).forEach(([prefix, scaleConfig]) => {
122
+ const scaleOpts = {
123
+ minWidth: scaleConfig.minWidth || config.minWidth,
124
+ maxWidth: scaleConfig.maxWidth || config.maxWidth,
125
+ generateAllCrossPairs:
126
+ scaleConfig.generateAllCrossPairs ?? config.generateAllCrossPairs,
127
+ };
128
+
129
+ const clampPairs = Object.entries(scaleConfig.pairs).map(
130
+ ([name, values]) => ({ name, values }),
131
+ );
132
+
133
+ scales[prefix] = generateClamps({
134
+ pairs: clampPairs,
135
+ ...scaleOpts,
136
+ });
137
+ });
138
+ };
139
+
140
+ // Initialize scales from config (if any)
141
+ if (Object.keys(config.scales).length > 0) {
142
+ initializeConfigScales(config.scales);
143
+ }
144
+
115
145
  /**
116
146
  * Parses parameters from @fluid at-rule
117
147
  * @param {Array} params - Parsed parameter nodes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postcss-ruler",
3
- "version": "1.4.0",
3
+ "version": "2.0.0",
4
4
  "description": "PostCSS plugin to generate fluid scales and values.",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "eslintConfig": {
39
39
  "parserOptions": {
40
- "ecmaVersion": 2018
40
+ "ecmaVersion": 2020
41
41
  },
42
42
  "env": {
43
43
  "node": true,