-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: grab only unmodified esc/leftarrow/rightarrow #1733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
WalkthroughReplaces numeric keyCode checks in src/lightgallery.ts with string-based e.key comparisons and adds a noModifier guard to ignore key events when Alt, Ctrl, Meta, or Shift are pressed for Escape, ArrowLeft, and ArrowRight handlers. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Browser
participant LightGallery
User->>Browser: keydown (Escape / ArrowLeft / ArrowRight)
Browser->>LightGallery: key event (e)
alt Any modifier pressed (Alt/Ctrl/Meta/Shift)
LightGallery-->>Browser: Ignore event
else No modifiers
opt e.key === "Escape"
LightGallery->>LightGallery: closeGallery()
end
opt e.key === "ArrowLeft"
LightGallery->>LightGallery: goToPrevious()
end
opt e.key === "ArrowRight"
LightGallery->>LightGallery: goToNext()
end
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Assessment against linked issues
Poem
Warning Review ran into problems🔥 ProblemsGit: Failed to clone repository. Please run the 📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
🧪 Generate 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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
src/lightgallery.ts (2)
2134-2169: DRY the modifier check and type the event for clarity.Minor readability win and better intellisense.
- $LG(window).on(`keydown.lg.global${this.lgId}`, (e) => { + $LG(window).on(`keydown.lg.global${this.lgId}`, (e: KeyboardEvent) => { + const hasModifier = e.altKey || e.ctrlKey || e.metaKey || e.shiftKey; if ( this.lgOpened && this.settings.escKey === true && - e.key === 'Escape' && - !(e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) + e.key === 'Escape' && + !hasModifier ) { e.preventDefault(); … } if (this.lgOpened && this.galleryItems.length > 1) { - if (e.key === 'ArrowLeft' && !(e.altKey || e.ctrlKey || e.metaKey || e.shiftKey)) { + if (e.key === 'ArrowLeft' && !hasModifier) { e.preventDefault(); this.goToPrevSlide(); } - if (e.key === 'ArrowRight' && !(e.altKey || e.ctrlKey || e.metaKey || e.shiftKey)) { + if (e.key === 'ArrowRight' && !hasModifier) { e.preventDefault(); this.goToNextSlide(); } }
2134-2169: Optional: add legacy key fallbacks if older browsers matter.If you still need to support older engines, consider tolerant checks (Esc/Left/Right and keyCode).
- if (this.lgOpened && this.galleryItems.length > 1) { + const isEsc = e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27; + const isLeft = e.key === 'ArrowLeft' || e.key === 'Left' || e.keyCode === 37; + const isRight = e.key === 'ArrowRight'|| e.key === 'Right'|| e.keyCode === 39; + if (this.lgOpened && this.galleryItems.length > 1) { - if (e.key === 'ArrowLeft' && !hasModifier) { + if (isLeft && !hasModifier) { e.preventDefault(); this.goToPrevSlide(); } - if (e.key === 'ArrowRight' && !hasModifier) { + if (isRight && !hasModifier) { e.preventDefault(); this.goToNextSlide(); } } - if (this.lgOpened && this.settings.escKey === true && e.key === 'Escape' && !hasModifier) { + if (this.lgOpened && this.settings.escKey === true && isEsc && !hasModifier) { e.preventDefault(); … }Please confirm your supported browser matrix before adopting this.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (44)
dist/lightgallery.es5.jsis excluded by!**/dist/**dist/lightgallery.min.jsis excluded by!**/dist/**,!**/*.min.jsdist/lightgallery.umd.jsis excluded by!**/dist/**dist/plugins/autoplay/lg-autoplay.es5.jsis excluded by!**/dist/**dist/plugins/autoplay/lg-autoplay.min.jsis excluded by!**/dist/**,!**/*.min.jsdist/plugins/autoplay/lg-autoplay.umd.jsis excluded by!**/dist/**dist/plugins/comment/lg-comment.es5.jsis excluded by!**/dist/**dist/plugins/comment/lg-comment.min.jsis excluded by!**/dist/**,!**/*.min.jsdist/plugins/comment/lg-comment.umd.jsis excluded by!**/dist/**dist/plugins/fullscreen/lg-fullscreen.es5.jsis excluded by!**/dist/**dist/plugins/fullscreen/lg-fullscreen.min.jsis excluded by!**/dist/**,!**/*.min.jsdist/plugins/fullscreen/lg-fullscreen.umd.jsis excluded by!**/dist/**dist/plugins/hash/lg-hash.es5.jsis excluded by!**/dist/**dist/plugins/hash/lg-hash.min.jsis excluded by!**/dist/**,!**/*.min.jsdist/plugins/hash/lg-hash.umd.jsis excluded by!**/dist/**dist/plugins/mediumZoom/lg-medium-zoom.es5.jsis excluded by!**/dist/**dist/plugins/mediumZoom/lg-medium-zoom.min.jsis excluded by!**/dist/**,!**/*.min.jsdist/plugins/mediumZoom/lg-medium-zoom.umd.jsis excluded by!**/dist/**dist/plugins/pager/lg-pager.es5.jsis excluded by!**/dist/**dist/plugins/pager/lg-pager.min.jsis excluded by!**/dist/**,!**/*.min.jsdist/plugins/pager/lg-pager.umd.jsis excluded by!**/dist/**dist/plugins/relativeCaption/lg-relative-caption.es5.jsis excluded by!**/dist/**dist/plugins/relativeCaption/lg-relative-caption.min.jsis excluded by!**/dist/**,!**/*.min.jsdist/plugins/relativeCaption/lg-relative-caption.umd.jsis excluded by!**/dist/**dist/plugins/rotate/lg-rotate.es5.jsis excluded by!**/dist/**dist/plugins/rotate/lg-rotate.min.jsis excluded by!**/dist/**,!**/*.min.jsdist/plugins/rotate/lg-rotate.umd.jsis excluded by!**/dist/**dist/plugins/share/lg-share.es5.jsis excluded by!**/dist/**dist/plugins/share/lg-share.min.jsis excluded by!**/dist/**,!**/*.min.jsdist/plugins/share/lg-share.umd.jsis excluded by!**/dist/**dist/plugins/thumbnail/lg-thumbnail.es5.jsis excluded by!**/dist/**dist/plugins/thumbnail/lg-thumbnail.min.jsis excluded by!**/dist/**,!**/*.min.jsdist/plugins/thumbnail/lg-thumbnail.umd.jsis excluded by!**/dist/**dist/plugins/video/lg-video.es5.jsis excluded by!**/dist/**dist/plugins/video/lg-video.min.jsis excluded by!**/dist/**,!**/*.min.jsdist/plugins/video/lg-video.umd.jsis excluded by!**/dist/**dist/plugins/vimeoThumbnail/lg-vimeo-thumbnail.es5.jsis excluded by!**/dist/**dist/plugins/vimeoThumbnail/lg-vimeo-thumbnail.min.jsis excluded by!**/dist/**,!**/*.min.jsdist/plugins/vimeoThumbnail/lg-vimeo-thumbnail.umd.jsis excluded by!**/dist/**dist/plugins/zoom/lg-zoom.es5.jsis excluded by!**/dist/**dist/plugins/zoom/lg-zoom.min.jsis excluded by!**/dist/**,!**/*.min.jsdist/plugins/zoom/lg-zoom.umd.jsis excluded by!**/dist/**package-lock.jsonis excluded by!**/package-lock.jsonyarn.lockis excluded by!**/yarn.lock,!**/*.lock
📒 Files selected for processing (1)
src/lightgallery.ts(2 hunks)
🔇 Additional comments (3)
src/lightgallery.ts (3)
2134-2151: Meets objective: Esc is handled only when unmodified.Switching to e.key and adding the modifier guard prevents hijacking combos like Alt+Esc. Looks good.
2153-2159: Left arrow: plain key only — good.This avoids capturing Alt+Left (Back) and similar combos while preserving plain ArrowLeft navigation.
2161-2167: Right arrow: plain key only — good.Same benefit for ArrowRight; behavior matches the PR intent.
This avoid hijacking for example Alt-Left, which is typically Back in the browser. lightGallery only uses the plain Esc, LeftArrow, RightArrow keys, and should not grab them with any modifiers. We now also use the KeyboardEvent.key property, instead of the deprecated keyCode. The key property has been available across browsers since 2017. Ref: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key Closes sachinchoolur#1732
52c8bd1 to
addadf8
Compare
This avoid hijacking for example Alt-Left, which is typically Back in
the browser. lightGallery only uses the plain Esc, LeftArrow, RightArrow
keys, and should not grab them with any modifiers.
We now also use the KeyboardEvent.key property, instead of the
deprecated keyCode. The key property has been available across browsers
since 2017. Ref:
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key
Closes #1732
Summary by CodeRabbit