Feat: Add next-themes for theme handling (#3263)
* chore: add next-themes * feat: add next theme provider * fix: modify useTheme hook to use next-themes * fix: use the new hook for theming * remove conditional rendering with isReady boolean flag * remove Theme component returned from useTheme hook * Update missing useTheme usage * Fix theme switching as per system. Also, ensure that booking and non booking pages can maintain their own requirements of theme simulatenously Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: zomars <[email protected]> Co-authored-by: Alex van Andel <[email protected]> Co-authored-by: Hariom Balhara <[email protected]>
This commit is contained in:
22 files changed
+1310
-1306
No files matched your search
@@ -15,7 +15,7 @@ const I18nextAdapter = appWithTranslation<NextJsAppProps & { children: React.Rea
|
||||
|
||||
// Workaround for https://github.com/vercel/next.js/issues/8592
|
||||
export type AppProps = Omit<NextAppProps, "Component"> & {
|
||||
Component: NextAppProps["Component"] & { requiresLicense?: boolean };
|
||||
Component: NextAppProps["Component"] & { requiresLicense?: boolean; isThemeSupported?: boolean };
|
||||
/** Will be defined only is there was an error */
|
||||
err?: Error;
|
||||
};
|
||||
|
||||
@@ -1,57 +1,25 @@
|
||||
import Head from "next/head";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useTheme as useNextTheme } from "next-themes";
|
||||
import { useEffect } from "react";
|
||||
|
||||
import { useEmbedTheme } from "@calcom/embed-core/embed-iframe";
|
||||
import { Maybe } from "@calcom/trpc/server";
|
||||
|
||||
// This method is stringified and executed only on client. So,
|
||||
// - Pass all the params explicitly to this method. Don't use closure
|
||||
function applyThemeAndAddListener(theme: string) {
|
||||
const mediaQueryList = window.matchMedia("(prefers-color-scheme: dark)");
|
||||
const applyTheme = function (theme: string, darkMatch: boolean) {
|
||||
if (!theme) {
|
||||
if (darkMatch) {
|
||||
document.documentElement.classList.add("dark");
|
||||
} else {
|
||||
document.documentElement.classList.remove("dark");
|
||||
}
|
||||
} else {
|
||||
document.documentElement.classList.remove("dark");
|
||||
document.documentElement.classList.remove("light");
|
||||
document.documentElement.classList.add(theme);
|
||||
}
|
||||
};
|
||||
mediaQueryList.onchange = function (e) {
|
||||
applyTheme(theme, e.matches);
|
||||
};
|
||||
applyTheme(theme, mediaQueryList.matches);
|
||||
}
|
||||
|
||||
// makes sure the ui doesn't flash
|
||||
export default function useTheme(theme?: Maybe<string>) {
|
||||
const [isReady, setIsReady] = useState(false);
|
||||
theme = theme || "system";
|
||||
const { theme: currentTheme, setTheme } = useNextTheme();
|
||||
const embedTheme = useEmbedTheme();
|
||||
// Embed UI configuration takes more precedence over App Configuration
|
||||
theme = embedTheme || theme;
|
||||
const [_theme, setTheme] = useState<Maybe<string>>(theme);
|
||||
useEffect(() => {
|
||||
// TODO: isReady doesn't seem required now. This is also impacting PSI Score for pages which are using isReady.
|
||||
setIsReady(true);
|
||||
setTheme(theme);
|
||||
}, [theme]);
|
||||
|
||||
function Theme() {
|
||||
const code = applyThemeAndAddListener.toString();
|
||||
const themeStr = _theme ? `"${_theme}"` : null;
|
||||
return (
|
||||
<Head>
|
||||
<script dangerouslySetInnerHTML={{ __html: `(${code})(${themeStr})` }} />
|
||||
</Head>
|
||||
);
|
||||
}
|
||||
useEffect(() => {
|
||||
if (theme) {
|
||||
setTheme(theme);
|
||||
}
|
||||
}, [theme, setTheme]);
|
||||
|
||||
return {
|
||||
isReady,
|
||||
Theme,
|
||||
currentTheme,
|
||||
setTheme,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user