-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-web): extract custom ThemeProvider and useTheme hook
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
Showing
5 changed files
with
32 additions
and
4 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
26 changes: 26 additions & 0 deletions
26
packages/components/react-web/src/ThemeProvider/ThemeProvider.js
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, { 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, | ||
}; |
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 @@ | ||
export * from './ThemeProvider'; |
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 @@ | ||
export { useTheme } from '../ThemeProvider'; |
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