2022-07-26 08:27:57 +00:00
|
|
|
import { useTheme as useNextTheme } from "next-themes";
|
2023-02-17 11:18:54 +00:00
|
|
|
import { useEffect } from "react";
|
2021-07-09 22:59:21 +00:00
|
|
|
|
2022-05-27 15:37:02 +00:00
|
|
|
import { useEmbedTheme } from "@calcom/embed-core/embed-iframe";
|
2021-10-14 19:22:01 +00:00
|
|
|
|
2023-04-17 12:16:54 +00:00
|
|
|
/**
|
2023-07-18 01:02:42 +00:00
|
|
|
* It should be called once per route if you intend to use a theme different from `system` theme. `system` theme is automatically supported using <ThemeProvider />
|
|
|
|
* If needed you can also set system theme by passing 'system' as `themeToSet`
|
|
|
|
* It handles embed configured theme automatically
|
|
|
|
* To just read the values pass `getOnly` as `true` and `themeToSet` as `null`
|
2023-04-17 12:16:54 +00:00
|
|
|
*/
|
2023-07-18 01:02:42 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
export default function useTheme(themeToSet: "system" | (string & {}) | undefined | null, getOnly = false) {
|
2023-02-17 11:18:54 +00:00
|
|
|
const { resolvedTheme, setTheme, forcedTheme, theme: activeTheme } = useNextTheme();
|
2022-04-08 05:33:24 +00:00
|
|
|
const embedTheme = useEmbedTheme();
|
2022-02-16 15:53:26 +00:00
|
|
|
|
2022-07-26 08:27:57 +00:00
|
|
|
useEffect(() => {
|
2023-07-18 01:02:42 +00:00
|
|
|
// Undefined themeToSet allow the hook to be used where the theme is fetched after calling useTheme hook
|
|
|
|
if (getOnly || themeToSet === undefined) {
|
2023-05-05 11:24:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-07-09 22:59:21 +00:00
|
|
|
|
2023-05-22 11:17:56 +00:00
|
|
|
// Embed theme takes precedence over theme configured in app.
|
|
|
|
// If embedTheme isn't set i.e. it's not explicitly configured with a theme, then it would use the theme configured in appearance.
|
|
|
|
// If embedTheme is set to "auto" then we consider it as null which then uses system theme.
|
2023-07-18 01:02:42 +00:00
|
|
|
const finalThemeToSet = embedTheme ? (embedTheme === "auto" ? "system" : embedTheme) : themeToSet;
|
2023-04-17 12:16:54 +00:00
|
|
|
|
|
|
|
if (!finalThemeToSet || finalThemeToSet === activeTheme) return;
|
|
|
|
|
|
|
|
setTheme(finalThemeToSet);
|
2023-04-21 10:50:27 +00:00
|
|
|
// We must not add `activeTheme` to the dependency list as it can cause an infinite loop b/w dark and theme switches
|
|
|
|
// because there might be another booking page with conflicting theme.
|
2023-05-05 11:24:06 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2023-04-21 10:50:27 +00:00
|
|
|
}, [themeToSet, setTheme, embedTheme]);
|
2023-07-18 01:02:42 +00:00
|
|
|
|
|
|
|
if (getOnly) {
|
|
|
|
return {
|
|
|
|
resolvedTheme,
|
|
|
|
forcedTheme,
|
|
|
|
activeTheme,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the currently set theme values.
|
|
|
|
*/
|
|
|
|
export function useGetTheme() {
|
|
|
|
const theme = useTheme(null, true);
|
|
|
|
if (!theme) {
|
|
|
|
throw new Error("useTheme must have a return value here");
|
|
|
|
}
|
|
|
|
return theme;
|
2021-07-11 19:35:56 +00:00
|
|
|
}
|