fix: use inline styles for theme switching instead of relying on dark class
This commit is contained in:
1 parent
a5a03792f6
commit
03f2b1670f
2 files changed
+57
-17
No files matched your search
+39
-11
@@ -50,28 +50,56 @@ export default function RootLayout({
|
|||||||
return (
|
return (
|
||||||
<html lang="en" suppressHydrationWarning={true}>
|
<html lang="en" suppressHydrationWarning={true}>
|
||||||
<head>
|
<head>
|
||||||
|
<style>{`
|
||||||
|
:root {
|
||||||
|
--bg-primary: #f8fafc;
|
||||||
|
--bg-secondary: #f1f5f9;
|
||||||
|
--text-primary: #1e293b;
|
||||||
|
--text-secondary: #64748b;
|
||||||
|
--border: #e2e8f0;
|
||||||
|
}
|
||||||
|
|
||||||
|
html.dark-mode {
|
||||||
|
--bg-primary: #0f172a;
|
||||||
|
--bg-secondary: #1e293b;
|
||||||
|
--text-primary: #f1f5f9;
|
||||||
|
--text-secondary: #cbd5e1;
|
||||||
|
--border: #334155;
|
||||||
|
background-color: #0f172a !important;
|
||||||
|
color: #f1f5f9 !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
html {
|
||||||
|
background-color: #f8fafc;
|
||||||
|
color: #1e293b;
|
||||||
|
}
|
||||||
|
`}</style>
|
||||||
<script dangerouslySetInnerHTML={{
|
<script dangerouslySetInnerHTML={{
|
||||||
__html: `
|
__html: `
|
||||||
(function() {
|
(function() {
|
||||||
const theme = localStorage.getItem('theme-mode') || 'dark';
|
const theme = localStorage.getItem('theme-mode') || 'dark';
|
||||||
const html = document.documentElement;
|
const html = document.documentElement;
|
||||||
|
|
||||||
if (theme === 'light') {
|
function applyTheme(t) {
|
||||||
|
if (t === 'dark') {
|
||||||
|
html.classList.add('dark-mode');
|
||||||
|
html.classList.add('dark');
|
||||||
|
html.style.colorScheme = 'dark';
|
||||||
|
document.body.style.backgroundColor = '#0f172a';
|
||||||
|
document.body.style.color = '#f1f5f9';
|
||||||
|
} else if (t === 'light') {
|
||||||
|
html.classList.remove('dark-mode');
|
||||||
html.classList.remove('dark');
|
html.classList.remove('dark');
|
||||||
html.style.colorScheme = 'light';
|
html.style.colorScheme = 'light';
|
||||||
} else if (theme === 'system') {
|
document.body.style.backgroundColor = '#f8fafc';
|
||||||
|
document.body.style.color = '#1e293b';
|
||||||
|
} else if (t === 'system') {
|
||||||
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||||
if (prefersDark) {
|
applyTheme(prefersDark ? 'dark' : 'light');
|
||||||
html.classList.add('dark');
|
|
||||||
html.style.colorScheme = 'dark';
|
|
||||||
} else {
|
|
||||||
html.classList.remove('dark');
|
|
||||||
html.style.colorScheme = 'light';
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
html.classList.add('dark');
|
|
||||||
html.style.colorScheme = 'dark';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
applyTheme(theme);
|
||||||
})();
|
})();
|
||||||
`
|
`
|
||||||
}} />
|
}} />
|
||||||
|
|||||||
@@ -25,29 +25,41 @@ export default function SettingsPage({ userEmail }: SettingsPageProps) {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleThemeChange = (newTheme: string) => {
|
const handleThemeChange = (newTheme: string) => {
|
||||||
console.log('Changing theme to:', newTheme);
|
|
||||||
|
|
||||||
setCurrentTheme(newTheme);
|
setCurrentTheme(newTheme);
|
||||||
localStorage.setItem('theme-mode', newTheme);
|
localStorage.setItem('theme-mode', newTheme);
|
||||||
|
|
||||||
const html = document.documentElement;
|
const html = document.documentElement;
|
||||||
|
const body = document.body;
|
||||||
|
|
||||||
if (newTheme === 'dark') {
|
if (newTheme === 'dark') {
|
||||||
html.classList.add('dark');
|
html.classList.add('dark-mode', 'dark');
|
||||||
html.style.colorScheme = 'dark';
|
html.style.colorScheme = 'dark';
|
||||||
|
body.style.backgroundColor = '#0f172a';
|
||||||
|
body.style.color = '#f1f5f9';
|
||||||
|
document.querySelectorAll('[class*="bg-"]').forEach(el => {
|
||||||
|
if (el.classList.toString().includes('bg-slate') || el.classList.toString().includes('bg-white')) {
|
||||||
|
el.style.backgroundColor = '';
|
||||||
|
}
|
||||||
|
});
|
||||||
toast.success('🌙 Switched to dark theme');
|
toast.success('🌙 Switched to dark theme');
|
||||||
} else if (newTheme === 'light') {
|
} else if (newTheme === 'light') {
|
||||||
html.classList.remove('dark');
|
html.classList.remove('dark-mode', 'dark');
|
||||||
html.style.colorScheme = 'light';
|
html.style.colorScheme = 'light';
|
||||||
|
body.style.backgroundColor = '#f8fafc';
|
||||||
|
body.style.color = '#1e293b';
|
||||||
toast.success('☀️ Switched to light theme');
|
toast.success('☀️ Switched to light theme');
|
||||||
} else if (newTheme === 'system') {
|
} else if (newTheme === 'system') {
|
||||||
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||||
if (prefersDark) {
|
if (prefersDark) {
|
||||||
html.classList.add('dark');
|
html.classList.add('dark-mode', 'dark');
|
||||||
html.style.colorScheme = 'dark';
|
html.style.colorScheme = 'dark';
|
||||||
|
body.style.backgroundColor = '#0f172a';
|
||||||
|
body.style.color = '#f1f5f9';
|
||||||
} else {
|
} else {
|
||||||
html.classList.remove('dark');
|
html.classList.remove('dark-mode', 'dark');
|
||||||
html.style.colorScheme = 'light';
|
html.style.colorScheme = 'light';
|
||||||
|
body.style.backgroundColor = '#f8fafc';
|
||||||
|
body.style.color = '#1e293b';
|
||||||
}
|
}
|
||||||
toast.success('🔄 Using system preference');
|
toast.success('🔄 Using system preference');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user