Open evandiamond opened 5 years ago
I'd love to see this too.
Same here! I was hoping I could programmatically click() on one of the .js-tab-trigger anchors, but it doesn't seem to work.
That’s a great idea. Thank you @evandiamond! I’ll look into how to add this over the next days.
Any updates on this feature? Would be love to see it as other elements working great.
Here's a quick and dirty approach that I have got it working for tabs:
1. Add Hash to URL on click
At the bottom of:
AccordionTabs.prototype._clickEvent = function (e) {
//ADD HASH TO URL
if (!this.isAccordion) {
var getHash = new URL(e.target);
history.replaceState(null, '', getHash.hash);
}
if (this.isAccordion) {
history.replaceState(null, '', '#' + closestTrigger.getAttribute('aria-controls'));
}
2. Set the tab to load by setting the data.selectedTab
at the bottom of: function AccordionTabs(el, options) {
before
// get Hash From URL
var URLhash = new URL(document.URL).hash;
URLhash = URLhash.replace('#', '');
if (URLhash) {
// get all tabSections
var tabSections = document.querySelectorAll("section.tabs-panel");
// initialize a counter variable
var count = 0;
// iterate through the elements to find the one with the specified URLhash
for (var i = 0; i < tabSections.length; i++) {
var tabSection = tabSections[i];
if (tabSection.id === URLhash) {
// increment the counter if the ID matches
count = i;
}
}
// log the final count
this.options.selectedTab = count;
}
3. Here's the complete JS script: https://gist.github.com/iamrobert/ae7fd738151e909f351adce0393d05fc
I ended up doing something quite similar using jQuery:
$(".js-tabs").each(function (index) {
var targetTab = 0;
if (window.location.hash) {
var hash = window.location.hash.substring(1);
$(this).find('.js-tabs-panel').each(function (i) {
if ($(this).attr('id') == hash) {
targetTab = i;
}
});
}
new AccordionTabs(this, {
breakpoint: 640,
tabsAllowed: true,
selectedTab: targetTab,
startCollapsed: false
});
});
Would it be possible to have a deeplink option with this tab set?
Clicking on the tab would add the hash to the end of the url. Like example below:
site.com/#tab-04 This would bring you directly to the 4th tab in the set?