Skip to content

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

Prevent MeasurementProvider detached root from flipping theme (#72931)

Andrei Chernykha5dcce0a28b1
diff --git a/frontend/src/metabase/ui/components/theme/MeasurementProviders.tsx b/frontend/src/metabase/ui/components/theme/MeasurementProviders.tsx
index e535858db03..0fba401094d 100644
--- a/frontend/src/metabase/ui/components/theme/MeasurementProviders.tsx
+++ b/frontend/src/metabase/ui/components/theme/MeasurementProviders.tsx
@@ -1,5 +1,7 @@
import type { ReactNode } from "react";
+import type { ResolvedColorScheme } from "metabase/utils/color-scheme";
+
import { EmotionCacheProvider } from "./EmotionCacheProvider";
import { ThemeProvider } from "./ThemeProvider";
@@ -7,10 +9,38 @@ interface MeasurementProvidersProps {
children: ReactNode;
}
+/**
+ * Providers used for detached React roots that only need to measure text/layout
+ * (e.g. `useColumnSizing` mounts this into a hidden `<div>` on `document.body`).
+ *
+ * Because the detached root lives outside the app's `<ColorSchemeProvider>`,
+ * `useColorScheme()` here returns its default (`"light"`), so without
+ * intervention the nested `<MantineProvider forceColorScheme="light">` would
+ * synchronously flip `data-mantine-color-scheme` on `<html>` to `"light"` —
+ * making every Mantine scheme-scoped rule (including `--mantine-color-body`)
+ * paint the whole app in light mode for one frame (UXW-3733).
+ *
+ * Read the current scheme off `<html>` (set by the app's main `MantineProvider`)
+ * and pass it through so the nested provider writes back the same value and the
+ * attribute never changes.
+ */
+function readCurrentColorScheme(): ResolvedColorScheme {
+ if (typeof document === "undefined") {
+ return "light";
+ }
+ return document.documentElement.getAttribute("data-mantine-color-scheme") ===
+ "dark"
+ ? "dark"
+ : "light";
+}
+
export function MeasurementProviders({ children }: MeasurementProvidersProps) {
+ const resolvedColorScheme = readCurrentColorScheme();
return (
<EmotionCacheProvider>
- <ThemeProvider>{children}</ThemeProvider>
+ <ThemeProvider resolvedColorScheme={resolvedColorScheme}>
+ {children}
+ </ThemeProvider>
</EmotionCacheProvider>
);
}
diff --git a/frontend/src/metabase/ui/components/theme/MeasurementProviders.unit.spec.tsx b/frontend/src/metabase/ui/components/theme/MeasurementProviders.unit.spec.tsx
new file mode 100644
index 00000000000..5d18b8de781
--- /dev/null
+++ b/frontend/src/metabase/ui/components/theme/MeasurementProviders.unit.spec.tsx
@@ -0,0 +1,58 @@
+import { render, screen } from "@testing-library/react";
+
+import { MeasurementProviders } from "./MeasurementProviders";
+
+const SCHEME_ATTR = "data-mantine-color-scheme";
+
+describe("MeasurementProviders", () => {
+ afterEach(() => {
+ document.documentElement.removeAttribute(SCHEME_ATTR);
+ });
+
+ it("does not flip `data-mantine-color-scheme` on <html> when rendered in dark mode (UXW-3733)", () => {
+ // Simulate the state set by the main app's MantineProvider.
+ document.documentElement.setAttribute(SCHEME_ATTR, "dark");
+
+ const { unmount } = render(
+ <MeasurementProviders>
+ <div>measurement</div>
+ </MeasurementProviders>,
+ );
+
+ // The nested MantineProvider would previously default to `forceColorScheme="light"`
+ // (detached root → useColorScheme() returns the context default "light"), flipping
+ // the html attribute. The fix reads the current scheme off <html> and passes it
+ // through so the attribute value never changes.
+ expect(document.documentElement).toHaveAttribute(SCHEME_ATTR, "dark");
+
+ unmount();
+
+ expect(document.documentElement).toHaveAttribute(SCHEME_ATTR, "dark");
+ });
+
+ it("does not flip `data-mantine-color-scheme` on <html> when rendered in light mode", () => {
+ document.documentElement.setAttribute(SCHEME_ATTR, "light");
+
+ const { unmount } = render(
+ <MeasurementProviders>
+ <div>measurement</div>
+ </MeasurementProviders>,
+ );
+
+ expect(document.documentElement).toHaveAttribute(SCHEME_ATTR, "light");
+
+ unmount();
+
+ expect(document.documentElement).toHaveAttribute(SCHEME_ATTR, "light");
+ });
+
+ it("renders its children", () => {
+ render(
+ <MeasurementProviders>
+ <div>measurement content</div>
+ </MeasurementProviders>,
+ );
+
+ expect(screen.getByText("measurement content")).toBeInTheDocument();
+ });
+});