]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/commitdiff
fix: update history when closing Reveal with `replaceState` #8012
authorNicolas Coden <nicolas@ncoden.fr>
Mon, 26 Mar 2018 08:08:31 +0000 (10:08 +0200)
committerNicolas Coden <nicolas@ncoden.fr>
Mon, 26 Mar 2018 08:08:31 +0000 (10:08 +0200)
When closing a Reveal with `replaceState: true`, push a new entry to reset the hash. So going back to the history reopen the modals opened before in reverse order.

Closes https://github.com/zurb/foundation-sites/issues/8012

js/foundation.reveal.js

index f0d87b99ee721775a176cac930dc9361f3da96ac..847d53f533df27dd0544f6058480edef1cd3521b 100644 (file)
@@ -437,13 +437,20 @@ class Reveal extends Plugin {
     }
 
     this.isActive = false;
-     if (_this.options.deepLink) {
-       if (window.history.replaceState) {
-         window.history.replaceState('', document.title, window.location.href.replace(`#${this.id}`, ''));
-       } else {
-         window.location.hash = '';
-       }
-     }
+    // If deepLink and we did not switched to an other modal...
+    if (_this.options.deepLink && window.location.hash === `#${this.id}`) {
+      // Remove the history hash
+      if (window.history.replaceState) {
+        const urlWithoutHash = window.location.pathname + window.location.search;
+        if (this.options.updateHistory) {
+          window.history.pushState({}, '', urlWithoutHash); // remove the hash
+        } else {
+          window.history.replaceState('', document.title, urlWithoutHash);
+        }
+      } else {
+        window.location.hash = '';
+      }
+    }
 
     this.$activeAnchor.focus();
   }