@applica-software-guru/react-admin 1.5.296 → 1.5.297

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/themes/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAUxC,OAAO,EAAE,iBAAiB,EAAW,MAAM,OAAO,CAAC;AAEnD,KAAK,uBAAuB,GAAG,iBAAiB,CAAC;IAAE,cAAc,EAAE,OAAO,CAAA;CAAE,CAAC,CAAC;AAE9E,iBAAS,0BAA0B,CAAC,EAAE,cAAc,EAAE,QAAQ,EAAE,EAAE,uBAAuB,2CAiExF;AAED,OAAO,EAAE,0BAA0B,EAAE,SAAS,IAAI,aAAa,EAAE,SAAS,IAAI,cAAc,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/themes/index.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAUxC,OAAO,EAAE,iBAAiB,EAAW,MAAM,OAAO,CAAC;AAEnD,KAAK,uBAAuB,GAAG,iBAAiB,CAAC;IAAE,cAAc,EAAE,OAAO,CAAA;CAAE,CAAC,CAAC;AAE9E,iBAAS,0BAA0B,CAAC,EAAE,cAAc,EAAE,QAAQ,EAAE,EAAE,uBAAuB,2CAqExF;AAED,OAAO,EAAE,0BAA0B,EAAE,SAAS,IAAI,aAAa,EAAE,SAAS,IAAI,cAAc,EAAE,CAAC"}
@@ -1,3 +1,4 @@
1
- declare function Palette(mode: 'light' | 'dark', presetColor: string): any;
1
+ import { ThemeMode } from '../components';
2
+ declare function Palette(mode: ThemeMode, presetColor: string): any;
2
3
  export { Palette };
3
4
  //# sourceMappingURL=palette.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"palette.d.ts","sourceRoot":"","sources":["../../../src/themes/palette.ts"],"names":[],"mappings":"AAIA,iBAAS,OAAO,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,GAAG,CA8DjE;AAED,OAAO,EAAE,OAAO,EAAE,CAAC"}
1
+ {"version":3,"file":"palette.d.ts","sourceRoot":"","sources":["../../../src/themes/palette.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAKzC,iBAAS,OAAO,CAAC,IAAI,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,GAAG,GAAG,CA8D1D;AAED,OAAO,EAAE,OAAO,EAAE,CAAC"}
package/package.json CHANGED
@@ -106,5 +106,5 @@
106
106
  "type": "module",
107
107
  "types": "dist/index.d.ts",
108
108
  "typings": "dist/index.d.ts",
109
- "version": "1.5.296"
109
+ "version": "1.5.297"
110
110
  }
@@ -1,7 +1,7 @@
1
1
  import { useLocalStorage } from '@/hooks';
2
2
  import { PropsWithChildren, createContext, useContext } from 'react';
3
3
 
4
- type ThemeMode = 'light' | 'dark';
4
+ type ThemeMode = 'light' | 'dark' | 'auto';
5
5
  type ThemeOrientation = 'vertical' | 'horizontal';
6
6
 
7
7
  type ThemeDirection = 'ltr' | 'rtl';
@@ -1,17 +1,107 @@
1
1
  import { HeaderToggleButton } from './Header';
2
- import { useThemeConfig } from '@/components/Layout/ThemeProvider';
2
+ import { ThemeMode, useThemeConfig } from '@/components/Layout/ThemeProvider';
3
+ import { usePopoverState } from '@/hooks';
3
4
  import { MoonOutlined, SunOutlined } from '@ant-design/icons';
