Skip to content

Commit

Permalink
feat: options.utilityClasses.[].property supports array of values
Browse files Browse the repository at this point in the history
  • Loading branch information
saneef committed Apr 22, 2024
1 parent 97ed343 commit b6ec5d7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/design-token-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,21 @@ module.exports = (
* @param {Object} obj
* @return {Rule}
*/
function generateUtilityClassRule(
function generateUtilityClassRules(
classObject,
{ Rule, Declaration, selector = ({ selectorBase }) => `.${selectorBase}` },
) {
const { selectorBase, prop, value } = classObject;
const rule = new Rule({ selector: selector(classObject) });
rule.append(new Declaration({ prop, value: /** @type {string} */ (value) }));
return rule;

const properties = Array.isArray(prop) ? prop : [prop];

return properties.map((prop) => {
const rule = new Rule({ selector: selector(classObject) });
rule.append(
new Declaration({ prop, value: /** @type {string} */ (value) }),
);
return rule;
});
}

function insertUtilityClasses(
Expand All @@ -84,8 +91,8 @@ function insertUtilityClasses(
return;
}

const rules = classObjects.map((c) =>
generateUtilityClassRule(c, {
const rules = classObjects.flatMap((c) =>
generateUtilityClassRules(c, {
Rule,
Declaration,
selector: ({ selectorBase }) => `.${selectorBase}`,
Expand All @@ -98,12 +105,12 @@ function insertUtilityClasses(
params: `(min-width: ${mqParams})`,
});
const rules = classObjects
.map((c) => {
.flatMap((c) => {
if (c.skipViewportVariant) {
return;
}

const rule = generateUtilityClassRule(c, {
const rule = generateUtilityClassRules(c, {
Rule,
Declaration,
selector: ({ selectorBase }) =>
Expand Down
19 changes: 19 additions & 0 deletions test/utility-classes.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ test("Generate with prefix", async (t) => {
);
});

test("Generate with multiple properties", async (t) => {
const tokens = { space: { m: "1rem", l: "2rem" } };
const input = `@design-token-utils (utility-classes);`;
const options = {
utilityClasses: [
{
id: "space",
prefix: "margin-y",
property: ["margin-top", "margin-bottom"],
},
],
};
const res = await run(tokens, input, options);
t.is(
res.css,
`.margin-y-m{margin-bottom:var(--space-m);margin-top:var(--space-m)}.margin-y-l{margin-bottom:var(--space-l);margin-top:var(--space-l)}`,
);
});

test("Generate with responsive variants", async (t) => {
const tokens = { color: { accent: "#ff0" } };
const input = `@design-token-utils (utility-classes);`;
Expand Down

0 comments on commit b6ec5d7

Please sign in to comment.