-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.js
161 lines (136 loc) · 4.65 KB
/
index.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { createContext, useContext } from "react"
import { useStickyState } from "hooks"
import styles from "./Theme.module.css"
import THEMES from "./THEMES"
export const NUMBER_OF_BODY_FONTS = 5
export const NUMBER_OF_HEADER_FONTS = 5
export const COLORS = [
"blue0",
"pink0",
"green0",
"purple0",
"orange0",
"blue1",
"green1",
"pink1",
"peach0",
"orange1",
]
export const NUMBER_OF_COLORS = COLORS.length
const colored = id => {
return {
onHover: styles[`${COLORS[id]}BgOnHover`],
classColor: styles[`${COLORS[id]}Color`],
var: `var(--${COLORS[id]})`,
}
}
const NUMBER_OF_THEMES = THEMES.length
/******************************
THEME
******************************/
const ThemeContext = createContext()
const ThemeProvider = ({ children }) => {
const [themeId, setThemeId] = useStickyState(0, "themeId")
const [colorId, setColorId] = useStickyState(0, "colorId")
const [headerFontId, setHeaderFontId] = useStickyState(0, "headerId")
const [bodyFontId, setBodyFontId] = useStickyState(0, "bodyFontId")
const theme = THEMES[themeId]
const onHoverClassName = colored(colorId).onHover
const nextColor = () => {
const n = (Number(colorId) + 1) % NUMBER_OF_COLORS
setColorId(n)
}
const nextBodyFont = () => {
const n = (Number(bodyFontId) + 1) % NUMBER_OF_BODY_FONTS
setBodyFontId(n)
}
const nextHeaderFont = () => {
const n = (Number(headerFontId) + 1) % NUMBER_OF_HEADER_FONTS
setHeaderFontId(n)
}
const nextPageTheme = () => {
const n = (Number(themeId) + 1) % NUMBER_OF_THEMES
setThemeId(n)
}
const primaryColor = colored(colorId).var
const headerFont = `var(--header-font-0${headerFontId})`
const bodyFont = `var(--body-font-0${bodyFontId})`
return (
<ThemeContext.Provider
value={{
bodyFont,
headerFont,
primaryColor,
themeId,
colorId,
nextBodyFont,
nextPageTheme,
nextColor,
nextHeaderFont,
onHoverClassName,
bg1ClassName: theme.body,
bg2ClassName: theme.section,
}}
>
{children}
</ThemeContext.Provider>
)
}
/******************************
BUTTON THEME
******************************/
const ButtonThemeContext = createContext()
const ButtonThemeProvider = ({ children }) => {
const { themeId, colorId } = useContext(ThemeContext)
const theme = THEMES[themeId]
const onHoverClassName = colored(colorId).onHover
const buttonClassNames = [
theme.button,
onHoverClassName,
colored(colorId).classColor,
theme.buttonOnHover,
]
const invertedButtonClassName = theme.invertedButton
return (
<ButtonThemeContext.Provider
value={{
buttonClassNames,
invertedButtonClassName,
disabledClassName: styles.disabled,
}}
>
{children}
</ButtonThemeContext.Provider>
)
}
const useButtonThemeClasses = (className, disabled, isInvertedColor) => {
const context = useContext(ButtonThemeContext)
if (!context) {
throw new Error(
`hook: useButtonThemeClasses must be used within a provider: useButtonThemeProvider`
)
}
const { buttonClassNames, invertedButtonClassName, disabledClassName } = context
// invertedButtonClassName
// - normal, opacity: slightly less than 1.0
// - hovered, opacity: 1.0
// - backgroundColor: UNDEFINED
// - color: based on theme (light (white), dark (black), funky (red))
// disabledClassName
// - normal: opacity: around 0.3
// - hovered: opacity: name as normal
const [
defaultBackground, // default button background color when not hovered (light (white), dark (black), funky (red))
defaultColorOnHover, // default color of the element when hovered (light (white), dark (black), funky (red))
defaultColor, // default color of element when not hovered (primaryColor)
defaultBackgroundOnHover, // default background color when hovered (primary color)
] = buttonClassNames
let final = isInvertedColor
? [invertedButtonClassName]
: [defaultBackground, defaultColor]
final = disabled
? [...final, disabledClassName]
: [...final, defaultColorOnHover, defaultBackgroundOnHover]
return [...final, className].join(" ")
}
export { ThemeProvider, ThemeContext, ButtonThemeProvider, useButtonThemeClasses }