-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance useBiteConsent with theme support
This commit adds theme support to the useBiteConsent hook, allowing users to specify a theme object containing color configurations. It updates the implementation to use the theme colors for the consent banner.
- Loading branch information
Showing
5 changed files
with
108 additions
and
26 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
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,35 +1,76 @@ | ||
import React, { createContext, ReactNode, useContext, useEffect, useState } from 'react' | ||
|
||
type ThemeMode = 'auto' | 'light' | 'dark' | ||
|
||
type ThemeContextData = { | ||
type ColorSet = { | ||
background?: string | ||
text?: string | ||
shadow?: string | ||
primaryActionBackground?: string | ||
primaryActionHoverBackground?: string | ||
primaryActionText?: string | ||
secondaryActionBackground?: string | ||
secondaryActionText?: string | ||
secondaryActionHoverBackground?: string | ||
} | ||
type Theme = { | ||
mode: ThemeMode | ||
light: ColorSet | ||
dark: ColorSet | ||
} | ||
|
||
const ThemeContext = createContext({} as ThemeContextData) | ||
const defaultTheme = { | ||
mode: 'auto', | ||
light: { | ||
background: '#fff', | ||
text: '#000', | ||
shadow: '0 0 10px rgba(0, 0, 0, 0.1)', | ||
primaryActionBackground: '#38bdf8', | ||
primaryActionHoverBackground: '#38bdf8', | ||
primaryActionText: '#fff', | ||
secondaryActionBackground: '#ffffff', | ||
secondaryActionText: '#000000', | ||
secondaryActionHoverBackground: '#f0f0f0' | ||
}, | ||
dark: { | ||
background: '#262626', | ||
text: '#ffffff', | ||
shadow: '0 0 10px rgba(255, 255, 255, 0.1)', | ||
primaryActionBackground: '#38bdf8', | ||
primaryActionHoverBackground: '#38bdf8', | ||
primaryActionText: '#ffffff', | ||
secondaryActionBackground: '#2c2c2c', | ||
secondaryActionText: '#ffffff', | ||
secondaryActionHoverBackground: '#333333' | ||
} | ||
} as Theme | ||
const ThemeContext = createContext(defaultTheme) | ||
|
||
const ThemeProvider = ({ mode: presetMode, children }: { mode: ThemeMode; children: ReactNode }) => { | ||
const [mode, setMode] = useState<ThemeMode>(presetMode ?? 'auto') | ||
const ThemeProvider = ({ theme: providedTheme, children }: { theme?: Theme; children: ReactNode }) => { | ||
const [theme, setTheme] = useState<Theme>({ | ||
mode: providedTheme?.mode ?? 'auto', | ||
light: { ...defaultTheme.light, ...providedTheme?.light }, | ||
dark: { ...defaultTheme.dark, ...providedTheme?.dark } | ||
}) | ||
|
||
useEffect(() => { | ||
if (typeof window === 'undefined' || typeof document === 'undefined' || mode !== 'auto') return | ||
if (typeof window === 'undefined' || typeof document === 'undefined' || theme.mode !== 'auto') return | ||
|
||
const themeQuery = window.matchMedia('(prefers-color-scheme: dark)') | ||
themeQuery.addEventListener('change', (e) => { | ||
setMode(e.matches ? 'dark' : 'light') | ||
setTheme({ ...theme, mode: e.matches ? 'dark' : 'light' }) | ||
}) | ||
|
||
return () => { | ||
themeQuery.removeEventListener('change', (e) => { | ||
setMode(e.matches ? 'dark' : 'light') | ||
setTheme({ ...theme, mode: e.matches ? 'dark' : 'light' }) | ||
}) | ||
} | ||
}, [setMode]) | ||
}, [setTheme]) | ||
|
||
return <ThemeContext.Provider value={{ mode }}>{children}</ThemeContext.Provider> | ||
return <ThemeContext.Provider value={theme}>{children}</ThemeContext.Provider> | ||
} | ||
|
||
const useTheme = () => useContext(ThemeContext) | ||
|
||
export default ThemeProvider | ||
export { ThemeMode as Theme, useTheme } | ||
export { Theme, useTheme } |
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