-
Notifications
You must be signed in to change notification settings - Fork 65
/
index.js
59 lines (45 loc) · 870 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
var jss = require('jss');
function useSheet(rules, options) {
var refs = 0,
sheet;
function attach() {
if (!sheet)
sheet = jss.createStyleSheet(rules, options);
sheet.attach();
}
function detach() {
sheet.detach();
}
function ref() {
if (refs === 0)
attach();
refs++;
return sheet;
}
function deref() {
refs--;
if (refs === 0)
detach();
}
var Mixin = {
componentWillMount: function () {
this.sheet = ref();
},
componentWillUnmount: function () {
deref();
this.sheet = null;
}
};
// Support React Hot Loader
if (module.hot) {
Mixin.componentWillUpdate = function () {
if (this.sheet !== sheet) {
this.sheet.detach();
this.sheet = ref();
}
};
}
return Mixin;
}
module.exports = useSheet;