Skip to content

Conversation

@cactuser-Lu
Copy link

@cactuser-Lu cactuser-Lu commented Dec 23, 2025

fix: ant-design/ant-design#56290

Summary by CodeRabbit

发布说明

  • Bug Fixes
    • 优化了预览中的键盘交互:按下 ESC 会阻止浏览器默认行为并关闭预览;左右箭头在显示导航控制时可用于前后切换,并阻止默认行为以确保一致响应。

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

@vercel
Copy link

vercel bot commented Dec 23, 2025

Someone is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Dec 23, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

在 Preview 组件的键盘处理器中,将对 ESC、LEFT、RIGHT 键事件调用 preventDefault(),并通过 onClose / onActive 分发相应动作,防止父级容器在切换或关闭时发生滚动或默认行为。

Changes

内聚组件/文件 变更摘要
键盘事件处理优化
src/Preview/index.tsx
将对 ESC、LEFT、RIGHT 键的显式 if/else 处理替换为统一的 switch 分支;为 ESC、LEFT、RIGHT 添加 event.preventDefault() 调用,ESC 触发 onClose(),LEFT/RIGHT 在 showLeftOrRightSwitches 为真时分别触发 onActive(-1)/onActive(+1)

Sequence Diagram(s)

(不生成序列图 — 变更限于单组件内的键盘事件处理,交互参与方少于 3 个)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 分钟

兔兔诗歌

🐰 轻敲键盘不慌张,
左右切换不翻墙,
ESC 一按闭上眼,
父容静静不摇晃,
预览更稳心里香 🥕✨

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题清晰准确地总结了主要变更,即通过键盘切换图片时防止页面滚动,与代码改动(switch语句中添加preventDefault)完全相符。
Linked Issues check ✅ Passed 代码改动通过在键盘处理程序中添加preventDefault()来解决问题#56290中报告的父级div滚动问题,完全满足链接问题中的编码要求。
Out of Scope Changes check ✅ Passed 所有改动仅限于Preview组件的键盘处理逻辑,直接针对解决图片预览时的滚动问题,不存在超出范围的改动。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 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 2f77a73 and 1885625.

📒 Files selected for processing (1)
  • src/Preview/index.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/Preview/index.tsx

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 @cactuser-Lu, 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 addresses a user experience issue within the image preview component by preventing default browser actions when specific keyboard keys are pressed. Previously, using keys like 'Escape' or arrow keys for navigation could inadvertently trigger page scrolling. The changes ensure that these keyboard inputs are exclusively handled by the image preview, providing a smoother and more predictable interaction for users.

Highlights

  • Keyboard Event Handling: Implemented event.preventDefault() for keyboard interactions within the image preview component to stop default browser behaviors.
  • Bug Fix: Resolved an issue where pressing ESC, LEFT, or RIGHT arrow keys could cause unintended page scrolling or other default browser actions while using the image preview.

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

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 correctly fixes the issue of page scrolling when switching images using the keyboard by adding event.preventDefault(). The change is straightforward and effective. I've also added a suggestion to refactor the keyboard event handling logic using a switch statement, which could improve code readability and maintainability. Overall, great job on fixing this bug.

Comment on lines 335 to 348
if (keyCode === KeyCode.ESC) {
event.preventDefault();
onClose?.();
}

if (showLeftOrRightSwitches) {
if (keyCode === KeyCode.LEFT) {
event.preventDefault();
onActive(-1);
} else if (keyCode === KeyCode.RIGHT) {
event.preventDefault();
onActive(1);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

While the current implementation is correct, you could refactor this block to use a switch statement. This would make the code more structured, readable, and easier to maintain, especially if more keyboard shortcuts are added in the future. It also helps to reduce some code duplication by grouping the LEFT and RIGHT arrow key handling.

      switch (keyCode) {
        case KeyCode.ESC:
          event.preventDefault();
          onClose?.();
          break;
        case KeyCode.LEFT:
        case KeyCode.RIGHT:
          if (showLeftOrRightSwitches) {
            event.preventDefault();
            onActive(keyCode === KeyCode.LEFT ? -1 : 1);
          }
          break;
        default:
          break;
      }

Copy link
Member

Choose a reason for hiding this comment

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

+1

Copy link
Author

Choose a reason for hiding this comment

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

OK

@codecov
Copy link

codecov bot commented Dec 23, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.41%. Comparing base (d3985c4) to head (2f77a73).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #493   +/-   ##
=======================================
  Coverage   99.41%   99.41%           
=======================================
  Files          17       17           
  Lines         509      512    +3     
  Branches      152      152           
=======================================
+ Hits          506      509    +3     
  Misses          3        3           

☔ 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

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an issue where using keyboard shortcuts (ESC, LEFT, RIGHT arrows) in the image preview component would trigger unwanted page scrolling. The fix adds event.preventDefault() calls to the keyboard event handler to prevent default browser behavior while maintaining the intended preview functionality.

Key Changes

  • Added event.preventDefault() for ESC key to prevent default behavior when closing preview
  • Added event.preventDefault() for LEFT and RIGHT arrow keys to prevent page scrolling when switching between images

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 336 to 346
event.preventDefault();
onClose?.();
}

if (showLeftOrRightSwitches) {
if (keyCode === KeyCode.LEFT) {
event.preventDefault();
onActive(-1);
} else if (keyCode === KeyCode.RIGHT) {
event.preventDefault();
onActive(1);
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

The keyboard event handling behavior, including the preventDefault calls, lacks test coverage. Consider adding tests to verify that:

  1. ESC key closes the preview and prevents default browser behavior
  2. LEFT/RIGHT arrow keys switch between images when showLeftOrRightSwitches is true
  3. preventDefault is properly called to prevent page scrolling

This would help ensure the fix for issue #56290 is properly validated and prevent regressions.

Copilot uses AI. Check for mistakes.
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 键盘切换图片,外部元素会跟着滚动

2 participants