Skip to content

Conversation

@SocietyNiu
Copy link

🤔 这个变动的性质是?/ What is the nature of this change?

  • bug 修复 / Fix bug

🔗 相关 Issue / Related Issue

ant-design/ant-design#55670

💡 需求背景和解决方案 / Background or solution

由于code demo使用iframe时和文档本身的隔离,使得demo内部无法获取到外部主题变更,导致切换主题时,demo主题颜色不变。

dumi_previewer20251122_222006

本次修改监听主题偏好,并使用key在切换主题时强制刷新iframe,从而实现颜色同步。

测试:
dumi_previewer_fix20251122_222137

📝 更新日志 / Changelog

修改了主题变更下使用iframe的组件demo的表现

Language Changelog
🇺🇸 English fix Previewer component under color scheme change with iframe key
🇨🇳 Chinese 使用key修改demo在颜色修改下的表现

@vercel
Copy link

vercel bot commented Nov 22, 2025

@SocietyNiu is attempting to deploy a commit to the chencheng's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an issue where demo components rendered in iframes do not update their color scheme when the user changes the theme preference. The fix uses React's key prop to force iframe remounting when the color scheme changes.

Key Changes:

  • Added usePrefersColor hook to track color scheme changes
  • Introduced state management for an iframe key that increments on color scheme changes
  • Applied the key to the iframe element to trigger re-rendering

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if (props.iframe) {
setIframeKey((key) => key + 1);
}
}, [preferredColorScheme, props.iframe]);
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The props.iframe dependency in the useEffect is unnecessary and could cause issues. When props.iframe changes from truthy to falsy (or vice versa), the effect runs and updates iframeKey even though there's no iframe to refresh.

The dependency array should only include preferredColorScheme since that's the only value that should trigger an iframe reload. The props.iframe check inside the effect is sufficient to prevent updates when there's no iframe.

Suggested change:

}, [preferredColorScheme]);
Suggested change
}, [preferredColorScheme, props.iframe]);
}, [preferredColorScheme]);

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iframe参数也可以设置为demo的高度,我的理解是这个参数变化应该也要更新demo块吧

const { hash } = useLocation();
const link = `#${props.asset.id}`;

const [preferredColorScheme] = usePrefersColor();
Copy link

Copilot AI Nov 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name preferredColorScheme is misleading. The first value returned by usePrefersColor() is the resolved color ('light' or 'dark'), not the preferred color scheme (which can be 'auto', 'light', or 'dark').

Consider renaming to color or colorScheme to match the convention used in the hook's implementation and to be more accurate about what it represents.

Example:

const [color] = usePrefersColor();

Copilot uses AI. Check for mistakes.
@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 26, 2025

More templates

dumi

npm i https://pkg.pr.new/umijs/dumi@2301

dumi-assets-types

npm i https://pkg.pr.new/umijs/dumi/dumi-assets-types@2301

@dumijs/vue-meta

npm i https://pkg.pr.new/umijs/dumi/@dumijs/vue-meta@2301

father-plugin-dumi-theme

npm i https://pkg.pr.new/umijs/dumi/father-plugin-dumi-theme@2301

@dumijs/preset-vue

npm i https://pkg.pr.new/umijs/dumi/@dumijs/preset-vue@2301

dumi-theme-mobile

npm i https://pkg.pr.new/umijs/dumi/dumi-theme-mobile@2301

commit: 35c6767

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant