chore: App theme added
This commit is contained in:
33
src/providers/ThemeProvider.tsx
Normal file
33
src/providers/ThemeProvider.tsx
Normal file
@@ -0,0 +1,33 @@
|
||||
import { useLayoutEffect, type JSX, type PropsWithChildren } from 'react';
|
||||
import { useLocalStorage } from '@/hooks/useLocalStorage';
|
||||
import type { AppTheme } from '@/types/theme';
|
||||
import { AppThemeContext } from '@/contexts/AppThemeContext';
|
||||
import { createTheme, ThemeProvider } from '@mui/material';
|
||||
import { useLangauge } from '@/hooks/useLanguage';
|
||||
|
||||
export const AppThemeProvider = (props: PropsWithChildren): JSX.Element => {
|
||||
const { language } = useLangauge();
|
||||
const [currentThemeMode, setCurrentTheme] = useLocalStorage<AppTheme>(
|
||||
'theme',
|
||||
'default',
|
||||
);
|
||||
|
||||
const muiTheme = createTheme({
|
||||
direction: language === 'fa' ? 'rtl' : 'ltr',
|
||||
});
|
||||
|
||||
useLayoutEffect(() => {
|
||||
document.body.setAttribute('theme', currentThemeMode);
|
||||
}, [currentThemeMode]);
|
||||
|
||||
return (
|
||||
<AppThemeContext.Provider
|
||||
value={{
|
||||
mode: currentThemeMode,
|
||||
changeTheme: setCurrentTheme,
|
||||
}}
|
||||
>
|
||||
<ThemeProvider theme={muiTheme}>{props.children}</ThemeProvider>
|
||||
</AppThemeContext.Provider>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user