From 7399d6d386282821d07b933f2e187d641ff5e13c Mon Sep 17 00:00:00 2001 From: Szymon Kin <68154191+hoolek77@users.noreply.github.com> Date: Sun, 29 Jan 2023 12:28:05 +0100 Subject: [PATCH] feat: change theme-color dynamically (#185) --- src/web/components/theme-toggle.astro | 5 +++++ src/web/head/initial-theme.astro | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/src/web/components/theme-toggle.astro b/src/web/components/theme-toggle.astro index be9fb13..16d0c71 100644 --- a/src/web/components/theme-toggle.astro +++ b/src/web/components/theme-toggle.astro @@ -17,6 +17,11 @@ import Icon from './icon.astro'; const toggleTheme = () => { const theme = localStorage.getItem('theme') ?? 'light'; const newTheme = theme === 'light' ? 'dark' : 'light'; + const themeMeta = document.getElementsByName('theme-color') as NodeListOf; + + if (themeMeta[0]) { + themeMeta[0].content = newTheme === 'dark' ? '#1a202c' : '#ffffff'; + } document.documentElement.classList[newTheme === 'dark' ? 'add' : 'remove']('dark'); localStorage.setItem('theme', newTheme); diff --git a/src/web/head/initial-theme.astro b/src/web/head/initial-theme.astro index 1cf2700..6612f84 100644 --- a/src/web/head/initial-theme.astro +++ b/src/web/head/initial-theme.astro @@ -6,6 +6,12 @@ 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') { document.documentElement.classList.remove('dark'); } else {