The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Properly apply transparent background for Public Embedding (#62394)
Timofey Kachalovbf1932af146f
diff --git a/e2e/support/helpers/e2e-embedding-helpers.js b/e2e/support/helpers/e2e-embedding-helpers.jsindex 030e3145be5..4281148bfbe 100644--- a/e2e/support/helpers/e2e-embedding-helpers.js+++ b/e2e/support/helpers/e2e-embedding-helpers.js@@ -31,10 +31,11 @@ import { openSharingMenu } from "./e2e-sharing-helpers";* @param {EmbedPayload} payload - The {@link EmbedPayload} we pass to this function* @param {object} [options]* @param {object} [options.setFilters]- * @param {PageStyle} options.pageStyle+ * @param {PageStyle} [options.pageStyle]* @param {object} [options.additionalHashOptions]* @param {string} [options.additionalHashOptions.locale]* @param {string[]} [options.additionalHashOptions.hideFilters]+ * @param {(window: Window) => void} [options.onBeforeLoad]* @param {object} [options.qs]** @examplediff --git a/e2e/support/helpers/e2e-misc-helpers.js b/e2e/support/helpers/e2e-misc-helpers.jsindex 87a3eb0b875..461cf41b47e 100644--- a/e2e/support/helpers/e2e-misc-helpers.js+++ b/e2e/support/helpers/e2e-misc-helpers.js@@ -392,8 +392,12 @@ export function visitPublicQuestion(id, { params = {}, hash = {} } = {}) {* @param {object} options* @param {Record<string, string>} options.params* @param {Record<string, string>} options.hash+ * @param {(window: Window) => void} [options.onBeforeLoad]*/-export function visitPublicDashboard(id, { params = {}, hash = {} } = {}) {+export function visitPublicDashboard(+ id,+ { params = {}, hash = {}, onBeforeLoad } = {},+) {const searchParams = new URLSearchParams(params).toString();const searchSection = searchParams ? `?${searchParams}` : "";const hashParams = new URLSearchParams(hash).toString();@@ -404,6 +408,7 @@ export function visitPublicDashboard(id, { params = {}, hash = {} } = {}) {cy.signOut();cy.visit({url: `/public/dashboard/${uuid}` + searchSection + hashSection,+ onBeforeLoad,});},);diff --git a/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.js b/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.jsindex 8b5cd616e9c..ce9e2a4f9e1 100644--- a/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.js+++ b/e2e/test/scenarios/embedding/embedding-dashboard.cy.spec.js@@ -1060,6 +1060,67 @@ describe("scenarios > embedding > dashboard appearance", () => {H.main().should("have.css", "font-family", "Roboto, sans-serif");});+ it("should disable background via `#background=false` hash parameter when rendered inside an iframe (metabase#62391)", () => {+ cy.request("PUT", `/api/dashboard/${ORDERS_DASHBOARD_ID}`, {+ enable_embedding: true,+ });+ cy.signOut();++ H.visitEmbeddedPage(+ {+ resource: { dashboard: ORDERS_DASHBOARD_ID },+ params: {},+ },+ {+ additionalHashOptions: {+ background: "false",+ },+ onBeforeLoad: (window) => {+ window.overrideIsWithinIframe = true;+ },+ },+ );++ cy.findByTestId("embed-frame").should("exist");++ cy.get("body.mb-wrapper").should(+ "have.css",+ "background-color",+ "rgba(0, 0, 0, 0)",+ );++ cy.window().then((win) => {+ delete win.overrideIsWithinIframe;+ });+ });++ it("should not disable background via `#background=false` hash parameter when rendered without an iframe", () => {+ cy.request("PUT", `/api/dashboard/${ORDERS_DASHBOARD_ID}`, {+ enable_embedding: true,+ });+ cy.signOut();++ H.visitEmbeddedPage(+ {+ resource: { dashboard: ORDERS_DASHBOARD_ID },+ params: {},+ },+ {+ additionalHashOptions: {+ background: "false",+ },+ },+ );++ cy.findByTestId("embed-frame").should("exist");++ cy.get("body.mb-wrapper").should(+ "not.have.css",+ "background-color",+ "rgba(0, 0, 0, 0)",+ );+ });+it("should use transparent pivot table cells in static embedding's dark mode (metabase#61741)", () => {const testQuery = {type: "query",diff --git a/e2e/test/scenarios/sharing/public-dashboard.cy.spec.js b/e2e/test/scenarios/sharing/public-dashboard.cy.spec.jsindex 1549e2e0479..a4c29d1d1f1 100644--- a/e2e/test/scenarios/sharing/public-dashboard.cy.spec.js+++ b/e2e/test/scenarios/sharing/public-dashboard.cy.spec.js@@ -346,4 +346,43 @@ describe("scenarios [EE] > public > dashboard", () => {cy.url().should("include", "locale=de");});++ it("should disable background via `#background=false` hash parameter when rendered inside an iframe (metabase#62391)", () => {+ cy.get("@dashboardId").then((id) => {+ H.visitPublicDashboard(id, {+ hash: { background: "false" },+ onBeforeLoad: (window) => {+ window.overrideIsWithinIframe = true;+ },+ });+ });++ cy.findByTestId("embed-frame").should("exist");++ cy.get("body.mb-wrapper").should(+ "have.css",+ "background-color",+ "rgba(0, 0, 0, 0)",+ );++ cy.window().then((win) => {+ delete win.overrideIsWithinIframe;+ });+ });++ it("should not disable background via `#background=false` hash parameter when rendered without an iframe", () => {+ cy.get("@dashboardId").then((id) => {+ H.visitPublicDashboard(id, {+ hash: { background: "false" },+ });+ });++ cy.findByTestId("embed-frame").should("exist");++ cy.get("body.mb-wrapper").should(+ "not.have.css",+ "background-color",+ "rgba(0, 0, 0, 0)",+ );+ });});diff --git a/frontend/src/metabase/app-embed.js b/frontend/src/metabase/app-embed.jsindex 12a0ca9c253..2c716c1912a 100644--- a/frontend/src/metabase/app-embed.js+++ b/frontend/src/metabase/app-embed.js@@ -3,14 +3,8 @@* file 'LICENSE-EMBEDDING.txt', which is part of this source code package.*/-import { isWithinIframe } from "metabase/lib/dom";-import { init } from "./app";import { publicReducers } from "./reducers-public";import { getRoutes } from "./routes-embed";-init(publicReducers, getRoutes, () => {- if (isWithinIframe()) {- document.body.style.backgroundColor = "transparent";- }-});+init(publicReducers, getRoutes, () => {});diff --git a/frontend/src/metabase/lib/dom.js b/frontend/src/metabase/lib/dom.jsindex 75ca220eff5..9b10ab75a86 100644--- a/frontend/src/metabase/lib/dom.js+++ b/frontend/src/metabase/lib/dom.js@@ -15,15 +15,15 @@ export const getScrollY = () =>// Storybook also uses an iframe to display story content, so we want to ignore itexport const isWithinIframe = function () {try {- if (isCypressActive || isStorybookActive) {- return false;- }-- // Mock that we're embedding, so we could visual test embed components+ // Mock that we're embedding, so we could test embed componentsif (window.overrideIsWithinIframe) {return true;}+ if (isCypressActive || isStorybookActive) {+ return false;+ }+return window.self !== window.top;} catch (e) {return true;diff --git a/frontend/src/metabase/public/containers/PublicApp/PublicApp.tsx b/frontend/src/metabase/public/containers/PublicApp/PublicApp.tsxindex 642697b97c8..2506cd570dd 100644--- a/frontend/src/metabase/public/containers/PublicApp/PublicApp.tsx+++ b/frontend/src/metabase/public/containers/PublicApp/PublicApp.tsx@@ -1,3 +1,6 @@+import { useLayoutEffect } from "react";++import { isWithinIframe } from "metabase/lib/dom";import { connect } from "metabase/lib/redux";import { PublicError } from "metabase/public/components/PublicError";import { PublicNotFound } from "metabase/public/components/PublicNotFound";@@ -22,9 +25,16 @@ function mapStateToProps(state: State) {}function PublicApp({ errorPage, children }: Props) {+ useLayoutEffect(() => {+ if (isWithinIframe()) {+ document.body.style.backgroundColor = "transparent";+ }+ }, []);+if (errorPage) {return errorPage.status === 404 ? <PublicNotFound /> : <PublicError />;}+return (<>{children}diff --git a/frontend/src/metabase/public/containers/PublicOrEmbeddedDashboard/PublicOrEmbeddedDashboardView-filters.stories.tsx b/frontend/src/metabase/public/containers/PublicOrEmbeddedDashboard/PublicOrEmbeddedDashboardView-filters.stories.tsxindex f16cbf2da7d..00ca63a32b7 100644--- a/frontend/src/metabase/public/containers/PublicOrEmbeddedDashboard/PublicOrEmbeddedDashboardView-filters.stories.tsx+++ b/frontend/src/metabase/public/containers/PublicOrEmbeddedDashboard/PublicOrEmbeddedDashboardView-filters.stories.tsx@@ -62,7 +62,6 @@ export default {decorators: [ReduxDecorator,createWaitForResizeToStopDecorator(TIME_UNTIL_ALL_ELEMENTS_STOP_RESIZING),- MockIsEmbeddingDecorator,],parameters: {layout: "fullscreen",@@ -150,16 +149,6 @@ function ReduxDecorator(Story: StoryFn, context: StoryContext) {);}-declare global {- interface Window {- overrideIsWithinIframe?: boolean;- }-}-function MockIsEmbeddingDecorator(Story: StoryFn) {- window.overrideIsWithinIframe = true;- return <Story />;-}-const DASHBOARD_ID = getNextId();const DASHCARD_BAR_ID = getNextId();const DASHCARD_TABLE_ID = getNextId();diff --git a/frontend/src/metabase/public/containers/PublicOrEmbeddedDashboard/PublicOrEmbeddedDashboardView.stories.tsx b/frontend/src/metabase/public/containers/PublicOrEmbeddedDashboard/PublicOrEmbeddedDashboardView.stories.tsxindex e61e64f82ff..6688cff3ee0 100644--- a/frontend/src/metabase/public/containers/PublicOrEmbeddedDashboard/PublicOrEmbeddedDashboardView.stories.tsx+++ b/frontend/src/metabase/public/containers/PublicOrEmbeddedDashboard/PublicOrEmbeddedDashboardView.stories.tsx@@ -50,11 +50,7 @@ registerVisualization(BarChart);export default {title: "App/Embed/PublicOrEmbeddedDashboardView",component: PublicOrEmbeddedDashboardView,- decorators: [- ReduxDecorator,- createWaitForResizeToStopDecorator(),- MockIsEmbeddingDecorator,- ],+ decorators: [ReduxDecorator, createWaitForResizeToStopDecorator()],parameters: {layout: "fullscreen",msw: {@@ -113,16 +109,6 @@ function ReduxDecorator(Story: StoryFn, context: StoryContext) {);}-declare global {- interface Window {- overrideIsWithinIframe?: boolean;- }-}-function MockIsEmbeddingDecorator(Story: StoryFn) {- window.overrideIsWithinIframe = true;- return <Story />;-}-const DASHBOARD_ID = getNextId();const DASHCARD_BAR_ID = getNextId();const DASHCARD_TABLE_ID = getNextId();diff --git a/frontend/src/metabase/public/containers/PublicOrEmbeddedQuestion/PublicOrEmbeddedQuestionView.stories.tsx b/frontend/src/metabase/public/containers/PublicOrEmbeddedQuestion/PublicOrEmbeddedQuestionView.stories.tsxindex 9698ae0480c..f7014507630 100644--- a/frontend/src/metabase/public/containers/PublicOrEmbeddedQuestion/PublicOrEmbeddedQuestionView.stories.tsx+++ b/frontend/src/metabase/public/containers/PublicOrEmbeddedQuestion/PublicOrEmbeddedQuestionView.stories.tsx@@ -49,11 +49,7 @@ registerVisualization(BarChart);export default {title: "App/Embed/PublicOrEmbeddedQuestionView",component: PublicOrEmbeddedQuestionView,- decorators: [- ReduxDecorator,- createWaitForResizeToStopDecorator(),- MockIsEmbeddingDecorator,- ],+ decorators: [ReduxDecorator, createWaitForResizeToStopDecorator()],parameters: {layout: "fullscreen",msw: {@@ -87,16 +83,6 @@ function ReduxDecorator(Story: StoryFn) {);}-declare global {- interface Window {- overrideIsWithinIframe?: boolean;- }-}-function MockIsEmbeddingDecorator(Story: StoryFn) {- window.overrideIsWithinIframe = true;- return <Story />;-}-const CARD_BAR_ID = getNextId();const initialState = createMockState({settings: createMockSettingsState({