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): derive dark mode color scheme based on sdk theme colors (#77269)

Phoomparin Mano07c824b6fbfb
diff --git a/.loki/reference/chrome_laptop_Components_Inputs_DatePicker_Dates_range_SDK.png b/.loki/reference/chrome_laptop_Components_Inputs_DatePicker_Dates_range_SDK.png
index 9a03c50e998..02e5376c6b0 100644
Binary files a/.loki/reference/chrome_laptop_Components_Inputs_DatePicker_Dates_range_SDK.png and b/.loki/reference/chrome_laptop_Components_Inputs_DatePicker_Dates_range_SDK.png differ
diff --git a/frontend/src/embedding-sdk-bundle/components/private/SdkThemeProvider.unit.spec.tsx b/frontend/src/embedding-sdk-bundle/components/private/SdkThemeProvider.unit.spec.tsx
new file mode 100644
index 00000000000..0d8f39d29c7
--- /dev/null
+++ b/frontend/src/embedding-sdk-bundle/components/private/SdkThemeProvider.unit.spec.tsx
@@ -0,0 +1,109 @@
+import { waitFor } from "@testing-library/react";
+import { useEffect } from "react";
+
+import { renderWithProviders } from "__support__/ui";
+import { SdkThemeProvider } from "embedding-sdk-bundle/components/private/SdkThemeProvider";
+import { DEFAULT_FONT } from "embedding-sdk-bundle/config";
+import { ensureMetabaseProviderPropsStore } from "embedding-sdk-shared/lib/ensure-metabase-provider-props-store";
+import { getMetabaseSdkCssVariables } from "metabase/styled-components/theme/css-variables";
+import { useMantineTheme } from "metabase/ui";
+import { METABASE_DARK_THEME, METABASE_LIGHT_THEME } from "metabase/ui/colors";
+
+const EXAMPLE_COLOR = "background_page-primary-inverse";
+
+const LIGHT_EXAMPLE_VALUE = METABASE_LIGHT_THEME.colors[EXAMPLE_COLOR];
+const DARK_EXAMPLE_VALUE = METABASE_DARK_THEME.colors[EXAMPLE_COLOR];
+
+const THEME_CASES = [
+ // V1 themes
+ {
+ themeName: "V1 light theme",
+ theme: {
+ colors: {
+ background: "#ffffff",
+ "text-primary": "#111111",
+ },
+ },
+ expectedColor: LIGHT_EXAMPLE_VALUE,
+ unexpectedColor: DARK_EXAMPLE_VALUE,
+ },
+ {
+ themeName: "V1 dark theme",
+ theme: {
+ colors: {
+ background: "#111111",
+ "text-primary": "#ffffff",
+ },
+ },
+ expectedColor: DARK_EXAMPLE_VALUE,
+ unexpectedColor: LIGHT_EXAMPLE_VALUE,
+ },
+
+ // V2 themes
+ {
+ themeName: "V2 dark theme",
+ theme: {
+ version: 2 as const,
+ colors: {
+ "background_page-primary": "#111111",
+ "text-primary": "#ffffff",
+ },
+ },
+ expectedColor: DARK_EXAMPLE_VALUE,
+ unexpectedColor: LIGHT_EXAMPLE_VALUE,
+ },
+];
+
+const SdkCssVariablesTester = ({
+ onChange,
+}: {
+ onChange: (cssVariables: string) => void;
+}) => {
+ const theme = useMantineTheme();
+
+ useEffect(() => {
+ onChange(getMetabaseSdkCssVariables({ theme, font: DEFAULT_FONT }).styles);
+ }, [onChange, theme]);
+
+ return null;
+};
+
+const renderSdkThemeProvider = (
+ theme: React.ComponentProps<typeof SdkThemeProvider>["theme"],
+ onCssVariablesChange: (cssVariables: string) => void,
+) =>
+ renderWithProviders(
+ <SdkThemeProvider theme={theme}>
+ <div className="mb-wrapper">SDK content</div>
+ <SdkCssVariablesTester onChange={onCssVariablesChange} />
+ </SdkThemeProvider>,
+ );
+
+describe("SdkThemeProvider", () => {
+ beforeEach(() => {
+ ensureMetabaseProviderPropsStore().cleanup();
+ });
+
+ it.each(THEME_CASES)(
+ "$themeName base color variables are color scheme dependent",
+ async ({ theme, expectedColor, unexpectedColor }) => {
+ const handleCssVariablesChange = jest.fn();
+
+ renderSdkThemeProvider(theme, handleCssVariablesChange);
+
+ await waitFor(() => {
+ const cssVariableKey = `--mb-color-${EXAMPLE_COLOR}`;
+ const cssVariables = handleCssVariablesChange.mock.lastCall?.[0] ?? "";
+
+ // This CSS variable is generated by createColorVars from the selected base
+ // Metabase theme. Their light and dark mode values differ, so we can verify which
+ // color scheme `getMetabaseSdkCssVariables` chose from the resolved palette.
+ expect(cssVariables).toContain(`${cssVariableKey}: ${expectedColor}`);
+
+ expect(cssVariables).not.toContain(
+ `${cssVariableKey}: ${unexpectedColor}`,
+ );
+ });
+ },
+ );
+});
diff --git a/frontend/src/metabase/styled-components/theme/css-variables.ts b/frontend/src/metabase/styled-components/theme/css-variables.ts
index fd81677ca42..8ca1a3470e0 100644
--- a/frontend/src/metabase/styled-components/theme/css-variables.ts
+++ b/frontend/src/metabase/styled-components/theme/css-variables.ts
@@ -6,7 +6,10 @@ import { css } from "@emotion/react";
import { getIn } from "icepick";
import { CSS_VARIABLES_TO_SDK_THEME_MAP } from "metabase/embedding-sdk/theme/css-vars-to-sdk-theme";
-import { getDynamicCssVariables } from "metabase/embedding-sdk/theme/dynamic-css-vars";
+import {
+ getDynamicCssVariables,
+ getIsDarkThemeFromPalette,
+} from "metabase/embedding-sdk/theme/dynamic-css-vars";
import {
SDK_TO_MAIN_APP_COLORS_MAPPING,
SDK_TO_MAIN_APP_TOOLTIP_COLORS_MAPPING,
@@ -66,10 +69,12 @@ export function getMetabaseSdkCssVariables({
font: string;
whitelabelColors?: ColorSettings | null;
}) {
+ const colorScheme = getIsDarkThemeFromPalette(theme) ? "dark" : "light";
+
return css`
:root {
--mb-default-font-family: ${getFontFamilyValue(font)};
- ${createColorVars("light", whitelabelColors)}
+ ${createColorVars(colorScheme, whitelabelColors)}
${getSdkDesignSystemCssVariables(theme)}
${getDynamicCssVariables(theme)}
${getThemeSpecificCssVariables(theme)}