feat: change theme-color dynamically (#185)
This commit is contained in:
parent
85e2437300
commit
7399d6d386
2 changed files with 11 additions and 0 deletions
|
|
@ -17,6 +17,11 @@ import Icon from './icon.astro';
|
||||||
const toggleTheme = () => {
|
const toggleTheme = () => {
|
||||||
const theme = localStorage.getItem('theme') ?? 'light';
|
const theme = localStorage.getItem('theme') ?? 'light';
|
||||||
const newTheme = theme === 'light' ? 'dark' : 'light';
|
const newTheme = theme === 'light' ? 'dark' : 'light';
|
||||||
|
const themeMeta = document.getElementsByName('theme-color') as NodeListOf<HTMLMetaElement>;
|
||||||
|
|
||||||
|
if (themeMeta[0]) {
|
||||||
|
themeMeta[0].content = newTheme === 'dark' ? '#1a202c' : '#ffffff';
|
||||||
|
}
|
||||||
|
|
||||||
document.documentElement.classList[newTheme === 'dark' ? 'add' : 'remove']('dark');
|
document.documentElement.classList[newTheme === 'dark' ? 'add' : 'remove']('dark');
|
||||||
localStorage.setItem('theme', newTheme);
|
localStorage.setItem('theme', newTheme);
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,12 @@
|
||||||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
const themeMeta = document.getElementsByName('theme-color');
|
||||||
|
|
||||||
|
if (themeMeta[0]) {
|
||||||
|
themeMeta[0].content = theme === 'dark' ? '#1a202c' : '#ffffff';
|
||||||
|
}
|
||||||
|
|
||||||
if (theme === 'light') {
|
if (theme === 'light') {
|
||||||
document.documentElement.classList.remove('dark');
|
document.documentElement.classList.remove('dark');
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue