-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[examples] Add dark mode example for Material UI + Pigment CSS (#44480)
- Loading branch information
Showing
7 changed files
with
110 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 9 additions & 11 deletions
20
examples/material-ui-pigment-css-nextjs-ts/src/app/layout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,26 @@ | ||
import * as React from 'react'; | ||
import type { Metadata } from 'next'; | ||
import { Roboto } from 'next/font/google'; | ||
import { cookies } from 'next/headers'; | ||
import '@mui/material-pigment-css/styles.css'; | ||
|
||
const roboto = Roboto({ | ||
subsets: ['latin'], | ||
weight: ['400', '500', '700'], | ||
display: 'swap', | ||
variable: '--font-roboto', | ||
}); | ||
import { ColorSchemeProvider } from '../components/ColorSchemeProvider'; | ||
import App from '../components/App'; | ||
|
||
export const metadata: Metadata = { | ||
title: 'Material UI x Pigment CSS', | ||
description: 'Generated by create next app', | ||
}; | ||
|
||
export default function RootLayout({ | ||
export default async function RootLayout({ | ||
children, | ||
}: Readonly<{ | ||
children: React.ReactNode; | ||
}>) { | ||
const cookieStore = await cookies(); | ||
const { value: colorScheme = 'light' } = cookieStore.get('colorScheme') ?? {}; | ||
return ( | ||
<html lang="en"> | ||
<body className={roboto.variable}>{children}</body> | ||
<ColorSchemeProvider colorScheme={colorScheme}> | ||
<App>{children}</App> | ||
</ColorSchemeProvider> | ||
</html> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
examples/material-ui-pigment-css-nextjs-ts/src/components/App.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { Roboto } from 'next/font/google'; | ||
import { useColorScheme } from './ColorSchemeProvider'; | ||
|
||
const roboto = Roboto({ | ||
subsets: ['latin'], | ||
weight: ['400', '500', '700'], | ||
display: 'swap', | ||
variable: '--font-roboto', | ||
}); | ||
|
||
function App({ className, ...other }: React.PropsWithChildren<{ className?: string }>) { | ||
const { colorScheme } = useColorScheme(); | ||
return <body {...other} className={`${roboto.variable} ${colorScheme}`} />; | ||
} | ||
|
||
export default App; |
63 changes: 63 additions & 0 deletions
63
examples/material-ui-pigment-css-nextjs-ts/src/components/ColorSchemeProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
|
||
const ColorSchemeContext = React.createContext<{ | ||
colorScheme: string; | ||
setColorScheme: React.Dispatch<React.SetStateAction<string>>; | ||
}>({ | ||
colorScheme: 'dark', | ||
setColorScheme: () => '', | ||
}); | ||
|
||
function setCookie(name: string, value: string, days: number = 100) { | ||
let expires = ''; | ||
if (days) { | ||
const date = new Date(); | ||
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); | ||
expires = `; expires=${date.toUTCString()}`; | ||
} | ||
document.cookie = `${name}=${value || ''}${expires}; path=/`; | ||
} | ||
|
||
export function ColorSchemeProvider({ | ||
colorScheme: initialColorScheme, | ||
children, | ||
}: React.PropsWithChildren<{ colorScheme: string }>) { | ||
const [colorScheme, setColorScheme] = React.useState<string>(initialColorScheme); | ||
|
||
const contextValue = React.useMemo( | ||
() => ({ colorScheme, setColorScheme }), | ||
[colorScheme, setColorScheme], | ||
); | ||
|
||
// Set the colorScheme in localStorage | ||
React.useEffect(() => { | ||
setCookie('colorScheme', colorScheme); | ||
localStorage.setItem('colorScheme', colorScheme); | ||
}, [colorScheme]); | ||
|
||
// Handle when localStorage has changed | ||
React.useEffect(() => { | ||
const handleStorage = (event: StorageEvent) => { | ||
const value = event.newValue; | ||
if ( | ||
typeof event.key === 'string' && | ||
event.key === 'colorScheme' && | ||
typeof value === 'string' | ||
) { | ||
setColorScheme(value); | ||
} | ||
}; | ||
// For syncing color-scheme changes between iframes | ||
window.addEventListener('storage', handleStorage); | ||
return () => { | ||
window.removeEventListener('storage', handleStorage); | ||
}; | ||
}, [setColorScheme]); | ||
|
||
return <ColorSchemeContext.Provider value={contextValue}>{children}</ColorSchemeContext.Provider>; | ||
} | ||
|
||
export const useColorScheme = () => { | ||
return React.useContext(ColorSchemeContext); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,8 @@ | |
], | ||
"paths": { | ||
"@/*": ["./src/*"] | ||
} | ||
}, | ||
"target": "ES2017" | ||
}, | ||
"include": [ | ||
"next-env.d.ts", | ||
|