-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcreateStyled.js
More file actions
63 lines (52 loc) · 1.59 KB
/
createStyled.js
File metadata and controls
63 lines (52 loc) · 1.59 KB
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
60
61
62
63
import styled from './styled'
import sheetsObserver from './utils/sheetsObserver'
import type {
BaseStylesType,
ComponentStyleType,
StyledType,
StyledElementAttrsType,
StyledElementType,
TagNameOrStyledElementType
} from './types'
sheetsObserver.listen()
const createStyled = (jss: Function) => (
baseStyles: BaseStylesType = {}
): StyledType => {
let staticSheet
let dynamicSheet
let dynamicSheetId
const addRule = (name: string, style: ComponentStyleType, data: Object) => {
if (data) {
dynamicSheet.detach().addRule(name, style)
dynamicSheet.update(name, data)
sheetsObserver.update(dynamicSheetId)
}
else {
staticSheet.addRule(name, style)
}
}
const mountSheets = () => {
if (!staticSheet) {
staticSheet = jss.createStyleSheet(baseStyles, {
meta: 'StaticBaseSheet',
}).attach()
dynamicSheet = jss.createStyleSheet({}, {
link: true,
meta: 'DynamicComponentSheet',
}).attach()
dynamicSheetId = sheetsObserver.add(dynamicSheet)
}
return {staticSheet, dynamicSheet}
}
return Object.assign((
tagNameOrStyledElement: TagNameOrStyledElementType,
ownStyle: ComponentStyleType
): StyledElementType => {
const {tagName, style}: StyledElementAttrsType = typeof tagNameOrStyledElement === 'string'
? {tagName: tagNameOrStyledElement, style: {}}
: tagNameOrStyledElement
const elementStyle = {...style, ...ownStyle}
return styled({tagName, baseStyles, elementStyle, mountSheets, addRule})
}, {mountSheets, styles: baseStyles})
}
export default createStyled