2022-12-14 18:35:27 +00:00
|
|
|
import { useUpdateTheme } from "../pages/_app";
|
2022-12-17 23:40:24 +00:00
|
|
|
import { useContext, useState } from 'react';
|
2022-12-15 20:23:54 +00:00
|
|
|
import { ThemeContext, DefaultTheme } from "styled-components";
|
2022-12-17 23:40:24 +00:00
|
|
|
import { darkTheme, lightTheme } from './themes';
|
2022-12-15 20:23:54 +00:00
|
|
|
import { ThemeDropDown, ThemeDropDownButton, ThemeDropDownOption, ThemeDropDownOptions } from "./styles/themedropdown";
|
2022-12-17 23:40:24 +00:00
|
|
|
import Themes from '../public/data/themes.json';
|
2022-12-14 18:35:27 +00:00
|
|
|
|
2022-12-15 21:33:40 +00:00
|
|
|
export const StyleSelector = () => {
|
2022-12-18 03:56:51 +00:00
|
|
|
const themes: DefaultTheme[] = Themes.themes;
|
2022-12-14 18:35:27 +00:00
|
|
|
const updateTheme = useUpdateTheme();
|
|
|
|
const currentTheme = useContext(ThemeContext);
|
2022-12-15 20:23:54 +00:00
|
|
|
const [selectedTheme, setSelectedTheme] = useState(themes[currentTheme.themeId]);
|
2022-12-14 18:35:27 +00:00
|
|
|
|
2022-12-15 22:41:48 +00:00
|
|
|
if(currentTheme !== selectedTheme) {
|
|
|
|
setSelectedTheme(currentTheme);
|
|
|
|
}
|
|
|
|
|
2022-12-15 20:23:54 +00:00
|
|
|
const updateThemeWithStorage = (newTheme: DefaultTheme) => {
|
|
|
|
if (newTheme.themeId === lightTheme.themeId) {
|
|
|
|
updateLightTheme(newTheme);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setSelectedTheme(newTheme);
|
|
|
|
localStorage.setItem("theme", newTheme.themeId.toString());
|
|
|
|
updateTheme(newTheme);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateLightTheme = (newTheme: DefaultTheme) => {
|
|
|
|
if (confirm("Really switch to Light Mode?")) {
|
|
|
|
setSelectedTheme(newTheme);
|
|
|
|
localStorage.setItem("theme", newTheme.themeId.toString());
|
|
|
|
updateTheme(newTheme)
|
|
|
|
}
|
2022-12-14 18:35:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-17 23:40:24 +00:00
|
|
|
const [visible, setVisible] = useState(false);
|
2022-12-18 03:56:51 +00:00
|
|
|
const [buttonFocus, setButtonFocus] = useState(visible);
|
2022-12-15 20:23:54 +00:00
|
|
|
|
2022-12-16 21:58:46 +00:00
|
|
|
function handleBlur(event:any) {
|
|
|
|
if (!event.currentTarget.contains(event.relatedTarget)) {
|
2022-12-18 03:56:51 +00:00
|
|
|
setButtonFocus(false);
|
2022-12-17 23:40:24 +00:00
|
|
|
setVisible(false);
|
2022-12-16 21:58:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 18:35:27 +00:00
|
|
|
return (
|
2022-12-16 21:58:46 +00:00
|
|
|
<ThemeDropDown onBlur={(event) => handleBlur(event)}>
|
2022-12-18 03:56:51 +00:00
|
|
|
<ThemeDropDownButton focus={+buttonFocus} show={+visible} onFocus={() => setButtonFocus(true)} onClick={() => setVisible(visible => !visible)}>{selectedTheme.themeName}
|
|
|
|
</ThemeDropDownButton>
|
2022-12-17 23:40:24 +00:00
|
|
|
<ThemeDropDownOptions id="themesDropdown" show={+visible}>
|
2022-12-15 20:23:54 +00:00
|
|
|
{themes.map((theme) => (
|
2022-12-17 23:40:24 +00:00
|
|
|
<ThemeDropDownOption active={theme.themeId === selectedTheme.themeId ? 1 : 0} key={theme.themeId} onClick={() => updateThemeWithStorage(theme)}>
|
2022-12-15 20:23:54 +00:00
|
|
|
{theme.themeName}
|
|
|
|
</ThemeDropDownOption>
|
|
|
|
))}
|
|
|
|
</ThemeDropDownOptions>
|
|
|
|
</ThemeDropDown>
|
2022-12-14 18:35:27 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-15 21:33:40 +00:00
|
|
|
export const StyleSelectorPlaceholder = () => {
|
|
|
|
return (
|
|
|
|
<ThemeDropDown></ThemeDropDown>
|
|
|
|
)
|
|
|
|
}
|
2022-12-15 20:23:54 +00:00
|
|
|
|
2022-12-17 23:40:24 +00:00
|
|
|
export function getTheme(themeId: number, themes: DefaultTheme[]): DefaultTheme {
|
|
|
|
let retTheme: DefaultTheme = darkTheme;
|
2022-12-14 18:35:27 +00:00
|
|
|
|
2022-12-17 23:40:24 +00:00
|
|
|
themes.forEach((theme) => {
|
|
|
|
if (theme.themeId === themeId) { retTheme = theme};
|
|
|
|
})
|
2022-12-14 18:35:27 +00:00
|
|
|
|
2022-12-17 23:40:24 +00:00
|
|
|
return retTheme;
|
2022-12-14 18:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default StyleSelector;
|