Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

fix(sdk): secondary background color is not mapped to theme (#65116)

Phoomparin Mano83b0eb317480
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/lib/theme/get-embedding-theme.ts b/enterprise/frontend/src/embedding-sdk-bundle/lib/theme/get-embedding-theme.ts
index b82e31fe572..5ba1305fefa 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/lib/theme/get-embedding-theme.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/lib/theme/get-embedding-theme.ts
@@ -14,6 +14,7 @@ import {
} from "metabase/embedding-sdk/theme";
import type { MappableSdkColor } from "metabase/embedding-sdk/theme/embedding-color-palette";
import {
+ SDK_MISSING_COLORS_FALLBACK,
SDK_TO_MAIN_APP_COLORS_MAPPING,
SDK_TO_MAIN_APP_TOOLTIP_COLORS_MAPPING,
} from "metabase/embedding-sdk/theme/embedding-color-palette";
@@ -60,9 +61,27 @@ export function getEmbeddingThemeOverride(
if (theme.colors) {
override.colors = {};
+ const userColors = { ...theme.colors };
+
+ // Apply fallback colors for missing colors that the user forgot to define.
+ // For example, if they forgot to define `background-secondary` but have
+ // defined `background`, we use it as a fallback.
+ for (const key in SDK_MISSING_COLORS_FALLBACK) {
+ const targetColor = key as MappableSdkColor;
+ const fallbackColor = SDK_MISSING_COLORS_FALLBACK[targetColor];
+
+ if (
+ fallbackColor &&
+ userColors[fallbackColor] &&
+ !userColors[targetColor]
+ ) {
+ userColors[targetColor] = userColors[fallbackColor];
+ }
+ }
+
// Apply color palette overrides
- for (const name in theme.colors) {
- const color = theme.colors[name as MetabaseColor];
+ for (const name in userColors) {
+ const color = userColors[name as MetabaseColor];
if (color && typeof color === "string") {
const themeColorNames =
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/lib/theme/get-embedding-theme.unit.spec.ts b/enterprise/frontend/src/embedding-sdk-bundle/lib/theme/get-embedding-theme.unit.spec.ts
index 483f84c987e..e1cdb2af90a 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/lib/theme/get-embedding-theme.unit.spec.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/lib/theme/get-embedding-theme.unit.spec.ts
@@ -17,6 +17,8 @@ describe("Transform Embedding Theme Override", () => {
"text-primary": "yellow",
"text-tertiary": "green",
"background-disabled": "pink",
+ background: "orange",
+ "background-secondary": "brown",
"text-white": "white",
focus: "blue",
},
@@ -40,6 +42,11 @@ describe("Transform Embedding Theme Override", () => {
"text-primary": expect.arrayContaining(["yellow"]),
"text-light": expect.arrayContaining(["green"]),
"text-tertiary": expect.arrayContaining(["green"]),
+ background: expect.arrayContaining(["orange"]),
+ "bg-primary": expect.arrayContaining(["orange"]),
+ "bg-white": expect.arrayContaining(["orange"]),
+ "bg-medium": expect.arrayContaining(["brown"]),
+ "bg-secondary": expect.arrayContaining(["brown"]),
"background-disabled": expect.arrayContaining(["pink"]),
"text-white": expect.arrayContaining(["white"]),
white: expect.arrayContaining(["white"]),
@@ -52,4 +59,24 @@ describe("Transform Embedding Theme Override", () => {
components: getEmbeddingComponentOverrides(),
});
});
+
+ it("sets background-secondary to the value of background when it is unset", () => {
+ const theme = getEmbeddingThemeOverride(
+ { colors: { background: "green" } },
+ "Roboto",
+ );
+
+ expect(theme).toEqual({
+ fontFamily: "Roboto",
+ colors: {
+ background: expect.arrayContaining(["green"]),
+ "bg-primary": expect.arrayContaining(["green"]),
+ "bg-white": expect.arrayContaining(["green"]),
+ "bg-medium": expect.arrayContaining(["green"]),
+ "bg-secondary": expect.arrayContaining(["green"]),
+ },
+ other: { fontSize: "14px", ...DEFAULT_EMBEDDED_COMPONENT_THEME },
+ components: getEmbeddingComponentOverrides(),
+ });
+ });
});
diff --git a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/utils/dynamic-sdk-color-defaults.ts b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/utils/dynamic-sdk-color-defaults.ts
index 493547c5bf5..52682df623a 100644
--- a/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/utils/dynamic-sdk-color-defaults.ts
+++ b/enterprise/frontend/src/metabase-enterprise/embedding_iframe_sdk_setup/utils/dynamic-sdk-color-defaults.ts
@@ -16,7 +16,7 @@ export const EMBED_FLOW_DERIVED_COLORS_CONFIG: EmbedFlowDerivedDefaultColorConfi
},
"background-secondary": {
light: { source: "background", darken: 0.05 },
- dark: { source: "background", lighten: 0.5 },
+ dark: { source: "background", darken: 0.2 },
},
"background-light": {
light: { source: "background", darken: 0.02 },
diff --git a/frontend/src/metabase/embedding-sdk/theme/embedding-color-palette.ts b/frontend/src/metabase/embedding-sdk/theme/embedding-color-palette.ts
index 8dde8004213..11badfae7bd 100644
--- a/frontend/src/metabase/embedding-sdk/theme/embedding-color-palette.ts
+++ b/frontend/src/metabase/embedding-sdk/theme/embedding-color-palette.ts
@@ -35,8 +35,10 @@ export type SemanticColorKey =
| "brand-lighter";
/**
- * Mapping of SDK colors to main app colors. There could be additional values
- * for new semantic colors we add to colors.module.css
+ * Mapping of SDK colors to main app colors.
+ *
+ * The main app colors are defined in `metabase/lib/colors/colors.ts`.
+ * One SDK theme color can map to multiple main app colors.
*/
export const SDK_TO_MAIN_APP_COLORS_MAPPING: Record<
MappableSdkColor,
@@ -53,7 +55,7 @@ export const SDK_TO_MAIN_APP_COLORS_MAPPING: Record<
"text-tertiary": ["text-light", "text-tertiary"],
background: ["bg-white", "bg-primary", "background"],
"background-hover": ["bg-light", "background-hover"],
- "background-secondary": ["bg-medium"],
+ "background-secondary": ["bg-medium", "bg-secondary"],
"background-disabled": ["background-disabled"],
"background-light": ["background-light"],
shadow: ["shadow"],
@@ -66,6 +68,16 @@ export const SDK_TO_MAIN_APP_COLORS_MAPPING: Record<
focus: ["focus"],
};
+/**
+ * If the user forgot to define a theme color,
+ * we apply the fallback color instead if it is also defined.
+ */
+export const SDK_MISSING_COLORS_FALLBACK: Partial<
+ Record<MappableSdkColor, MappableSdkColor>
+> = {
+ "background-secondary": "background",
+};
+
/**
* These colors must never be changed.
* For example, the blue Metabase brand color.