5
+ import { Popper } from '@mui/base';
6
+ import { ClickAwayListener, List, ListItemButton, Paper, Typography, useTheme } from '@mui/material';
7
+ import { Transitions } from '@/components/@extended';
8
+ import { MainCard } from '@/components/MainCard';
9
+ import { Brightness6 } from '@mui/icons-material';
10
+ import { useCallback } from 'react';
11
+ import { useTranslate } from 'ra-core';
12
+
13
+ type ThemeModePopoverProps = {
14
+ open: boolean;
15
+ anchorEl: HTMLElement | null;
16
+ handleClose: () => void;
17
+ theme: any;
18
+ mode: ThemeMode;
19
+ toggleThemeMode: (theme: ThemeMode) => void;
20
+ };
21
+
22
+ function ThemeModePopover({ open, anchorEl, handleClose, theme, mode, toggleThemeMode }: ThemeModePopoverProps) {
23
+ const translate = useTranslate();
24
+ return (
25
+ <Popper placement="bottom-end" open={open} anchorEl={anchorEl} role={undefined} transition disablePortal>
26
+ {({ TransitionProps }) => (
27
+ <Transitions type="grow" position="top-right" in={open} {...TransitionProps}>
28
+ <Paper
29
+ sx={{
30
+ boxShadow: theme.customShadows.z1,
31
+ minWidth: 200,
32
+ [theme.breakpoints.down('md')]: { maxWidth: 250 },
33
+ marginTop: 1
34
+ }}
35
+ >
36
+ <ClickAwayListener onClickAway={handleClose}>
37
+ <MainCard elevation={0} border={false} content={false}>
38
+ <List>
39
+ <ListItemButton selected={mode === 'light'} onClick={() => toggleThemeMode('light')}>
40
+ <SunOutlined />
41
+ <Typography variant="subtitle1" ml={1}>
42
+ {translate('ra.preferences.theme.light')}
43
+ </Typography>
44
+ </ListItemButton>
45
+
46
+ <ListItemButton selected={mode === 'dark'} onClick={() => toggleThemeMode('dark')}>
47
+ <MoonOutlined />
48
+ <Typography variant="subtitle1" ml={1}>
49
+ {translate('ra.preferences.theme.dark')}
50
+ </Typography>
51
+ </ListItemButton>
52
+
53
+ <ListItemButton selected={mode === 'auto'} onClick={() => toggleThemeMode('auto')}>
54
+ <Brightness6 />
55
+ <Typography variant="subtitle1" ml={1} textTransform={'none'}>
56
+ {translate('ra.preferences.theme.auto')}
57
+ </Typography>
58
+ </ListItemButton>
59
+ </List>
60
+ </MainCard>
61
+ </ClickAwayListener>
62
+ </Paper>
63
+ </Transitions>
64
+ )}
65
+ </Popper>
66
+ );
67
+ }
4
68
 
5
69
  function ThemeToggler(): JSX.Element {
70
+ const theme = useTheme();
6
71
  const { mode, setMode } = useThemeConfig();
72
+ const { open, anchorEl, handleToggle, handleClose } = usePopoverState();
73
+
74
+ const toggleThemeMode = useCallback(
75
+ (theme: ThemeMode) => {
76
+ setMode(theme);
77
+ },
78
+ [setMode]
79
+ );
80
+
81
+ const ThemeIcon = useCallback(() => {
82
+ switch (mode) {
83
+ case 'light':
84
+ return <SunOutlined />;
85
+ case 'dark':
86
+ return <MoonOutlined />;
87
+ default:
88
+ return <Brightness6 />;
89
+ }
90
+ }, [mode]);
91
+
7
92
  return (
8
- <HeaderToggleButton
9
- value="theme-mode"
10
- aria-label="toggle mode"
11
- onChange={() => setMode(mode === 'dark' ? 'light' : 'dark')}
12
- selected={false}
13
- >
14
- {mode === 'dark' ? <SunOutlined /> : <MoonOutlined />}
93
+ <HeaderToggleButton value="theme-mode" aria-label="toggle mode" onClick={handleToggle} selected={false}>
94
+ <ThemeIcon />
95
+ {open ? (
96
+ <ThemeModePopover
97
+ open={open}
98
+ anchorEl={anchorEl}
99
+ handleClose={handleClose}
100
+ theme={theme}
101
+ mode={mode}
102
+ toggleThemeMode={toggleThemeMode}
103
+ />
104
+ ) : null}
15
105
  </HeaderToggleButton>
16
106
  );
17
107
  }
