Skip to content

Commit 5ea51f3

Browse files
ncodenjamiechong
andcommitted
fix: return to initial state when reseting hash in Accordion & Tabs foundation#11100
When deep-linking is enabled and the user goes back in the history up to when no hash is set, display the initially-selected content/tab instead of staying with the content/tab that was selected the first. Closes foundation#11100 Replaces foundation#11101 Co-authored-by: Jamie Chong <[email protected]>
1 parent bcd208d commit 5ea51f3

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

js/foundation.accordion.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,25 @@ class Accordion extends Plugin {
5959

6060
$content.attr({'role': 'tabpanel', 'aria-labelledby': linkId, 'aria-hidden': true, 'id': id});
6161
});
62+
6263
var $initActive = this.$element.find('.is-active').children('[data-tab-content]');
6364
this.firstTimeInit = true;
64-
if($initActive.length){
65+
if ($initActive.length) {
66+
// Save up the initial hash to return to it later when going back in history
67+
this._initialAnchor = $initActive.prev('a').attr('href');
68+
6569
this.down($initActive, this.firstTimeInit);
6670
this.firstTimeInit = false;
6771
}
6872

6973
this._checkDeepLink = () => {
7074
var anchor = window.location.hash;
75+
76+
// if there is no anchor, return to the initial panel
77+
if (!anchor.length && this._initialAnchor) {
78+
anchor = this._initialAnchor;
79+
}
80+
7181
//need a hash and a relevant anchor in this tabset
7282
if(anchor.length) {
7383
var $link = this.$element.find('[href$="'+anchor+'"]'),

js/foundation.tabs.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class Tabs extends Plugin {
7373
'aria-labelledby': linkId
7474
});
7575

76+
// Save up the initial hash to return to it later when going back in history
77+
if (isActive) {
78+
_this._initialAnchor = `#${hash}`;
79+
}
80+
7681
if(!isActive) {
7782
$tabContent.attr('aria-hidden', 'true');
7883
}
@@ -85,6 +90,7 @@ class Tabs extends Plugin {
8590
});
8691
}
8792
});
93+
8894
if(this.options.matchHeight) {
8995
var $images = this.$tabContent.find('img');
9096

@@ -98,8 +104,14 @@ class Tabs extends Plugin {
98104
//current context-bound function to open tabs on page load or history hashchange
99105
this._checkDeepLink = () => {
100106
var anchor = window.location.hash;
107+
108+
// if there is no anchor, return to the initial tab
109+
if (!anchor.length && this._initialAnchor) {
110+
anchor = this._initialAnchor;
111+
}
112+
101113
//need a hash and a relevant anchor in this tabset
102-
if(anchor.length) {
114+
if (anchor.length) {
103115
var anchorNoHash = (anchor.indexOf('#') >= 0 ? anchor.slice(1) : anchor);
104116
var $link = this.$element.find(`[href$="${anchor}"],[data-tabs-target="${anchorNoHash}"]`).first();
105117
if ($link.length) {

0 commit comments

Comments
 (0)