Skip to content

Commit

Permalink
feat(react-web): extract custom ThemeProvider and useTheme hook
Browse files Browse the repository at this point in the history
Create our own `ThemeContext` that has a default value
This way components that we convert to web-components don't
have to be individually wrapped by a `ThemeProvider`
  • Loading branch information
kidroca committed Jun 29, 2021
1 parent 11dc70d commit e6de919
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
6 changes: 2 additions & 4 deletions packages/components/react-web/.storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import React from 'react';
import { ThemeProvider } from '@emotion/react';
import { ThemeProvider } from '../src';

export const parameters = {
actions: { argTypesRegex: '^on[A-Z].*' },
backgrounds: { grid: { cellSize: 16, opacity: 0.25 } },
};

const theme = require('@compassion-gds/tokens');

export const decorators = [
(Story) => (
<ThemeProvider theme={theme}>
<ThemeProvider>
<Story />
</ThemeProvider>
),
Expand Down
26 changes: 26 additions & 0 deletions packages/components/react-web/src/ThemeProvider/ThemeProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React, { ThemeProvider as EmotionThemeProvider } from '@emotion/react';
import PropTypes from 'prop-types';
import { useContext, createContext } from 'react';
import defaultTheme from "@compassion-gds/tokens";

const ThemeContext = createContext(defaultTheme);

export const useTheme = () => useContext(ThemeContext);

export const ThemeProvider = ({ theme = defaultTheme, children }) => (
<ThemeContext.Provider value={theme}>
<EmotionThemeProvider theme={theme}>
{children}
</EmotionThemeProvider>
</ThemeContext.Provider>
);

ThemeProvider.propTypes = {
theme: PropTypes.shape({
component: PropTypes.objectOf(PropTypes.object).isRequired,
}),
};

ThemeProvider.defaultProps = {
theme: defaultTheme,
};
1 change: 1 addition & 0 deletions packages/components/react-web/src/ThemeProvider/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ThemeProvider';
1 change: 1 addition & 0 deletions packages/components/react-web/src/hooks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useTheme } from '../ThemeProvider';
2 changes: 2 additions & 0 deletions packages/components/react-web/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ export * from './Select';
export * from './Image';
export * from './ProgressBar';
export * from './Uploader';
export * from './ThemeProvider';
export * from './hooks';

0 comments on commit e6de919

Please sign in to comment.