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