Skip to content

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

feat(sdk,embed-js): Adjust mobile layout for SDK question topbar (#72853)

Timofey Kachalov6c5b3fa5a3c1
diff --git a/e2e/embedding-sdk-host-apps/next-15-app-router-host-app/src/app/interactive-question/page.tsx b/e2e/embedding-sdk-host-apps/next-15-app-router-host-app/src/app/interactive-question/page.tsx
index e1d60e81f9a..0d2e247c51e 100644
--- a/e2e/embedding-sdk-host-apps/next-15-app-router-host-app/src/app/interactive-question/page.tsx
+++ b/e2e/embedding-sdk-host-apps/next-15-app-router-host-app/src/app/interactive-question/page.tsx
@@ -17,7 +17,7 @@ export default function StaticDashboardPage() {
}, [searchParams]);
return (
- <main style={{ padding: "1rem" }}>
+ <main style={{ padding: "1rem", width: "800px" }}>
<h1 style={{ marginBottom: "4rem" }}>Interactive Question Example</h1>
<InteractiveQuestion questionId={questionId} />
</main>
diff --git a/e2e/embedding-sdk-host-apps/next-15-pages-router-host-app/src/pages/interactive-question.tsx b/e2e/embedding-sdk-host-apps/next-15-pages-router-host-app/src/pages/interactive-question.tsx
index 8cfaf04888b..748834b068c 100644
--- a/e2e/embedding-sdk-host-apps/next-15-pages-router-host-app/src/pages/interactive-question.tsx
+++ b/e2e/embedding-sdk-host-apps/next-15-pages-router-host-app/src/pages/interactive-question.tsx
@@ -15,7 +15,7 @@ export default function StaticDashboardPage() {
}, [searchParams]);
return (
- <main style={{ padding: "1rem" }}>
+ <main style={{ padding: "1rem", width: "800px" }}>
<h1 style={{ marginBottom: "4rem" }}>Interactive Question Example</h1>
<InteractiveQuestion questionId={questionId} />
</main>
diff --git a/e2e/embedding-sdk-host-apps/vite-6-host-app/src/App.jsx b/e2e/embedding-sdk-host-apps/vite-6-host-app/src/App.jsx
index 40f28301ad1..2a45581abe0 100644
--- a/e2e/embedding-sdk-host-apps/vite-6-host-app/src/App.jsx
+++ b/e2e/embedding-sdk-host-apps/vite-6-host-app/src/App.jsx
@@ -30,7 +30,7 @@ function App() {
);
return (
- <div className="App" style={{ width: "500px", height: "800px" }}>
+ <div className="App" style={{ width: "800px", height: "800px" }}>
<MetabaseProvider authConfig={config} locale={locale}>
<Switch>
<Route
diff --git a/e2e/test-component/scenarios/embedding-sdk/sdk-question.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/sdk-question.cy.spec.tsx
index d2698465587..6fdd1f1a4ba 100644
--- a/e2e/test-component/scenarios/embedding-sdk/sdk-question.cy.spec.tsx
+++ b/e2e/test-component/scenarios/embedding-sdk/sdk-question.cy.spec.tsx
@@ -490,6 +490,48 @@ describe("scenarios > embedding-sdk > interactive-question", () => {
});
});
+ describe("mobile layout", () => {
+ it("should hide Filter, Summarize, Breakout and Download dropdowns when the question's container is narrow", () => {
+ cy.intercept("GET", "/api/card/*").as("getCard");
+
+ cy.get<number>("@questionId").then((questionId) => {
+ mountSdkContent(
+ <div style={{ width: 400 }}>
+ <InteractiveQuestion questionId={questionId} withDownloads />
+ </div>,
+ );
+ });
+
+ cy.wait("@getCard");
+
+ getSdkRoot()
+ .findByTestId("interactive-question-result-toolbar")
+ .within(() => {
+ cy.get(".Icon-filter").should("not.exist");
+ cy.get(".Icon-sum").should("not.exist");
+ cy.get(".Icon-arrow_split").should("not.exist");
+ cy.findByTestId("question-download-widget-button").should(
+ "not.exist",
+ );
+ });
+ });
+
+ it("should show Filter, Summarize, Breakout and Download dropdowns when the question's container is wide", () => {
+ mountInteractiveQuestion({ withDownloads: true });
+
+ getSdkRoot()
+ .findByTestId("interactive-question-result-toolbar")
+ .within(() => {
+ cy.get(".Icon-filter").should("be.visible");
+ cy.get(".Icon-sum").should("be.visible");
+ cy.get(".Icon-arrow_split").should("be.visible");
+ cy.findByTestId("question-download-widget-button").should(
+ "be.visible",
+ );
+ });
+ });
+ });
+
describe("loading behavior for both entity IDs and number IDs (metabase#49581)", () => {
const successTestCases = [
{
diff --git a/e2e/test-component/scenarios/embedding-sdk/static-question.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/static-question.cy.spec.tsx
index a6624200840..9621da8db43 100644
--- a/e2e/test-component/scenarios/embedding-sdk/static-question.cy.spec.tsx
+++ b/e2e/test-component/scenarios/embedding-sdk/static-question.cy.spec.tsx
@@ -12,6 +12,7 @@ import { getSdkRoot } from "e2e/support/helpers/e2e-embedding-sdk-helpers";
import {
DEFAULT_SDK_AUTH_PROVIDER_CONFIG,
mountSdk,
+ mountSdkContent,
mountStaticQuestion,
} from "e2e/support/helpers/embedding-sdk-component-testing";
import { signInAsAdminAndEnableEmbeddingSdk } from "e2e/support/helpers/embedding-sdk-testing";
@@ -248,5 +249,27 @@ describe("scenarios > embedding-sdk > static-question", () => {
getSdkRoot().button("Alerts").should("be.visible").click();
modal().findByRole("heading", { name: "New alert" }).should("be.visible");
});
+
+ it("should hide the Alerts button when the question's container is narrow", () => {
+ cy.intercept("GET", "/api/card/*").as("getCard");
+
+ cy.get<number>("@questionId").then((questionId) => {
+ mountSdkContent(
+ <div style={{ width: 400 }}>
+ <StaticQuestion questionId={questionId} withAlerts />
+ </div>,
+ );
+ });
+
+ cy.wait("@getCard");
+
+ getSdkRoot().button("Alerts").should("not.exist");
+ });
+
+ it("should show the Alerts button when the question's container is wide", () => {
+ mountStaticQuestion({ withAlerts: true });
+
+ getSdkRoot().button("Alerts").should("be.visible");
+ });
});
});
diff --git a/frontend/src/embedding-sdk-bundle/components/private/FlexibleSizeComponent.tsx b/frontend/src/embedding-sdk-bundle/components/private/FlexibleSizeComponent.tsx
index 6cb9c25c291..e5d0164760e 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/FlexibleSizeComponent.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/private/FlexibleSizeComponent.tsx
@@ -1,4 +1,4 @@
-import type { CSSProperties, PropsWithChildren } from "react";
+import { type CSSProperties, type PropsWithChildren, forwardRef } from "react";
import type { CommonStylingProps } from "embedding-sdk-bundle/types/props";
import { Box } from "metabase/ui";
@@ -23,23 +23,37 @@ export type FlexibleSizeProps = PropsWithChildren<
}
>;
-export const FlexibleSizeComponent = ({
- height: propHeight = FLEXIBLE_SIZE_DEFAULT_HEIGHT,
- width: propWidth = FLEXIBLE_SIZE_DEFAULT_WIDTH,
- className,
- style,
- children,
-}: FlexibleSizeProps) => (
- <Box h={propHeight} w={propWidth} mih={propHeight} miw={propWidth}>
+export const FlexibleSizeComponent = forwardRef<
+ HTMLDivElement,
+ FlexibleSizeProps
+>(function FlexibleSizeComponent(
+ {
+ height: propHeight = FLEXIBLE_SIZE_DEFAULT_HEIGHT,
+ width: propWidth = FLEXIBLE_SIZE_DEFAULT_WIDTH,
+ className,
+ style,
+ children,
+ },
+ ref,
+) {
+ return (
<Box
- h="100%"
- w="100%"
- mih="100%"
- miw="100%"
- style={style}
- className={className}
+ ref={ref}
+ h={propHeight}
+ w={propWidth}
+ mih={propHeight}
+ miw={propWidth}
>
- {children}
+ <Box
+ h="100%"
+ w="100%"
+ mih="100%"
+ miw="100%"
+ style={style}
+ className={className}
+ >
+ {children}
+ </Box>
</Box>
- </Box>
-);
+ );
+});
diff --git a/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx b/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
index f89b9e2b52a..56922aaaf65 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
@@ -13,6 +13,7 @@ import { QuestionVisualization } from "embedding-sdk-bundle/components/private/S
import { SdkQuestion } from "embedding-sdk-bundle/components/public/SdkQuestion";
import { QuestionAlertsButton } from "embedding-sdk-bundle/components/public/notifications/QuestionAlertsButton";
import { useCollectionData } from "embedding-sdk-bundle/hooks/private/use-collection-data";
+import { useMobileLayout } from "embedding-sdk-bundle/hooks/private/use-mobile-layout";
import { useQuestionEditorSync } from "embedding-sdk-bundle/hooks/private/use-question-editor-sync";
import { useSdkBreadcrumbs } from "embedding-sdk-bundle/hooks/private/use-sdk-breadcrumb";
import { shouldRunCardQuery } from "embedding-sdk-bundle/lib/sdk-question";
@@ -159,6 +160,8 @@ export const SdkQuestionDefaultView = ({
{ skipCollectionFetching: !isSaveEnabled },
);
+ const { ref: containerRef, isMobile } = useMobileLayout();
+
if (
!isEditorOpen &&
(isLocaleLoading || isQuestionLoading || isQueryResultLoading)
@@ -183,6 +186,7 @@ export const SdkQuestionDefaultView = ({
return (
<FlexibleSizeComponent
+ ref={containerRef}
height={height}
width={width}
className={cx(InteractiveQuestionS.Container, className)}
@@ -232,7 +236,7 @@ export const SdkQuestionDefaultView = ({
<QuestionSettingsDropdown />
</Button.Group>
- {!isNativeQuestion && (
+ {!isNativeQuestion && !isMobile && (
<Divider
mx="xs"
orientation="vertical"
@@ -244,7 +248,7 @@ export const SdkQuestionDefaultView = ({
</>
)}
- {!isNativeQuestion && (
+ {!isNativeQuestion && !isMobile && (
<>
<FilterDropdown />
<SummarizeDropdown />
@@ -255,7 +259,7 @@ export const SdkQuestionDefaultView = ({
)}
</RenderIfHasContent>
<RenderIfHasContent component={Group} gap="sm" ml="auto">
- {!isEditorOpen && (
+ {!isEditorOpen && !isMobile && (
<>
<DownloadWidgetDropdown />
<QuestionAlertsButton />
diff --git a/frontend/src/embedding-sdk-bundle/components/public/StaticQuestion/StaticQuestion.tsx b/frontend/src/embedding-sdk-bundle/components/public/StaticQuestion/StaticQuestion.tsx
index 171f6261e69..619bc1ad56e 100644
--- a/frontend/src/embedding-sdk-bundle/components/public/StaticQuestion/StaticQuestion.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/public/StaticQuestion/StaticQuestion.tsx
@@ -29,6 +29,7 @@ import {
type SdkQuestionProps,
} from "embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion";
import { QuestionAlertsButton } from "embedding-sdk-bundle/components/public/notifications/QuestionAlertsButton";
+import { useMobileLayout } from "embedding-sdk-bundle/hooks/private/use-mobile-layout";
import { useNormalizeGuestEmbedQuestionOrDashboardComponentProps } from "embedding-sdk-bundle/hooks/private/use-normalize-guest-embed-question-or-dashboard-component-props";
import { useSdkSelector } from "embedding-sdk-bundle/store";
import { getIsGuestEmbed } from "embedding-sdk-bundle/store/selectors";
@@ -111,6 +112,8 @@ const StaticQuestionInner = (
const isGuestEmbed = useSdkSelector(getIsGuestEmbed);
+ const { ref: containerRef, isMobile } = useMobileLayout();
+
const getClickActionMode: ClickActionModeGetter = ({
question,
}: {
@@ -138,6 +141,7 @@ const StaticQuestionInner = (
>
{children ?? (
<FlexibleSizeComponent
+ ref={containerRef}
className={className}
width={width}
height={height}
@@ -166,7 +170,8 @@ const StaticQuestionInner = (
<RenderIfHasContent component={Group} gap="sm" ml="auto">
<SdkQuestion.DownloadWidgetDropdown />
- <QuestionAlertsButton />
+
+ {!isMobile && <QuestionAlertsButton />}
</RenderIfHasContent>
</RenderIfHasContent>
diff --git a/frontend/src/embedding-sdk-bundle/hooks/private/use-mobile-layout.ts b/frontend/src/embedding-sdk-bundle/hooks/private/use-mobile-layout.ts
new file mode 100644
index 00000000000..f0e62cde420
--- /dev/null
+++ b/frontend/src/embedding-sdk-bundle/hooks/private/use-mobile-layout.ts
@@ -0,0 +1,24 @@
+import { useCallback, useRef, useState } from "react";
+
+const SDK_MOBILE_BREAKPOINT = 640;
+
+export function useMobileLayout() {
+ const [width, setWidth] = useState(0);
+ const observerRef = useRef<ResizeObserver | null>(null);
+
+ const ref = useCallback((node: HTMLElement | null) => {
+ observerRef.current?.disconnect();
+
+ if (node) {
+ const observer = new ResizeObserver(([entry]) => {
+ setWidth(entry.contentBoxSize[0].inlineSize);
+ });
+ observer.observe(node);
+ observerRef.current = observer;
+ }
+ }, []);
+
+ const isMobile = width > 0 && width < SDK_MOBILE_BREAKPOINT;
+
+ return { ref, isMobile };
+}