Skip to content

fix: prevent parent scroll on keyboard navigation in preview - #526

Open
waterWang wants to merge 1 commit into
react-component:masterfrom
waterWang:fix/prevent-keyboard-scroll-parent
Open

fix: prevent parent scroll on keyboard navigation in preview#526
waterWang wants to merge 1 commit into
react-component:masterfrom
waterWang:fix/prevent-keyboard-scroll-parent

Conversation

@waterWang

@waterWang waterWang commented Aug 3, 2026

Copy link
Copy Markdown

Description

When the Image preview is open and the user presses LEFT/RIGHT arrow keys to switch images, the browser's default scroll behavior was not prevented. This caused the parent element to scroll instead of the Image preview navigating between images.

Root Cause

The onKeyDown handler in Preview/index.tsx handles LEFT/RIGHT arrow keys to navigate between images, but does not call event.preventDefault(). The browser's default behavior for arrow keys is to scroll the scrollable parent element.

Fix

Added event.preventDefault() when handling LEFT and RIGHT key events in the Preview component, preventing the browser's default scroll behavior.

Related Issue

Closes ant-design/ant-design#56290 — Image 键盘切换图片,外部元素会跟着滚动

Summary by CodeRabbit

  • Bug 修复
    • 优化预览中的左右方向键操作,避免触发浏览器默认行为。

When the Image preview is open and the user presses LEFT/RIGHT arrow keys
to switch images, the browser's default scroll behavior was not prevented.
This caused the parent element to scroll instead of the Image preview
navigating between images.

The fix adds event.preventDefault() when handling LEFT and RIGHT key
events in the Preview component, preventing the browser's default
scroll behavior.

Closes ant-design/ant-design#56290
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@vercel

vercel Bot commented Aug 3, 2026

Copy link
Copy Markdown

@waterWang is attempting to deploy a commit to the afc163's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

预览组的左右方向键处理新增 event.preventDefault() 调用。浏览器不再因键盘导航触发外部元素滚动。

Changes

预览键盘导航

Layer / File(s) Summary
阻止方向键默认行为
src/Preview/index.tsx
预览组处理左、右方向键后调用 event.preventDefault()

Estimated code review effort: 1 (简单) | ~5 minutes

Suggested reviewers: qdyanbing

Poem

兔子按键跳呀跳,
左右切图不乱跑。
默认滚动停一停,
预览画面稳稳好。

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确描述了阻止预览键盘导航触发父元素滚动的主要变更。
Linked Issues check ✅ Passed 代码为左右方向键调用 event.preventDefault(),满足问题 #56290 中防止父级 div 滚动的要求。
Out of Scope Changes check ✅ Passed 变更仅涉及预览键盘导航的默认行为,未发现与问题 #56290 无关的代码修改。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/Preview/index.tsx (1)

351-354: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

为方向键默认行为增加回归断言。

现有 tests/previewGroup.test.tsx 只验证图片切换,没有验证 event.defaultPrevented。请为 LEFT 和 RIGHT 事件增加断言,确保后续改动不会移除 event.preventDefault()

🤖 Prompt for 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.

In `@src/Preview/index.tsx` around lines 351 - 354, 在 tests/previewGroup.test.tsx
的方向键事件测试中,为 LEFT 和 RIGHT 两种按键分别增加 event.defaultPrevented 断言,确保触发图片切换时仍调用
event.preventDefault();保留现有图片切换断言不变。
🤖 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.

Nitpick comments:
In `@src/Preview/index.tsx`:
- Around line 351-354: 在 tests/previewGroup.test.tsx 的方向键事件测试中,为 LEFT 和 RIGHT
两种按键分别增加 event.defaultPrevented 断言,确保触发图片切换时仍调用
event.preventDefault();保留现有图片切换断言不变。

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d36a4d48-b6b1-4645-ba05-12ec05679cfd

📥 Commits

Reviewing files that changed from the base of the PR and between ca57540 and 8ee1abc.

📒 Files selected for processing (1)
  • src/Preview/index.tsx

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.

Image 键盘切换图片,外部元素会跟着滚动

1 participant