az-digital / az_quickstart

UArizona's web content management system built with Drupal 10.
https://quickstart.arizona.edu
GNU General Public License v2.0
30 stars 20 forks source link

Add functionality to Accordions paragraph #295

Open danahertzberg opened 4 years ago

danahertzberg commented 4 years ago

Requirements:

Notes: If I recall correctly, we (developers, not me lol) could implement functionality where if one accordion is open, it appends the ID to the URL. And if multiple accordions were open, no ID would be added. Need to determine best functionality for the user.

From Chris: Go here: https://review.digital.arizona.edu/arizona-bootstrap/feature/93-chris/docs/0.0/components/collapse/#ex2_headingTwo

Add to console: if (location.hash === '#ex2_headingTwo') { var target = $(location.hash).find( '[data-toggle="collapse"]' ).attr('data-target'); $(target).collapse('show'); }

Dependent on AZ Digital's review/edits and approval these requirements

trackleft commented 10 months ago

This would actually be a fairly easy thing

document.addEventListener("DOMContentLoaded", () => {
  // Function to handle accordion based on hash
  const handleAccordion = (hash) => {
    if (hash) {
      const $targetAccordion = jQuery(hash);
      if ($targetAccordion.length) {
        $targetAccordion.collapse("show");
        window.location.hash = hash;
        history.pushState(null, null, hash);
      }
    }
  };

  // Check for hash on initial load
  handleAccordion(window.location.hash);

  // Add an event listener to handle clicks on anchor links
  document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
    anchor.addEventListener("click", function (e) {
      e.preventDefault();

      // Extract the hash from the anchor link and handle the accordion
      const hash = this.getAttribute("href");
      handleAccordion(hash);
    });
  });
});
danahertzberg commented 10 months ago

@trackleft Go for it!

trackleft commented 7 months ago

Improved by #3219