Skip to content

Commit

Permalink
An attempt to add an "animateHistoryBrowsing" option to make animatio…
Browse files Browse the repository at this point in the history
…ns work not only when user navigates through links but also using back/forward buttons. May be useful for PWAs etc.
  • Loading branch information
OSDVF committed Sep 28, 2018
1 parent d13f6c1 commit d843c9a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default class Swup {
}
return true;
},
animateHistoryBrowsing: false,

LINK_SELECTOR: 'a[href^="' + window.location.origin + '"]:not([data-no-swup]), a[href^="/"]:not([data-no-swup]), a[href^="#"]:not([data-no-swup])',
FORM_SELECTOR: 'form[data-swup-form]',
Expand Down
8 changes: 5 additions & 3 deletions src/modules/loadPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function (data, popstate) {

let animationPromises = []

if (!popstate) {
if (!popstate || this.options.animateHistoryBrowsing) {
// start animation
document.documentElement.classList.add('is-changing')
document.documentElement.classList.add('is-leaving')
Expand Down Expand Up @@ -43,7 +43,8 @@ module.exports = function (data, popstate) {
} else {
var pop = data.url;
}
this.createState(pop)
if(!popstate)
this.createState(pop)
} else {
// proceed without animating
this.triggerEvent('animationSkipped')
Expand Down Expand Up @@ -98,6 +99,7 @@ module.exports = function (data, popstate) {
}

// go back to the actual page were still at
window.history.go(-1)
if(!this.options.animateHistoryBrowsing)
window.history.go(-1)
});
}
2 changes: 1 addition & 1 deletion src/modules/renderPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = function (page, popstate) {
document.documentElement.classList.remove('is-leaving')

// only add for non-popstate transitions
if (!popstate) {
if (!popstate||this.options.animateHistoryBrowsing) {
document.documentElement.classList.add('is-rendering')
}

Expand Down

0 comments on commit d843c9a

Please sign in to comment.