Skip to content

Commit 6f1ead5

Browse files
committed
feat: 🎸 improve cssom addon
BREAKING CHANGE: cssom addon API changed, pipe addon as a consequence now behaves differently, too
1 parent 6976707 commit 6f1ead5

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

‎.storybook/CSSOM.stories.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ addonCSSOM(nano);
88

99
const rule = nano.createRule('.test_cssom_rule');
1010
rule.style.color = 'red';
11+
rule.style.borderColor = 'red';
12+
rule.selectorText = '.test_cssom_rule2';
1113
console.log('rule', rule);
14+
for (let i = 0; i < rule.style.length; i++) {
15+
console.log(rule.style[i]);
16+
}
1217

1318
const atrule = nano.createRule('.test_cssom_atrule', '@media only screen and (max-width: 600px)');
1419
atrule.style.color = 'blue';
1520
console.log('atrule', atrule);
1621

1722
storiesOf('Addons/CSSOM', module)
1823
.add('rule', () =>
19-
h('div', {className: 'test_cssom_rule'}, 'Hello world')
24+
h('div', {className: 'test_cssom_rule2'}, 'Hello world')
2025
)
2126
.add('with @media at rule', () =>
22-
h('div', {className: 'test_cssom_rule test_cssom_atrule'}, 'Hello world')
27+
h('div', {className: 'test_cssom_atrule'}, 'Hello world')
2328
)

‎addon/cssom.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ exports.addon = function (renderer) {
88
// CSSOM support only browser environment.
99
if (!renderer.client) return;
1010

11+
// Style sheet for media queries.
12+
document.head.appendChild(renderer.msh = document.createElement('style'));
13+
1114
renderer.createRule = function (selector, prelude) {
1215
var rawCss = selector + '{}';
1316
if (prelude) rawCss = prelude + '{' + rawCss + '}';
14-
var sheet = renderer.sh.sheet;
17+
var sheet = prelude ? renderer.msh.sheet : renderer.sh.sheet;
1518
var index = sheet.insertRule(rawCss, sheet.cssRules.length);
1619
var rule = (sheet.cssRules || sheet.rules)[index];
1720

@@ -23,7 +26,9 @@ exports.addon = function (renderer) {
2326
// If rule has media query (it has prelude), move style (CSSStyleDeclaration)
2427
// object to the "top" to normalize it with a rule without the media
2528
// query, so that both rules have `.style` property available.
26-
rule.style = (rule.cssRules || rule.rules)[0].style;
29+
var selectorRule = (rule.cssRules || rule.rules)[0];
30+
rule.style = selectorRule.style;
31+
rule.styleMap = selectorRule.styleMap;
2732
}
2833

2934
return rule;

‎addon/pipe.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ exports.addon = function (renderer) {
4848
},
4949
remove: function () {
5050
for (var selectorTemplate in rules)
51-
rules[selectorTemplate].remove();
51+
renderer.sh.deleteRule(rule.index);
5252
}
5353
};
5454

0 commit comments

Comments
 (0)