File size: 1,633 Bytes
689359a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | "use client";
import { usePathname } from "next/navigation";
import { ThemeProvider as NextThemesProvider } from "next-themes";
import { createContext, useContext, useEffect, useState } from "react";
// Routes that only exist inside the signed-in app. They use the saved theme
// from the first paint. Everything else (landing, marketing, auth, share
// pages) has no dark design and stays light.
const APP_ROUTE = /^\/(list|tasks|settings|analytics|admin)(\/|$)/;
const AppThemeScope = createContext<(active: boolean) => void>(() => {});
/** Light by default; the saved dark choice only applies inside the app. */
export function ThemeProvider({ children }: { children: React.ReactNode }) {
const pathname = usePathname() ?? "";
const [shellActive, setShellActive] = useState(false);
const themed = shellActive || APP_ROUTE.test(pathname);
return (
<AppThemeScope.Provider value={setShellActive}>
<NextThemesProvider
attribute="class"
defaultTheme="light"
enableSystem={false}
forcedTheme={themed ? undefined : "light"}
disableTransitionOnChange
>
{children}
</NextThemesProvider>
</AppThemeScope.Provider>
);
}
/**
* Applies the saved theme while the signed-in app shell is mounted. "/" is
* both the landing page and the signed-in Create page, so the route alone
* cannot tell them apart.
*/
export function useAppTheme(active: boolean) {
const setShellActive = useContext(AppThemeScope);
useEffect(() => {
if (!active) return;
setShellActive(true);
return () => setShellActive(false);
}, [active, setShellActive]);
}
|