Skip to content

[codex] support function scroll offset#358

Merged
zombieJ merged 1 commit into
masterfrom
codex/scroll-offset-info
May 20, 2026
Merged

[codex] support function scroll offset#358
zombieJ merged 1 commit into
masterfrom
codex/scroll-offset-info

Conversation

@zombieJ
Copy link
Copy Markdown
Member

@zombieJ zombieJ commented May 20, 2026

Summary

  • Allow scrollTo({ offset }) to receive a function.
  • Pass getSize into the offset function so callers can derive dynamic offsets from measured item ranges.
  • Export the new ScrollOffset and ScrollOffsetInfo types.

Validation

  • npm run lint
  • bunx tsc --noEmit
  • npm run compile
  • bun run test -- --coverage --silent

Summary by CodeRabbit

  • 新功能

    • 滚动偏移增强:scrollTo 现支持以函数动态计算偏移,可基于元素尺寸/位置信息确定目标滚动位置。
  • 修复

    • 当偏移函数返回非法值(如 NaN)时,滚动行为已回退为安全默认,避免异常位置。
  • 测试

    • 增加针对函数偏移计算与非法返回值回退的自动化测试,覆盖实际滚动结果。

Review Change Stack

@vercel
Copy link
Copy Markdown

vercel Bot commented May 20, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
virtual-list Ready Ready Preview, Comment May 20, 2026 8:28am

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5edafef7-92a0-4968-8b21-9ae866a6bbfc

📥 Commits

Reviewing files that changed from the base of the PR and between 8cf8b44 and ba1fd3d.

📒 Files selected for processing (4)
  • src/List.tsx
  • src/hooks/useScrollTo.tsx
  • src/index.ts
  • tests/scroll.test.js

Walkthrough

新增可函数化的 ScrollOffset(支持 number | (info) => number);useScrollTo 接收 getSize 并在解析 offset 时调用;List 提前计算并传入 getSize;更新导出并添加测试覆盖函数式 offset 行为与 NaN 回退。

变更内容

动态滚动偏移功能

层 / 文件(s) 摘要
滚动偏移类型与 useScrollTo 钩子扩展
src/hooks/useScrollTo.tsx
新增 ScrollOffsetInfo 接口与 ScrollOffset 类型(number | ((info: ScrollOffsetInfo) => number));更新 ScrollTarget.offset 类型;在 useScrollTo 中新增 getSize: GetSize 参数并实现对函数式 offset 的求值与同步(syncState)逻辑。
List 组件中的 getSize 传递与类型重新导出
src/List.tsx
在调用 useScrollTo 之前通过 useGetSize 计算 getSize 并将其作为参数传入;移除 Extra 区域原处的 getSize 定义;从 List 重新导出 ScrollOffsetScrollOffsetInfo 类型。
公开 API 导出与功能测试
src/index.ts, tests/scroll.test.js
src/index.ts 的类型导出扩展为具名多行形式并包含 ScrollOffsetScrollOffsetInfo;在 tests/scroll.test.js 中新增用例,验证函数式 offset 会接收包含 getSize 的参数并正确计算 scrollTop,以及当返回 NaN 时回退到固定值。

Sequence Diagram

sequenceDiagram
  participant Consumer
  participant List
  participant useGetSize
  participant useScrollTo
  participant DOM_ul
  Consumer->>List: 调用 scrollTo(target with offset)
  List->>useGetSize: 计算 getSize
  List->>useScrollTo: 传入 getSize 调用 useScrollTo
  useScrollTo->>useScrollTo: 若 offset 为函数,调用 offset({ getSize }) 得到数值
  useScrollTo->>DOM_ul: 设置 ul.scrollTop
Loading

预估审查工作量

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

建议审查者

  • afc163

彩蛋诗

🐰 我在代码堆里跳跃,
把偏移当函数轻轻发觉,
getSize 传来尺寸的歌,
滚动位置随风算落花,
小列表也能舞翩跹。

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

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.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题清晰准确地总结了主要变更:支持函数式滚动偏移,与代码改动直接对应且表述简洁明确。
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/scroll-offset-info

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
Copy Markdown

@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 introduces support for functional offsets in the scrollTo method, allowing developers to calculate scroll offsets dynamically using getSize information. The implementation updates the ScrollTarget type and the useScrollTo hook to handle these functional offsets. My feedback highlights that the current implementation evaluates the offset function only once, which may lead to stale values during the multi-pass scroll synchronization process. I have suggested ensuring the result is always a valid number to prevent potential issues with NaN values.

Comment thread src/hooks/useScrollTo.tsx Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.50%. Comparing base (5334600) to head (ba1fd3d).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #358      +/-   ##
==========================================
+ Coverage   97.48%   97.50%   +0.01%     
==========================================
  Files          19       19              
  Lines         796      800       +4     
  Branches      189      191       +2     
==========================================
+ Hits          776      780       +4     
  Misses         20       20              

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

@zombieJ zombieJ force-pushed the codex/scroll-offset-info branch from b43683c to 8cf8b44 Compare May 20, 2026 08:22
@zombieJ zombieJ marked this pull request as ready for review May 20, 2026 08:22
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/hooks/useScrollTo.tsx`:
- Around line 185-186: The code sets offset from arg.rawOffset (possibly a
function) without runtime numeric validation, so if the callback returns
undefined/NaN an invalid value will be stored in syncState.offset; update the
assignment to validate the computed value (use Number.isFinite or similar) and
fall back to a safe default (e.g., 0) before writing into state: when computing
offset from rawOffset({ getSize }), check that the result is a finite number and
otherwise use the fallback so syncState.offset always receives a valid numeric
value.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 33a1ed69-f3f1-4a7f-bd88-ed643b6826f9

📥 Commits

Reviewing files that changed from the base of the PR and between 5334600 and 8cf8b44.

📒 Files selected for processing (4)
  • src/List.tsx
  • src/hooks/useScrollTo.tsx
  • src/index.ts
  • tests/scroll.test.js

Comment thread src/hooks/useScrollTo.tsx Outdated
@zombieJ
Copy link
Copy Markdown
Member Author

zombieJ commented May 20, 2026

@gemini-code-assist CR again

Copy link
Copy Markdown

@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 enhances the scrollTo functionality by allowing the offset property to be a function that receives a getSize utility, enabling dynamic offset calculations based on item sizes. The implementation includes updates to the ScrollOffset type, a new getOffset helper to resolve functional offsets with a fallback for invalid values, and comprehensive unit tests for these new capabilities. I have no feedback to provide.

@zombieJ zombieJ merged commit 429d64e into master May 20, 2026
11 checks passed
@zombieJ zombieJ deleted the codex/scroll-offset-info branch May 20, 2026 08:43
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