Skip to content

Conversation

zombieJ
Copy link
Member

@zombieJ zombieJ commented Sep 23, 2025

调整一下对齐的逻辑,平移的对齐部分 jsdom 模拟都没有什么意义

Summary by CodeRabbit

  • 重构
    • 增加偏移样式的缓存,减少重复计算与瞬时跳动,渲染更平滑。
    • 统一改用合并后的配置进行对齐、类名与弹层渲染,行为更一致、可预测。
    • 优化依赖与渲染条件,降低不必要的更新,提升复杂场景下的稳定性。
    • 无公开接口变更;现有功能与外观保持不变。

Copy link

coderabbitai bot commented Sep 23, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

在 UniqueBody 中新增偏移样式缓存并在 ready 为 true 时同步;在 UniqueProvider 中把各处对 options.* 的使用统一替换为 mergedOptions.*,涵盖对齐、类名计算、依赖项与渲染门控。无公开 API 变更,仅内部渲染与样式流程调整。

Changes

Cohort / File(s) Change Summary
样式偏移缓存
src/UniqueProvider/UniqueBody.tsx
引入 cachedOffsetStyleRef 缓存 offsetStyle;在 ready 为 true 时于渲染流程同步 current;主容器样式改为合并 cachedOffsetStyleRef.current 与 sizeStyle、motionStyle、uniqueBgStyle。
合并选项统一使用
src/UniqueProvider/index.tsx
将对 options.* 的访问全面改为 mergedOptions.*:useAlign 参数、alignedClassName 早退条件、基础类名与 popup className 计算、memo 依赖、以及 Popup/TriggerContext 的渲染门控。

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor C as Component
  participant P as UniqueProvider
  participant A as Align/Trigger
  participant B as UniqueBody

  rect rgb(240,248,255)
    note over P: 计算 mergedOptions(含 postTriggerProps 合并)
    C->>P: 渲染并传入 props
    P->>A: useAlign(target, placement, builtinPlacements, popupAlign) 使用 mergedOptions.*
    P-->>A: 提供 getPopupClassNameFromAlign / prefixCls 等
  end

  rect rgb(245,255,240)
    note over B: 样式渲染与偏移缓存
    A->>B: 传入对齐/位置信息与样式
    alt ready 为 true
      B->>B: cachedOffsetStyleRef.current = 最新 offsetStyle
    else 未就绪
      B-->>B: 保留已有 cachedOffsetStyleRef.current
    end
    B-->>C: 使用 {sizeStyle + motionStyle + uniqueBgStyle + cachedOffsetStyleRef.current} 渲染
  end

  note right of A: 对齐与类名计算统一引用 mergedOptions
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 分钟

Possibly related PRs

Poem

小兔敲键轻如风,
偏移入匣不再慌。
选项并流成一脉,
对齐起舞影成双。
代码悄然更秩序,
耳尖一抖:稳如霜。 🐇✨

✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch align

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bbbb755 and fa5bf88.

📒 Files selected for processing (2)
  • src/UniqueProvider/UniqueBody.tsx (2 hunks)
  • src/UniqueProvider/index.tsx (2 hunks)

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

Summary of Changes

Hello @zombieJ, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the component alignment logic by introducing a caching mechanism for offset styles, which helps stabilize positioning once a component is ready, particularly in environments where layout calculations might be unreliable. Additionally, it improves code consistency by ensuring that all alignment-related configurations are derived from a merged set of options rather than raw input options.

Highlights

  • Alignment Logic Refinement: Introduced a caching mechanism for offsetStyle in UniqueBody.tsx using useRef. This cache is updated only when the component is ready, ensuring stable alignment calculations and potentially addressing issues in jsdom environments where dynamic layout might be unreliable.
  • Configuration Consistency: Refactored UniqueProvider/index.tsx to consistently use mergedOptions instead of options for all alignment-related configurations, including parameters passed to the useAlign hook, alignedClassName calculations, and conditional rendering of the Popup component. This ensures that the final, processed options are always applied.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

codecov bot commented Sep 23, 2025

Codecov Report

❌ Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 95.97%. Comparing base (bbbb755) to head (fa5bf88).

Files with missing lines Patch % Lines
src/UniqueProvider/UniqueBody.tsx 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #580      +/-   ##
==========================================
- Coverage   96.07%   95.97%   -0.10%     
==========================================
  Files          17       17              
  Lines         942      945       +3     
  Branches      268      278      +10     
==========================================
+ Hits          905      907       +2     
- Misses         37       38       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

本次 PR 调整了对齐逻辑,主要通过缓存样式来解决 jsdom 环境下弹窗定位的问题。这个改动是有效的,但也引入了一个在浏览器中会导致UI闪烁的严重问题,并且在渲染函数中直接修改 ref 存在副作用,违反了 React 的最佳实践。建议修复闪烁问题,并采用更安全的方式来管理状态和副作用。此外,PR 中将 options 替换为 mergedOptions 的重构是正确的,提高了组件的灵活性。

Comment on lines +60 to +65
const cachedOffsetStyleRef = React.useRef(offsetStyle);

// Update cached offset style when ready is true
if (ready) {
cachedOffsetStyleRef.current = offsetStyle;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

这部分代码存在两个问题:

  1. 高危:UI 闪烁 (Flicker)
    此实现在浏览器中会导致 UI 闪烁。当弹窗首次打开时(open 变为 true),ready 此时为 falseoffsetStyle 会返回一个将弹窗移出屏幕的样式。然而,cachedOffsetStyleRef.current 仍然保持着组件首次渲染时的值(通常是 { left: 0, top: 0, ... })。这导致弹窗在计算出正确位置之前,会在 0,0 位置闪现一帧。

  2. 中危:在渲染函数中产生副作用
    在渲染函数中直接修改 ref (cachedOffsetStyleRef.current = offsetStyle;) 是一个副作用。React 的函数组件应该设计为纯函数,在渲染期间执行副作用可能会在未来的 React 版本(特别是并发模式下)导致不可预测的行为。

为了解决 jsdom 中对齐无意义的问题,同时避免在浏览器中产生闪烁,需要更精细的逻辑。一个可能的思路是仅在弹窗关闭动画期间使用缓存的样式。例如:

const styleToUse = !open ? cachedOffsetStyleRef.current : offsetStyle;

然后将 styleToUse 应用到 div 上。但这可能会让弹窗在 jsdom 环境下不可见,与本次修改的初衷相悖。请重新评估此方案,找到一个能同时满足测试环境和生产环境需求的方案,避免引入UI缺陷。

@zombieJ zombieJ merged commit a9f4fad into master Sep 23, 2025
7 of 10 checks passed
@zombieJ zombieJ deleted the align branch September 23, 2025 07:52
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