Skip to content

Commit

Permalink
fix: return to initial state when reseting hash in Accordion & Tabs #…
Browse files Browse the repository at this point in the history
…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 #11100
Replaces #11101
  • Loading branch information
ncoden committed Aug 26, 2018
1 parent bcd208d commit d01d8b6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 11 additions & 1 deletion js/foundation.accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,25 @@ class Accordion extends Plugin {

$content.attr({'role': 'tabpanel', 'aria-labelledby': linkId, 'aria-hidden': true, 'id': id});
});

var $initActive = this.$element.find('.is-active').children('[data-tab-content]');
this.firstTimeInit = true;
if($initActive.length){
if ($initActive.length) {
// Save up the initial hash to return to it later when going back in history
this._initialAnchor = $initActive.prev('a').attr('href');

this.down($initActive, this.firstTimeInit);
this.firstTimeInit = false;
}

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;
}

//need a hash and a relevant anchor in this tabset
if(anchor.length) {
var $link = this.$element.find('[href$="'+anchor+'"]'),
Expand Down
14 changes: 13 additions & 1 deletion js/foundation.tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class Tabs extends Plugin {
'aria-labelledby': linkId
});

// Save up the initial hash to return to it later when going back in history
if (isActive) {
_this._initialAnchor = `#${hash}`;
}

if(!isActive) {
$tabContent.attr('aria-hidden', 'true');
}
Expand All @@ -85,6 +90,7 @@ class Tabs extends Plugin {
});
}
});

if(this.options.matchHeight) {
var $images = this.$tabContent.find('img');

Expand All @@ -98,8 +104,14 @@ class Tabs extends Plugin {
//current context-bound function to open tabs on page load or history hashchange
this._checkDeepLink = () => {
var anchor = window.location.hash;

// if there is no anchor, return to the initial tab
if (!anchor.length && this._initialAnchor) {
anchor = this._initialAnchor;
}

//need a hash and a relevant anchor in this tabset
if(anchor.length) {
if (anchor.length) {
var anchorNoHash = (anchor.indexOf('#') >= 0 ? anchor.slice(1) : anchor);
var $link = this.$element.find(`[href$="${anchor}"],[data-tabs-target="${anchorNoHash}"]`).first();
if ($link.length) {
Expand Down

0 comments on commit d01d8b6

Please sign in to comment.