Files
Homarr/src/pages/_app.tsx

73 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-04-24 22:36:47 +02:00
import { GetServerSidePropsContext } from 'next';
import { useState } from 'react';
import { AppProps } from 'next/app';
import { getCookie, setCookies } from 'cookies-next';
import Head from 'next/head';
import { MantineProvider, ColorScheme, ColorSchemeProvider, MantineTheme } from '@mantine/core';
2022-04-24 22:36:47 +02:00
import { NotificationsProvider } from '@mantine/notifications';
import { useHotkeys } from '@mantine/hooks';
import { ConfigProvider } from '../tools/state';
import { theme } from '../tools/theme';
2022-06-01 19:53:57 +02:00
import { styles } from '../tools/styles';
import { ColorTheme } from '../tools/color';
2022-04-24 22:36:47 +02:00
export default function App(this: any, props: AppProps & { colorScheme: ColorScheme }) {
2022-04-24 22:36:47 +02:00
const { Component, pageProps } = props;
const [colorScheme, setColorScheme] = useState<ColorScheme>(props.colorScheme);
2022-04-24 22:36:47 +02:00
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-04-24 22:36:47 +02:00
const toggleColorScheme = (value?: ColorScheme) => {
const nextColorScheme = value || (colorScheme === 'dark' ? 'light' : 'dark');
setColorScheme(nextColorScheme);
setCookies('color-scheme', nextColorScheme, { maxAge: 60 * 60 * 24 * 30 });
2022-04-24 22:36:47 +02:00
};
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>
<ColorSchemeProvider colorScheme={colorScheme} toggleColorScheme={toggleColorScheme}>
<ColorTheme.Provider value={colorTheme}>
<MantineProvider
theme={{
...theme,
primaryColor,
primaryShade,
colorScheme,
}}
styles={{
...styles,
}}
withGlobalStyles
withNormalizeCSS
>
<NotificationsProvider limit={4} position="bottom-left">
<ConfigProvider>
<Component {...pageProps} />
</ConfigProvider>
</NotificationsProvider>
</MantineProvider>
</ColorTheme.Provider>
2022-04-24 22:36:47 +02:00
</ColorSchemeProvider>
</>
);
}
App.getInitialProps = ({ ctx }: { ctx: GetServerSidePropsContext }) => ({
colorScheme: getCookie('color-scheme', ctx) || 'light',
2022-04-24 22:36:47 +02:00
});