_app.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import PropTypes from 'prop-types';
  2. import Head from 'next/head';
  3. import { CacheProvider } from '@emotion/react';
  4. import theme from '../src/theme';
  5. import createEmotionCache from '../src/createEmotionCache';
  6. import Layout from '../components/Layout'
  7. import * as React from 'react';
  8. import App from 'next/app';
  9. import { useState } from "react";
  10. import "@fontsource/roboto";
  11. import {
  12. CssBaseline, FormGroup,
  13. } from "@mui/material";
  14. import { createTheme, ThemeProvider } from "@mui/material/styles";
  15. import Grid from "@material-ui/core/Grid";
  16. import { makeStyles } from "@material-ui/core";
  17. import { useTheme } from "@material-ui/core/styles";
  18. import useMediaQuery from "@material-ui/core/useMediaQuery";
  19. import { withTheme } from "../components/light_dark";
  20. import FormControlLabel from "@material-ui/core/FormControlLabel";
  21. import Switch from "@material-ui/core/Switch";
  22. const useStyles = makeStyles((theme) => ({
  23. root: {
  24. width: "100%",
  25. height: "100%",
  26. [theme.breakpoints.down("xs")]: {
  27. paddingTop: theme.spacing(2),
  28. },
  29. },
  30. }));
  31. const DynamicComponentWithNoSSR = <>Some JSX</>
  32. // Client-side cache, shared for the whole session of the user in the browser.
  33. const clientSideEmotionCache = createEmotionCache();
  34. function MyApp(props) {
  35. const { darkMode, setDarkMode } = props;
  36. const classes = useStyles();
  37. const Theme = useTheme();
  38. const matches = useMediaQuery(theme.breakpoints.down("xs"));
  39. const { Component, emotionCache = clientSideEmotionCache, pageProps } = props;
  40. if (typeof window !== 'undefined') {
  41. console.log('You are on the browser')
  42. } else {
  43. console.log('You are on the server')
  44. }
  45. return (
  46. <>
  47. <CacheProvider value={emotionCache}>
  48. <Head>
  49. <meta name="viewport" content="initial-scale=1, width=device-width" />
  50. </Head>
  51. <Layout>
  52. <ThemeProvider theme={Theme}>
  53. {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
  54. <CssBaseline />
  55. <Component {...pageProps} />
  56. <FormGroup>
  57. <FormControlLabel
  58. control={
  59. <Switch
  60. checked={theme}
  61. onChange={() => withTheme(!theme)} />
  62. }
  63. label="Dark Mode"
  64. />
  65. </FormGroup>
  66. </ThemeProvider>
  67. </Layout>
  68. </CacheProvider>
  69. </>
  70. );
  71. }
  72. MyApp.propTypes = {
  73. Component: PropTypes.elementType.isRequired,
  74. emotionCache: PropTypes.object,
  75. pageProps: PropTypes.object.isRequired,
  76. };
  77. export default withTheme(MyApp);