@@ -5,3 +5,4 @@ export * from './useMemoizedObject';
5
5
  export * from './usePopoverState';
6
6
  export * from './useRefDimensions';
7
7
  export * from './useResourceTitle';
8
+ export * from './useSystemTheme';
@@ -0,0 +1,40 @@
1
+ import { ThemeMode, useThemeConfig } from '@/components/Layout/ThemeProvider';
2
+ import { useEffect, useRef, useState } from 'react';
3
+
4
+ /**
5
+ * This hook is a listener for system theme changes (when automatic mode theme is enabled).
6
+ *
7
+ * @returns {ThemeMode} theme
8
+ */
9
+ function useSystemTheme(): ThemeMode {
10
+ const { mode } = useThemeConfig();
11
+ const colorSchemeMatchMedia = window.matchMedia('(prefers-color-scheme: dark)');
12
+ const [theme, setTheme] = useState<ThemeMode>(() => {
13
+ return mode === 'auto' ? (colorSchemeMatchMedia.matches ? 'dark' : 'light') : mode;
14
+ });
15
+ const previousMode = useRef(theme);
16
+
17
+ useEffect(() => {
18
+ function updateTheme() {
19
+ const themeType = colorSchemeMatchMedia.matches ? 'dark' : 'light';
20
+
21
+ if (themeType !== previousMode.current) {
22
+ setTheme(themeType);
23
+ previousMode.current = themeType;
24
+ }
25
+ }
26
+
27
+ updateTheme();
28
+
29
+ const mediaQuery = colorSchemeMatchMedia;
30
+ mediaQuery.addEventListener('change', updateTheme);
31
+
32
+ return () => {
33
+ mediaQuery.removeEventListener('change', updateTheme);
34
+ };
35
+ }, [colorSchemeMatchMedia]);
36
+
37
+ return theme;
38
+ }
39
+
40
+ export { useSystemTheme };
@@ -1,3 +1,4 @@
1
+ import { useSystemTheme } from '@/hooks';
1
2
  import { getColors } from './getColors';
2
3
  import { getShadow } from './getShadow';
3
4
  import { ComponentsOverrides } from './overrides';
@@ -16,8 +17,12 @@ type ThemeCustomizationProps = PropsWithChildren<{ themeOverrides: unknown }>;
16
17
  function ThemeCustomizationProvider({ themeOverrides, children }: ThemeCustomizationProps) {
17
18
  const _themeConfig = useThemeConfig();
18
19
  const { themeDirection, mode, presetColor, fontFamily } = _themeConfig;
20
+ const systemMode = useSystemTheme();
21
+ const paletteMode = useMemo(() => (mode === 'auto' ? systemMode : mode), [mode, systemMode]);
19
22
 
20
- const theme = useMemo(() => Palette(mode, presetColor), [mode, presetColor]);
23
+ const theme = useMemo(() => {
24
+ return Palette(paletteMode, presetColor);
25
+ }, [paletteMode, presetColor]);
21
26
 
22
27
  // eslint-disable-next-line react-hooks/exhaustive-deps
23
28
  const themeTypography = useMemo(() => Typography(fontFamily), [fontFamily]);
@@ -1,8 +1,9 @@
1
+ import { ThemeMode } from '@/components';
1
2
  import { Theme } from './theme';
2
3
  import { presetDarkPalettes, presetPalettes } from '@ant-design/colors';
3
4
  import { alpha, createTheme } from '@mui/material/styles';
4
5
 
5
- function Palette(mode: 'light' | 'dark', presetColor: string): any {
6
+ function Palette(mode: ThemeMode, presetColor: string): any {
6
7
  const colors = mode === 'dark' ? presetDarkPalettes : presetPalettes;
7
8
 
8
9
  let greyPrimary = [