Alhadis / Accordion

Silky-smooth accordion widgets with no external dependencies.
https://www.npmjs.com/package/accordion
ISC License
32 stars 11 forks source link

Deep linking #15

Closed robertvrabel closed 5 years ago

robertvrabel commented 5 years ago

Do you have an example where you visit a page with a hash in the URL and it opens the appropriate accordion item and scrolls to it?

I am currently not seeing a way to trigger an individual open myself once I get the link element.

Any help would be appreciated!

Alhadis commented 5 years ago

Does the hash match an element ID in the page at startup? If so, the scrolling will be automatic (and not terribly pretty). Things also get a bit tricky when nested accordions are involved... 🤔

I really need to improve deep-linking support...

robertvrabel commented 5 years ago

@Alhadis Yea I guess the scrolling part isn't as important since the browser can automatically handle it, really depends on where you put the ID if it's visible or not to the user. In a case I'm playing around with, I'm putting it on the accordion links so they are all visible.

So my question is how do you open an accordion item once you have the element and the accordion is applied already?

    if(window.location.hash != '') {
        let item = document.getElementById(window.location.hash.substr(1));

        // Open item somehow...
    }
Alhadis commented 5 years ago

Every Accordion instance has a .folds property which provides access to the Fold instances that control the physical DOM element. Setting a Fold's .open property to false will close it; similarly, setting it to true will open it.

The rest of the API docs should hopefully explain the rest. :-)

robertvrabel commented 5 years ago

Great, thank you for the help! I have it working now with this code. I have an overall loop checking for multiple accordion instances and setting the accordion instance to accordion.

        // See if the hash is within this accordion so we can open it
        if(window.location.hash != '') {
            accordion.folds.forEach(function (fold) {
                if(fold.heading.getAttribute('id') == window.location.hash.substr(1)) {
                    fold.open = true;
                }
            });
        }

The only thing I noticed is it seems the height of the fold is calculated differently when using fold.open = true vs clicking it. Not a huge deal, but it looks a little cramped. Any insights?

Deep Link

deep-link

Clicked

clicked
Alhadis commented 5 years ago

Sometimes the height of a fold's content can change, such as when line-wrapping occurs, or when images and stuff load and push content further down. In these scenarios, call the .fit() method of the opened fold after a delay. You might have to do this a few times if the page hasn't finished loading.

robertvrabel commented 5 years ago

The only way I was successful was by adding a setTimeout to trigger the open later.

        // See if the hash is within this accordion so we can open it
        if(window.location.hash != '') {
            accordion.folds.forEach(function (fold) {
                if(fold.heading.getAttribute('id') == window.location.hash.substr(1)) {
                    window.setTimeout(function () {
                        fold.open = true;
                    }, 500);
                }
            });
        }

I typically dislike using it but I am not seeing a way to tie into an event for when Accordion is finished loading to trigger the code above.

Regardless I think this will be good enough for my situation. Feel free to close and thank you for the help! :)