Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent dark mode flash on page refresh #182

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions hooks/useTheme.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
import { useState, useEffect } from 'react';

export type ThemeOption = 'system' | 'light' | 'dark';
const applyTheme = (theme: 'light' | 'dark') => {
if (theme === 'light') {
window.document.documentElement.classList.remove('darkmode');
window.document.documentElement.style.colorScheme = 'light';
} else {
window.document.documentElement.classList.add('darkmode');
window.document.documentElement.style.colorScheme = 'dark';
}
};
export function useTheme(initialTheme?: ThemeOption) {
const [theme, setTheme] = useState<ThemeOption | undefined>(initialTheme);

useEffect(() => {
const mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)');
const handleSystemThemeChange = (e: MediaQueryListEvent) => {
if (e.matches) {
window.document.body.classList.add('darkmode');
} else {
window.document.body.classList.remove('darkmode');
}
applyTheme(e.matches ? 'dark' : 'light');
};

if (theme === 'dark') {
window.document.body.classList.add('darkmode');
applyTheme('dark');
window.localStorage.setItem('theme', 'dark');
} else if (theme === 'light') {
window.document.body.classList.remove('darkmode');
applyTheme('light');
window.localStorage.setItem('theme', 'light');
} else if (theme === 'system') {
window.localStorage.setItem('theme', 'system');
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
window.document.body.classList.add('darkmode');
}
applyTheme(mediaQueryList.matches ? 'dark' : 'light');
mediaQueryList.addEventListener('change', handleSystemThemeChange);
}
return () => {
Expand Down
27 changes: 27 additions & 0 deletions pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Html, Head, Main, NextScript } from 'next/document';
import Script from 'next/script';

export default function Document() {
return (
<Html>
<Head />
<body>
<Script id="initial-theme" strategy="beforeInteractive">
{`
(function () {
const theme = window.localStorage.getItem('theme') || 'system';
const isDarkMode = theme === 'dark' ||
(theme === 'system' && window.matchMedia('(prefers-color-scheme: dark)').matches);
if (isDarkMode) {
window.document.documentElement.classList.add('darkmode');
window.document.documentElement.style.colorScheme = 'dark';
}
})();
`}
</Script>
<Main />
<NextScript />
</body>
</Html>
);
}
4 changes: 4 additions & 0 deletions styles/base/_common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ a {
@extend %visuallyhidden;
}

html {
background-color: var(--gray-000);
}

// color
$palette: (
'gray': (
Expand Down
Loading