Skip to content

Conversation

@quite
Copy link

@quite quite commented Sep 7, 2025

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

  • Bug Fixes
    • Improved keyboard navigation in the light gallery for more reliable cross-browser behavior.
    • Left/Right arrows now change slides only when no modifier keys (Alt, Ctrl, Meta, Shift) are pressed, avoiding OS/browser shortcut conflicts.
    • Escape now consistently closes the gallery only when no modifier keys are held.

@CLAassistant
Copy link

CLAassistant commented Sep 7, 2025

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link

coderabbitai bot commented Sep 7, 2025

Walkthrough

Replaces 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

Cohort / File(s) Summary
Keyboard event handling
src/lightgallery.ts
Replaced numeric keyCode checks with e.key string comparisons for 'Escape', 'ArrowLeft', 'ArrowRight'; added noModifier guard (ignores events when Alt/Ctrl/Meta/Shift are held).

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
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Assessment against linked issues

Objective Addressed Explanation
Handle plain Escape/LeftArrow/RightArrow only; ignore with modifiers [#1732]

Poem

A rabbit taps keys with delicate care,
Alt no longer snatched from the air.
Left and Right hop only when plain,
Escape exits tidy, calm, and humane.
I thump in joy — soft paddles on the lane 🐇✨

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 52c8bd1 and addadf8.

📒 Files selected for processing (1)
  • src/lightgallery.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/lightgallery.ts
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

@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: 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

📥 Commits

Reviewing files that changed from the base of the PR and between 92717fc and 52c8bd1.

⛔ Files ignored due to path filters (44)
  • dist/lightgallery.es5.js is excluded by !**/dist/**
  • dist/lightgallery.min.js is excluded by !**/dist/**, !**/*.min.js
  • dist/lightgallery.umd.js is excluded by !**/dist/**
  • dist/plugins/autoplay/lg-autoplay.es5.js is excluded by !**/dist/**
  • dist/plugins/autoplay/lg-autoplay.min.js is excluded by !**/dist/**, !**/*.min.js
  • dist/plugins/autoplay/lg-autoplay.umd.js is excluded by !**/dist/**
  • dist/plugins/comment/lg-comment.es5.js is excluded by !**/dist/**
  • dist/plugins/comment/lg-comment.min.js is excluded by !**/dist/**, !**/*.min.js
  • dist/plugins/comment/lg-comment.umd.js is excluded by !**/dist/**
  • dist/plugins/fullscreen/lg-fullscreen.es5.js is excluded by !**/dist/**
  • dist/plugins/fullscreen/lg-fullscreen.min.js is excluded by !**/dist/**, !**/*.min.js
  • dist/plugins/fullscreen/lg-fullscreen.umd.js is excluded by !**/dist/**
  • dist/plugins/hash/lg-hash.es5.js is excluded by !**/dist/**
  • dist/plugins/hash/lg-hash.min.js is excluded by !**/dist/**, !**/*.min.js
  • dist/plugins/hash/lg-hash.umd.js is excluded by !**/dist/**
  • dist/plugins/mediumZoom/lg-medium-zoom.es5.js is excluded by !**/dist/**
  • dist/plugins/mediumZoom/lg-medium-zoom.min.js is excluded by !**/dist/**, !**/*.min.js
  • dist/plugins/mediumZoom/lg-medium-zoom.umd.js is excluded by !**/dist/**
  • dist/plugins/pager/lg-pager.es5.js is excluded by !**/dist/**
  • dist/plugins/pager/lg-pager.min.js is excluded by !**/dist/**, !**/*.min.js
  • dist/plugins/pager/lg-pager.umd.js is excluded by !**/dist/**
  • dist/plugins/relativeCaption/lg-relative-caption.es5.js is excluded by !**/dist/**
  • dist/plugins/relativeCaption/lg-relative-caption.min.js is excluded by !**/dist/**, !**/*.min.js
  • dist/plugins/relativeCaption/lg-relative-caption.umd.js is excluded by !**/dist/**
  • dist/plugins/rotate/lg-rotate.es5.js is excluded by !**/dist/**
  • dist/plugins/rotate/lg-rotate.min.js is excluded by !**/dist/**, !**/*.min.js
  • dist/plugins/rotate/lg-rotate.umd.js is excluded by !**/dist/**
  • dist/plugins/share/lg-share.es5.js is excluded by !**/dist/**
  • dist/plugins/share/lg-share.min.js is excluded by !**/dist/**, !**/*.min.js
  • dist/plugins/share/lg-share.umd.js is excluded by !**/dist/**
  • dist/plugins/thumbnail/lg-thumbnail.es5.js is excluded by !**/dist/**
  • dist/plugins/thumbnail/lg-thumbnail.min.js is excluded by !**/dist/**, !**/*.min.js
  • dist/plugins/thumbnail/lg-thumbnail.umd.js is excluded by !**/dist/**
  • dist/plugins/video/lg-video.es5.js is excluded by !**/dist/**
  • dist/plugins/video/lg-video.min.js is excluded by !**/dist/**, !**/*.min.js
  • dist/plugins/video/lg-video.umd.js is excluded by !**/dist/**
  • dist/plugins/vimeoThumbnail/lg-vimeo-thumbnail.es5.js is excluded by !**/dist/**
  • dist/plugins/vimeoThumbnail/lg-vimeo-thumbnail.min.js is excluded by !**/dist/**, !**/*.min.js
  • dist/plugins/vimeoThumbnail/lg-vimeo-thumbnail.umd.js is excluded by !**/dist/**
  • dist/plugins/zoom/lg-zoom.es5.js is excluded by !**/dist/**
  • dist/plugins/zoom/lg-zoom.min.js is excluded by !**/dist/**, !**/*.min.js
  • dist/plugins/zoom/lg-zoom.umd.js is excluded by !**/dist/**
  • package-lock.json is excluded by !**/package-lock.json
  • yarn.lock is 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
@quite quite force-pushed the grab-only-plain-keys branch from 52c8bd1 to addadf8 Compare September 7, 2025 11:10
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.

lightGallery unneccessarily grabs for example alt-LeftArrow, which should be browser Back

2 participants