styled-components#ThemeProviderProps TypeScript Examples

The following examples show how to use styled-components#ThemeProviderProps. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ThemeProvider.tsx    From design-system with Apache License 2.0 6 votes vote down vote up
ThemeProvider = ({ theme = light, children }: ThemeProviderProps<any>) => {
	const [selectedTheme, setSelectedTheme] = useState(theme);
	// Handle nested Providers: parent Provider doesn't have context, child does
	const context = useContext(ThemeContext);

	React.useEffect(() => {
		setSelectedTheme(theme);
	}, [theme]);

	const switchTheme = (newTheme: DefaultTheme) => setSelectedTheme(newTheme);
	return (
		<ThemeContext.Provider value={context.theme ? context : { switchTheme, theme: selectedTheme }}>
			<StyledThemeProvider theme={context.theme || selectedTheme}>{children}</StyledThemeProvider>
		</ThemeContext.Provider>
	);
}