Skip to content

Commit d15d3c8

Browse files
committed
feat: add controller.closeOnEscape setting
Allow users to disable the lightbox's Escape-to-close behavior with `controller={{ closeOnEscape: false }}`. Useful when a custom slide contains an interactive element with its own Escape handling — without this, the bubbled Escape would also dismiss the lightbox. Defaults to `true` to preserve existing behavior.
1 parent 1d08bb3 commit d15d3c8

4 files changed

Lines changed: 13 additions & 2 deletions

File tree

docs/documentation.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ import "yet-another-react-lightbox/styles.css";
182182
&nbsp;&nbsp;closeOnPullUp?: boolean;<br />
183183
&nbsp;&nbsp;closeOnPullDown?: boolean;<br />
184184
&nbsp;&nbsp;closeOnBackdropClick?: boolean;<br />
185+
&nbsp;&nbsp;closeOnEscape?: boolean;<br />
185186
&nbsp;&nbsp;disableSwipeNavigation?: boolean;<br />
186187
&#125;
187188
</td>
@@ -195,10 +196,16 @@ import "yet-another-react-lightbox/styles.css";
195196
<li>`closeOnPullUp` - if `true`, close the lightbox on pull-up gesture</li>
196197
<li>`closeOnPullDown` - if `true`, close the lightbox on pull-down gesture</li>
197198
<li>`closeOnBackdropClick` - if `true`, close the lightbox when the backdrop is clicked</li>
199+
<li>`closeOnEscape` - if `true`, close the lightbox on Escape key press (default: `true`)</li>
198200
<li>`disableSwipeNavigation` - if `true`, disable slide change on pointer swipe / drag</li>
199201
</ul>
200202
<p>
201-
Default value: <span class="font-mono">&#123; ref: null, focus: true, aria: false, touchAction: "none" &#125;</span>
203+
Default value:
204+
<span class="font-mono">
205+
&#123; ref: null, focus: true, aria: false, touchAction: "none", closeOnPullUp: false,
206+
closeOnPullDown: false, closeOnBackdropClick: false, closeOnEscape: true,
207+
disableSwipeNavigation: false &#125;
208+
</span>
202209
</p>
203210
</td>
204211
</tr>

src/modules/Navigation/useKeyboardNavigation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { useNavigationState } from "./useNavigationState.js";
1616
export function useKeyboardNavigation<T extends Element>(subscribeSensors: UseSensors<T>["subscribeSensors"]) {
1717
const isRTL = useRTL();
1818
const { publish } = useEvents();
19-
const { animation } = useLightboxProps();
19+
const { animation, controller } = useLightboxProps();
2020
const { prevDisabled, nextDisabled } = useNavigationState();
2121

2222
const throttle = (animation.navigation ?? animation.swipe) / 2;
@@ -27,6 +27,7 @@ export function useKeyboardNavigation<T extends Element>(subscribeSensors: UseSe
2727
const handleKeyDown = useEventCallback((event: React.KeyboardEvent) => {
2828
switch (event.key) {
2929
case VK_ESCAPE:
30+
if (!controller.closeOnEscape) return;
3031
publish(ACTION_CLOSE);
3132
break;
3233
case VK_ARROW_LEFT:

src/props.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const LightboxDefaultProps: LightboxProps = {
3535
closeOnPullUp: false,
3636
closeOnPullDown: false,
3737
closeOnBackdropClick: false,
38+
closeOnEscape: true,
3839
preventDefaultWheelX: true,
3940
preventDefaultWheelY: false,
4041
disableSwipeNavigation: false,

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ export interface ControllerSettings {
228228
closeOnPullDown: boolean;
229229
/** if `true`, close the lightbox when the backdrop is clicked */
230230
closeOnBackdropClick: boolean;
231+
/** if `true`, close the lightbox on Escape key press (default: `true`) */
232+
closeOnEscape: boolean;
231233
/** if `true`, prevent default for horizontal wheel scroll events (for internal use only) */
232234
preventDefaultWheelX: boolean;
233235
/** if `true`, prevent default for vertical wheel scroll events (for internal use only) */

0 commit comments

Comments
 (0)