From 33c55db80802ce4be9d2347c22e6d68823a46b0a Mon Sep 17 00:00:00 2001 From: Nicolas Coden Date: Tue, 9 Oct 2018 23:49:11 +0200 Subject: [PATCH] fix: fix scroll to deep-linking default anchor on initialization in Tabs/Accordion Related to https://github.com/zurb/foundation-sites/issues/11527 --- js/foundation.accordion.js | 12 +++++++++--- js/foundation.tabs.js | 11 ++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/js/foundation.accordion.js b/js/foundation.accordion.js index 78bf35aa8..13ab360d6 100644 --- a/js/foundation.accordion.js +++ b/js/foundation.accordion.js @@ -40,6 +40,8 @@ class Accordion extends Plugin { * @private */ _init() { + this._isInitializing = true; + this.$element.attr('role', 'tablist'); this.$tabs = this.$element.children('[data-accordion-item]'); @@ -70,9 +72,11 @@ class Accordion extends Plugin { this._checkDeepLink = () => { var anchor = window.location.hash; - // If there is no anchor, return to the initial panel - if (!anchor.length && this._initialAnchor) { - anchor = this._initialAnchor; + if (!anchor.length) { + // If we are still initializing and there is no anchor, then there is nothing to do + if (this._isInitializing) return; + // Otherwise, move to the initial anchor + if (this._initialAnchor) anchor = this._initialAnchor; } var $anchor = anchor && $(anchor); @@ -112,6 +116,8 @@ class Accordion extends Plugin { } this._events(); + + this._isInitializing = false; } /** diff --git a/js/foundation.tabs.js b/js/foundation.tabs.js index ba35f4afc..d780646c0 100644 --- a/js/foundation.tabs.js +++ b/js/foundation.tabs.js @@ -45,6 +45,7 @@ class Tabs extends Plugin { */ _init() { var _this = this; + this._isInitializing = true; this.$element.attr({'role': 'tablist'}); this.$tabTitles = this.$element.find(`.${this.options.linkClass}`); @@ -105,9 +106,11 @@ class Tabs extends Plugin { this._checkDeepLink = () => { var anchor = window.location.hash; - // If there is no anchor, return to the initial panel - if (!anchor.length && this._initialAnchor) { - anchor = this._initialAnchor; + if (!anchor.length) { + // If we are still initializing and there is no anchor, then there is nothing to do + if (this._isInitializing) return; + // Otherwise, move to the initial anchor + if (this._initialAnchor) anchor = this._initialAnchor; } var anchorNoHash = anchor.indexOf('#') >= 0 ? anchor.slice(1) : anchor; @@ -144,6 +147,8 @@ class Tabs extends Plugin { } this._events(); + + this._isInitializing = false; } /** -- 2.47.3