Skip to content

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

Init custom theme with selected theme colors in embedding wizard (#73288)

Sébastien630ed4bff859
diff --git a/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/Appearance/ThemeSelectorSection.tsx b/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/Appearance/ThemeSelectorSection.tsx
index 1c46d11a72d..1c728080a90 100644
--- a/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/Appearance/ThemeSelectorSection.tsx
+++ b/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/Appearance/ThemeSelectorSection.tsx
@@ -6,10 +6,28 @@ import { Box, Flex, Icon, SimpleGrid, Tooltip } from "metabase/ui";
import type { EmbeddingTheme } from "metabase-types/api/embedding-theme";
import type { SdkIframeEmbedSetupTheme } from "../../types";
+import { getConfigurableThemeColors } from "../../utils/theme-colors";
import { ColorCustomizationSection } from "./ColorCustomizationSection";
import { ThemeCard, getThemeColors } from "./ThemeCard";
+const pickConfigurableColors = (
+ colors: Partial<MetabaseColors> | undefined,
+): Partial<MetabaseColors> | undefined => {
+ if (!colors) {
+ return undefined;
+ }
+
+ const picked: Partial<MetabaseColors> = {};
+ for (const { key } of getConfigurableThemeColors()) {
+ if (colors[key] !== undefined) {
+ picked[key] = colors[key];
+ }
+ }
+
+ return Object.keys(picked).length > 0 ? picked : undefined;
+};
+
type ThemeSelection =
| { type: "default" }
| { type: "saved"; themeId: number }
@@ -19,6 +37,7 @@ interface ThemeSelectorSectionProps {
savedThemes: EmbeddingTheme[];
theme: SdkIframeEmbedSetupTheme | undefined;
onThemeChange: (themeId: number | undefined) => void;
+ onCustomSelect: (initialColors: Partial<MetabaseColors> | undefined) => void;
onColorChange: (colors: Partial<MetabaseColors>) => void;
onColorReset: () => void;
}
@@ -27,6 +46,7 @@ export const ThemeSelectorSection = ({
savedThemes,
theme,
onThemeChange,
+ onCustomSelect,
onColorChange,
onColorReset,
}: ThemeSelectorSectionProps) => {
@@ -60,8 +80,15 @@ export const ThemeSelectorSection = ({
};
const handleCustomClick = () => {
+ const previouslySelectedSavedTheme =
+ selection.type === "saved"
+ ? savedThemes.find((t) => t.id === selection.themeId)
+ : undefined;
+
setSelection({ type: "custom" });
- onThemeChange(undefined);
+ onCustomSelect(
+ pickConfigurableColors(previouslySelectedSavedTheme?.settings.colors),
+ );
};
const hasThemes = savedThemes.length > 0;
diff --git a/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/Appearance/ThemeSelectorSection.unit.spec.tsx b/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/Appearance/ThemeSelectorSection.unit.spec.tsx
index 480adb612c9..4a9db0fbf7c 100644
--- a/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/Appearance/ThemeSelectorSection.unit.spec.tsx
+++ b/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/Appearance/ThemeSelectorSection.unit.spec.tsx
@@ -39,12 +39,14 @@ function setup({
savedThemes = SAVED_THEMES,
theme = undefined,
onThemeChange = jest.fn(),
+ onCustomSelect = jest.fn(),
onColorChange = jest.fn(),
onColorReset = jest.fn(),
}: {
savedThemes?: EmbeddingTheme[];
theme?: Parameters<typeof ThemeSelectorSection>[0]["theme"];
onThemeChange?: jest.Mock;
+ onCustomSelect?: jest.Mock;
onColorChange?: jest.Mock;
onColorReset?: jest.Mock;
} = {}) {
@@ -53,12 +55,19 @@ function setup({
savedThemes={savedThemes}
theme={theme}
onThemeChange={onThemeChange}
+ onCustomSelect={onCustomSelect}
onColorChange={onColorChange}
onColorReset={onColorReset}
/>,
);
- return { ...utils, onThemeChange, onColorChange, onColorReset };
+ return {
+ ...utils,
+ onThemeChange,
+ onCustomSelect,
+ onColorChange,
+ onColorReset,
+ };
}
describe("ThemeSelectorSection", () => {
@@ -171,6 +180,72 @@ describe("ThemeSelectorSection", () => {
expect(screen.getByText("Background color")).toBeInTheDocument();
});
+ it("initializes Custom from no theme when no theme was previously selected", async () => {
+ const { onCustomSelect } = setup();
+
+ await userEvent.click(screen.getByTestId("theme-card-Custom"));
+
+ expect(onCustomSelect).toHaveBeenCalledTimes(1);
+ expect(onCustomSelect).toHaveBeenCalledWith(undefined);
+ });
+
+ it("initializes Custom with the configurable colors of the previously selected saved theme", async () => {
+ const { onCustomSelect } = setup();
+
+ await userEvent.click(screen.getByTestId("theme-card-Dark Theme"));
+ await userEvent.click(screen.getByTestId("theme-card-Custom"));
+
+ expect(onCustomSelect).toHaveBeenCalledTimes(1);
+ expect(onCustomSelect).toHaveBeenCalledWith({
+ brand: "#BB86FC",
+ "text-primary": "#FFFFFF",
+ background: "#121212",
+ });
+ });
+
+ it("ignores non-configurable settings (e.g. fontFamily) when initializing Custom from a saved theme", async () => {
+ const themesWithExtras: EmbeddingTheme[] = [
+ {
+ id: 10,
+ name: "Extras Theme",
+ settings: {
+ fontFamily: "Inter",
+ colors: {
+ brand: "#111111",
+ "text-primary": "#222222",
+ background: "#333333",
+ // Not in the configurable set:
+ "text-secondary": "#999999",
+ },
+ },
+ created_at: "2024-01-01T00:00:00Z",
+ updated_at: "2024-01-01T00:00:00Z",
+ },
+ ];
+
+ const { onCustomSelect } = setup({ savedThemes: themesWithExtras });
+
+ await userEvent.click(screen.getByTestId("theme-card-Extras Theme"));
+ await userEvent.click(screen.getByTestId("theme-card-Custom"));
+
+ expect(onCustomSelect).toHaveBeenLastCalledWith({
+ brand: "#111111",
+ "text-primary": "#222222",
+ background: "#333333",
+ });
+ });
+
+ it("initializes Custom from no theme when switching from Instance theme", async () => {
+ const { onCustomSelect } = setup();
+
+ // Select a saved theme, then go back to Instance theme, then click Custom
+ await userEvent.click(screen.getByTestId("theme-card-Dark Theme"));
+ await userEvent.click(screen.getByTestId("theme-card-Instance theme"));
+ await userEvent.click(screen.getByTestId("theme-card-Custom"));
+
+ expect(onCustomSelect).toHaveBeenLastCalledWith(undefined);
+ });
+
it("hides color inputs when switching from Custom to Instance theme", async () => {
setup();
diff --git a/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/SelectEmbedOptionsStep.tsx b/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/SelectEmbedOptionsStep.tsx
index eff1e3c4f0a..f9553c7adb9 100644
--- a/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/SelectEmbedOptionsStep.tsx
+++ b/frontend/src/metabase/embedding/embedding-iframe-sdk-setup/components/SelectEmbedOptionsStep.tsx
@@ -345,6 +345,15 @@ const AppearanceSection = () => {
[updateSettings],
);
+ const initializeCustomTheme = useCallback(
+ (initialColors: Partial<MetabaseColors> | undefined) => {
+ updateSettings({
+ theme: initialColors ? { colors: initialColors } : undefined,
+ } satisfies Partial<typeof settings>);
+ },
+ [updateSettings],
+ );
+
const updateThemePreset = useCallback(
(preset: MetabaseThemePreset) => {
updateSettings({ theme: { preset } } satisfies Partial<typeof settings>);
@@ -420,6 +429,7 @@ const AppearanceSection = () => {
savedThemes={savedThemes ?? []}
theme={theme}
onThemeChange={updateThemeId}
+ onCustomSelect={initializeCustomTheme}
onColorChange={updateColors}
onColorReset={resetTheme}
/>