-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: cascade layer is not applied when there is no style content #207
fix: cascade layer is not applied when there is no style content #207
Conversation
📝 WalkthroughWalkthrough此拉取请求对 Changes
Assessment against linked issues
Possibly related issues
Possibly related PRs
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #207 +/- ##
==========================================
- Coverage 95.07% 94.88% -0.20%
==========================================
Files 33 33
Lines 2883 2891 +8
Branches 456 456
==========================================
+ Hits 2741 2743 +2
- Misses 142 148 +6 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (3)
tests/layer.spec.tsx (1)
73-98
: 测试用例设计合理且完整测试用例很好地覆盖了当 style 注册返回空数组时的场景:
- 正确设置了 layer 配置
- 验证了生成的样式元素为空
- 直接针对 #51867 issue 中报告的问题
建议添加以下测试场景以提高覆盖率:
it('empty braces (#51867)', () => { const theme = createTheme(() => ({})); + // 测试多个空样式的情况 + const styles = []; + for (let i = 0; i < 3; i++) { const Demo = () => { useStyleRegister( { theme, token: { _tokenKey: 'test' }, path: ['shared'], layer: { name: 'shared', }, }, () => [], ); return null; }; render( <StyleProvider layer cache={createCache()}> <Demo /> </StyleProvider>, ); + styles.push(...Array.from(document.head.querySelectorAll('style'))); + } - const styles = Array.from(document.head.querySelectorAll('style')); expect(styles[0].innerHTML.trim()).toEqual(''); + // 验证多次注册不会产生重复的空样式元素 + expect(styles).toHaveLength(1); });src/hooks/useStyleRegister.tsx (2)
336-340
: 修复实现简洁有效通过添加
if (styleStr)
条件检查,确保只有在存在样式内容时才会应用@layer
包装,这个修复:
- 解决了 #51867 中报告的重复
@layer
问题- 实现简单且不影响现有功能
- 符合 CSS 规范的最佳实践
建议添加更详细的注释说明这个修复的必要性:
- // fixme: https://github.com/thysultan/stylis/pull/339 + // 只在存在样式内容时才添加 @layer 包装 + // 这个检查解决了当样式内容为空时产生重复 @layer 声明的问题 + // 相关问题: https://github.com/ant-design/ant-design/issues/51867 + // Stylis 修复: https://github.com/thysultan/stylis/pull/339 if (styleStr) { styleStr = `@layer ${layer.name} {${styleStr}}`; }
336-340
: 建议添加性能优化当前实现在每次解析样式时都会进行字符串拼接,建议通过缓存优化性能:
+ // 使用 Set 缓存已处理的 layer 名称,避免重复处理 + const processedLayers = new Set<string>(); + if (styleStr) { + const layerKey = `${layer.name}:${styleStr}`; + if (!processedLayers.has(layerKey)) { styleStr = `@layer ${layer.name} {${styleStr}}`; + processedLayers.add(layerKey); + } }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
src/hooks/useStyleRegister.tsx
(1 hunks)tests/layer.spec.tsx
(1 hunks)
🔇 Additional comments (1)
tests/layer.spec.tsx (1)
70-72
: TODO 注释提供了有价值的上下文信息
这个 TODO 注释很好地引用了相关的 Stylis PR 和 Ant Design issue,帮助理解这个测试用例的背景。建议保留这个注释直到 Stylis 的修复被合并。
fix: ant-design/ant-design#51867
Solution
antd 的某些组件,比如 popover 用的 tooltips, 设置了 injectStyle 为 false, 使得 useStyleRegister 放回空样式, stylis 处理后结果刚好碰到 thysultan/stylis#339 处理逻辑。导致多拼接了一个级联层。
Summary by CodeRabbit
新功能
测试