Allow for custom hues inside a theme provider

This commit is contained in:
Eric Bailey
2024-12-06 10:44:28 -06:00
parent 3ab6c435df
commit 4840ae0eb4
2 changed files with 12 additions and 7 deletions
+11 -6
View File
@@ -33,6 +33,7 @@ export type Alf = {
setFontScale: (fontScale: Exclude<Device['fontScale'], undefined>) => void setFontScale: (fontScale: Exclude<Device['fontScale'], undefined>) => void
setFontFamily: (fontFamily: Device['fontFamily']) => void setFontFamily: (fontFamily: Device['fontFamily']) => void
} }
setHue: (hue: number) => void
/** /**
* Feature flags or other gated options * Feature flags or other gated options
*/ */
@@ -59,6 +60,7 @@ export const Context = React.createContext<Alf>({
setFontScale: () => {}, setFontScale: () => {},
setFontFamily: () => {}, setFontFamily: () => {},
}, },
setHue: () => {},
flags: {}, flags: {},
}) })
@@ -94,20 +96,21 @@ export function ThemeProvider({
}, },
[setFontFamily], [setFontFamily],
) )
const [hue, setHue] = React.useState(BLUE_HUE)
const themes = React.useMemo(() => { const themes = React.useMemo(() => {
return createThemes({ return createThemes({
hues: { hues: {
primary: BLUE_HUE, primary: hue,
negative: RED_HUE, negative: RED_HUE,
positive: GREEN_HUE, positive: GREEN_HUE,
}, },
}) })
}, []) }, [hue])
const value = React.useMemo<Alf>( const value = React.useMemo<Alf>(
() => ({ () => ({
themes, themes,
themeName: themeName, themeName,
theme: themes[themeName], theme: themes[themeName],
fonts: { fonts: {
scale: fontScale, scale: fontScale,
@@ -116,6 +119,7 @@ export function ThemeProvider({
setFontScale: setFontScaleAndPersist, setFontScale: setFontScaleAndPersist,
setFontFamily: setFontFamilyAndPersist, setFontFamily: setFontFamilyAndPersist,
}, },
setHue: setHue,
flags: {}, flags: {},
}), }),
[ [
@@ -126,6 +130,7 @@ export function ThemeProvider({
fontFamily, fontFamily,
setFontFamilyAndPersist, setFontFamilyAndPersist,
fontScaleMultiplier, fontScaleMultiplier,
setHue,
], ],
) )
@@ -136,11 +141,11 @@ export function useAlf() {
return React.useContext(Context) return React.useContext(Context)
} }
export function useTheme(theme?: ThemeName) { export function useTheme(themeName?: ThemeName) {
const alf = useAlf() const alf = useAlf()
return React.useMemo(() => { return React.useMemo(() => {
return theme ? alf.themes[theme] : alf.theme return themeName ? alf.themes[themeName] : alf.theme
}, [theme, alf]) }, [themeName, alf])
} }
export function useBreakpoints() { export function useBreakpoints() {
+1 -1
View File
@@ -60,7 +60,7 @@ export function createThemes({
dim: Theme dim: Theme
} { } {
const color = { const color = {
trueBlack: '#000000', trueBlack: 'hsl(0, 0%, 0%)',
gray_0: `hsl(${hues.primary}, 20%, ${defaultScale[14]}%)`, gray_0: `hsl(${hues.primary}, 20%, ${defaultScale[14]}%)`,
gray_25: `hsl(${hues.primary}, 20%, ${defaultScale[13]}%)`, gray_25: `hsl(${hues.primary}, 20%, ${defaultScale[13]}%)`,