-
Notifications
You must be signed in to change notification settings - Fork 959
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(theme): add simple implementation of provider/consumer
- Loading branch information
1 parent
2f8df0f
commit a38af67
Showing
4 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
export const description = '@rk-kit/theme'; | ||
import getThemeProvider from './primitives/themeProvider'; | ||
import withTheme from './primitives/themeConsumer'; | ||
|
||
export {getThemeProvider, withTheme}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import React from 'react'; | ||
|
||
const {Consumer, Provider} = React.createContext({}); | ||
|
||
export {Consumer, Provider}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from 'react'; | ||
import {Consumer} from './createContext'; | ||
|
||
function withTheme(Component) { | ||
return ( | ||
<Consumer> | ||
{theme => { | ||
return React.cloneElement(Component, {theme}); | ||
}} | ||
</Consumer> | ||
) | ||
} | ||
|
||
export default withTheme; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import {Provider} from './createContext'; | ||
|
||
interface PropsType { | ||
children: JSX.Element, | ||
theme: Object | ||
} | ||
|
||
function getThemeProvider() { | ||
return class ThemeProvider extends React.PureComponent<PropsType> { | ||
|
||
static defaultProps = { | ||
theme: {} | ||
}; | ||
|
||
render() { | ||
return ( | ||
<Provider value={this.props.theme}> | ||
{this.props.children} | ||
</Provider> | ||
) | ||
} | ||
} | ||
} | ||
|
||
export default getThemeProvider; |