Fix theme inject warning

This commit is contained in:
Kalle
2026-08-16 17:11:50 +03:00
parent d8733fd9c1
commit 34bb01a34b
2 changed files with 18 additions and 23 deletions

View File

@@ -14,6 +14,22 @@
<meta name="mobile-web-app-capable" content="yes" />
<meta name="theme-color" content="#010115" />
%sveltekit.head%
<script>
(() => {
const cl = document.documentElement.classList;
if (cl.contains("light") || cl.contains("dark")) return;
const theme = window.matchMedia("(prefers-color-scheme: light)").matches
? "light"
: "dark";
cl.add(theme);
const meta = document.querySelector("meta[name=color-scheme]");
if (meta) {
meta.content = theme === "dark" ? "dark light" : "light dark";
}
})();
</script>
</head>
<body>
%sveltekit.body%

View File

@@ -8,34 +8,13 @@ interface Props {
let { theme }: Props = $props();
// when following the system theme the blocking script in app.html corrects
// this before first paint
const metaColorScheme = $derived(
theme === "light" ? "light dark" : "dark light",
);
// applied before first paint when no explicit theme is set, so the page
// never flashes the wrong theme; the html class is otherwise SSR'd
const CLIENT_THEME_SCRIPT = `
;(() => {
const theme = window.matchMedia("(prefers-color-scheme: light)").matches
? 'light'
: 'dark';
const cl = document.documentElement.classList;
const themeAlreadyApplied = cl.contains('light') || cl.contains('dark');
if (!themeAlreadyApplied) {
cl.add(theme);
}
const meta = document.querySelector('meta[name=color-scheme]');
if (meta) {
meta.content = theme === 'dark' ? 'dark light' : 'light dark';
}
})();
`;
</script>
<svelte:head>
<meta name="color-scheme" content={metaColorScheme} />
{#if theme === null}
<!-- eslint-disable-next-line svelte/no-at-html-tags -->
{@html `<script>${CLIENT_THEME_SCRIPT}</script>`}
{/if}
</svelte:head>