Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

1.4 KB
Raw Blame
import { useSyncedRef } from "phase/react";
import { useEffect, useState } from "react";

// Owns browser theme subscriptions for the Eve hero.
// INVARIANT: resolution order matches the old index.tsx hooks exactly.
// Imported only by index.tsx.

function getCurrentTheme(prefersDarkTheme: boolean): "light" | "dark" {
  if (typeof document === "undefined") return "light";
  const root = document.documentElement;
  if (
    root.classList.contains("dark") ||
    root.classList.contains("dark-theme") ||
    root.dataset.theme === "dark"
  ) {
    return "dark";
  }
  if (
    root.classList.contains("light") ||
    root.classList.contains("light-theme") ||
    root.dataset.theme === "light"
  ) {
    return "light";
  }
  return prefersDarkTheme ? "dark" : "light";
}

export function useResolvedTheme(prefersDarkTheme: boolean) {
  const [theme, setTheme] = useState<"light" | "dark">("light");
  const prefersDarkThemeRef = useSyncedRef(prefersDarkTheme);

  useEffect(() => {
    const syncTheme = () => setTheme(getCurrentTheme(prefersDarkThemeRef.current));
    const observer = new MutationObserver(syncTheme);

    syncTheme();
    observer.observe(document.documentElement, {
      attributes: true,
      attributeFilter: ["class", "data-theme"],
    });
    return () => {
      observer.disconnect();
    };
  }, []);

  useEffect(() => {
    setTheme(getCurrentTheme(prefersDarkTheme));
  }, [prefersDarkTheme]);

  return theme;
}