fix: theme toggle error

- Streamlined theme toggle logic to better synchronize with user preferences and system settings.
- Updated the initialization of the theme to utilize a more robust method for determining the default mode.
- Improved accessibility by ensuring aria attributes are correctly set for the theme toggle button.
- Enhanced global CSS styles for better consistency and added support for view transitions during theme changes.
- Removed redundant code and improved overall readability of the theme toggle component.
This commit is contained in:
radishzzz 2025-01-23 10:13:30 +00:00
parent 77f94f646f
commit 419b8b5611
3 changed files with 49 additions and 82 deletions

View file

@ -1,39 +1,25 @@
<script> <script>
// Initialize theme toggle button // Initialize theme toggle button
const themeToggle = document.querySelector('button[aria-pressed]') as HTMLButtonElement const themeToggle = document.querySelector('button[aria-pressed]') as HTMLButtonElement
themeToggle.setAttribute('aria-pressed', String(document.documentElement.classList.contains('dark')))
// Set theme and sync all states // Core theme switching logic
function setTheme(isDark: boolean) { function switchTheme() {
const theme = isDark ? 'dark' : 'light' const isDark = document.documentElement.classList.toggle('dark')
document.documentElement.setAttribute('data-theme', theme)
document.documentElement.classList.toggle('dark', isDark)
themeToggle.setAttribute('aria-pressed', String(isDark)) themeToggle.setAttribute('aria-pressed', String(isDark))
localStorage.setItem('theme', theme) localStorage.setItem('theme', isDark ? 'dark' : 'light')
document.dispatchEvent(new Event('theme-changed')) document.dispatchEvent(new Event('theme-changed'))
} }
// Initialize theme: use saved preference or fallback to system preference // Handle theme toggle with view transitions API
const savedTheme = localStorage.getItem('theme')
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches
setTheme(savedTheme ? savedTheme === 'dark' : prefersDark)
// Toggle theme with view transitions API support
function toggleTheme() { function toggleTheme() {
const isDark = document.documentElement.getAttribute('data-theme') !== 'dark'
if (!document.startViewTransition) { if (!document.startViewTransition) {
setTheme(isDark) switchTheme()
return return
} }
document.startViewTransition(() => setTheme(isDark)) document.startViewTransition(switchTheme)
} }
// Sync with system theme changes only if user hasn't set a preference
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
if (!localStorage.getItem('theme')) {
setTheme(e.matches)
}
})
themeToggle.addEventListener('click', toggleTheme) themeToggle.addEventListener('click', toggleTheme)
</script> </script>

View file

@ -94,9 +94,10 @@ const { cdn, commentURL = '', imageHostURL = '', customGoogleAnalyticsURL = '',
{baidu && <meta name="baidu-site-verification" content={baidu} />} {baidu && <meta name="baidu-site-verification" content={baidu} />}
<!-- Theme Toggle --> <!-- Theme Toggle -->
<script is:inline> <script is:inline define:vars={{ defaultMode: themeConfig.color.mode }}>
const systemTheme = matchMedia('(prefers-color-scheme: dark)')
const theme = localStorage.getItem('theme') const theme = localStorage.getItem('theme')
?? (matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') ?? (systemTheme.matches !== null ? (systemTheme.matches ? 'dark' : 'light') : defaultMode)
document.documentElement.classList.toggle('dark', theme === 'dark') document.documentElement.classList.toggle('dark', theme === 'dark')
</script> </script>

View file

@ -1,90 +1,70 @@
/* Base settings */
:root { :root {
--uno-colors-primary: theme('colors.primary'); --uno-colors-primary: theme('colors.primary');
--uno-colors-secondary: theme('colors.secondary'); --uno-colors-secondary: theme('colors.secondary');
--uno-colors-background: theme('colors.background'); --uno-colors-background: theme('colors.background');
} }
html { html {
--at-apply: 'scroll-smooth antialiased text-62.5%'; --at-apply: 'scroll-smooth antialiased text-62.5%';
} }
body { body {
--at-apply: 'bg-background c-secondary text-1.6rem'; --at-apply: 'bg-background c-secondary text-1.6rem';
} }
* { * {
scrollbar-width: none; scrollbar-width: none;
-ms-overflow-style: none; -ms-overflow-style: none;
&::-webkit-scrollbar { &::-webkit-scrollbar {
display: none; display: none;
}
} }
/* Typography */ }
h1, h2, h3 { h1, h2, h3 {
text-rendering: optimizeLegibility; text-rendering: optimizeLegibility;
} }
h1 { h1 {
--at-apply: 'text-3.6rem'; --at-apply: 'text-3.6rem';
} }
h2 { h2 {
--at-apply: 'text-3rem'; --at-apply: 'text-3rem';
} }
h3 { h3 {
--at-apply: 'text-2.4rem'; --at-apply: 'text-2.4rem';
} }
h4 { h4 {
--at-apply: 'text-2rem'; --at-apply: 'text-2rem';
} }
h5 { h5 {
--at-apply: 'text-1.8rem'; --at-apply: 'text-1.8rem';
} }
h6 { h6 {
--at-apply: 'text-1.6rem'; --at-apply: 'text-1.6rem';
} }
/* Functional styles */
article img { article img {
cursor: zoom-in; cursor: zoom-in;
} }
/* Default to dark theme when system prefers dark mode */ /* Horizontal reveal animation on theme toggle */
@media (prefers-color-scheme: dark) {
:root:not([data-theme='light']) {
color-scheme: dark;
}
:root:not([data-theme='light']) html {
--at-apply: 'dark';
}
}
/* When user explicitly chooses dark theme */
:root[data-theme='dark'] {
color-scheme: dark;
}
:root[data-theme='dark'] html {
--at-apply: 'dark';
}
/* Theme toggle animation */
@keyframes reveal { @keyframes reveal {
from { from {
clip-path: inset(var(--from)); clip-path: inset(var(--from));
} }
} }
html.dark { html.dark {
--from: 0 0 100% 0; --from: 0 0 100% 0;
} }
html:not(.dark) { html:not(.dark) {
--from: 100% 0 0 0; --from: 100% 0 0 0;
}
::view-transition-new(root) {
transition: none;
animation: reveal 1s cubic-bezier(0.4, 0, 0.2, 1);
clip-path: inset(0 0 0 0);
z-index: 2;
}
::view-transition-old(root) {
transition: none;
animation: none;
z-index: -1;
} }
@supports not (view-transition-name: none) { @supports not (view-transition-name: none) {
body { body {
--at-apply: 'transition-all duration-500 ease-in-out'; --at-apply: 'transition-all duration-500 ease-in-out';
} }
} }
/* View Transitions */
::view-transition-new(root) {
transition: none;
animation: reveal 1s cubic-bezier(0.4, 0, 0.2, 1);
clip-path: inset(0 0 0 0);
z-index: 2;
}
::view-transition-old(root) {
transition: none;
animation: none;
z-index: -1;
}