mirror of
https://github.com/fergalmoran/supanextail.git
synced 2025-12-22 01:10:01 +00:00
44 lines
1020 B
TypeScript
44 lines
1020 B
TypeScript
import Document, {
|
|
DocumentContext,
|
|
DocumentInitialProps,
|
|
Head,
|
|
Html,
|
|
Main,
|
|
NextScript,
|
|
} from 'next/document';
|
|
|
|
class MyDocument extends Document {
|
|
static async getInitialProps(
|
|
context_: DocumentContext
|
|
): Promise<DocumentInitialProps> {
|
|
return await Document.getInitialProps(context_);
|
|
}
|
|
|
|
render(): JSX.Element {
|
|
// This will set the initial theme, saved in localstorage
|
|
const setInitialTheme = `
|
|
function getUserPreference() {
|
|
if(window.localStorage.getItem('theme')) {
|
|
return window.localStorage.getItem('theme')
|
|
}
|
|
return window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
? 'dark'
|
|
: 'supaTheme'
|
|
}
|
|
document.body.dataset.theme = getUserPreference();
|
|
`;
|
|
return (
|
|
<Html>
|
|
<Head />
|
|
<body>
|
|
<script dangerouslySetInnerHTML={{ __html: setInitialTheme }} />
|
|
<Main />
|
|
<NextScript />
|
|
</body>
|
|
</Html>
|
|
);
|
|
}
|
|
}
|
|
|
|
export default MyDocument;
|