Skip to content

Commit

Permalink
feat(theme): add simple implementation of provider/consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
malashkevich committed Oct 31, 2018
1 parent 2f8df0f commit a38af67
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/framework/theme/index.ts
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};
5 changes: 5 additions & 0 deletions src/framework/theme/primitives/createContext.ts
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};
14 changes: 14 additions & 0 deletions src/framework/theme/primitives/themeConsumer.tsx
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;
26 changes: 26 additions & 0 deletions src/framework/theme/primitives/themeProvider.tsx
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;

0 comments on commit a38af67

Please sign in to comment.