Skip to content

Conversation

@zombieJ
Copy link
Member

@zombieJ zombieJ commented Jan 15, 2026

Summary by CodeRabbit

发布说明

  • 改进

    • 优化了对窗口和元素尺寸变化的监测和响应处理。
  • 测试

    • 新增测试用例,验证组件引用相关功能。

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 15, 2026

演练

该 PR 将 @rc-component/resize-observer 版本从 ^1.0.0 升级到 ^1.1.1,并重构了 Trigger 组件的 ResizeObserver 实现。原有的组件包装器被替换为基于 hook 的 useResizeObserver,同时改进了 ref 组合和 DOM 元素处理的逻辑。

变更

内聚体 / 文件 变更摘要
依赖更新
package.json
@rc-component/resize-observer 从 ^1.0.0 升级到 ^1.1.1
Hook 迁移与 Ref 重构
src/index.tsx
将直接 ResizeObserver 组件包装器替换为 useResizeObserver hook;引入 getDOMisDOMgetNodeRefuseComposeRef 用于改进的 ref 组合和 DOM 元素规范化;使用 getNodeRef(child) 获取触发节点 ref,通过 useComposeRef 创建 mergedRef
Ref 处理测试
tests/ref.test.tsx
新增测试验证通过 React.forwardRefuseImperativeHandle 暴露的子组件能够提供 nativeElement,且 Trigger 组件能正确获取相同的 DOM 元素

代码审查工作量评估

🎯 3 (中等) | ⏱️ ~20 分钟

可能相关的 PR

  • refactor: Remove legacy api #539:两个 PR 都修改了 src/index.tsx 中的触发器 ref 处理逻辑——移除了包装器/旧版的 getTriggerDOMNode 模式,转向直接 ref 组合(现通过 getNodeRef/useComposeRef 和 resize hook)。

建议审查者

  • afc163
  • Wxh16144

诗歌

🐰 Resize hook 翩翩而至,
Ref 组合更加优雅,
DOM 规范化保驾护航,
旧日组件成历史,
新流程如兔足轻盈!✨

🚥 Pre-merge checks | ✅ 1 | ❌ 2
❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The pull request title is incomplete and vague, ending with 'instead of' without specifying what is being replaced. Complete the title to clearly state what is being replaced, e.g., 'refactor: Trigger use hooks instead of ResizeObserver wrapper' or similar.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch hooks

🧹 Recent nitpick comments
tests/ref.test.tsx (1)

54-84: 测试用例覆盖了 useImperativeHandle 场景,结构清晰。

该测试验证了 Trigger 可以从使用 forwardRef + useImperativeHandle 暴露 nativeElement 的子组件中正确提取 DOM 元素,与 src/index.tsxgetDOM 的使用相呼应。

测试名称 "should support ref.nativeElement" 与第 26 行的 "support nativeElement" 相似,建议使用更具描述性的名称以区分两种场景:

