cal/packages/lib/hooks/useTheme.tsx
Nafees Nazik 485619602f
refactor: usetheme hook (#7112)
* refactor: useTheme hook

* feat: add mounted state hook

* feat: add mounted hook

* fix: add comments
2023-02-17 11:18:54 +00:00

34 lines
1.0 KiB
TypeScript

import { useTheme as useNextTheme } from "next-themes";
import { useEffect } from "react";
import { useEmbedTheme } from "@calcom/embed-core/embed-iframe";
import type { Maybe } from "@calcom/trpc/server";
import useMountedState from "./useMountedState";
// makes sure the ui doesn't flash
export default function useTheme(theme?: Maybe<string>) {
let currentTheme: Maybe<string> = theme || "system";
const { resolvedTheme, setTheme, forcedTheme, theme: activeTheme } = useNextTheme();
const embedTheme = useEmbedTheme();
const isMounted = useMountedState();
// Embed UI configuration takes more precedence over App Configuration
currentTheme = embedTheme || theme;
useEffect(() => {
if (currentTheme !== activeTheme && typeof currentTheme === "string") {
setTheme(currentTheme);
}
// eslint-disable-next-line react-hooks/exhaustive-deps -- we do not want activeTheme to re-render this effect
}, [currentTheme, setTheme]);
return {
resolvedTheme,
setTheme,
forcedTheme,
activeTheme,
isReady: isMounted(),
};
}