-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
withStyles.ts
45 lines (40 loc) 路 1.42 KB
/
withStyles.ts
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
import { h } from './core.ts'
import { Component } from './component.ts'
import { Fragment } from './fragment.ts'
import { Helmet } from './components/helmet.ts'
interface ObjectHasToString {
toString: () => string
}
type Styles = string
type FunctionReturnsString = () => string
type WithStylesType = Styles | ObjectHasToString | FunctionReturnsString
export const withStyles: any =
(...styles: WithStylesType[]) =>
(WrappedComponent: any) => {
return class extends Component {
render() {
const { children, ...rest } = this.props
const helmets: any[] = []
styles.forEach(style => {
if (typeof style === 'string') {
helmets.push(h(Helmet, null, h('style', null, style)))
} else if (typeof style === 'function') {
const _style = style()
if (typeof _style === 'string') {
helmets.push(h(Helmet, null, h('style', null, _style)))
}
} else if (typeof style === 'object') {
const _style = style.toString?.()
if (typeof _style === 'string') {
helmets.push(h(Helmet, null, h('style', null, _style)))
}
}
})
const component =
children && children.length > 0
? h(WrappedComponent, { ...rest }, children)
: h(WrappedComponent, { ...this.props })
return h(Fragment, null, ...helmets, component)
}
}
}