♻️ 建议的修改
-  it('should support ref.nativeElement', () => {
+  it('should extract nativeElement from child with useImperativeHandle', () => {

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1f75252 and 2cc87ef.

📒 Files selected for processing (3)
  • package.json
  • src/index.tsx
  • tests/ref.test.tsx
🧰 Additional context used
🧬 Code graph analysis (1)
tests/ref.test.tsx (1)
src/index.tsx (1)
  • TriggerRef (45-49)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (5)
src/index.tsx (4)

4-7: 导入结构清晰且符合重构需求。

新增的工具函数导入支持了从组件包装器到 hook 的迁移:

  • useResizeObserver 替代了之前的 ResizeObserver 组件
  • getDOM/isDOM 用于规范化 DOM 节点处理
  • getNodeRef/useComposeRef 用于组合子元素的 ref

264-271: DOM 节点规范化处理正确。

使用 getDOM(node) 可以正确处理子组件通过 useImperativeHandle 暴露 { nativeElement: HTMLElement } 的场景,isDOM 检查确保了类型安全。


788-794: Hook 方式的 resize 观察实现简洁。

使用 useResizeObserver hook 替代组件包装器是一个良好的重构。useComposeRef 正确地合并了内部 ref 处理器和子元素的 ref。

需要确认:当 mergedOpenfalse 时不观察 resize,这是否会影响首次打开时的尺寸计算?从代码看,onPrepare(第 560-564 行)中的 syncTargetSize() 应该可以处理这种情况。


797-806: 渲染逻辑简化得当。

移除 ResizeObserver 组件包装器,改用 hook 方式处理 resize 观察,使得渲染结构更加扁平和清晰。mergedRef 正确地传递给了克隆的子元素。

package.json (1)

46-46: 依赖版本升级正确。

@rc-component/resize-observer v1.1.1 已正确导出 useResizeObserver hook,与 src/index.tsx 中的导入用法一致。

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.


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.

@gemini-code-assist
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 refactors the internal implementation of the Trigger component to embrace a more modern, hook-based approach for handling resize observations. By migrating from a component-based ResizeObserver to the useResizeObserver hook and enhancing ref management with new utility functions, the changes aim to improve the component's maintainability, potentially its performance, and its compatibility with diverse ref patterns, including those that wrap native DOM elements. The update ensures more reliable interaction with underlying DOM nodes.

Highlights

  • Dependency Update: The @rc-component/resize-observer dependency has been updated to version ^1.1.1.
  • Refactor to Hooks: The ResizeObserver component in src/index.tsx has been replaced with the useResizeObserver hook, modernizing the resize observation mechanism.
  • Enhanced Ref Handling: New utilities getDOM, getNodeRef, and useComposeRef have been introduced and integrated to improve the robustness and flexibility of ref management within the Trigger component, particularly for handling nativeElement properties.
  • New Test Case: A new test has been added to tests/ref.test.tsx to ensure that the Trigger component correctly supports child components that expose their native DOM element via a nativeElement property in their ref.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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.

@codecov
Copy link

codecov bot commented Jan 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.26%. Comparing base (1f75252) to head (2cc87ef).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #604   +/-   ##
=======================================
  Coverage   97.26%   97.26%           
=======================================
  Files          17       17           
  Lines         949      952    +3     
  Branches      274      268    -6     
=======================================
+ Hits          923      926    +3     
  Misses         26       26           

☔ 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.

@socket-security
Copy link

Warning

Review the following alerts detected in dependencies.

According to your organization's Security Policy, it is recommended to resolve "Warn" alerts. Learn more about Socket for GitHub.

Action Severity Alert  (click "▶" to expand/collapse)
Warn High
Obfuscated code: npm buffer-okam is 96.0% likely obfuscated

Confidence: 0.96

Location: Package overview

From: ?npm/[email protected]

ℹ Read more on: This package | This alert | What is obfuscated code?

Next steps: Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at [email protected].

Suggestion: Packages should not obfuscate their code. Consider not using packages with obfuscated code.

Mark the package as acceptable risk. To ignore this alert only in this pull request, reply with the comment @SocketSecurity ignore npm/[email protected]. You can also ignore all packages with @SocketSecurity ignore-all. To ignore an alert for all future pull requests, use Socket's Dashboard to change the triage state of this alert.

Warn High
Obfuscated code: npm buffer is 96.0% likely obfuscated

Confidence: 0.96

Location: Package overview

From: ?npm/[email protected]

ℹ Read more on: This package | This alert | What is obfuscated code?

Next steps: Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at [email protected].

Suggestion: Packages should not obfuscate their code. Consider not using packages with obfuscated code.

Mark the package as acceptable risk. To ignore this alert only in this pull request, reply with the comment @SocketSecurity ignore npm/[email protected]. You can also ignore all packages with @SocketSecurity ignore-all. To ignore an alert for all future pull requests, use Socket's Dashboard to change the triage state of this alert.

Warn High
Obfuscated code: npm entities is 91.0% likely obfuscated

Confidence: 0.91

Location: Package overview

From: ?npm/[email protected]

ℹ Read more on: This package | This alert | What is obfuscated code?

Next steps: Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at [email protected].

Suggestion: Packages should not obfuscate their code. Consider not using packages with obfuscated code.

Mark the package as acceptable risk. To ignore this alert only in this pull request, reply with the comment @SocketSecurity ignore npm/[email protected]. You can also ignore all packages with @SocketSecurity ignore-all. To ignore an alert for all future pull requests, use Socket's Dashboard to change the triage state of this alert.

View full report

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

This pull request refactors the Trigger component to use the useResizeObserver hook instead of the <ResizeObserver> component. This is a great improvement that makes the code cleaner and more aligned with modern React hook patterns. The changes also improve the component's robustness by using getDOM to handle different types of child components, which is well-supported by a new test case.

I've added one suggestion to improve the type safety in the setTargetRef function. Also, the pull request title seems to be incomplete.

Comment on lines 264 to 271
const setTargetRef = useEvent((node: HTMLElement) => {
if (isDOM(node) && targetEle !== node) {
setTargetEle(node);
externalForwardRef.current = node;
const domNode = getDOM(node) as HTMLElement;

if (isDOM(domNode) && targetEle !== domNode) {
setTargetEle(domNode);
externalForwardRef.current = domNode;
}
});
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The type annotation for node is HTMLElement, but with the introduction of getDOM, it can now also be a React component instance (as demonstrated by the new test case). It's better to use a more general type like any to reflect this and avoid potential type issues.

Additionally, the type assertion as HTMLElement is not entirely safe, as getDOM(node) can return null or other node types. It's better to remove the cast and rely on the isDOM() type guard to ensure domNode is an HTMLElement before using it.

Suggested change
const setTargetRef = useEvent((node: HTMLElement) => {
if (isDOM(node) && targetEle !== node) {
setTargetEle(node);
externalForwardRef.current = node;
const domNode = getDOM(node) as HTMLElement;
if (isDOM(domNode) && targetEle !== domNode) {
setTargetEle(domNode);
externalForwardRef.current = domNode;
}
});
const setTargetRef = useEvent((node: any) => {
const domNode = getDOM(node);
if (isDOM(domNode) && targetEle !== domNode) {
setTargetEle(domNode);
externalForwardRef.current = domNode;
}
});

@zombieJ zombieJ merged commit 47d51da into master Jan 15, 2026
10 checks passed
@zombieJ zombieJ deleted the hooks branch January 15, 2026 07:38
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.

2 participants