rusackas commented on code in PR #42602:
URL: https://github.com/apache/superset/pull/42602#discussion_r3681000169


##########
docs/src/components/StorybookWrapper.jsx:
##########
@@ -78,15 +80,32 @@ function getProviders() {
       return container || document.body;
     };
 
+    // `themeObject` is a module-level singleton (superset-core/src/theme
+    // index.tsx: `Theme.fromConfig()`), created once with no dark/light
+    // config, so SupersetThemeProvider always rendered whatever that default
+    // algorithm was -- it had no way to know about Docusaurus's theme toggle.
+    // Docusaurus tracks the toggle in React context (useColorMode), so
+    // mirror it onto the singleton via the toggleDarkMode() method Theme
+    // already exposes for exactly this purpose.
+    function ThemeSync({ children }) {
+      const { colorMode } = useColorMode();
+      React.useEffect(() => {
+        themeObject.toggleDarkMode(colorMode === 'dark');
+      }, [colorMode]);
+      return children;
+    }
+
     SupersetProviders = ({ children }) => (
-      <themeObject.SupersetThemeProvider>
-        <ConfigProvider
-          getPopupContainer={getPopupContainer}
-          getTargetContainer={() => document.body}
-        >
-          <App>{children}</App>
-        </ConfigProvider>
-      </themeObject.SupersetThemeProvider>
+      <ThemeSync>
+        <themeObject.SupersetThemeProvider>

Review Comment:
   This one's real, and it's worse than what's described. Since every demo on a 
docs page shares the same `themeObject` singleton, only the 
most-recently-rendered provider's callback ever survived, so earlier demos 
would stay stuck on the old theme entirely, not just briefly. Reworked 
`Theme.tsx` to keep a set of listeners instead of one shared callback, so every 
mounted provider for that instance gets notified.



##########
superset-frontend/packages/superset-core/src/theme/SupersetThemeProvider.test.tsx:
##########
@@ -0,0 +1,100 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { render, screen, act } from '@testing-library/react';
+import { theme as antdThemeImport } from 'antd';
+import { Theme } from './Theme';
+
+// SupersetThemeProvider stores theme state via React.useState, then
+// overrides Theme#updateProviders (a no-op by default) to call setThemeState
+// whenever a *later* call to setConfig/toggleDarkMode runs on the same
+// instance. Consumers (docs/src/components/StorybookWrapper.jsx in
+// particular) rely on this: they call toggleDarkMode() from outside, on an
+// already-mounted provider, expecting it to propagate.
+//
+// The probe below reads the theme via antd's theme.useToken() -- the same
+// context-consumption path every real antd component (Button, Input, ...)
+// uses internally -- rather than reading themeObject.theme directly off the
+// singleton. That distinction matters: React bails out of re-rendering a
+// child whose element reference didn't change (the common "static children
+// prop" case, true here since <Probe /> is passed once and never
+// recreated), UNLESS that child consumes a React Context whose value
+// changed, which bypasses the bail-out. A probe reading the plain object
+// directly would misleadingly appear "not updated" even though every real
+// themed component downstream re-renders correctly.
+test('an already-mounted SupersetThemeProvider re-renders context-consuming 
children when toggleDarkMode is called on the same instance', () => {
+  const themeObject = Theme.fromConfig();
+  let renderCount = 0;
+  let lastColorBgBase: string | undefined;
+
+  function Probe() {
+    const { token } = antdThemeImport.useToken();
+    renderCount += 1;
+    lastColorBgBase = token.colorBgBase;
+    return <div data-test="probe" />;
+  }
+
+  render(
+    <themeObject.SupersetThemeProvider>
+      <Probe />
+    </themeObject.SupersetThemeProvider>,
+  );
+
+  expect(screen.getByTestId('probe')).toBeTruthy();
+  const rendersBefore = renderCount;
+  const tokenBefore = lastColorBgBase;
+
+  act(() => {
+    themeObject.toggleDarkMode(true);
+  });
+
+  expect(renderCount).toBeGreaterThan(rendersBefore);
+  expect(lastColorBgBase).not.toBe(tokenBefore);
+});
+
+test('a toggleDarkMode call on an unmounted (or different) theme instance does 
not affect a mounted provider', () => {

Review Comment:
   Fair, the second test never actually unmounted anything, it only toggled a 
different instance. Added a real unmount case, plus one covering two providers 
mounted on the same instance at once (which is the actual scenario the fix 
above needed covering).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to