Skip to content

Commit aafd1d4

Browse files
committed
Refactor createStyled, move styled function to the separate file
1 parent 3860127 commit aafd1d4

4 files changed

Lines changed: 97 additions & 87 deletions

File tree

src/index.js

Lines changed: 13 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
1-
import {PureComponent, createElement} from 'react'
2-
import {create as createJss, getDynamicStyles} from 'jss'
1+
import {create as createJss} from 'jss'
32
import preset from 'jss-preset-default'
43

5-
import filterProps from './utils/filterProps'
6-
import composeClasses from './utils/composeClasses'
7-
import generateTagName from './utils/generateTagName'
4+
import styled from './styled'
85
import type {
96
BaseStylesType,
107
ComponentStyleType,
118
StyledType,
129
StyledElementAttrsType,
1310
StyledElementType,
14-
TagNameOrStyledElementType,
15-
StyledElementPropsType
11+
TagNameOrStyledElementType
1612
} from './types'
1713

1814
const jssDefault = createJss(preset())
1915

20-
2116
const createStyled = (
2217
jss?: Function = jssDefault
2318
) => (
2419
baseStyles: BaseStylesType = {}
2520
): StyledType => {
26-
const sheets = {}
21+
let staticSheet
22+
let dynamicSheet
2723

2824
const mountSheets = () => {
29-
if (!sheets.staticSheet) {
30-
sheets.staticSheet = jss.createStyleSheet(baseStyles, {
25+
if (!staticSheet) {
26+
staticSheet = jss.createStyleSheet(baseStyles, {
3127
meta: 'StaticBaseSheet',
3228
}).attach()
3329

34-
sheets.dynamicSheet = jss.createStyleSheet({}, {
30+
dynamicSheet = jss.createStyleSheet({}, {
3531
link: true,
3632
meta: 'DynamicComponentSheet',
3733
}).attach()
3834
}
35+
36+
return {staticSheet, dynamicSheet}
3937
}
4038

41-
const styled = (
39+
return Object.assign((
4240
tagNameOrStyledElement: TagNameOrStyledElementType,
4341
ownStyle: ComponentStyleType
4442
): StyledElementType => {
@@ -47,72 +45,9 @@ const createStyled = (
4745
: tagNameOrStyledElement
4846

4947
const elementStyle = {...style, ...ownStyle}
50-
const dynamicStyle = getDynamicStyles(elementStyle)
51-
const staticTagName = generateTagName(tagName)
52-
53-
return class StyledElement extends PureComponent {
54-
static tagName: string = tagName
55-
static style: ComponentStyleType = elementStyle
56-
57-
props: StyledElementPropsType
58-
59-
dynamicTagName = ''
60-
61-
constructor(props) {
62-
super(props)
63-
this.dynamicTagName = generateTagName(tagName)
64-
}
65-
66-
componentWillMount() {
67-
mountSheets()
68-
69-
if (!sheets.staticSheet.getRule(staticTagName)) {
70-
sheets.staticSheet.addRule(staticTagName, elementStyle)
71-
}
72-
73-
if (dynamicStyle && !sheets.dynamicSheet.getRule(this.dynamicTagName)) {
74-
sheets.dynamicSheet
75-
.detach()
76-
.addRule(this.dynamicTagName, dynamicStyle)
77-
sheets.dynamicSheet
78-
.update(this.dynamicTagName, this.props)
79-
.attach()
80-
.link()
81-
}
82-
}
83-
84-
componentWillReceiveProps(nextProps: StyledElementPropsType) {
85-
if (dynamicStyle) {
86-
sheets.dynamicSheet.update(this.dynamicTagName, nextProps)
87-
}
88-
}
89-
90-
componentWillUnmount() {
91-
sheets.dynamicSheet.deleteRule(this.dynamicTagName)
92-
}
93-
94-
render() {
95-
if (!sheets.staticSheet) return null
96-
97-
const {children, className, ...attrs} = this.props
98-
99-
const props = filterProps(attrs)
100-
const tagClass = composeClasses(
101-
sheets.staticSheet.classes[staticTagName],
102-
sheets.dynamicSheet.classes[this.dynamicTagName],
103-
className
104-
)
105-
106-
return createElement(tagName, {...props, className: tagClass}, children)
107-
}
108-
}
109-
}
11048

111-
return Object.assign(styled, {
112-
sheets,
113-
mountSheets,
114-
styles: baseStyles
115-
})
49+
return styled({tagName, baseStyles, elementStyle, mountSheets})
50+
}, {mountSheets, styles: baseStyles})
11651
}
11752

11853
const defaultStyledCreator = createStyled()

src/injectStyled.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import composeClasses from './utils/composeClasses'
44
import type {StyledType} from './types'
55

66
const injectStyled = (styled: StyledType) => (InnerComponent: ReactClass<any>) => {
7-
styled.mountSheets()
8-
9-
const {sheets} = styled
10-
const {staticSheet, dynamicSheet} = sheets
7+
const {staticSheet, dynamicSheet} = styled.mountSheets()
118

129
const classNames = Object.keys({...staticSheet.classes, ...dynamicSheet.classes})
1310

src/styled.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import {PureComponent, createElement} from 'react'
2+
import {getDynamicStyles} from 'jss'
3+
4+
import filterProps from './utils/filterProps'
5+
import composeClasses from './utils/composeClasses'
6+
import generateTagName from './utils/generateTagName'
7+
8+
import type {
9+
ComponentStyleType,
10+
StyledElementPropsType
11+
} from './types'
12+
13+
type StyledArgs = {
14+
tagName: string,
15+
elementStyle: Object,
16+
mountSheets: Function
17+
}
18+
19+
const styled = ({tagName, elementStyle, mountSheets}: StyledArgs) => {
20+
const dynamicStyle = getDynamicStyles(elementStyle)
21+
const staticTagName = generateTagName(tagName)
22+
23+
return class StyledElement extends PureComponent {
24+
static tagName: string = tagName
25+
static style: ComponentStyleType = elementStyle
26+
27+
props: StyledElementPropsType
28+
29+
dynamicTagName = ''
30+
staticSheet: Object
31+
dynamicSheet: Object
32+
33+
constructor(props: StyledElementPropsType) {
34+
super(props)
35+
this.dynamicTagName = generateTagName(tagName)
36+
}
37+
38+
componentWillMount() {
39+
Object.assign(this, mountSheets())
40+
41+
if (!this.staticSheet.getRule(staticTagName)) {
42+
this.staticSheet.addRule(staticTagName, elementStyle)
43+
}
44+
45+
if (dynamicStyle && !this.dynamicSheet.getRule(this.dynamicTagName)) {
46+
this.dynamicSheet
47+
.detach()
48+
.addRule(this.dynamicTagName, dynamicStyle)
49+
this.dynamicSheet
50+
.update(this.dynamicTagName, this.props)
51+
.attach()
52+
.link()
53+
}
54+
}
55+
56+
componentWillReceiveProps(nextProps: StyledElementPropsType) {
57+
if (dynamicStyle) {
58+
this.dynamicSheet.update(this.dynamicTagName, nextProps)
59+
}
60+
}
61+
62+
componentWillUnmount() {
63+
this.dynamicSheet.deleteRule(this.dynamicTagName)
64+
}
65+
66+
render() {
67+
if (!this.staticSheet) return null
68+
69+
const {children, className, ...attrs} = this.props
70+
71+
const props = filterProps(attrs)
72+
const tagClass = composeClasses(
73+
this.staticSheet.classes[staticTagName],
74+
this.dynamicSheet.classes[this.dynamicTagName],
75+
className
76+
)
77+
78+
return createElement(tagName, {...props, className: tagClass}, children)
79+
}
80+
}
81+
}
82+
83+
export default styled

src/types/index.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ export type JssDynamicSheet = JssStaticSheet
66
export type BaseStylesType = JssStyles
77
export type ComponentStyleType = JssStyle
88
export type StyledType = Function & {
9-
sheets: {
10-
// TODO: use types from jss
11-
staticSheet: JssStaticSheet,
12-
dynamicSheet: JssDynamicSheet,
13-
},
149
mountSheets: Function,
1510
styles: JssStyles
1611
}

0 commit comments

Comments
 (0)