2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-08-07 04:12:11 +00:00

fix color schema switcher

This commit is contained in:
Matthias Mair
2024-04-15 22:41:23 +02:00
parent 0cf5fcfe3d
commit 6d50d09d16
4 changed files with 76 additions and 19 deletions

View File

@@ -5,11 +5,14 @@ import { vars } from '../../theme';
export function ColorToggle() {
const { colorScheme, toggleColorScheme } = useMantineColorScheme();
//const computedColorScheme = useComputedColorScheme('light', {
// getInitialValueInEffect: true
//});
return (
<Group justify="center">
<ActionIcon
onClick={() => toggleColorScheme()}
onClick={toggleColorScheme}
size="lg"
style={{
color:

View File

@@ -1,10 +1,5 @@
import { t } from '@lingui/macro';
import {
MantineProvider,
createTheme,
localStorageColorSchemeManager
} from '@mantine/core';
import { useColorScheme } from '@mantine/hooks';
import { MantineProvider, createTheme } from '@mantine/core';
import { ModalsProvider } from '@mantine/modals';
import { Notifications } from '@mantine/notifications';
@@ -13,6 +8,7 @@ import { LicenseModal } from '../components/modals/LicenseModal';
import { QrCodeModal } from '../components/modals/QrCodeModal';
import { ServerInfoModal } from '../components/modals/ServerInfoModal';
import { useLocalState } from '../states/LocalState';
import { colorSchema } from './colorSchema';
export function ThemeContext({ children }: { children: JSX.Element }) {
const [primaryColor, whiteColor, blackColor, radius] = useLocalState(
@@ -24,12 +20,6 @@ export function ThemeContext({ children }: { children: JSX.Element }) {
]
);
// Color Scheme
const preferredColorScheme = useColorScheme();
const colorSchemeManager = localStorageColorSchemeManager({
key: 'scheme'
});
// Theme
const myTheme = createTheme({
primaryColor: primaryColor,
@@ -46,11 +36,7 @@ export function ThemeContext({ children }: { children: JSX.Element }) {
});
return (
<MantineProvider
theme={myTheme}
defaultColorScheme={preferredColorScheme}
colorSchemeManager={colorSchemeManager}
>
<MantineProvider theme={myTheme} colorSchemeManager={colorSchema}>
<Notifications />
<ModalsProvider
labels={{ confirm: t`Submit`, cancel: t`Cancel` }}

View File

@@ -0,0 +1,67 @@
import {
MantineColorScheme,
MantineColorSchemeManager,
isMantineColorScheme
} from '@mantine/core';
export interface LocalStorageColorSchemeManagerOptions {
/** Local storage key used to retrieve value with `localStorage.getItem(key)`, `mantine-color-scheme` by default */
key?: string;
}
export function localStorageColorSchemeManager({
key = 'mantine-color-scheme'
}: LocalStorageColorSchemeManagerOptions = {}): MantineColorSchemeManager {
let handleStorageEvent: (event: StorageEvent) => void;
return {
get: (defaultValue) => {
if (typeof window === 'undefined') {
return defaultValue;
}
try {
return (
(window.localStorage.getItem(key) as MantineColorScheme) ||
defaultValue
);
} catch {
return defaultValue;
}
},
set: (value) => {
try {
window.localStorage.setItem(key, value);
} catch (error) {
// eslint-disable-next-line no-console
console.warn(
'[@mantine/core] Local storage color scheme manager was unable to save color scheme.',
error
);
}
},
subscribe: (onUpdate) => {
handleStorageEvent = (event) => {
if (event.storageArea === window.localStorage && event.key === key) {
isMantineColorScheme(event.newValue) && onUpdate(event.newValue);
}
};
window.addEventListener('storage', handleStorageEvent);
},
unsubscribe: () => {
window.removeEventListener('storage', handleStorageEvent);
},
clear: () => {
window.localStorage.removeItem(key);
}
};
}
export const colorSchema = localStorageColorSchemeManager({
key: 'scheme'
});

View File

@@ -1,11 +1,12 @@
import { Center, Loader, MantineProvider, Stack } from '@mantine/core';
import { Suspense } from 'react';
import { colorSchema } from '../contexts/colorSchema';
import { theme } from '../theme';
function LoadingFallback() {
return (
<MantineProvider theme={theme}>
<MantineProvider theme={theme} colorSchemeManager={colorSchema}>
<Stack>
<Center>
<Loader />