Skip to content
This repository was archived by the owner on Jan 19, 2023. It is now read-only.

Commit a1fc2b1

Browse files
committed
Add option to close the lightbox when the user clicks off the image
1 parent 089f612 commit a1fc2b1

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,4 @@ keyRepeatKeyupBonus | number | `40` | | Amount of time (ms) r
9595
imageTitle | string | | | Image title
9696
toolbarButtons | node[] | | | Array of custom toolbar buttons
9797
imagePadding | number | `10` | | Padding (px) between the edge of the window and the lightbox
98+
clickOutsideToClose | bool | `true` | | When true, clicks outside of the image close the lightbox

src/ReactImageLightbox.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ const ReactImageLightbox = React.createClass({
123123

124124
// Padding (px) between the edge of the window and the lightbox
125125
imagePadding: PropTypes.number,
126+
127+
// When true, clicks outside of the image close the lightbox
128+
clickOutsideToClose: PropTypes.bool,
126129
},
127130

128131
getDefaultProps() {
@@ -140,6 +143,7 @@ const ReactImageLightbox = React.createClass({
140143
keyRepeatKeyupBonus : 40,
141144

142145
imagePadding: 10,
146+
clickOutsideToClose: true,
143147
};
144148
},
145149

@@ -531,12 +535,12 @@ const ReactImageLightbox = React.createClass({
531535

532536
// Request that the lightbox be closed
533537
requestClose(event) {
534-
const closeLightbox = () => {
535-
// Call the parent close request
536-
return this.props.onCloseRequest(event);
537-
};
538+
// Call the parent close request
539+
const closeLightbox = () => this.props.onCloseRequest(event);
538540

539-
if (this.props.animationDisabled || (event.type === 'keydown' && !this.props.animationOnKeyInput)) {
541+
if (this.props.animationDisabled ||
542+
(event.type === 'keydown' && !this.props.animationOnKeyInput)
543+
) {
540544
// No animation
541545
return closeLightbox();
542546
} else {
@@ -591,6 +595,12 @@ const ReactImageLightbox = React.createClass({
591595
this.requestMove('next', event);
592596
},
593597

598+
closeIfClickInner(event) {
599+
if (event.target.className.search(/\binner\b/) > -1) {
600+
this.requestClose(event);
601+
}
602+
},
603+
594604
// Attach key and mouse input events
595605
attachListeners() {
596606
if (!this.listenersAttached) {
@@ -954,6 +964,7 @@ const ReactImageLightbox = React.createClass({
954964

955965
<div // Image holder
956966
className="inner"
967+
onClick={this.props.clickOutsideToClose ? this.closeIfClickInner : noop}
957968
style={[Styles.inner]}
958969
>
959970
{images}

0 commit comments

Comments
 (0)