Skip to content

Commit

Permalink
feat: 🎸 improve cssom addon
Browse files Browse the repository at this point in the history
BREAKING CHANGE: cssom addon API changed, pipe addon as a consequence now behaves
differently, too
  • Loading branch information
streamich committed Mar 21, 2019
1 parent 6976707 commit 6f1ead5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
9 changes: 7 additions & 2 deletions .storybook/CSSOM.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ addonCSSOM(nano);

const rule = nano.createRule('.test_cssom_rule');
rule.style.color = 'red';
rule.style.borderColor = 'red';
rule.selectorText = '.test_cssom_rule2';
console.log('rule', rule);
for (let i = 0; i < rule.style.length; i++) {
console.log(rule.style[i]);
}

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

storiesOf('Addons/CSSOM', module)
.add('rule', () =>
h('div', {className: 'test_cssom_rule'}, 'Hello world')
h('div', {className: 'test_cssom_rule2'}, 'Hello world')
)
.add('with @media at rule', () =>
h('div', {className: 'test_cssom_rule test_cssom_atrule'}, 'Hello world')
h('div', {className: 'test_cssom_atrule'}, 'Hello world')
)
9 changes: 7 additions & 2 deletions addon/cssom.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ exports.addon = function (renderer) {
// CSSOM support only browser environment.
if (!renderer.client) return;

// Style sheet for media queries.
document.head.appendChild(renderer.msh = document.createElement('style'));

renderer.createRule = function (selector, prelude) {
var rawCss = selector + '{}';
if (prelude) rawCss = prelude + '{' + rawCss + '}';
var sheet = renderer.sh.sheet;
var sheet = prelude ? renderer.msh.sheet : renderer.sh.sheet;
var index = sheet.insertRule(rawCss, sheet.cssRules.length);
var rule = (sheet.cssRules || sheet.rules)[index];

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

return rule;
Expand Down
2 changes: 1 addition & 1 deletion addon/pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ exports.addon = function (renderer) {
},
remove: function () {
for (var selectorTemplate in rules)
rules[selectorTemplate].remove();
renderer.sh.deleteRule(rule.index);
}
};

Expand Down

0 comments on commit 6f1ead5

Please sign in to comment.