|
1 | 1 | ---
|
| 2 | +import { SELECTORS } from '@/constants/dom'; |
2 | 3 | import * as themeConstants from '@/constants/theme';
|
3 | 4 | import { CONFIG_CLIENT } from '@/config/client';
|
4 | 5 | import { getDefaultThemes } from '@/utils/theme';
|
5 | 6 |
|
6 | 7 | // can not import server vars, is:inline
|
7 | 8 |
|
8 | 9 | const { DEFAULT_MODE } = CONFIG_CLIENT;
|
| 10 | +const { THEME_META_TAG_SELECTOR } = SELECTORS; |
9 | 11 | const defaultThemes = getDefaultThemes();
|
10 | 12 | ---
|
11 | 13 |
|
12 | 14 | {/* 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 | +> |
14 | 19 | // this is JavaScript, not TypeScript
|
15 | 20 | const { MODES, THEMES, THEME_CONFIG } = themeConstants;
|
16 | 21 | const { DATA_ATTRIBUTE, CHANGE_EVENT, LOCAL_STORAGE_KEY } = THEME_CONFIG;
|
@@ -147,6 +152,22 @@ const defaultThemes = getDefaultThemes();
|
147 | 152 | if (typeof localStorage !== 'undefined') {
|
148 | 153 | localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(newTheme));
|
149 | 154 | }
|
| 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); |
150 | 171 | };
|
151 | 172 |
|
152 | 173 | // initial setup
|
|
0 commit comments