postcss-ruler 1.2.0 → 1.3.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/index.js +36 -5
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -201,6 +201,7 @@ module.exports = (opts) => {
|
|
|
201
201
|
property: null,
|
|
202
202
|
scale: null,
|
|
203
203
|
generateAllCrossPairs: null,
|
|
204
|
+
attribute: null,
|
|
204
205
|
};
|
|
205
206
|
|
|
206
207
|
for (let i = 0; i < params.length; i++) {
|
|
@@ -213,6 +214,7 @@ module.exports = (opts) => {
|
|
|
213
214
|
switch (key) {
|
|
214
215
|
case "selector":
|
|
215
216
|
case "scale":
|
|
217
|
+
case "attribute":
|
|
216
218
|
utilityParams[key] = value.replace(/['"]/g, "");
|
|
217
219
|
i++;
|
|
218
220
|
break;
|
|
@@ -307,10 +309,24 @@ module.exports = (opts) => {
|
|
|
307
309
|
|
|
308
310
|
const utilityParams = parseUtilityParams(params);
|
|
309
311
|
|
|
312
|
+
// Validate attribute-specific constraints first
|
|
313
|
+
if (utilityParams.attribute !== null) {
|
|
314
|
+
if (utilityParams.attribute === "") {
|
|
315
|
+
throw new Error(
|
|
316
|
+
'[postcss-ruler] @ruler utility() attribute parameter cannot be empty',
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
if (!/^[a-zA-Z0-9_-]+$/.test(utilityParams.attribute)) {
|
|
320
|
+
throw new Error(
|
|
321
|
+
'[postcss-ruler] @ruler utility() attribute parameter must contain only letters, numbers, hyphens, and underscores',
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
310
326
|
// Validate required parameters
|
|
311
|
-
if (!utilityParams.selector) {
|
|
327
|
+
if (!utilityParams.selector && !utilityParams.attribute) {
|
|
312
328
|
throw new Error(
|
|
313
|
-
'[postcss-ruler] @ruler utility() requires
|
|
329
|
+
'[postcss-ruler] @ruler utility() requires either "selector" or "attribute" parameter',
|
|
314
330
|
);
|
|
315
331
|
}
|
|
316
332
|
if (!utilityParams.property) {
|
|
@@ -346,11 +362,26 @@ module.exports = (opts) => {
|
|
|
346
362
|
|
|
347
363
|
// Generate utility classes as PostCSS nodes
|
|
348
364
|
const rules = scaleItems.map((item) => {
|
|
349
|
-
|
|
350
|
-
|
|
365
|
+
let ruleSelector;
|
|
366
|
+
let ruleValue;
|
|
367
|
+
|
|
368
|
+
if (utilityParams.attribute) {
|
|
369
|
+
// Attribute mode: [data-attr="value"] or .class[data-attr="value"]
|
|
370
|
+
const attrSelector = `[${utilityParams.attribute}="${item.label}"]`;
|
|
371
|
+
ruleSelector = utilityParams.selector
|
|
372
|
+
? `${utilityParams.selector}${attrSelector}`
|
|
373
|
+
: attrSelector;
|
|
374
|
+
ruleValue = `var(--${utilityParams.scale}-${item.label})`;
|
|
375
|
+
} else {
|
|
376
|
+
// Class mode (existing behavior)
|
|
377
|
+
ruleSelector = `${utilityParams.selector}-${item.label}`;
|
|
378
|
+
ruleValue = item.clamp;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const rule = postcss.rule({ selector: ruleSelector });
|
|
351
382
|
|
|
352
383
|
properties.forEach((prop) => {
|
|
353
|
-
rule.append(postcss.decl({ prop, value:
|
|
384
|
+
rule.append(postcss.decl({ prop, value: ruleValue }));
|
|
354
385
|
});
|
|
355
386
|
|
|
356
387
|
return rule;
|