Skip to content

Commit

Permalink
Upgrade: MUI v5 to v6
Browse files Browse the repository at this point in the history
  • Loading branch information
sashko9807 committed Sep 29, 2024
1 parent fe28eb6 commit cfe5679
Show file tree
Hide file tree
Showing 8 changed files with 2,295 additions and 289 deletions.
2 changes: 1 addition & 1 deletion next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
"sitemap": "next-sitemap"
},
"dependencies": {
"@emotion/cache": "^11.7.1",
"@emotion/react": "^11.11.4",
"@emotion/cache": "^11.13.1",
"@emotion/react": "^11.13.3",
"@emotion/server": "^11.4.0",
"@emotion/styled": "^11.11.5",
"@emotion/styled": "^11.13.0",
"@mdxeditor/editor": "^0.14.2",
"@mui/icons-material": "^5.15.19",
"@mui/icons-material": "^6.1.1",
"@mui/lab": "^5.0.0-alpha.170",
"@mui/material": "^5.15.19",
"@mui/material": "^6.1.1",
"@mui/material-nextjs": "^6.1.1",
"@mui/styles": "^5.15.19",
"@mui/x-data-grid": "^6.16.1",
"@mui/x-date-pickers": "^6.16.1",
Expand All @@ -55,10 +56,11 @@
"formik": "2.2.9",
"i18next": "^23.5.1",
"jwt-decode": "^3.1.2",
"latest": "^0.2.0",
"lodash": "^4.17.21",
"mobx": "6.3.2",
"mobx-react": "7.2.0",
"next": "14.2.10",
"next": "^14.2.13",
"next-auth": "^4.24.5",
"next-i18next": "^14.0.3",
"nookies": "^2.5.2",
Expand Down
2 changes: 1 addition & 1 deletion src/common/createEmotionCache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import createCache from '@emotion/cache'

export default function createEmotionCache() {
return createCache({ key: 'css', prepend: true })
return createCache({ key: 'css' })
}
16 changes: 4 additions & 12 deletions src/components/client/campaigns/DonationWishes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ import React, { useMemo, useRef, useState } from 'react'

import { useTranslation } from 'next-i18next'

import {
Unstable_Grid2 as Grid2,
Stack,
Typography,
Grid,
Button,
TextField,
InputAdornment,
} from '@mui/material'
import { Grid2, Stack, Typography, Grid, Button, TextField, InputAdornment } from '@mui/material'
import SearchIcon from '@mui/icons-material/Search'
import KeyboardArrowUpIcon from '@mui/icons-material/KeyboardArrowUp'
import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown'
Expand Down Expand Up @@ -156,7 +148,7 @@ export default function DonationWishes({ campaignId, pageSize = 5 }: Props) {
key={id}
direction="row"
sx={{ p: 2, bgcolor: 'grey.100', borderRadius: theme.spacing(2) }}>
<Grid2 xs={12}>
<Grid2 size={{ xs: 12 }}>
<Stack direction="row" spacing={2}>
<Grid pt={0.7}>
<AccountCircleIcon
Expand Down Expand Up @@ -250,12 +242,12 @@ export default function DonationWishes({ campaignId, pageSize = 5 }: Props) {
</Grid2>
</Grid2>
))}
<Grid2 xs={12}>
<Grid2 size={{ xs: 12 }}>
{data?.items?.length === 0 && searchValue !== '' && (
<Typography align="center"> {t('campaign.sort.noResults')}</Typography>
)}
</Grid2>
<Grid2 xs={12}>
<Grid2 size={{ xs: 12 }}>
{numOfPages > 1 && (
<Pagination
count={numOfPages}
Expand Down
16 changes: 8 additions & 8 deletions src/components/common/navigation/AdminLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState, useCallback, useEffect, useMemo } from 'react'
import { useTheme } from '@mui/styles'
import { useTheme } from '@mui/material/styles'
import MuiDrawer from '@mui/material/Drawer'
import { styled, Theme, CSSObject } from '@mui/material/styles'
import { IconButton, List, Box, Button, Typography } from '@mui/material'
Expand Down Expand Up @@ -79,18 +79,18 @@ const DrawerHeader = styled('div')(({ theme }) => ({
}))

const Drawer = styled(MuiDrawer, { shouldForwardProp: (prop: string) => prop !== 'open' })(
({ theme, open }: { theme: Theme; open: boolean }) => ({
({ theme, open }: { theme?: Theme; open: boolean }) => ({
width: drawerWidth,
flexShrink: 0,
whiteSpace: 'nowrap',
boxSizing: 'border-box',
...(open && {
...openedMixin(theme),
'& .MuiDrawer-paper': openedMixin(theme),
...openedMixin(theme as Theme),
'& .MuiDrawer-paper': openedMixin(theme as Theme),
}),
...(!open && {
...closedMixin(theme),
'& .MuiDrawer-paper': closedMixin(theme),
...closedMixin(theme as Theme),
'& .MuiDrawer-paper': closedMixin(theme as Theme),
}),
}),
)
Expand Down Expand Up @@ -124,14 +124,14 @@ export default function AdminLayout({ children }: Props) {
const toggleMenu = useCallback(() => setOpen((open) => !open), [])
return (
<StyledBox className={classes.wrapper}>
<Drawer variant="permanent" open={open} theme={theme}>
<Drawer variant="permanent" open={open}>
<Box
sx={{
display: 'flex',
position: 'absolute',
left: 5,
top: 9,
padding: theme.spacing(0, 1),
// padding: theme.spacing(0, 1),
}}>
<Link href={routes.admin.index} passHref>
{/* A11Y TODO: Translate alt text */}
Expand Down
26 changes: 10 additions & 16 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Head from 'next/head'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'

import createEmotionCache from 'common/createEmotionCache'
import theme from 'common/theme'
import useGTM from 'common/util/useGTM'

Expand All @@ -26,27 +25,19 @@ import {
} from 'components/client/layout/NotificationSnackBar/props/global'
import { getCookieConsentValue, Cookies } from 'react-cookie-consent'
import CookieConsentPopup from 'components/common/CookieConsentPopup'

import { AppCacheProvider } from '@mui/material-nextjs/v14-pagesRouter'
// Client-side cache, shared for the whole session of the user in the browser.
const clientSideEmotionCache = createEmotionCache()

declare module '@mui/styles/defaultTheme' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface DefaultTheme extends Theme {}
}

interface CustomAppProps extends AppProps {
emotionCache?: EmotionCache
}

function CustomApp({
Component,
emotionCache = clientSideEmotionCache,
pageProps: { session, dehydratedState, ...pageProps },
}: CustomAppProps) {
function CustomApp(props: AppProps) {
const router = useRouter()
const { i18n } = useTranslation()
const { initialize, trackEvent } = useGTM()
const { Component, pageProps } = props

const handleAcceptCookie = () => {
initialize({
Expand Down Expand Up @@ -101,17 +92,20 @@ function CustomApp({
)

return (
<CacheProvider value={emotionCache}>
<AppCacheProvider {...props}>
<Head>
<title>Podkrepi.bg</title>
<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />
</Head>
<ThemeProvider theme={theme}>
{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
<CssBaseline />
<SessionProvider session={session} refetchInterval={60} refetchOnWindowFocus={true}>
<SessionProvider
session={pageProps.session}
refetchInterval={60}
refetchOnWindowFocus={true}>
<QueryClientProvider client={queryClient}>
<Hydrate state={dehydratedState}>
<Hydrate state={pageProps.dehydratedState}>
<Provider {...stores}>
<Component {...pageProps} />
<NotificationSnackBar
Expand All @@ -127,7 +121,7 @@ function CustomApp({
handleAcceptCookie={handleAcceptCookie}
handleDeclineCookie={handleDeclineCookie}
/>
</CacheProvider>
</AppCacheProvider>
)
}

Expand Down
100 changes: 24 additions & 76 deletions src/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,87 +1,35 @@
/* eslint-disable @next/next/no-document-import-in-page */
/* eslint-disable @typescript-eslint/no-explicit-any */
import React from 'react'
import Document, { Html, Head, Main, NextScript } from 'next/document'
import { Html, Head, Main, NextScript, DocumentProps } from 'next/document'

import theme from 'common/theme'
import FaviconMetadata from 'components/common/brand/FaviconMetadata'
import createEmotionServer from '@emotion/server/create-instance'
import createEmotionCache from 'common/createEmotionCache'

export default class CustomDocument extends Document {
render() {
return (
<Html>
<Head>
{/* PWA primary color */}
<meta name="theme-color" content={theme.palette.primary.main} />
import { DocumentHeadTags, documentGetInitialProps } from '@mui/material-nextjs/v14-pagesRouter'

<FaviconMetadata />
{/* Inject MUI styles first to match with the prepend: true configuration. */}
{(this.props as any).emotionStyleTags}
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
interface MyDocumentProps extends DocumentProps {
emotionStyleTags: JSX.Element[]
}

// `getInitialProps` belongs to `_document` (instead of `_app`),
// it's compatible with server-side generation (SSG).
CustomDocument.getInitialProps = async (ctx) => {
// Resolution order
//
// On the server:
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. document.getInitialProps
// 4. app.render
// 5. page.render
// 6. document.render
//
// On the server with error:
// 1. document.getInitialProps
// 2. app.render
// 3. page.render
// 4. document.render
//
// On the client
// 1. app.getInitialProps
// 2. page.getInitialProps
// 3. app.render
// 4. page.render

// Render app and page and get the context of the page with collected side effects.
const originalRenderPage = ctx.renderPage
const cache = createEmotionCache()
const { extractCriticalToChunks } = createEmotionServer(cache)

ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App: any) =>
function EnhanceApp(props) {
return <App emotionCache={cache} {...props} />
},
})

const initialProps = await Document.getInitialProps(ctx)
// This is important. It prevents emotion to render invalid HTML.
// See https://github.com/mui/material-ui/issues/26561#issuecomment-855286153
const emotionStyles = extractCriticalToChunks(initialProps.html)
const emotionStyleTags = emotionStyles.styles.map((style) => (
<style
data-emotion={`${style.key} ${style.ids.join(' ')}`}
key={style.key}
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{ __html: style.css }}
/>
))

return {
...initialProps,
emotionStyleTags,
}
export default function MyDocument(props: MyDocumentProps) {
return (
<Html>
<Head>
{/* PWA primary color */}
<meta name="theme-color" content={theme.palette.primary.main} />

<FaviconMetadata />
{/* Inject MUI styles first to match with the prepend: true configuration. */}
<meta name="emotion-insertion-point" content="" />
<DocumentHeadTags {...props} />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}

MyDocument.getInitialProps = documentGetInitialProps
Loading

0 comments on commit cfe5679

Please sign in to comment.