fix: brand colors set by user not respected in embed (#17399)

Co-authored-by: Hariom <[email protected]>
This commit is contained in:
Anwar SadathandHariom authored and GitHub committed 2024-12-03 04:10:58 +00:00
1 parent 6dcb6aece4
commit 3a472997ff
6 files changed
+117 -38

No files matched your search

+18 -16
View File
@@ -9,6 +9,7 @@ import { TextArea } from "@calcom/ui";
import type { EmbedFramework, EmbedType, PreviewState } from "../types";
import { Codes } from "./EmbedCodes";
import { buildCssVarsPerTheme } from "./buildCssVarsPerTheme";
import { embedLibUrl, EMBED_PREVIEW_HTML_URL } from "./constants";
import { getApiNameForReactSnippet, getApiNameForVanillaJsSnippet } from "./getApiName";
import { getDimension } from "./getDimension";
@@ -175,25 +176,27 @@ const getEmbedTypeSpecificString = ({
let uiInstructionStringArg: {
apiName: string;
theme: PreviewState["theme"];
brandColor: string;
brandColor: string | null;
darkBrandColor: string | null;
hideEventTypeDetails: boolean;
layout?: BookerLayout;
};
const baseUiInstructionStringArg = {
theme: previewState.theme,
brandColor: previewState.palette.brandColor,
darkBrandColor: previewState.palette.darkBrandColor,
hideEventTypeDetails: previewState.hideEventTypeDetails,
layout: previewState.layout,
};
if (embedFramework === "react") {
uiInstructionStringArg = {
...baseUiInstructionStringArg,
apiName: getApiNameForReactSnippet({ mainApiName: "cal" }),
theme: previewState.theme,
brandColor: previewState.palette.brandColor,
hideEventTypeDetails: previewState.hideEventTypeDetails,
layout: previewState.layout,
};
} else {
uiInstructionStringArg = {
...baseUiInstructionStringArg,
apiName: getApiNameForVanillaJsSnippet({ namespace, mainApiName: "Cal" }),
theme: previewState.theme,
brandColor: previewState.palette.brandColor,
hideEventTypeDetails: previewState.hideEventTypeDetails,
layout: previewState.layout,
};
}
if (!frameworkCodes[embedType]) {
@@ -230,27 +233,26 @@ const getEmbedUIInstructionString = ({
apiName,
theme,
brandColor,
darkBrandColor,
hideEventTypeDetails,
layout,
}: {
apiName: string;
theme?: string;
brandColor: string;
brandColor: string | null;
darkBrandColor: string | null;
hideEventTypeDetails: boolean;
layout?: string;
}) => {
theme = theme !== "auto" ? theme : undefined;
return getInstructionString({
apiName,
instructionName: "ui",
instructionArg: {
theme,
styles: {
branding: {
brandColor,
},
},
hideEventTypeDetails: hideEventTypeDetails,
cssVarsPerTheme: buildCssVarsPerTheme({ brandColor, darkBrandColor }),
hideEventTypeDetails,
layout,
},
});
@@ -0,0 +1,39 @@
function _buildCssVarsPerTheme({
light,
dark,
}: {
light: { "cal-brand": string | null };
dark: { "cal-brand": string | null };
}) {
// Setting this value should remove it from the API
const VALUE_WHEN_WE_DONT_WANT_IT_IN_API = undefined;
const cssVarsPerTheme = Object.entries({ light, dark }).reduce((acc, [theme, themeCssVars]) => {
if (!Object.values(themeCssVars).some(Boolean)) return acc;
const truthyValues = Object.fromEntries(
Object.entries(themeCssVars).filter(([_, value]) => Boolean(value))
);
return {
...acc,
[theme]: truthyValues,
};
}, {});
if (Object.keys(cssVarsPerTheme).length === 0) return VALUE_WHEN_WE_DONT_WANT_IT_IN_API;
return cssVarsPerTheme;
}
export function buildCssVarsPerTheme({
brandColor,
darkBrandColor,
}: {
brandColor: string | null;
darkBrandColor: string | null;
}) {
return _buildCssVarsPerTheme({
light: { "cal-brand": brandColor },
dark: { "cal-brand": darkBrandColor },
});
}