52 - garden-astro - add tailwind

  • pnpm astro add tailwind
  • pnpm install -D @tailwindcss/typography
  • Add dark mode with switch ref.
<script is:inline>
  const theme = (() => {
    if (typeof localStorage !== "undefined" && localStorage.getItem("theme")) {
      return localStorage.getItem("theme");
    }
    if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
      return "dark";
    }
    return "light";
  })();

  if (theme === "light") {
    document.documentElement.classList.remove("dark");
  } else {
    document.documentElement.classList.add("dark");
  }
  window.localStorage.setItem("theme", theme);
</script>
  • Wrap everything into layout
<!DOCTYPE html>
<html lang="en">
	<head>
		<BaseHead title={SITE_TITLE} description={SITE_DESCRIPTION} />
        <DarkModeScript />
	</head>
	<body class="flex flex-col h-[100svh] bg-white dark:bg-slate-800 text-black dark:text-white">
		<Header title={SITE_TITLE} />
		<main>
            <slot />
		</main>
		<Footer />
	</body>
</html>
  • Dark theme switcher (Svelte)
<script lang="ts">
  let theme = localStorage.getItem("theme") ?? "light"

  $: {
    if (theme === "dark") {
      document.documentElement.classList.add("dark")
    } else {
      document.documentElement.classList.remove("dark")
    }

    localStorage.setItem("theme", theme)
  }

  function toggleTheme() {
    theme = theme === "light" ? "dark" : "light"
  }
</script>

<button on:click={toggleTheme}>{theme === "light" ? "🌞" : "🌙"}</button>

Here’s how it look like as of today