-
Notifications
You must be signed in to change notification settings - Fork 26
Open
Description
Description
When using ThemeEditor wrapped in a ConfigProvider with dynamic algorithm (switching between theme.darkAlgorithm and theme.defaultAlgorithm), the editor's internal UI (left panel with Color, Size, Style tabs) does not update to reflect the theme change. The change only takes effect after a full page refresh.
Environment
- antd-token-previewer version: 3.0.0
- antd version: 6.1.4
- React version: 19.2.3
Steps to Reproduce
- Wrap
ThemeEditorin aConfigProviderwith dynamic algorithm based on external dark mode state - Toggle the external dark/light mode state
- Observe that the editor's internal UI does not update
Code Example
import React from "react";
import { ConfigProvider, theme } from "antd";
import ThemeEditor from "antd-token-previewer/es/ThemeEditor";
function MyThemeEditor() {
const [isDarkMode, setIsDarkMode] = React.useState(false);
const [editorTheme, setEditorTheme] = React.useState({
name: "Custom Theme",
key: "my-theme",
config: {
token: {},
algorithm: theme.defaultAlgorithm,
},
});
// Update editor theme when dark mode changes
React.useEffect(() => {
setEditorTheme((prev) => ({
...prev,
config: {
...prev.config,
algorithm: isDarkMode ? theme.darkAlgorithm : theme.defaultAlgorithm,
},
}));
}, [isDarkMode]);
return (
<div>
<button onClick={() => setIsDarkMode(!isDarkMode)}>
Toggle Dark Mode (Current: {isDarkMode ? "Dark" : "Light"})
</button>
<ConfigProvider
theme={{
hashed: true,
algorithm: isDarkMode ? theme.darkAlgorithm : theme.defaultAlgorithm,
}}
>
<ThemeEditor
key={isDarkMode ? "dark" : "light"}
theme={editorTheme}
darkAlgorithm={theme.darkAlgorithm}
onThemeChange={setEditorTheme}
/>
</ConfigProvider>
</div>
);
}Expected Behavior
When toggling dark mode externally:
- The
ConfigProvider's algorithm changes - The
ThemeEditor's internal UI (panel backgrounds, text colors, etc.) should update to match the new theme - The
themeprop's algorithm is also updated
Actual Behavior
- The
ThemeEditor's internal UI remains in the previous theme state - Only a full page refresh causes the UI to update
- The internal
makeStylehooks appear to cache styles and don't respond to theConfigProvideralgorithm change
Attempted Solutions
- ✅ Wrapped with
ConfigProviderwithhashed: trueand dynamicalgorithm- Did not work - ✅ Added
keyprop to force remount - Did not work - ✅ Passed
darkAlgorithmprop - Did not work - ✅ Manually unmounting/remounting with state and setTimeout - Did not work
- ✅ Updated the
themeprop'sconfig.algorithmvia useEffect - Did not work
Analysis
Looking at the source code:
- The
makeStyleutility insrc/utils/makeStyle.tsxusesantdTheme.useToken()for styling - The internal
useControlledThemehook andswitchAlgorithmfunction handle the internal Light/Dark segmented control - It appears the internal styles are cached and don't invalidate when the external
ConfigProvidercontext changes
Possible Solutions
- Add a prop like
externalDarkModethat the component respects - Ensure
makeStyleproperly responds toConfigProvideralgorithm changes - Expose a method to programmatically trigger the internal
switchAlgorithmfunction
Related
The official demo in docs/examples/theme-editor-simple.tsx shows a similar pattern with ConfigProvider, but the demo controls dark mode via the editor's internal segmented control, not externally.
Thank you for your work on this excellent library! 🙏
Metadata
Metadata
Assignees
Labels
No labels