What's your biggest current challenge? Have your say in Community Polls along the right column.

Ensuring Only One Accordion Panel Remains Open at a Time in the Dawn Theme Footer

Solved

Ensuring Only One Accordion Panel Remains Open at a Time in the Dawn Theme Footer

Sivadarshan
Shopify Partner
175 1 29

I have custom-coded an accordion section in the footer of the Dawn theme for mobile view. The goal is to ensure that when one accordion panel opens, any previously opened panel closes automatically. In other words, only one accordion panel should remain open at a time.

 
Accepted Solution (1)
Huptech-Web
Shopify Partner
1011 204 217

This is an accepted solution.

@Sivadarshan  Please replace your Accordia JS code with the code provided below.

window.addEventListener('DOMContentLoaded', () => {
  const headings = document.querySelectorAll('.grid .footer-block__heading');

  // Handle collapse logic
  const handleCollapse = (heading) => {
    const content = heading.nextElementSibling;

    // Close all other sections
    headings.forEach((otherHeading) => {
      if (otherHeading !== heading && !otherHeading.classList.contains('block-collapsed')) {
        const otherContent = otherHeading.nextElementSibling;
        otherHeading.classList.add('block-collapsed');
        otherHeading.setAttribute('aria-expanded', 'false');
        otherContent.style.display = 'none';
      }
    });

    // Toggle the clicked section
    if (heading.classList.contains('block-collapsed')) {
      heading.classList.remove('block-collapsed');
      heading.setAttribute('aria-expanded', 'true');
      content.style.display = 'block';
    } else {
      heading.classList.add('block-collapsed');
      heading.setAttribute('aria-expanded', 'false');
      content.style.display = 'none';
    }
  };

  headings.forEach((heading, index) => {
    heading.classList.add('block-collapsed');
    heading.setAttribute('role', 'button');
    heading.setAttribute('aria-expanded', 'false');
    heading.setAttribute('tabindex', '0');

    const content = heading.nextElementSibling;
    content.setAttribute('id', `footer-block-index-${index}`);
    heading.setAttribute('aria-controls', `footer-block-index-${index}`);

    // Add event listeners for click and keydown (Enter, Space)
    heading.addEventListener('click', () => {
      handleCollapse(heading);
    });

    heading.addEventListener('keydown', (event) => {
      if (event.key === 'Enter' || event.key === ' ') {
        handleCollapse(heading);
      }
    });
  });
});

If you need further assistance, please let me know. If you found my help useful, consider liking this message and marking it as the Accepted Solution.
Best regards
K.K

If you found this response helpful, please do like and accept the solution. Thanks!
Need support with Customizing your Shopify store?
Feel free to contact me at info@huptechweb.com or Visit our website Huptech Web.
Instant Shortcode Builder: Integrate customizable UI features anywhere in your store - No coding knowledge required

View solution in original post

Replies 7 (7)

Huptech-Web
Shopify Partner
1011 204 217

Hey @Sivadarshan Could you please share the store URL? This will allow me to inspect it and provide you with a more tailored solution.

If you found this response helpful, please do like and accept the solution. Thanks!
Need support with Customizing your Shopify store?
Feel free to contact me at info@huptechweb.com or Visit our website Huptech Web.
Instant Shortcode Builder: Integrate customizable UI features anywhere in your store - No coding knowledge required
Sivadarshan
Shopify Partner
175 1 29

website: https://rugd-in.myshopify.com/

password: rugged

Huptech-Web
Shopify Partner
1011 204 217

This is an accepted solution.

@Sivadarshan  Please replace your Accordia JS code with the code provided below.

window.addEventListener('DOMContentLoaded', () => {
  const headings = document.querySelectorAll('.grid .footer-block__heading');

  // Handle collapse logic
  const handleCollapse = (heading) => {
    const content = heading.nextElementSibling;

    // Close all other sections
    headings.forEach((otherHeading) => {
      if (otherHeading !== heading && !otherHeading.classList.contains('block-collapsed')) {
        const otherContent = otherHeading.nextElementSibling;
        otherHeading.classList.add('block-collapsed');
        otherHeading.setAttribute('aria-expanded', 'false');
        otherContent.style.display = 'none';
      }
    });

    // Toggle the clicked section
    if (heading.classList.contains('block-collapsed')) {
      heading.classList.remove('block-collapsed');
      heading.setAttribute('aria-expanded', 'true');
      content.style.display = 'block';
    } else {
      heading.classList.add('block-collapsed');
      heading.setAttribute('aria-expanded', 'false');
      content.style.display = 'none';
    }
  };

  headings.forEach((heading, index) => {
    heading.classList.add('block-collapsed');
    heading.setAttribute('role', 'button');
    heading.setAttribute('aria-expanded', 'false');
    heading.setAttribute('tabindex', '0');

    const content = heading.nextElementSibling;
    content.setAttribute('id', `footer-block-index-${index}`);
    heading.setAttribute('aria-controls', `footer-block-index-${index}`);

    // Add event listeners for click and keydown (Enter, Space)
    heading.addEventListener('click', () => {
      handleCollapse(heading);
    });

    heading.addEventListener('keydown', (event) => {
      if (event.key === 'Enter' || event.key === ' ') {
        handleCollapse(heading);
      }
    });
  });
});

