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

@nrps9909 nrps9909 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.

Reviewed exact head 8ee1abcd9d73b24a019d962933e8fdcce8997f5e.

The change is correctly scoped to an open group preview: LEFT/RIGHT still perform the existing image navigation and now cancel the browser's default scrolling behavior after that navigation. Single-image/non-group previews do not enter this branch, and unrelated keys remain uncancelled. I also checked the first/last-image boundary behavior; cancelling LEFT/RIGHT while the modal owns keyboard navigation is consistent even when there is no further image in that direction.

Validation performed on this exact head:

  • npm test -- tests/previewGroup.test.tsx --runInBand โ€” 16/16 passed
  • npm test -- --runInBand โ€” 80/80 passed, 1/1 snapshot
  • independent temporary probe โ€” before opening, RIGHT was not cancelled; in an open group preview, RIGHT and LEFT were cancelled while navigating 1/2 โ†’ 2/2 โ†’ 1/2; UP remained uncancelled
  • npm run tsc โ€” passed
  • npm run lint -- --no-fix โ€” 0 errors (12 pre-existing warnings)
  • npx prettier --check src/Preview/index.tsx โ€” passed
  • npm run build โ€” passed
  • git diff --check ca5754037261b89a60681bf85c056d6533adee8d...8ee1abcd9d73b24a019d962933e8fdcce8997f5e โ€” passed

The reported Vercel failure is an external deployment-authorization check, not a code failure. I found no blocking issue in the change.

Disclosure: Codex assisted with source tracing, test execution, and drafting this review; I independently verified the behavior and conclusion against the exact PR head.

@codecov

codecov Bot commented Aug 17, 2026

Copy link
Copy Markdown

Codecov Report

โœ… All modified and coverable lines are covered by tests.
โœ… Project coverage is 99.26%. Comparing base (ca57540) to head (8ee1abc).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #526   +/-   ##
=======================================
  Coverage   99.26%   99.26%           
=======================================
  Files          17       17           
  Lines         543      545    +2     
  Branches      166      167    +1     
=======================================
+ Hits          539      541    +2     
  Misses          4        4           

โ˜” View full report in Codecov by Harness.
๐Ÿ“ข 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.

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