ConceptPortal-public/rsconcept/frontend/src/context/ThemeContext.tsx

142 lines
3.8 KiB
TypeScript
Raw Normal View History

'use client';
2023-12-21 00:12:24 +03:00
import clsx from 'clsx';
import { createContext, useCallback, useContext, useLayoutEffect, useMemo, useState } from 'react';
2023-07-15 17:46:19 +03:00
import Tooltip from '@/components/ui/Tooltip';
import useLocalStorage from '@/hooks/useLocalStorage';
2024-03-26 12:49:38 +03:00
import { FontStyle } from '@/models/miscellaneous';
import { animationDuration } from '@/styling/animations';
import { darkT, IColorTheme, lightT } from '@/styling/color';
import { globals, storage } from '@/utils/constants';
2023-07-15 17:46:19 +03:00
interface IThemeContext {
2023-12-28 14:04:44 +03:00
viewportHeight: string;
mainHeight: string;
colors: IColorTheme;
darkMode: boolean;
toggleDarkMode: () => void;
2024-03-26 12:49:38 +03:00
mathFont: FontStyle;
setMathFont: (value: FontStyle) => void;
2023-12-28 14:04:44 +03:00
noNavigationAnimation: boolean;
noNavigation: boolean;
toggleNoNavigation: () => void;
noFooter: boolean;
setNoFooter: (value: boolean) => void;
showScroll: boolean;
setShowScroll: (value: boolean) => void;
2024-03-12 19:54:10 +03:00
calculateHeight: (offset: string) => string;
2023-07-15 17:46:19 +03:00
}
const ThemeContext = createContext<IThemeContext | null>(null);
export const useConceptTheme = () => {
const context = useContext(ThemeContext);
if (!context) {
2023-08-27 15:39:49 +03:00
throw new Error('useConceptTheme has to be used within <ThemeState.Provider>');
}
return context;
2023-12-28 14:04:44 +03:00
};
2023-07-15 17:46:19 +03:00
interface ThemeStateProps {
2023-12-28 14:04:44 +03:00
children: React.ReactNode;
2023-07-15 17:46:19 +03:00
}
export const ThemeState = ({ children }: ThemeStateProps) => {
const [darkMode, setDarkMode] = useLocalStorage(storage.themeDark, false);
const [mathFont, setMathFont] = useLocalStorage<FontStyle>(storage.rseditFont, 'math');
2023-08-27 00:19:19 +03:00
const [colors, setColors] = useState<IColorTheme>(lightT);
const [noNavigation, setNoNavigation] = useState(false);
const [noNavigationAnimation, setNoNavigationAnimation] = useState(false);
2023-08-27 15:39:49 +03:00
const [noFooter, setNoFooter] = useState(false);
2023-09-05 00:23:53 +03:00
const [showScroll, setShowScroll] = useState(false);
2023-07-15 17:46:19 +03:00
2023-08-27 15:39:49 +03:00
function setDarkClass(isDark: boolean) {
2023-07-15 17:46:19 +03:00
const root = window.document.documentElement;
if (isDark) {
root.classList.add('dark');
} else {
root.classList.remove('dark');
}
2023-07-20 17:11:03 +03:00
root.setAttribute('data-color-scheme', !isDark ? 'light' : 'dark');
2023-08-27 15:39:49 +03:00
}
2023-07-15 17:46:19 +03:00
2023-08-24 14:23:17 +03:00
useLayoutEffect(() => {
setDarkClass(darkMode);
2023-07-15 17:46:19 +03:00
}, [darkMode]);
2023-08-27 00:19:19 +03:00
useLayoutEffect(() => {
2023-12-28 14:04:44 +03:00
setColors(darkMode ? darkT : lightT);
2023-08-27 00:19:19 +03:00
}, [darkMode, setColors]);
2023-12-28 14:04:44 +03:00
const toggleNoNavigation = useCallback(() => {
if (noNavigation) {
setNoNavigationAnimation(false);
setNoNavigation(false);
} else {
setNoNavigationAnimation(true);
setTimeout(() => setNoNavigation(true), animationDuration.navigationToggle);
}
}, [noNavigation]);
2024-03-12 19:54:10 +03:00
const calculateHeight = useCallback(
(offset: string) => {
if (noNavigation) {
return `calc(100vh - (${offset}))`;
} else if (noFooter) {
return `calc(100vh - 3rem - (${offset}))`;
} else {
return `calc(100vh - 6.75rem - (${offset}))`;
}
},
[noNavigation, noFooter]
);
2023-12-28 14:04:44 +03:00
const mainHeight = useMemo(() => {
2024-03-12 19:54:10 +03:00
return !noNavigation ? 'calc(100vh - 6.75rem)' : '100vh';
2023-08-24 14:23:17 +03:00
}, [noNavigation]);
2023-12-28 14:04:44 +03:00
const viewportHeight = useMemo(() => {
2024-03-12 19:54:10 +03:00
return !noNavigation ? 'calc(100vh - 3rem)' : '100vh';
2023-08-24 14:23:17 +03:00
}, [noNavigation]);
2023-07-15 17:46:19 +03:00
return (
2023-12-28 14:04:44 +03:00
<ThemeContext.Provider
value={{
darkMode,
colors,
2024-03-26 12:49:38 +03:00
mathFont,
setMathFont,
2023-12-28 14:04:44 +03:00
noNavigationAnimation,
noNavigation,
noFooter,
showScroll,
toggleDarkMode: () => setDarkMode(prev => !prev),
toggleNoNavigation: toggleNoNavigation,
setNoFooter,
setShowScroll,
viewportHeight,
2024-03-12 19:54:10 +03:00
mainHeight,
calculateHeight
2023-12-28 14:04:44 +03:00
}}
>
<>
<Tooltip
2023-12-28 14:04:44 +03:00
float
id={`${globals.tooltip}`}
2023-12-28 14:04:44 +03:00
layer='z-topmost'
place='right-start'
className={clsx('mt-3 translate-y-1/2', 'max-w-[20rem]')}
/>
{children}
</>
</ThemeContext.Provider>
);
};