-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial theme switcher functionality - No UI Fixes
- Loading branch information
1 parent
c01a362
commit 05cecaf
Showing
3 changed files
with
48 additions
and
36 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
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,41 +1,46 @@ | ||
import { ChakraProvider, ChakraTheme, extendTheme } from '@chakra-ui/react'; | ||
import { withProse } from '@nikolovlazar/chakra-ui-prose'; | ||
import sitecoreTheme from '@sitecore/blok-theme'; | ||
import { ChakraProvider } from '@chakra-ui/react'; | ||
import { corporateTheme } from 'chakra/theme/corporate/theme'; | ||
import fantasyTheme from 'chakra/theme/fantasy/theme'; | ||
import { useGameInfoContext } from 'components/Contexts'; | ||
import { FC, useEffect, useState } from 'react'; | ||
import React, { FC, useState, createContext } from 'react'; | ||
|
||
interface ThemeSwitcherProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
export const ThemeSwitcher: FC<ThemeSwitcherProps> = ({ children }) => { | ||
const gameInfoContext = useGameInfoContext(); | ||
const [theme, setTheme] = useState<ChakraTheme | Record<string, any>>(sitecoreTheme); | ||
|
||
useEffect(() => { | ||
if (gameInfoContext?.theme?.chakraTheme == 'fantasy') { | ||
setTheme( | ||
extendTheme( | ||
fantasyTheme, | ||
withProse({ | ||
baseStyle: { | ||
p: { | ||
fontFamily: 'inherit', | ||
}, | ||
}, | ||
}) | ||
) | ||
); | ||
} else if (gameInfoContext?.theme?.chakraTheme == 'corporate') { | ||
setTheme(corporateTheme); | ||
} | ||
}, [gameInfoContext?.theme]); | ||
export const ThemeSwitcher = createContext<ThemeSwitcherType>({} as ThemeSwitcherType); | ||
|
||
export interface ThemeSwitcherType { | ||
changeTheme: (themeChoice: string) => void; | ||
} | ||
|
||
export const useThemeSwitcher = () => React.useContext(ThemeSwitcher); | ||
|
||
|
||
export const ThemeSwitcherProvider: FC<ThemeSwitcherProps> = ({ children }) => { | ||
const themes = { | ||
"corporate": corporateTheme, | ||
"fantasy": fantasyTheme | ||
}; | ||
|
||
const [themeName, setThemeName] = useState<string>("corporate"); | ||
|
||
const changeTheme = (themeChoice: string) => { | ||
setThemeName(themeChoice); | ||
} | ||
|
||
const currentTheme = themes[themeName as keyof typeof themes]; | ||
|
||
return ( | ||
<> | ||
<ChakraProvider theme={theme}>{children}</ChakraProvider> | ||
</> | ||
<ThemeSwitcher.Provider | ||
value={{ | ||
changeTheme: (themeChoice: string) => { | ||
changeTheme(themeChoice); | ||
}, | ||
}} | ||
> | ||
<ChakraProvider theme={currentTheme}> | ||
{children} | ||
</ChakraProvider> | ||
</ThemeSwitcher.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