1 parent
7399d6421e
commit
ed808c3be6
244 files changed
+7237
-3128
No files matched your search
@@ -4,8 +4,18 @@ import { useState, useEffect, CSSProperties } from "react";
|
||||
import { sdkActionManager } from "./sdk-event";
|
||||
|
||||
export interface UiConfig {
|
||||
theme: string;
|
||||
styles: EmbedStyles;
|
||||
theme?: "dark" | "light" | "auto";
|
||||
styles?: EmbedStyles;
|
||||
}
|
||||
declare global {
|
||||
interface Window {
|
||||
CalEmbed: {
|
||||
__logQueue?: any[];
|
||||
embedStore: any;
|
||||
};
|
||||
CalComPageStatus: string;
|
||||
CalComPlan: string;
|
||||
}
|
||||
}
|
||||
|
||||
const embedStore = {
|
||||
@@ -13,7 +23,6 @@ const embedStore = {
|
||||
styles: {},
|
||||
namespace: null,
|
||||
embedType: undefined,
|
||||
theme: null,
|
||||
// Store all React State setters here.
|
||||
reactStylesStateSetters: {},
|
||||
parentInformedAboutContentHeight: false,
|
||||
@@ -21,17 +30,20 @@ const embedStore = {
|
||||
} as {
|
||||
styles: UiConfig["styles"];
|
||||
namespace: string | null;
|
||||
theme: string | null;
|
||||
embedType: undefined | null | string;
|
||||
reactStylesStateSetters: any;
|
||||
parentInformedAboutContentHeight: boolean;
|
||||
windowLoadEventFired: boolean;
|
||||
theme?: UiConfig["theme"];
|
||||
setTheme: (arg0: string) => void;
|
||||
};
|
||||
|
||||
let isSafariBrowser = false;
|
||||
const isBrowser = typeof window !== "undefined";
|
||||
|
||||
if (isBrowser) {
|
||||
window.CalEmbed = window.CalEmbed || {};
|
||||
window.CalEmbed.embedStore = embedStore;
|
||||
const ua = navigator.userAgent.toLowerCase();
|
||||
isSafariBrowser = ua.includes("safari") && !ua.includes("chrome");
|
||||
if (isSafariBrowser) {
|
||||
@@ -47,23 +59,11 @@ function runAsap(fn: (...arg: any) => void) {
|
||||
return requestAnimationFrame(fn);
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
CalEmbed: {
|
||||
__logQueue?: any[];
|
||||
};
|
||||
CalComPageStatus: string;
|
||||
CalComPlan: string;
|
||||
}
|
||||
}
|
||||
|
||||
function log(...args: any[]) {
|
||||
if (isBrowser) {
|
||||
const namespace = getNamespace();
|
||||
|
||||
const searchParams = new URL(document.URL).searchParams;
|
||||
//TODO: Send postMessage to parent to get all log messages in the same queue.
|
||||
window.CalEmbed = window.CalEmbed || {};
|
||||
const logQueue = (window.CalEmbed.__logQueue = window.CalEmbed.__logQueue || []);
|
||||
args.push({
|
||||
ns: namespace,
|
||||
@@ -132,17 +132,14 @@ function isValidNamespace(ns: string | null | undefined) {
|
||||
|
||||
export const useEmbedTheme = () => {
|
||||
const router = useRouter();
|
||||
let [theme, setTheme] = useState(embedStore.theme || (router.query.theme as string));
|
||||
useEffect(() => {
|
||||
router.events.on("routeChangeComplete", () => {
|
||||
sdkActionManager?.fire("__routeChanged", {});
|
||||
});
|
||||
}, [router.events]);
|
||||
|
||||
if (embedStore.theme) {
|
||||
return embedStore.theme;
|
||||
}
|
||||
const theme = (embedStore.theme = router.query.theme as string);
|
||||
return theme;
|
||||
embedStore.setTheme = setTheme;
|
||||
return theme === "auto" ? null : theme;
|
||||
};
|
||||
|
||||
// TODO: Make it usable as an attribute directly instead of styles value. It would allow us to go beyond styles e.g. for debugging we can add a special attribute indentifying the element on which UI config has been applied
|
||||
@@ -271,11 +268,16 @@ export const methods = {
|
||||
}
|
||||
|
||||
// body can't be styled using React state hook as it is generated by _document.tsx which doesn't support hooks.
|
||||
if (stylesConfig.body?.background) {
|
||||
if (stylesConfig?.body?.background) {
|
||||
document.body.style.background = stylesConfig.body.background as string;
|
||||
}
|
||||
|
||||
setEmbedStyles(stylesConfig);
|
||||
if (uiConfig.theme) {
|
||||
embedStore.theme = uiConfig.theme as UiConfig["theme"];
|
||||
embedStore.setTheme(uiConfig.theme);
|
||||
}
|
||||
|
||||
setEmbedStyles(stylesConfig || {});
|
||||
},
|
||||
parentKnowsIframeReady: () => {
|
||||
log("Method: `parentKnowsIframeReady` called");
|
||||
@@ -370,6 +372,7 @@ function keepParentInformedAboutDimensionChanges() {
|
||||
|
||||
if (isBrowser) {
|
||||
const url = new URL(document.URL);
|
||||
embedStore.theme = (url.searchParams.get("theme") || "auto") as UiConfig["theme"];
|
||||
if (url.searchParams.get("prerender") !== "true" && isEmbed()) {
|
||||
log("Initializing embed-iframe");
|
||||
// HACK
|
||||
|
||||
Reference in new issue
Block a user