Even with this copied directly from the tutorial it does not work:
<script is:inline>
// Function to set the dark mode based on localStorage or system preference
function setDarkMode() {
// Check if 'color-theme' is set in localStorage or if the user has a system preference
if (
localStorage.getItem("color-theme") === "dark" ||
(!("color-theme" in localStorage) &&
window.matchMedia("(prefers-color-scheme: dark)").matches)
) {
// Apply the dark theme
document.documentElement.classList.add("dark");
} else {
// Remove the dark theme
document.documentElement.classList.remove("dark");
}
}
// Call setDarkMode on initial load
setDarkMode();
// Listen for the 'astro:before-swap' event to update the theme on document swaps
document.addEventListener('astro:before-swap', ev => {
// Pass the incoming document to set the theme on it
// Note: This assumes that the incoming document will have the same script logic
// to handle theme setting. If not, you may need to directly manipulate the
// incoming document's classList as done in the setDarkMode function.
setDarkMode();
});
</script>