main-site/components/themeselector.tsx

99 lines
3.6 KiB
TypeScript
Raw Permalink Normal View History

import { useUpdateTheme } from "../pages/_app";
import { useContext, useState } from 'react';
2022-12-15 20:23:54 +00:00
import { ThemeContext, DefaultTheme } from "styled-components";
import { darkTheme, lightTheme } from './themes';
2022-12-19 20:03:08 +00:00
import { ThemeDropDown, ThemeDropDownButton, ThemeDropDownOption, ThemeDropDownOptions } from "./styles/themedropdown/desktop";
import { ThemeDropDownMobile, ThemeDropDownButtonMobile, ThemeDropDownOptionMobile, ThemeDropDownOptionsMobile } from "./styles/themedropdown/mobile";
import Themes from '../public/data/themes.json';
2022-12-19 20:03:08 +00:00
export const StyleSelector = ({ mobile }: { mobile: number }) => {
const themes: DefaultTheme[] = Themes.themes;
const updateTheme = useUpdateTheme();
const currentTheme = useContext(ThemeContext);
2022-12-15 20:23:54 +00:00
const [selectedTheme, setSelectedTheme] = useState(themes[currentTheme.themeId]);
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)
}
}
const [visible, setVisible] = useState(false);
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)) {
setButtonFocus(false);
setVisible(false);
2022-12-16 21:58:46 +00:00
}
}
2022-12-19 20:03:08 +00:00
let themeselector: JSX.Element;
if(mobile) {
themeselector = (
<ThemeDropDownMobile onBlur={(event) => handleBlur(event)}>
<ThemeDropDownButtonMobile focus={+buttonFocus} show={+visible} onFocus={() => setButtonFocus(true)} onClick={() => setVisible(visible => !visible)}>
{selectedTheme.themeName}
</ThemeDropDownButtonMobile>
<ThemeDropDownOptionsMobile focus={+buttonFocus} id="themesDropdown" show={+visible}>
{themes.map((theme) => (
<ThemeDropDownOptionMobile active={theme.themeId === selectedTheme.themeId ? 1 : 0} key={theme.themeId} onClick={() => updateThemeWithStorage(theme)}>
{theme.themeName}
</ThemeDropDownOptionMobile>
))}
</ThemeDropDownOptionsMobile>
</ThemeDropDownMobile>
);
}
else {
themeselector = (
<ThemeDropDown onBlur={(event) => handleBlur(event)}>
<ThemeDropDownButton focus={+buttonFocus} show={+visible} onFocus={() => setButtonFocus(true)} onClick={() => setVisible(visible => !visible)}>{selectedTheme.themeName}
</ThemeDropDownButton>
<ThemeDropDownOptions id="themesDropdown" show={+visible}>
{themes.map((theme) => (
<ThemeDropDownOption active={theme.themeId === selectedTheme.themeId ? 1 : 0} key={theme.themeId} onClick={() => updateThemeWithStorage(theme)}>
{theme.themeName}
</ThemeDropDownOption>
))}
</ThemeDropDownOptions>
</ThemeDropDown>
);
}
return themeselector;
}
export const StyleSelectorPlaceholder = () => {
return (
<ThemeDropDown></ThemeDropDown>
)
}
2022-12-15 20:23:54 +00:00
export function getTheme(themeId: number, themes: DefaultTheme[]): DefaultTheme {
let retTheme: DefaultTheme = darkTheme;
themes.forEach((theme) => {
if (theme.themeId === themeId) { retTheme = theme};
})
return retTheme;
}
export default StyleSelector;