Skip to content
This repository was archived by the owner on Mar 6, 2020. It is now read-only.

Commit c1b5adf

Browse files
committed
support raw CSS strings and Webpack styles in plugin style APIs
1 parent 9e0bec9 commit c1b5adf

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

src/Plugin.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import Settings from './models/Settings';
88
import Style from './util/Style';
99
import SettingsView from './views/users/settings/DefaultSettingsView';
1010

11+
function isWebpackStyle(v) {
12+
return Array.isArray(v) && typeof v.i === 'function';
13+
}
14+
1115
const stubHook = () => {};
1216
const hooks = ['enable', 'disable'];
1317

@@ -99,7 +103,14 @@ const Plugin = Class.extend({
99103

100104
// Styles API
101105
createStyle(defaults = {}) {
102-
const style = new Style(defaults);
106+
const style = new Style();
107+
if (typeof defaults === 'string') {
108+
style.raw(defaults);
109+
} else if (isWebpackStyle(defaults)) {
110+
style.raw(defaults.toString());
111+
} else {
112+
style.set(defaults);
113+
}
103114
this[stylesSymbol].push(style);
104115
return style;
105116
},

src/util/Style.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Style = Class.extend({
99
init(defaults) {
1010
this.sistyl = new Sistyl(defaults);
1111
this.timeout = null;
12+
this.rawStyles = [];
1213

1314
this.refresh = this.refresh.bind(this);
1415
this.id = _.uniqueId('eps-');
@@ -23,6 +24,15 @@ const Style = Class.extend({
2324
this.refresh();
2425
},
2526

27+
raw(text) {
28+
this.rawStyles.push(text);
29+
30+
// throttle updates
31+
clearTimeout(this.timeout);
32+
this.timeout = setTimeout(this.refresh, 1);
33+
return this;
34+
},
35+
2636
$() {
2737
let el = this.el;
2838
if (popoutView._window) { // eslint-disable-line no-underscore-dangle
@@ -58,9 +68,8 @@ const Style = Class.extend({
5868
},
5969

6070
toString() {
61-
return this.sistyl.toString();
71+
return `${this.sistyl} \n ${this.rawStyles.join('\n\n')}`;
6272
},
63-
6473
});
6574

6675
export default Style;

0 commit comments

Comments
 (0)