Skip to content

Commit aeacdca

Browse files
author
Sana Javed
authored
Merge pull request #3 from oddbird/data-model
First walkthrough, initializing data model
2 parents 194881f + 1dddaa5 commit aeacdca

File tree

6 files changed

+82
-28
lines changed

6 files changed

+82
-28
lines changed

package-lock.json

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
node_modules
2+
dist
23
package-lock.json
34
yarn.lock
5+
*.log*
46
*.result.css
57
*.result.css.map
68
dist/*

plugins/postcss-cascade-layers/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,8 @@
4848
},
4949
"volta": {
5050
"extends": "../../package.json"
51+
},
52+
"devDependencies": {
53+
"postcss-tape": "^6.0.1"
5154
}
5255
}

plugins/postcss-cascade-layers/src/index.ts

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
1-
import type { Container } from 'postcss';
1+
import { Container } from "postcss";
22

33
function postcssCascadeLayers(opts) {
44
return {
5-
postcssPlugin: 'postcss-cascade-layers',
5+
postcssPlugin: "postcss-cascade-layers",
66
Once(root: Container) {
7-
root.walkAtRules((atRule) => {
8-
if (atRule.name !== 'layer') {
9-
return;
10-
}
7+
let layerCount = 0;
8+
let layerOrder = {};
119

12-
// if layer anon, name
13-
if(atRule.params === '') {
14-
atRule.params = ` anon${(Math.random()).toString()}`;
10+
// 1st walkthrough to rename anon layers and store state (no modification of layer styles)
11+
root.walkAtRules("layer", (atRule) => {
12+
// give anonymous layers a name
13+
if (!atRule.params) {
14+
atRule.params = `anon${layerCount}`;
1515
}
1616

17-
if (atRule.nodes && atRule.nodes.length) {
18-
const atRuleClone = atRule.clone();
19-
atRuleClone.nodes.forEach((node) => {
20-
const modifiedSelectors = node.selectors.map((selector) => {
21-
return `${selector}:not(<N times #foo>)`;
22-
});
23-
atRule.parent.insertBefore(atRule, node.clone({ selectors: modifiedSelectors }));
24-
});
25-
26-
console.log('nodes', atRule.nodes);
27-
} else {
28-
console.log(atRule.name, atRule.params);
29-
// parse .params as list of layer names
30-
/*
31-
pattern:
32-
@layer foo foo.baz;
33-
*/
34-
}
17+
layerCount += 1;
18+
layerOrder[atRule.params] = layerCount;
3519
});
20+
21+
// 2nd walkthrough to transform unlayered styles - need highest specificity (layerCount + 1)
22+
// root.walkRules((rule) => {
23+
// console.log(rule, "second walkthrough");
24+
// });
25+
26+
// 3rd walkthrough to transform layered styles:
27+
// - move out styles from atRule, insert before: https://postcss.org/api/#container-insertbefore
28+
// - delete empty atRule
29+
// - give selectors the specifity they need based on layerPriority state
30+
// root.walkAtRules((atRule) => {
31+
// console.log(atRule, "third walkthrough");
32+
// });
3633
},
3734
};
3835
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@layer A {
2+
target {
3+
color: red;
4+
}
5+
}
6+
7+
@layer B {
8+
target {
9+
color: orange;
10+
}
11+
}
12+
13+
@layer C {
14+
target {
15+
color: yellow;
16+
}
17+
}
18+
19+
@layer {
20+
target {
21+
color: green;
22+
}
23+
}
24+
25+
target {
26+
color: purple;
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
target:not(#something #something #something #something #something) {
2+
color: purple;
3+
}
4+
5+
target:not(#something #something #something #something) {
6+
color: green;
7+
}
8+
9+
target:not(#something #something #something) {
10+
color: yellow;
11+
}
12+
13+
target:not(#something #something) {
14+
color: orange;
15+
}
16+
17+
target:not(#something #something #something) {
18+
color: red;
19+
}

0 commit comments

Comments
 (0)