Files
Homarr/src/pages/_app.tsx

97 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-12-04 17:36:30 +01:00
import { ColorScheme, ColorSchemeProvider, MantineProvider, MantineTheme } from '@mantine/core';
2022-11-12 22:26:45 +09:00
import { useColorScheme, useHotkeys, useLocalStorage } from '@mantine/hooks';
2022-08-07 12:14:37 +02:00
import { ModalsProvider } from '@mantine/modals';
2022-12-04 17:36:30 +01:00
import { NotificationsProvider } from '@mantine/notifications';
2022-08-26 16:12:40 +02:00
import { QueryClientProvider } from '@tanstack/react-query';
2022-12-04 17:36:30 +01:00
import { getCookie } from 'cookies-next';
import { GetServerSidePropsContext } from 'next';
import { appWithTranslation } from 'next-i18next';
import { AppProps } from 'next/app';
import Head from 'next/head';
import { useState } from 'react';
import { ConfigProvider } from '../config/provider';
import { ColorTheme } from '../tools/color';
2022-08-26 16:12:40 +02:00
import { queryClient } from '../tools/queryClient';
2022-12-04 17:36:30 +01:00
import { theme } from '../tools/theme';
2022-04-24 22:36:47 +02:00
2022-08-22 09:50:54 +02:00
function App(this: any, props: AppProps & { colorScheme: ColorScheme }) {
2022-04-24 22:36:47 +02:00
const { Component, pageProps } = props;
const [primaryColor, setPrimaryColor] = useState<MantineTheme['primaryColor']>('red');
const [secondaryColor, setSecondaryColor] = useState<MantineTheme['primaryColor']>('orange');
const [primaryShade, setPrimaryShade] = useState<MantineTheme['primaryShade']>(6);
const colorTheme = {
primaryColor,
secondaryColor,
setPrimaryColor,
setSecondaryColor,
primaryShade,
setPrimaryShade,
};
2022-11-12 22:26:45 +09:00
// hook will return either 'dark' or 'light' on client
// and always 'light' during ssr as window.matchMedia is not available
const preferredColorScheme = useColorScheme();
const [colorScheme, setColorScheme] = useLocalStorage<ColorScheme>({
key: 'mantine-color-scheme',
defaultValue: preferredColorScheme,
getInitialValueInEffect: true,
});
const toggleColorScheme = (value?: ColorScheme) =>
setColorScheme(value || (colorScheme === 'dark' ? 'light' : 'dark'));
2022-05-15 12:42:53 +02:00
useHotkeys([['mod+J', () => toggleColorScheme()]]);
2022-04-24 22:36:47 +02:00
return (
<>
<Head>
<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />
</Head>
2022-08-26 16:12:40 +02:00
<QueryClientProvider client={queryClient}>
<ColorSchemeProvider colorScheme={colorScheme} toggleColorScheme={toggleColorScheme}>
<ColorTheme.Provider value={colorTheme}>
<MantineProvider
theme={{
...theme,
components: {
Checkbox: {
styles: {
input: { cursor: 'pointer' },
label: { cursor: 'pointer' },
},
2022-08-08 15:43:04 +02:00
},
2022-08-26 16:12:40 +02:00
Switch: {
styles: {
input: { cursor: 'pointer' },
label: { cursor: 'pointer' },
},
2022-08-08 15:43:04 +02:00
},
},
2022-08-26 16:12:40 +02:00
primaryColor,
primaryShade,
colorScheme,
}}
withGlobalStyles
withNormalizeCSS
>
<NotificationsProvider limit={4} position="bottom-left">
<ModalsProvider>
<ConfigProvider>
<Component {...pageProps} />
</ConfigProvider>
</ModalsProvider>
</NotificationsProvider>
</MantineProvider>
</ColorTheme.Provider>
</ColorSchemeProvider>
</QueryClientProvider>
2022-04-24 22:36:47 +02:00
</>
);
}
App.getInitialProps = ({ ctx }: { ctx: GetServerSidePropsContext }) => ({
colorScheme: getCookie('color-scheme', ctx) || 'light',
2022-04-24 22:36:47 +02:00
});
2022-08-22 09:50:54 +02:00
export default appWithTranslation(App);