Skip to content
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

option for closing the lightbox with the browser back button #428

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/featherlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
}

/* document wide key handler */
var eventMap = { keyup: 'onKeyUp', resize: 'onResize' };
var eventMap = { keyup: 'onKeyUp', resize: 'onResize', popstate: 'onPopState' };

var globalEventHandler = function(event) {
$.each(Featherlight.opened().reverse(), function() {
Expand Down Expand Up @@ -166,8 +166,10 @@
afterClose: $.noop, /* Called after close. Gets event as parameter, this contains all data */
onKeyUp: $.noop, /* Called on key up for the frontmost featherlight */
onResize: $.noop, /* Called after new content and when a window is resized */
onPopState: $.noop, /* Called when the history entry changes (back/forward button, link click, ...) */
type: null, /* Specify type of lightbox. If unset, it will check for the targetAttrs value. */
contentFilters: ['jquery', 'image', 'html', 'ajax', 'iframe', 'text'], /* List of content filters to use to determine the content */
useHistory: false, /* Use Browser History API */

/*** methods ***/
/* setup iterates over a single instance of featherlight and prepares the background and binds the events */
Expand Down Expand Up @@ -299,6 +301,9 @@

if($content) {
opened.push(self);
if (self.useHistory) {
history.pushState({featherlightInstanceCount: Featherlight.opened().length}, '');
}

toggleGlobalEvents(true);

Expand Down Expand Up @@ -336,6 +341,9 @@
toggleGlobalEvents(false);
}

if (self.useHistory && (history.state?.featherlightInstanceCount ?? 0) > $.featherlight.opened().length) {
history.back();
}
self.$instance.fadeOut(self.closeSpeed,function(){
self.$instance.detach();
self.afterClose(event);
Expand Down Expand Up @@ -651,6 +659,14 @@
this.$instance.find('[autofocus]:not([disabled])').focus();
this.onResize(event);
return r;
},

onPopState: function(_super, event){
if (this.useHistory && (event.originalEvent.state?.featherlightInstanceCount ?? 0) < $.featherlight.opened().length) {
$.featherlight.close(event);
}

return _super(event);
}
}
});
Expand Down