-
Notifications
You must be signed in to change notification settings - Fork 3
/
ThemeProvider.tsx
74 lines (61 loc) · 2.07 KB
/
ThemeProvider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React, { FC, createContext, useContext, useState } from 'react'
import { getTextColor, opacityColor } from './helpers'
import classes from './themeProvider.module.scss'
type LayoutMode = 'fullViewportHeight' | 'flexible'
const defaultMode: LayoutMode = 'fullViewportHeight'
interface ThemeContextType {
accentColor: string
layoutMode: LayoutMode
updateLayoutMode: (mode: LayoutMode) => void
resetLayoutMode: () => void
}
const ThemeContext = createContext<ThemeContextType | undefined>(undefined)
export interface ThemeProviderProps {
children: React.ReactNode | string
accentColor?: string
}
export const ThemeProvider: FC<ThemeProviderProps> = ({
children,
accentColor = 'var(--awell-brand100, #004ac2)',
}) => {
const [layoutMode, setLayoutMode] = useState<LayoutMode>(defaultMode)
const style = {
'--awell-accent-color': accentColor,
'--awell-accent-text-color': getTextColor(accentColor),
'--awell-accent-hover-color': opacityColor(accentColor, 0.9),
'--awell-accent-ring-color-inputs': accentColor,
'--awell-accent-ring-color-buttons': opacityColor(accentColor, 0.4),
'--awell-secondary-color': opacityColor(accentColor, 0.2),
'--awell-secondary-text-color': accentColor,
'--awell-secondary-hover-color': opacityColor(accentColor, 0.3),
'--awell-secondary-ring-color-inputs': accentColor,
'--awell-secondary-ring-color-buttons': accentColor,
height: '100%',
} as React.CSSProperties
const updateLayoutMode = (mode: LayoutMode) => {
setLayoutMode(mode)
}
const resetLayoutMode = () => {
setLayoutMode(defaultMode)
}
const contextValue = {
accentColor,
layoutMode,
updateLayoutMode,
resetLayoutMode,
}
return (
<ThemeContext.Provider value={contextValue}>
<div className={classes.awell_themeProvider} style={style}>
{children}
</div>
</ThemeContext.Provider>
)
}
export const useTheme = (): ThemeContextType => {
const context = useContext(ThemeContext)
if (!context) {
throw new Error('useTheme must be used within a ThemeProvider')
}
return context
}