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
fix(sdk-bundle): host style not respected when navigating between dashboards in InteractiveDashboard (#74345)
Mahatthana (Kelvin) Nomsawadi21abb3dc9ba1
diff --git a/e2e/test-component/scenarios/embedding-sdk/internal-navigation.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/internal-navigation.cy.spec.tsxindex 974365d8b2e..672c3490862 100644--- a/e2e/test-component/scenarios/embedding-sdk/internal-navigation.cy.spec.tsx+++ b/e2e/test-component/scenarios/embedding-sdk/internal-navigation.cy.spec.tsx@@ -87,7 +87,7 @@ describe("scenarios > embedding-sdk > internal-navigation", () => {row: 0,col: 0,size_x: 24,- size_y: 8,+ size_y: 30,parameter_mappings: [{parameter_id: DASHBOARD_B_FILTER.id,@@ -623,6 +623,47 @@ describe("scenarios > embedding-sdk > internal-navigation", () => {cy.findByText(/Back to/).should("not.exist");});});++ it("should keep filters sticky on the navigated dashboard (EMB-1746)", () => {+ cy.get<number>("@dashboardAId").then((dashboardAId) => {+ mountSdkContent(+ <InteractiveDashboard+ dashboardId={dashboardAId}+ enableEntityNavigation+ style={{ height: 400 }}+ />,+ );+ });++ cy.wait("@getDashboard");+ cy.wait("@dashcardQuery");++ getSdkRoot().within(() => {+ cy.findByText("Dashboard A").should("be.visible");++ cy.log("Navigate to Dashboard B via custom click behavior");+ H.getDashboardCard().findAllByText("Go to Dashboard B").first().click();+ cy.wait("@getDashboard");+ cy.findByText("Dashboard B").should("be.visible");+ H.filterWidget().should("be.visible");++ // Pre-fix: nested wrapper dropped user height, so the scroll context+ // was gone and the filter row scrolled off with the content.+ cy.log("Scroll dashboard; filter row must stay pinned at wrapper top");+ cy.findByTestId("sdk-dashboard-styled-wrapper").scrollTo("bottom");++ cy.findByTestId("sdk-dashboard-styled-wrapper").then(($wrapper) => {+ const wrapperTop = $wrapper[0].getBoundingClientRect().top;++ H.filterWidget()+ .should("be.visible")+ .and(($filter) => {+ const filterTop = $filter[0].getBoundingClientRect().top;+ expect(filterTop).to.be.closeTo(wrapperTop, 20);+ });+ });+ });+ });});describe("same-dashboard navigation (EMB-1714)", () => {diff --git a/frontend/src/embedding-sdk-bundle/components/private/SdkInternalNavigation/SdkInternalNavigationProvider.tsx b/frontend/src/embedding-sdk-bundle/components/private/SdkInternalNavigation/SdkInternalNavigationProvider.tsxindex 03ccfd69f6b..191e30e461a 100644--- a/frontend/src/embedding-sdk-bundle/components/private/SdkInternalNavigation/SdkInternalNavigationProvider.tsx+++ b/frontend/src/embedding-sdk-bundle/components/private/SdkInternalNavigation/SdkInternalNavigationProvider.tsx@@ -137,6 +137,19 @@ const SdkInternalNavigationProviderInner = ({[stack.length],);+ // "Virtual" entries are entries that are rendered by the previous entity (ie: drills, new question from dashboard)+ // we don't have to render them, but we need them in the stack to make the back button work correctly+ const nonVirtualEntries = useMemo(+ () => stack.filter((entry) => !entry.virtual),+ [stack],+ );++ const entryToRender = nonVirtualEntries.at(-1);+ const entryIndex = entryToRender ? stack.indexOf(entryToRender) : -1;+ // If the entry is the original entry, we just need to return the children.+ const entryIsOriginalEntity = stack.length === 0 || entryIndex === 0;+ const hasNavigatedToEntity = !entryIsOriginalEntity;+const value = useMemo(() => ({stack,@@ -149,22 +162,11 @@ const SdkInternalNavigationProviderInner = ({// the breadcrumbs.canGoBack: stack.filter((e) => e.type !== "metabase-browser").length > 1,initWithDashboard,+ hasNavigatedToEntity,}),- [stack, push, pop, initWithDashboard],+ [stack, push, pop, initWithDashboard, hasNavigatedToEntity],);- // "Virtual" entries are entries that are rendered by the previous entity (ie: drills, new question from dashboard)- // we don't have to render them, but we need them in the stack to make the back button work correctly- const nonVirtualEntries = useMemo(- () => stack.filter((entry) => !entry.virtual),- [stack],- );-- const entryToRender = nonVirtualEntries.at(-1);- const entryIndex = entryToRender ? stack.indexOf(entryToRender) : -1;- // If the entry is the original entry, we just need to return the children.- const entryIsOriginalEntity = stack.length === 0 || entryIndex === 0;-const shouldRenderBackButton = match(stack.at(-1)?.type ?? null).with(null, () => false).with("dashboard", () => true)@@ -182,8 +184,6 @@ const SdkInternalNavigationProviderInner = ({.with({ activeEntry: { type: "dashboard" } }, ({ activeEntry }) => (<InteractiveDashboardContent{...dashboardProps}- style={undefined}- className={undefined}dashboardId={activeEntry.id}initialParameters={activeEntry.parameters}enableEntityNavigationdiff --git a/frontend/src/embedding-sdk-bundle/components/private/SdkInternalNavigation/context.tsx b/frontend/src/embedding-sdk-bundle/components/private/SdkInternalNavigation/context.tsxindex 0597e780574..a8390440275 100644--- a/frontend/src/embedding-sdk-bundle/components/private/SdkInternalNavigation/context.tsx+++ b/frontend/src/embedding-sdk-bundle/components/private/SdkInternalNavigation/context.tsx@@ -67,6 +67,12 @@ export type SdkInternalNavigationContextValue = {canGoBack: boolean;previousEntry: SdkInternalNavigationEntry | undefined;initWithDashboard: (dashboard: { id: SdkDashboardId; name: string }) => void;+ /**+ * True when nav stack has pushed beyond the original entity. SdkDashboard reads+ * this to skip its own styled wrapper so the outer wrapper from the provider+ * stays the sole styled container (height/sticky/scroll behavior preserved).+ */+ hasNavigatedToEntity: boolean;};export const SdkInternalNavigationContext =diff --git a/frontend/src/embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion.unit.spec.tsx b/frontend/src/embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion.unit.spec.tsxindex 0c0c064cc74..33cfa0592a4 100644--- a/frontend/src/embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion.unit.spec.tsx+++ b/frontend/src/embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion.unit.spec.tsx@@ -112,6 +112,7 @@ function makeMockNavigation(currentEntry: undefined,previousEntry: undefined,initWithDashboard: jest.fn(),+ hasNavigatedToEntity: false,...overrides,};}diff --git a/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboard.tsx b/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboard.tsxindex 527cbf89d56..05ad3d90124 100644--- a/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboard.tsx+++ b/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboard.tsx@@ -79,13 +79,29 @@ import type {SdkQuestionProps,} from "../SdkQuestion";-import {- SdkDashboardStyledWrapper,- SdkDashboardStyledWrapperWithRef,-} from "./SdkDashboardStyleWrapper";+import { SdkDashboardStyledWrapper } from "./SdkDashboardStyleWrapper";import { SdkDashboardProvider } from "./context";import { useCommonDashboardParams } from "./use-common-dashboard-params";+const MaybeStyledWrapper = ({+ skip,+ className,+ style,+ children,+}: {+ skip: boolean;+ className?: string;+ style?: React.CSSProperties;+ children: React.ReactNode;+}) =>+ skip ? (+ <>{children}</>+ ) : (+ <SdkDashboardStyledWrapper className={className} style={style}>+ {children}+ </SdkDashboardStyledWrapper>+ );+/*** @interface* @inline@@ -368,6 +384,11 @@ const SdkDashboardInner = ({const isDashboardDirty = useSelector(getIsDirty);const sdkNavigation = useSdkInternalNavigationOptional();+ // When this SdkDashboard renders below a nav-stack push, the outer+ // SdkInternalNavigationProvider already wraps with SdkDashboardStyledWrapper+ // carrying user style/className. Skip our own wrapper to avoid double+ // wrapping (which drops user height/sticky/scroll behavior).+ const skipStyledWrapper = !!sdkNavigation?.hasNavigatedToEntity;// Initialize navigation stack with dashboard entry when we have the nameuseEffect(() => {@@ -419,17 +440,25 @@ const SdkDashboardInner = ({if (isLocaleLoading) {return (- <SdkDashboardStyledWrapper className={className} style={style}>+ <MaybeStyledWrapper+ skip={skipStyledWrapper}+ className={className}+ style={style}+ ><SdkLoader />- </SdkDashboardStyledWrapper>+ </MaybeStyledWrapper>);}if (tokenError) {return (- <SdkDashboardStyledWrapper className={className} style={style}>+ <MaybeStyledWrapper+ skip={skipStyledWrapper}+ className={className}+ style={style}+ ><SdkError message={tokenError} />;- </SdkDashboardStyledWrapper>+ </MaybeStyledWrapper>);}@@ -439,9 +468,13 @@ const SdkDashboardInner = ({if (isStaticEmbeddingEntityLoadingError(errorPage, { isGuestEmbed })) {return (- <SdkDashboardStyledWrapper className={className} style={style}>+ <MaybeStyledWrapper+ skip={skipStyledWrapper}+ className={className}+ style={style}+ ><SdkError message={errorPage.data ?? t`Something's gone wrong`} />- </SdkDashboardStyledWrapper>+ </MaybeStyledWrapper>);}@@ -452,19 +485,27 @@ const SdkDashboardInner = ({if (!dashboardId || isDashboardNotFound) {return (- <SdkDashboardStyledWrapper className={className} style={style}>+ <MaybeStyledWrapper+ skip={skipStyledWrapper}+ className={className}+ style={style}+ ><DashboardNotFoundError id={dashboardId ?? ""} />- </SdkDashboardStyledWrapper>+ </MaybeStyledWrapper>);}if (errorPage) {return (- <SdkDashboardStyledWrapper className={className} style={style}>+ <MaybeStyledWrapper+ skip={skipStyledWrapper}+ className={className}+ style={style}+ ><SdkErrormessage={errorPage.data?.message ?? t`Something's gone wrong`}/>- </SdkDashboardStyledWrapper>+ </MaybeStyledWrapper>);}@@ -538,7 +579,8 @@ const SdkDashboardInner = ({>{match({ finalRenderMode, isGuestEmbed }).with({ finalRenderMode: "question" }, () => (- <SdkDashboardStyledWrapperWithRef+ <MaybeStyledWrapper+ skip={skipStyledWrapper}className={className}style={style}>@@ -551,7 +593,7 @@ const SdkDashboardInner = ({>{AdHocQuestionView && <AdHocQuestionView />}</SdkAdHocQuestion>- </SdkDashboardStyledWrapperWithRef>+ </MaybeStyledWrapper>)).with({ finalRenderMode: "dashboard" }, () => (<SdkDashboardProvider@@ -559,23 +601,28 @@ const SdkDashboardInner = ({onEditQuestion={onEditQuestion}>{children ?? (- <SdkDashboardStyledWrapperWithRef+ <MaybeStyledWrapper+ skip={skipStyledWrapper}className={className}style={style}><Dashboard className={EmbedFrameS.EmbedFrame} /><AutoRefreshController refreshPeriod={autoRefreshInterval} />- </SdkDashboardStyledWrapperWithRef>+ </MaybeStyledWrapper>)}</SdkDashboardProvider>)).with({ finalRenderMode: "queryBuilder" }, ({ isGuestEmbed }) =>isGuestEmbed ? (- <SdkDashboardStyledWrapper className={className} style={style}>+ <MaybeStyledWrapper+ skip={skipStyledWrapper}+ className={className}+ style={style}+ ><SdkErrormessage={t`You can't save questions in Guest Embed mode`}/>- </SdkDashboardStyledWrapper>+ </MaybeStyledWrapper>) : (<DashboardQueryBuilderonCreate={(question) => {diff --git a/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboardStyleWrapper.tsx b/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboardStyleWrapper.tsxindex 7bd56586e15..92e33ac4b4c 100644--- a/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboardStyleWrapper.tsx+++ b/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboardStyleWrapper.tsx@@ -1,51 +1,31 @@import cx from "classnames";-import { type PropsWithChildren, forwardRef } from "react";+import type { PropsWithChildren } from "react";import type { CommonStylingProps } from "embedding-sdk-bundle/types/props";import CS from "metabase/css/core/index.css";-import { useDashboardContext } from "metabase/dashboard/context";import { Flex } from "metabase/ui";import SdkDashboardStyleWrapperS from "./SdkDashboardStyleWrapper.module.css";-export const SdkDashboardStyledWrapper = forwardRef(- function SdkDashboardStyledWrapperInner(- { className, style, children }: PropsWithChildren<CommonStylingProps>,- fullscreenRef: React.Ref<HTMLDivElement>,- ) {- return (- <Flex- direction="column"- justify="flex-start"- align="stretch"- className={cx(- className,- SdkDashboardStyleWrapperS.SdkDashboardStyleWrapper,- CS.overflowAuto,- )}- style={style}- ref={fullscreenRef}- >- {children}- </Flex>- );- },-);--export const SdkDashboardStyledWrapperWithRef = ({+export const SdkDashboardStyledWrapper = ({className,style,children,}: PropsWithChildren<CommonStylingProps>) => {- const { fullscreenRef } = useDashboardContext();-return (- <SdkDashboardStyledWrapper- className={className}+ <Flex+ direction="column"+ justify="flex-start"+ align="stretch"+ className={cx(+ className,+ SdkDashboardStyleWrapperS.SdkDashboardStyleWrapper,+ CS.overflowAuto,+ )}style={style}- ref={fullscreenRef}+ data-testid="sdk-dashboard-styled-wrapper">{children}- </SdkDashboardStyledWrapper>+ </Flex>);};