Skip to content

Commit

Permalink
Merge pull request #36 from utilitycss/feat/nested-rules
Browse files Browse the repository at this point in the history
Feat/nested rules
  • Loading branch information
axyz authored May 29, 2020
2 parents bde2a5c + 2593021 commit 2fdcb6c
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 18 deletions.
18 changes: 18 additions & 0 deletions example/example.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,29 @@ const modules = [
isResponsive: true
}),
colors({
nestedRules: {
".dark {}": {
whitelist: ["c"],
isResponsive: true,
colorValues: {
black: "#ffffff",
white: "#1a1a1a"
}
},
"@media foo": {
whitelist: ["c"],
colorValues: {
black: "#ffffff",
white: "#1a1a1a"
}
}
},
whitelist: ["c", "bgc", "bdc"],
pseudoClasses: { c: [":hover"], bgc: [":hover", ":active"] },
colorValues: {
orange: "#ff6900",
black: "#1a1a1a",
white: "#ffffff",
grey: "#777777",
grey2: "#a5a5a5",
grey3: "#dddddd",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@utilitycss/utility",
"version": "0.5.5",
"version": "0.6.0",
"description": "Generator for Utility CSS frameworks",
"author": "Andrea Moretti (@axyz) <[email protected]>",
"repository": "utilitycss/utility",
Expand Down
14 changes: 8 additions & 6 deletions src/helpers/build-sections.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ module.exports = function({ modules }) {
modules[module].forEach(rule => {
// console.log(JSON.stringify(rule));
const { nodes, selector, media } = rule;
rules.push({
selector,
nodes,
media,
class: selectorReplace(selector)
});
if (/^\.[A-Za-z0-9\-\:\_]+$/.test(selector)) {
rules.push({
selector,
nodes,
media,
class: selectorReplace(selector)
});
}
});
const sectionHtml = sectionTemplate({
module: moduleName,
Expand Down
64 changes: 56 additions & 8 deletions src/util/applyRules.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
const postcss = require("postcss");
const defineClass = require("./defineClass");
const defineSeries = require("./defineSeries");
const responsive = require("./responsive");
const template = require("./template");

module.exports = ({ config, globalConfig, defaultNames, getRules, meta }) => {
const applyRules = ({
config,
globalConfig,
defaultNames,
getRules,
meta,
classTemplate = "{}"
}) => {
const {
names = {},
whitelist = [],
blacklist = [],
isResponsive = false,
responsiveWhiteList = [],
responsiveBlackList = [],
pseudoClasses = {}
} =
config || {};
pseudoClasses = {},
nestedRules
} = config || {};

const {
breakPoints = {},
breakPointSeparator = "",
pseudoClassesSeparator = "",
seriesSeparator = ""
} =
globalConfig || {};
} = globalConfig || {};

const customNames = Object.assign({}, defaultNames, names);
const customRules = getRules(customNames, config || {});
Expand All @@ -45,7 +53,8 @@ module.exports = ({ config, globalConfig, defaultNames, getRules, meta }) => {
seriesSeparator,
pseudoClasses: modifiers,
pseudoClassesSeparator,
meta: { ...meta, id: curr }
meta: { ...meta, id: curr },
classTemplate
})
);
} else if (typeof value === "string" || typeof value === "number") {
Expand All @@ -57,7 +66,10 @@ module.exports = ({ config, globalConfig, defaultNames, getRules, meta }) => {

return prev.concat(
defineClass(
`${name}${separator}${pseudoClass}${pseudo}`,
template(
classTemplate,
`${name}${separator}${pseudoClass}${pseudo}`
),
{ [`${key}`]: value },
{ ...meta, id: curr }
)
Expand Down Expand Up @@ -88,5 +100,41 @@ module.exports = ({ config, globalConfig, defaultNames, getRules, meta }) => {
);
}

if (nestedRules) {
Object.keys(nestedRules).forEach(nestedRule => {
const isAtRule = nestedRule[0] === "@";

if (isAtRule) {
const name = nestedRule.split(" ")[0].slice(1);
const nestedRuleNode = postcss.atRule({
name: name,
params: nestedRule.split(`${name} `)[1],
nodes: applyRules({
config: nestedRules[nestedRule],
globalConfig,
defaultNames,
getRules,
meta
})
});

result = result.concat(nestedRuleNode);
} else {
result = result.concat(
applyRules({
config: nestedRules[nestedRule],
globalConfig,
defaultNames,
getRules,
meta,
classTemplate: nestedRule
})
);
}
});
}

return result;
};

module.exports = applyRules;
2 changes: 1 addition & 1 deletion src/util/defineClass.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const postcss = require("postcss");

const escapeClassName = className =>
className.replace(/([^A-Za-z0-9\-\:\_])/g, "\\$1"); //eslint-disable-line
className.replace(/([^A-Za-z0-9\-\:\_\ \.])/g, "\\$1"); //eslint-disable-line

module.exports = (name, props, meta = {}) => {
const decls = Object.keys(props).map(prop =>
Expand Down
9 changes: 7 additions & 2 deletions src/util/defineSeries.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const defineClass = require("./defineClass");
const template = require("./template");

module.exports = (name, prop, values, options = {}) => {
const {
seriesSeparator = "",
pseudoClassesSeparator = "",
pseudoClasses = [],
meta = {}
meta = {},
classTemplate = "{}"
} = options;

return [""].concat(pseudoClasses).reduce((prev, pseudo) => {
Expand All @@ -14,7 +16,10 @@ module.exports = (name, prop, values, options = {}) => {
const pseudoClass = pseudo.replace(/:/g, "");
const nodes = Object.keys(values).map(value =>
defineClass(
`${name}${separator}${value}${pseudoSeparator}${pseudoClass}${pseudo}`,
template(
classTemplate,
`${name}${separator}${value}${pseudoSeparator}${pseudoClass}${pseudo}`
),
{
[`${prop}`]: values[value]
},
Expand Down
5 changes: 5 additions & 0 deletions src/util/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* template function to generate class selectors.
* the placeholder "{}" will get substituted with str
*/
module.exports = (t, str) => t.replace(/{}/g, `.${str}`).replace(".", "");

0 comments on commit 2fdcb6c

Please sign in to comment.