1
- import { createTheme , responsiveFontSizes } from "@mui/material" ;
1
+ import { createTheme , responsiveFontSizes , ThemeOptions } from "@mui/material" ;
2
2
import merge from "lodash/merge" ;
3
3
4
4
import components from "./components" ;
@@ -16,7 +16,7 @@ export const initialSettings: themeSettingsTypes = {
16
16
responsiveFontSizes : true ,
17
17
} ;
18
18
19
- const baseOptions = {
19
+ const baseOptions : ThemeOptions = {
20
20
typography : {
21
21
fontFamily : "'Montserrat', sans-serif" ,
22
22
h1 : {
@@ -56,20 +56,32 @@ const baseOptions = {
56
56
} ;
57
57
58
58
export const createCustomTheme = ( settings : themeSettingsTypes ) => {
59
- let themeOptions : object = themesOptions [ settings . theme ] ;
59
+ // Валидируем настройки
60
+ if ( ! settings || typeof settings . theme !== "string" ) {
61
+ settings = { ...initialSettings } ;
62
+ }
63
+
64
+ let themeOptions : ThemeOptions = themesOptions [
65
+ settings . theme
66
+ ] as unknown as ThemeOptions ;
60
67
61
68
if ( ! themeOptions ) {
62
- themeOptions = themesOptions [ THEMES . LIGHT ] ;
69
+ themeOptions = themesOptions [ THEMES . LIGHT ] as unknown as ThemeOptions ;
63
70
}
64
71
65
- const mergedThemeOptions = merge ( { } , baseOptions , themeOptions ) ;
72
+ try {
73
+ const mergedThemeOptions = merge ( { } , baseOptions , themeOptions ) ;
66
74
67
- let theme = createTheme ( mergedThemeOptions ) ;
75
+ let theme = createTheme ( mergedThemeOptions ) ;
68
76
69
- theme . components = components ( theme ) ;
70
- if ( settings . responsiveFontSizes ) {
71
- theme = responsiveFontSizes ( theme ) ;
72
- }
77
+ theme . components = components ( theme ) ;
78
+ if ( settings . responsiveFontSizes !== false ) {
79
+ theme = responsiveFontSizes ( theme ) ;
80
+ }
73
81
74
- return theme ;
82
+ return theme ;
83
+ } catch ( error ) {
84
+ // Возвращаем базовую светлую тему в случае ошибки
85
+ return createTheme ( themesOptions [ THEMES . LIGHT ] as unknown as ThemeOptions ) ;
86
+ }
75
87
} ;
0 commit comments