1 | import React, { useContext } from 'react'
|
2 | import ThemeContext from './ThemeContext'
|
3 |
|
4 |
|
5 | export default function themed (Component) {
|
6 | function ThemeWrapper (props) {
|
7 | let contextTheme = useContext(ThemeContext)
|
8 | let theme = props.theme || contextTheme
|
9 | let res
|
10 | if (theme && !props.theme) {
|
11 | res = Component({ theme, ...props })
|
12 | } else {
|
13 | res = Component(props)
|
14 | }
|
15 | return (props.theme && (!contextTheme || contextTheme !== props.theme)) ? (
|
16 | React.createElement(
|
17 | ThemeContext.Provider,
|
18 | { value: props.theme },
|
19 | res
|
20 | )
|
21 | ) : (
|
22 | res
|
23 | )
|
24 | }
|
25 | ThemeWrapper.displayName = Component.displayName || Component.name
|
26 | ThemeWrapper.propTypes = Component.propTypes
|
27 | ThemeWrapper.defaultProps = Component.defaultProps
|
28 | return ThemeWrapper
|
29 | }
|