Skip to content

Commit d409fa5

Browse files
committed
update theme meta tag bg color
1 parent ab130bf commit d409fa5

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/components/BaseHead.astro

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ const ogImageUrl = new URL(image, url);
4343
<meta name="viewport" content="width=device-width,initial-scale=1" />
4444

4545
{/* wrong, set from theme toggle */}
46-
<meta name="theme-color" content="#c1ddf9" media="(prefers-color-scheme: light)" />
47-
<meta name="theme-color" content="#0f172a" media="(prefers-color-scheme: dark)" />
46+
<meta name="theme-color" content="" />
4847

4948
{/* Links */}
5049
{/* Root-Relative URL (Starts with /): */}

src/components/ThemeScript.astro

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
---
2+
import { SELECTORS } from '@/constants/dom';
23
import * as themeConstants from '@/constants/theme';
34
import { CONFIG_CLIENT } from '@/config/client';
45
import { getDefaultThemes } from '@/utils/theme';
56
67
// can not import server vars, is:inline
78
89
const { DEFAULT_MODE } = CONFIG_CLIENT;
10+
const { THEME_META_TAG_SELECTOR } = SELECTORS;
911
const defaultThemes = getDefaultThemes();
1012
---
1113

1214
{/* Inlined to avoid flash of white content. */}
13-
<script is:inline define:vars={{ themeConstants, defaultThemes, DEFAULT_MODE }}>
15+
<script
16+
is:inline
17+
define:vars={{ themeConstants, defaultThemes, DEFAULT_MODE, THEME_META_TAG_SELECTOR }}
18+
>
1419
// this is JavaScript, not TypeScript
1520
const { MODES, THEMES, THEME_CONFIG } = themeConstants;
1621
const { DATA_ATTRIBUTE, CHANGE_EVENT, LOCAL_STORAGE_KEY } = THEME_CONFIG;
@@ -147,6 +152,22 @@ const defaultThemes = getDefaultThemes();
147152
if (typeof localStorage !== 'undefined') {
148153
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(newTheme));
149154
}
155+
156+
// must have dealy for bg color to apply
157+
setTimeout(() => updateMetaTag(), 1000);
158+
};
159+
160+
const updateMetaTag = () => {
161+
const bodyElement = document.body;
162+
if (!bodyElement) return;
163+
164+
const computedStyles = window.getComputedStyle(bodyElement);
165+
const bgColor = computedStyles.backgroundColor;
166+
167+
const metaElement = document.querySelector(THEME_META_TAG_SELECTOR);
168+
if (!metaElement) return;
169+
170+
metaElement.setAttribute('content', bgColor);
150171
};
151172

152173
// initial setup

src/constants/dom.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ export const SELECTORS = {
66
GITHUB_MARKDOWN_BODY_SELECTOR: '#markdown-body-id',
77
SCROLL_TO_TOP_ID: 'main-header',
88
SCROLL_TO_TOP_SELECTOR: '#main-header',
9+
THEME_META_TAG_SELECTOR: 'meta[name="theme-color"]',
910
} as const;

0 commit comments

Comments
 (0)