Coder Social home page Coder Social logo

Deep linking about accordion HOT 6 CLOSED

alhadis avatar alhadis commented on June 15, 2024
Deep linking

from accordion.

Comments (6)

robertvrabel avatar robertvrabel commented on June 15, 2024 1

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! :)

from accordion.

Alhadis avatar Alhadis commented on June 15, 2024

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...

from accordion.

robertvrabel avatar robertvrabel commented on June 15, 2024

@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...
    }

from accordion.

Alhadis avatar Alhadis commented on June 15, 2024

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. :-)

from accordion.

robertvrabel avatar robertvrabel commented on June 15, 2024

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

from accordion.

Alhadis avatar Alhadis commented on June 15, 2024

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.

from accordion.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.