-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcreateStyled.js
More file actions
55 lines (44 loc) · 1.34 KB
/
createStyled.js
File metadata and controls
55 lines (44 loc) · 1.34 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
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 sheet
const mountSheet = () => {
if (!sheet) {
sheet = jss.createStyleSheet(baseStyles, {
link: true,
meta: 'styled-jss',
}).attach()
}
return sheet
}
const styledWrapper = (
tagNameOrStyledElement: TagNameOrStyledElementType
) => (
ownStyle: ComponentStyleType
): StyledElementType => {
const {tagName, style} = getStyledArgs(tagNameOrStyledElement)
const elementStyle = {...style, ...ownStyle}
return styled({tagName, baseStyles, elementStyle, mountSheet, jss})
}
Object.defineProperty(styledWrapper, 'sheet', ({
get: () => sheet,
}: Object)) // https://github.com/facebook/flow/issues/285
return Object.assign(styledWrapper, {mountSheet, styles: baseStyles})
}
export default createStyled