Skip to content

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

Properly pass mantine-color-scheme (#73216)

Sébastien69ddfae14dc6
diff --git a/frontend/src/embedding-sdk-bundle/components/private/PublicComponentStylesWrapper.tsx b/frontend/src/embedding-sdk-bundle/components/private/PublicComponentStylesWrapper.tsx
index 555db212e7e..ed3d2004f37 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/PublicComponentStylesWrapper.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/private/PublicComponentStylesWrapper.tsx
@@ -6,6 +6,7 @@ import cx from "classnames";
import type React from "react";
import { forwardRef } from "react";
+import { useComputedColorScheme } from "metabase/ui";
import { saveDomImageStyles } from "metabase/visualizations/lib/image-exports";
import S from "./PublicComponentStylesWrapper.style.css";
@@ -24,14 +25,23 @@ export const PublicComponentStylesWrapper = forwardRef<
HTMLDivElement,
React.ComponentProps<"div">
>(function PublicComponentStylesWrapper(props, ref) {
+ // Mantine scopes its CSS variables to `cssVariablesSelector=".mb-wrapper"`,
+ // so the variables for the active color scheme only apply when the wrapper
+ // itself carries `data-mantine-color-scheme`. Read the resolved scheme from
+ // the parent MantineProvider so portals (e.g. the filter-changed toast in
+ // guest embeds with `theme.preset: "dark"`) inherit it instead of being
+ // pinned to light (EMB-1560).
+ const colorScheme = useComputedColorScheme("light", {
+ getInitialValueInEffect: false,
+ });
+
return (
<PublicComponentStylesWrapperInner
{...props}
ref={ref}
dir="ltr"
className={cx("mb-wrapper", S.publicComponentWrapper, props.className)}
- // Mantine's cssVariablesSelector expects data-mantine-color-scheme to be set on the target element
- data-mantine-color-scheme="light"
+ data-mantine-color-scheme={colorScheme}
/>
);
});
diff --git a/frontend/src/embedding-sdk-bundle/components/private/PublicComponentStylesWrapper.unit.spec.tsx b/frontend/src/embedding-sdk-bundle/components/private/PublicComponentStylesWrapper.unit.spec.tsx
new file mode 100644
index 00000000000..2c3689accb2
--- /dev/null
+++ b/frontend/src/embedding-sdk-bundle/components/private/PublicComponentStylesWrapper.unit.spec.tsx
@@ -0,0 +1,32 @@
+import { render, screen } from "@testing-library/react";
+
+import { ThemeProvider } from "metabase/ui";
+
+import { PublicComponentStylesWrapper } from "./PublicComponentStylesWrapper";
+
+const setup = (resolvedColorScheme: "light" | "dark") =>
+ render(
+ <ThemeProvider resolvedColorScheme={resolvedColorScheme}>
+ <PublicComponentStylesWrapper data-testid="wrapper">
+ <span>content</span>
+ </PublicComponentStylesWrapper>
+ </ThemeProvider>,
+ );
+
+describe("PublicComponentStylesWrapper", () => {
+ it("inherits the parent MantineProvider's light color scheme", () => {
+ setup("light");
+ expect(screen.getByTestId("wrapper")).toHaveAttribute(
+ "data-mantine-color-scheme",
+ "light",
+ );
+ });
+
+ it("inherits the parent MantineProvider's dark color scheme (EMB-1560)", () => {
+ setup("dark");
+ expect(screen.getByTestId("wrapper")).toHaveAttribute(
+ "data-mantine-color-scheme",
+ "dark",
+ );
+ });
+});
diff --git a/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/SdkError.tsx b/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/SdkError.tsx
index 390096c6eda..46c447e52f6 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/SdkError.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/private/PublicComponentWrapper/SdkError.tsx
@@ -1,5 +1,5 @@
import { useDisclosure } from "@mantine/hooks";
-import { type CSSProperties, type PropsWithChildren, useMemo } from "react";
+import { type PropsWithChildren, useMemo } from "react";
import { jt, t } from "ttag";
import { ERROR_DOC_LINKS } from "embedding-sdk-bundle/errors";
@@ -10,7 +10,6 @@ import type { SdkErrorComponentProps } from "embedding-sdk-bundle/types";
import { Alert } from "metabase/common/components/Alert";
import { EMBEDDING_SDK_PORTAL_ROOT_ELEMENT_ID } from "metabase/embedding-sdk/config";
import { Anchor, Box, Center, Code, Flex, Portal } from "metabase/ui";
-import { color } from "metabase/ui/colors";
export const SdkError = ({
message,
@@ -93,16 +92,8 @@ export function SdkPortalErrorWrapper({ children }: PropsWithChildren) {
);
}
-const FORCE_DARK_TEXT_COLOR = {
- // The Alert component has a light background, we need to force a dark text
- // color. The sdk aliases text-primary to the primary color, which in dark themes
- // is a light color, making the text un-readable
- "--mb-color-text-primary": color("text-primary"),
- "--mb-color-text-secondary": color("text-secondary"),
-} as CSSProperties;
-
const DefaultErrorMessage = ({ message, onClose }: SdkErrorComponentProps) => (
- <Box p="sm" style={FORCE_DARK_TEXT_COLOR} maw={600}>
+ <Box p="sm" maw={600}>
<Alert variant="error" icon="warning" onClose={onClose}>
<Box
style={{
diff --git a/frontend/src/embedding-sdk-bundle/components/private/SdkThemeProvider.tsx b/frontend/src/embedding-sdk-bundle/components/private/SdkThemeProvider.tsx
index 196f68a4c84..102c7a537b5 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/SdkThemeProvider.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/private/SdkThemeProvider.tsx
@@ -6,21 +6,38 @@ import { DEFAULT_FONT } from "embedding-sdk-bundle/config";
import { useEmbeddingThemeOverride } from "embedding-sdk-bundle/hooks/private/use-embedding-theme-override";
import { EnsureSingleInstance } from "embedding-sdk-shared/components/EnsureSingleInstance/EnsureSingleInstance";
import { useSetting } from "metabase/common/hooks";
-import type { MetabaseEmbeddingTheme } from "metabase/embedding-sdk/theme";
+import {
+ type MetabaseEmbeddingTheme,
+ isEmbeddingThemeV1,
+} from "metabase/embedding-sdk/theme";
import { useSelector } from "metabase/redux";
import { getFont } from "metabase/styled-components/selectors";
import { getMetabaseSdkCssVariables } from "metabase/styled-components/theme/css-variables";
import { ThemeProvider, useMantineTheme } from "metabase/ui";
import { ThemeProviderContext } from "metabase/ui/components/theme/ThemeProvider/context";
+import type { ResolvedColorScheme } from "metabase/utils/color-scheme";
interface Props {
theme?: MetabaseEmbeddingTheme;
children: React.ReactNode;
}
+const getResolvedColorSchemeFromTheme = (
+ theme: MetabaseEmbeddingTheme | undefined,
+): ResolvedColorScheme | undefined => {
+ if (!isEmbeddingThemeV1(theme)) {
+ return undefined;
+ }
+ return theme.preset === "dark" || theme.preset === "light"
+ ? theme.preset
+ : undefined;
+};
+
export const SdkThemeProvider = ({ theme, children }: Props) => {
const themeOverride = useEmbeddingThemeOverride(theme);
+ const resolvedColorScheme = getResolvedColorSchemeFromTheme(theme);
+
const { withCssVariables, withGlobalClasses } =
useContext(ThemeProviderContext);
@@ -40,6 +57,7 @@ export const SdkThemeProvider = ({ theme, children }: Props) => {
>
<ThemeProvider
theme={themeOverride}
+ resolvedColorScheme={resolvedColorScheme}
cssVariablesSelector=".mb-wrapper"
>
{isInstanceToRender && <GlobalSdkCssVariables />}
diff --git a/frontend/src/metabase/ui/index.ts b/frontend/src/metabase/ui/index.ts
index 1c35e77a0d6..85d908a51e9 100644
--- a/frontend/src/metabase/ui/index.ts
+++ b/frontend/src/metabase/ui/index.ts
@@ -1,4 +1,10 @@
-export { rem, useCombobox, useMantineTheme, useMatches } from "@mantine/core";
+export {
+ rem,
+ useCombobox,
+ useComputedColorScheme,
+ useMantineTheme,
+ useMatches,
+} from "@mantine/core";
export type {
MenuDropdownProps,
ComboboxStore,