a11y-accordion-tabs icon indicating copy to clipboard operation
a11y-accordion-tabs copied to clipboard

Deeplinking addon?

Open evandiamond opened this issue 6 years ago • 6 comments

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?

evandiamond avatar Feb 01 '19 15:02 evandiamond

I'd love to see this too.

StephanieF avatar Oct 04 '19 01:10 StephanieF

Same here! I was hoping I could programmatically click() on one of the .js-tab-trigger anchors, but it doesn't seem to work.

jacobaarnold avatar Jan 27 '20 20:01 jacobaarnold

That’s a great idea. Thank you @evandiamond! I’ll look into how to add this over the next days.

matthiasott avatar May 21 '20 15:05 matthiasott

Any updates on this feature? Would be love to see it as other elements working great.

jane-tif avatar Dec 10 '21 17:12 jane-tif

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

iamrobert avatar Dec 04 '22 12:12 iamrobert

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

jacobaarnold avatar Dec 05 '22 14:12 jacobaarnold