If you need further assistance, please let me know. If you found my help useful, consider liking this message and marking it as the Accepted Solution.
Best regards
K.K

If you found this response helpful, please do like and accept the solution. Thanks!
Need support with Customizing your Shopify store?
Feel free to contact me at info@huptechweb.com or Visit our website Huptech Web.
Instant Shortcode Builder: Integrate customizable UI features anywhere in your store - No coding knowledge required
Sivadarshan
Shopify Partner
175 1 29

But it is applying for all the views like mobile, desktop and tablet view, but my need is only for mobile view

Huptech-Web
Shopify Partner
1011 204 217

@Sivadarshan Please try this code; it only works in mobile view.

window.addEventListener('DOMContentLoaded', () => {
  const headings = document.querySelectorAll('.grid .footer-block__heading');
  
  // Check screen size
  const isMobile = () => window.innerWidth < 749;

  const handleCollapse = (heading) => {
    const content = heading.nextElementSibling;

    // Close all other sections on mobile
    if (isMobile()) {
      headings.forEach((otherHeading) => {
        if (otherHeading !== heading && !otherHeading.classList.contains('block-collapsed')) {
          const otherContent = otherHeading.nextElementSibling;
          otherHeading.classList.add('block-collapsed');
          otherHeading.setAttribute('aria-expanded', 'false');
          otherContent.style.display = 'none';
        }
      });
    }

    // Toggle the clicked section
    if (heading.classList.contains('block-collapsed')) {
      heading.classList.remove('block-collapsed');
      heading.setAttribute('aria-expanded', 'true');
      content.style.display = 'block';
    } else {
      heading.classList.add('block-collapsed');
      heading.setAttribute('aria-expanded', 'false');
      content.style.display = 'none';
    }
  };

  const applyCollapseBehavior = () => {
    headings.forEach((heading, index) => {
      const content = heading.nextElementSibling;

      if (isMobile()) {
        // Apply collapse behavior for mobile
        heading.classList.add('block-collapsed');
        heading.setAttribute('aria-expanded', 'false');
        content.style.display = 'none';
      } else {
        // Ensure content is visible on larger screens
        heading.classList.remove('block-collapsed');
        heading.setAttribute('aria-expanded', 'true');
        content.style.display = 'block';
      }
    });
  };

  // Set initial collapse behavior based on screen size
  applyCollapseBehavior();

  // Add event listeners for click and keydown (Enter, Space)
  headings.forEach((heading) => {
    heading.setAttribute('role', 'button');
    heading.setAttribute('tabindex', '0');

    heading.addEventListener('click', () => {
      if (isMobile()) {
        handleCollapse(heading);
      }
    });

    heading.addEventListener('keydown', (event) => {
      if ((event.key === 'Enter' || event.key === ' ') && isMobile()) {
        handleCollapse(heading);
      }
    });
  });

  // Reapply behavior on window resize
  window.addEventListener('resize', applyCollapseBehavior);
});

If you need further assistance, please let me know. If you found my help useful, consider liking this message and marking it as the Accepted Solution.
Best regards
K.K  

If you found this response helpful, please do like and accept the solution. Thanks!
Need support with Customizing your Shopify store?
Feel free to contact me at info@huptechweb.com or Visit our website Huptech Web.
Instant Shortcode Builder: Integrate customizable UI features anywhere in your store - No coding knowledge required

Sivadarshan
Shopify Partner
175 1 29

website: https://rugd-in.myshopify.com/

password: rugged

Huptech-Web
Shopify Partner
1011 204 217

Thanks @Sivadarshan 

If you found this response helpful, please do like and accept the solution. Thanks!
Need support with Customizing your Shopify store?
Feel free to contact me at info@huptechweb.com or Visit our website Huptech Web.
Instant Shortcode Builder: Integrate customizable UI features anywhere in your store - No coding knowledge required