-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathThemes.tsx
181 lines (168 loc) · 7.61 KB
/
Themes.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import "../../styles/Settings.scss";
import { motion } from "framer-motion";
import useLocalStorage from 'use-local-storage';
import { RgbaColor, RgbaColorPicker } from "react-colorful";
import { useState, useEffect, FunctionComponent } from "react";
type ThemesProps = {changeTheme: (arg: string) => void;}
const Themes : FunctionComponent<ThemesProps> = ({changeTheme}) => {
const defaultBG = "#14161F";
const [chatbackgroundcol, setbackgroundcol] = useLocalStorage<string>('chatBackgroundcol', defaultBG);
const [themeCol, sethemeCol] = useLocalStorage<string>('themeCol', defaultBG);
const [chatbackgroundimg, setchatbackgroundimg] = useLocalStorage<any>('chatbackgroundimg', null);//don't change any type because of FileReader ArrayBuffer
const [RGBACP_color, setColor] = useState({ r: 200, g: 150, b: 35, a: 0.5 });
const getCurrentTheme = () => window.matchMedia("(prefers-color-scheme: dark)").matches;
const [isDarkTheme, setIsDarkTheme] = useState<boolean>(getCurrentTheme());
const mqListener = ((e: { matches: boolean | ((prevState: boolean) => boolean); }) => {setIsDarkTheme(e.matches);});
function uploadImg(e: any){
const image = e.target.files[0];
const reader = new FileReader();
reader.readAsDataURL(image);
reader.addEventListener('load', () => setchatbackgroundimg(reader.result));
}
function convertRGBAtoHex(obj: RgbaColor){
setColor(obj);
let r = obj.r.toString(16);
let g = obj.g.toString(16);
let b = obj.b.toString(16);
let a = Math.round(obj.a * 255).toString(16);
if (r.length == 1)
r = "0" + r;
if (g.length == 1)
g = "0" + g;
if (b.length == 1)
b = "0" + b;
if (a.length == 1)
a = "0" + a;
setbackgroundcol( "#" + r + g + b + a);
}
function changeThemeColours(colour: string, theme: string){
sethemeCol(colour);
changeTheme(theme);
}
function getSystemTheme(){changeThemeColours("dark-blue-col-light-white-col", isDarkTheme === true ? "dark" : "light");}
useEffect(() => {
const darkThemeMq = window.matchMedia("(prefers-color-scheme: dark)");
darkThemeMq.addEventListener('change',mqListener);
return () => darkThemeMq.removeEventListener('change', mqListener);
}, []);
useEffect(() => {
if(themeCol !== "dark-blue-col-light-white-col")return;
getSystemTheme();
}, [isDarkTheme]);
return (
<div className="themes">
<div className="top-bar-blur"><h1>Themes</h1></div>
<div className="themes-box">
<h2>App Color scheme</h2>
<div className="colour-palletes">
<div className="theme-selector">
<motion.div
className={themeCol === "dark-blue-col-light-white-col" ? "colour-circle selected-colour-circle" : "colour-circle"}
id="dark-blue-col-light-white-col"
whileHover={{scale: 1.03}}
whileTap={{scale: 0.97}}
onClick={getSystemTheme}>
</motion.div>
<h3>System Theme</h3>
</div>
<div className="theme-selector">
<motion.div
className={themeCol === "light-white-col" ? "colour-circle selected-colour-circle" : "colour-circle"}
id="light-white-col"
whileHover={{scale: 1.03}}
whileTap={{scale: 0.97}}
onClick={() => changeThemeColours("light-white-col", "light")}>
</motion.div>
<h3>Light Theme</h3>
</div>
<div className="theme-selector">
<motion.div
className={themeCol === "dark-blue-col" ? "colour-circle selected-colour-circle" : "colour-circle"}
id="dark-blue-col"
whileHover={{scale: 1.03}}
whileTap={{scale: 0.97}}
onClick={() => changeThemeColours("dark-blue-col", "dark")}>
</motion.div>
<h3>Dark Theme</h3>
</div>
<div className="theme-selector">
<motion.div
className={themeCol === "light-pink-col" ? "colour-circle selected-colour-circle" : "colour-circle"}
id="light-pink-col"
whileHover={{scale: 1.03}}
whileTap={{scale: 0.97}}
onClick={() => changeThemeColours("light-pink-col", "lpink")}>
</motion.div>
<h3>Light pink theme</h3>
</div>
<div className="theme-selector">
<motion.div
className={themeCol === "dark-pink-col" ? "colour-circle selected-colour-circle" : "colour-circle"}
id="dark-pink-col"
whileHover={{scale: 1.03}}
whileTap={{scale: 0.97}}
onClick={() => changeThemeColours("dark-pink-col", "dpink")}>
</motion.div>
<h3>Dark pink theme</h3>
</div>
<div className="theme-selector">
<motion.div
className={themeCol === "light-red-col" ? "colour-circle selected-colour-circle" : "colour-circle"}
id="light-red-col"
whileHover={{scale: 1.03}}
whileTap={{scale: 0.97}}
onClick={() => changeThemeColours("light-red-col", "lred")}>
</motion.div>
<h3>Light red theme</h3>
</div>
<div className="theme-selector">
<motion.div
className={themeCol === "dark-red-col" ? "colour-circle selected-colour-circle" : "colour-circle"}
id="dark-red-col"
whileHover={{scale: 1.03}}
whileTap={{scale: 0.97}}
onClick={() => changeThemeColours("dark-red-col", "dred")}>
</motion.div>
<h3>Dark red theme</h3>
</div>
<div className="theme-selector">
<motion.div
className={themeCol === "light-green-col" ? "colour-circle selected-colour-circle" : "colour-circle"}
id="light-green-col"
whileHover={{scale: 1.03}}
whileTap={{scale: 0.97}}
onClick={() => changeThemeColours("light-green-col", "lgreen")}>
</motion.div>
<h3>Light green theme</h3>
</div>
<div className="theme-selector">
<motion.div
className={themeCol === "dark-green-col" ? "colour-circle selected-colour-circle" : "colour-circle"}
id="dark-green-col"
whileHover={{scale: 1.03}}
whileTap={{scale: 0.97}}
onClick={() => changeThemeColours("dark-green-col", "dgreen")}>
</motion.div>
<h3>Dark green theme</h3>
</div>
</div>
<div className="currently-selected">
<div className="currently-selected-shape">
{chatbackgroundimg ? <img src={chatbackgroundimg} alt="background-img" className="background-img"/> :
<div className="selected-colour" style={{background: chatbackgroundcol}}></div>}
</div>
<h3>Currently selected chat background colour/image</h3>
</div>
<motion.label className="upload-btn-container" whileTap={{scale: 0.97}}>
<input name="background-img" type="file" accept="image/png, image/jpeg" onChange={uploadImg}/>
upload chat wallpaper
</motion.label>
<h4>Colour picker</h4>
<div className="colour-picker-holder">
<RgbaColorPicker color={RGBACP_color} onChange={(colour: RgbaColor) => convertRGBAtoHex(colour)} />
</div>
</div>
</div>
)
}
export default Themes