-
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.
Save style to persistent storage to prevent flashes after loading fin…
…ishes (#860)
- Loading branch information
Showing
9 changed files
with
149 additions
and
159 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
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,46 +1,42 @@ | ||
import { customTheme } from '../../../util/configs/theme.config' | ||
import { useConfigContext } from '../config/ConfigContext' | ||
import { ChakraProvider, useColorMode } from '@chakra-ui/react' | ||
import { PropsWithChildren, useEffect, useMemo } from 'react' | ||
import { getColorShadesForColor } from '../../../util/core-functions.util' | ||
import { PropsWithChildren, useEffect, useMemo, useState } from 'react' | ||
import { Style } from '../config/types.ts' | ||
import { getCustomTheme } from '../../../util/configs/theme.config.ts' | ||
import { getPersistentStyle, PersistentStyleSettingContext, savePersistentStyle } from '../../../util/configs/themeStyle.config.ts' | ||
|
||
export const ThemeConfig = ({ children }: PropsWithChildren) => { | ||
const config = useConfigContext() | ||
|
||
useThemeUpdate(config?.components?.style) | ||
const chakraConfig = useMemo(() => { | ||
if (config?.components.style) { | ||
customTheme.colors.brand = getColorShadesForColor(config.components.style.lightBrandingColor) | ||
customTheme.colors.lightContainerColor = getColorShadesForColor(config.components.style.lightContainerColor) | ||
customTheme.colors.lightContainerBg = config.components.style.lightContainerColor | ||
customTheme.colors.darkContainerColor = getColorShadesForColor(config.components.style.darkContainerColor) | ||
customTheme.colors.darkContainerBg = config.components.style.darkContainerColor | ||
customTheme.fonts = { | ||
heading: config.components.style.mainFontName, | ||
body: config.components.style.mainFontName, | ||
display: config.components.style.displayFontName, | ||
mono: 'monospace' | ||
} | ||
customTheme.components.Heading = { | ||
...customTheme.components.Heading, | ||
variants: { | ||
'main-title': { fontFamily: config.components.style.displayFontName } | ||
} | ||
} | ||
} | ||
return customTheme | ||
}, [config]) | ||
|
||
return <ChakraProvider theme={chakraConfig}>{children}</ChakraProvider> | ||
} | ||
|
||
const useThemeUpdate = (style?: Style) => { | ||
const ColorModeSetter = ({ children, style }: PropsWithChildren & { style?: Style }) => { | ||
const { colorMode, setColorMode } = useColorMode() | ||
useEffect(() => { | ||
if (!setColorMode) return | ||
if (!style) return | ||
if (colorMode !== 'dark' && style.forceDarkMode) { | ||
setColorMode('dark') | ||
} else if (!style.darkModeEnabled) setColorMode('white') | ||
}, [!!style, style?.deviceTheme, style?.forceDarkMode]) | ||
}, [!!setColorMode, !!style, style?.deviceTheme, style?.forceDarkMode]) | ||
|
||
return <>{children}</> | ||
} | ||
|
||
export const ThemeConfig = ({ children }: PropsWithChildren) => { | ||
const [persistentStyle, setPersistentStyle] = useState<Style | undefined>(getPersistentStyle()) | ||
|
||
const theme = useMemo(() => getCustomTheme(persistentStyle), [persistentStyle]) | ||
const contextData = useMemo( | ||
() => ({ | ||
persistentStyle, | ||
setPersistentStyle: (style?: Style) => { | ||
savePersistentStyle(style) | ||
setPersistentStyle(style) | ||
} | ||
}), | ||
[persistentStyle] | ||
) | ||
|
||
return ( | ||
<PersistentStyleSettingContext.Provider value={contextData}> | ||
<ChakraProvider theme={theme}> | ||
<ColorModeSetter style={persistentStyle}>{children}</ColorModeSetter> | ||
</ChakraProvider> | ||
</PersistentStyleSettingContext.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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,36 +1,67 @@ | ||
import { extendTheme } from '@chakra-ui/react' | ||
import { mode } from '@chakra-ui/theme-tools' | ||
import { getColorShadesForColor } from '../core-functions.util.ts' | ||
import { Style } from '../../api/contexts/config/types.ts' | ||
|
||
// See more: https://chakra-ui.com/docs/theming/customize-theme | ||
export let customTheme = extendTheme({ | ||
styles: { | ||
global: (props: any) => ({ | ||
body: { | ||
color: mode('gray.900', 'whiteAlpha.900')(props), | ||
bg: mode('white', 'gray.900')(props) | ||
export const KirDevColor = '#F15A29' | ||
|
||
export const getCustomTheme = (style?: Style) => | ||
extendTheme( | ||
{ | ||
styles: { | ||
global: (props: any) => ({ | ||
body: { | ||
color: mode('gray.900', 'whiteAlpha.900')(props), | ||
bg: mode('white', 'gray.900')(props) | ||
} | ||
}) | ||
}, | ||
colors: { | ||
brand: { | ||
50: '#d9fae7', | ||
100: '#fcded4', | ||
200: '#f9bda9', | ||
300: '#f79c7f', | ||
400: '#f47b54', | ||
500: '#F15A29', | ||
600: '#c14821', | ||
700: '#913619', | ||
800: '#602410', | ||
900: '#301208' | ||
}, | ||
kirDev: '#F15A29' | ||
}, | ||
components: { | ||
Heading: { | ||
baseStyle: { | ||
marginTop: 5 | ||
} | ||
} | ||
} | ||
}) | ||
}, | ||
colors: { | ||
brand: { | ||
50: '#d9fae7', | ||
100: '#fcded4', | ||
200: '#f9bda9', | ||
300: '#f79c7f', | ||
400: '#f47b54', | ||
500: '#F15A29', | ||
600: '#c14821', | ||
700: '#913619', | ||
800: '#602410', | ||
900: '#301208' | ||
}, | ||
kirDev: '#F15A29' | ||
}, | ||
components: { | ||
Heading: { | ||
baseStyle: { | ||
marginTop: 5 | ||
getThemeExtensionFromStyle(style) || {} | ||
) | ||
|
||
const getThemeExtensionFromStyle = (style?: Style) => | ||
style && { | ||
colors: { | ||
brand: getColorShadesForColor(style.lightBrandingColor), | ||
lightContainerColor: getColorShadesForColor(style.lightContainerColor), | ||
lightContainerBg: style.lightContainerColor, | ||
darkContainerColor: getColorShadesForColor(style.darkContainerColor), | ||
darkContainerBg: style.darkContainerColor | ||
}, | ||
fonts: { | ||
heading: style.mainFontName, | ||
body: style.mainFontName, | ||
display: style.displayFontName, | ||
mono: 'monospace' | ||
}, | ||
components: { | ||
Heading: { | ||
variants: { | ||
'main-title': { fontFamily: style.displayFontName } | ||
} | ||
} | ||
} | ||
} | ||
}) |
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 { Style } from '../../api/contexts/config/types.ts' | ||
import { createContext, useContext } from 'react' | ||
|
||
export type PersistentStyleSettingData = { | ||
persistentStyle?: Style | ||
setPersistentStyle: (style?: Style) => void | ||
} | ||
|
||
export const PersistentStyleSettingContext = createContext<PersistentStyleSettingData>({ | ||
persistentStyle: undefined, | ||
setPersistentStyle: () => {} | ||
}) | ||
|
||
export const usePersistentStyleSetting = () => useContext(PersistentStyleSettingContext) | ||
|
||
const StyleKey = 'persistent-style-setting' | ||
export const getPersistentStyle = () => { | ||
const styleJson = localStorage.getItem(StyleKey) | ||
if (!styleJson) return undefined | ||
return JSON.parse(styleJson) as Style | ||
} | ||
|
||
export const savePersistentStyle = (value?: Style) => { | ||
if (!value) localStorage.removeItem(StyleKey) | ||
else localStorage.setItem(StyleKey, JSON.stringify(value)) | ||
} |