1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/frontend/src/hooks/useThemeMode.ts

39 lines
1.0 KiB
TypeScript
Raw Normal View History

import UIContext, { themeMode } from 'contexts/UIContext';
import { useContext } from 'react';
import { setLocalStorageItem } from 'utils/storage';
import mainTheme from 'themes/theme';
import darkTheme from 'themes/dark-theme';
import { Theme } from '@emotion/react';
interface IUseThemeModeOutput {
resolveTheme: () => Theme;
onSetThemeMode: () => void;
themeMode: themeMode;
}
export const useThemeMode = (): IUseThemeModeOutput => {
const { themeMode, setThemeMode } = useContext(UIContext);
const key = 'unleash-theme';
const resolveTheme = () => {
if (themeMode === 'light') {
return mainTheme;
}
return darkTheme;
};
const onSetThemeMode = () => {
setThemeMode((prev: themeMode) => {
if (prev === 'light') {
setLocalStorageItem(key, 'dark');
return 'dark';
}
setLocalStorageItem(key, 'light');
return 'light';
});
};
return { resolveTheme, onSetThemeMode, themeMode };
};