13th Dec '23
/
7 comments

Making Bricks Nestable Accordion’s First Item Open on Desktop and Closed on Mobile

Updated on 5 Nov 2024

In the Bricks Facebook group, a user asks:

How can I make the nestable accordion first entry open on desktop but closed on mobile?

Here’s a way to do it using a bit of custom JavaScript.

First select your Accordion (Nestable) element in the builder and toggle “Expand first item” on.

Edit your Page/Template with Bricks.

Click on Settings (gear icon) → PAGE SETTINGS → CUSTOM CODE. Add this under Body (footer) scripts:

<script>

document.addEventListener('DOMContentLoaded', function() {
    // Specifically for mobile, check if the first item is open and close it
    if (window.innerWidth <= 768) { // Adjust as needed for your mobile breakpoint
        const firstContent = document.querySelector('#brxe-nmcaww .brxe-block.listening:first-child .accordion-content-wrapper');
        if (firstContent) {
            firstContent.style.display = 'none';
        }
    }
});

</script>

Replace #brxe-nmcaww with the HTML selector of your Accordion (Nestable) element.

Note: This only works after a page refresh 768px and below. You can’t see it take effect if you are resizing the window down from desktop to mobile.

Update 1: If you are using Frames Accordion add this code instead:

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Function to handle mobile accordion state
    const handleMobileAccordion = () => {
        if (window.innerWidth <= 768) {
            const firstAccordionItem = document.querySelector('.fr-faq-accordion-alpha .fr-accordion__item:first-child');
            
            if (firstAccordionItem) {
                const header = firstAccordionItem.querySelector('.fr-accordion__header');
                const body = firstAccordionItem.querySelector('.fr-accordion__body');
                const icon = firstAccordionItem.querySelector('.fr-accordion__icon');
                
                // Remove expanded state
                header.setAttribute('aria-expanded', 'false');
                
                // Reset body height to 0
                body.style.height = '0px';
                
                // Remove flipped class from icon if present
                if (icon) {
                    icon.classList.remove('fr-accordion__icon--flipped');
                }
            }
        }
    };

    // Run on initial load
    handleMobileAccordion();

    // Run on resize
    window.addEventListener('resize', handleMobileAccordion);
});
</script>

For multiple Frames accordions on the same page add this instead:

document.addEventListener('DOMContentLoaded', function() {
    // Function to handle mobile accordion state
    const handleMobileAccordions = () => {
        if (window.innerWidth <= 768) {
            // Get all accordions on the page
            const accordions = document.querySelectorAll('[data-fr-accordion-options]');
            
            accordions.forEach(accordion => {
                // Parse the accordion options
                const options = JSON.parse(accordion.dataset.frAccordionOptions || '{}');
                
                // Only process accordions that have firstItemOpened set to true
                if (options.firstItemOpened) {
                    const firstAccordionItem = accordion.querySelector('.fr-accordion__item:first-child');
                    
                    if (firstAccordionItem) {
                        const header = firstAccordionItem.querySelector('.fr-accordion__header');
                        const body = firstAccordionItem.querySelector('.fr-accordion__body');
                        const icon = firstAccordionItem.querySelector('.fr-accordion__icon');
                        
                        // Remove expanded state
                        header.setAttribute('aria-expanded', 'false');
                        
                        // Reset body height to 0
                        body.style.height = '0px';
                        
                        // Remove flipped class from icon if present
                        if (icon) {
                            icon.classList.remove('fr-accordion__icon--flipped');
                        }
                    }
                }
            });
        }
    };

    // Run on initial load
    handleMobileAccordions();

    // Run on resize with debounce
    let resizeTimer;
    window.addEventListener('resize', () => {
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout(handleMobileAccordions, 250);
    });
});
Get access to all 610 Bricks code tutorials with BricksLabs Pro

7 comments

  • I simply added style and block in the custom attribute section of the content part. No code at all needed

  • Manos G

    Really useful. Thanks!

  • I can't get it to work. I'm trying to make it work in a Bricks template. I copied the code and pasted it into the Page Settings --> custom code --> header scripts. Ofcourse I replaced the #brxe-nmcaww with my own. What am I doing wrong?

    • A

      Just tested and works fine here.

      Make sure the HTML ID is indeed present in the DOM on the front end for your nestable accordion.

      Also, the code should be added under Body (footer) scripts. I've updated the post to inform the same.

      • Simon Giaccotto

        Great!! Now it works :) It was the body(footer) part.

  • Hi!

    And how modify the code, if I want to open ALL accordions in desktop and closed on mobile. :-)

    Thanks you help!

Leave your comment

 

Related Tutorials..

Pro
Dynamic Accordion in WordPress using Meta Box and Alpine.js

Dynamic Accordion in WordPress using Meta Box and Alpine.js

This Pro tutorial provides the steps to implement an accordion using Alpine.js in WordPress that pulls the values of sub fields of a Meta Box…
Auto-Scroll to the New Open Accordion Item

Auto-Scroll to the New Open Accordion Item

How we can make sure the scroll position is always synced with the accordion when opened.
Categories:
Tags:
Bricks Accordion (Nestable) Element’s Icons

Bricks Accordion (Nestable) Element’s Icons

Adding icon rotations to Bricks' Accordion (Nestable) element when opened.
Categories:
Tags:
Pro
Dynamic Posts Side Menu with Expandable and Collapsible Accordion Submenus in WordPress

Dynamic Posts Side Menu with Expandable and Collapsible Accordion Submenus in WordPress

This Pro tutorial provides the steps to show all the posts of a specified post type as menu items with accordion functionality for submenus. This…
Categories:
Pro
Accordion Grid Query Loop in Bricks

Accordion Grid Query Loop in Bricks

How to set up a accordion in Bricks such that the "titles" are arranged in a grid with the full width content opening below.
Categories:
Pro
Dynamic Horizontal Posts Accordion in Bricks

Dynamic Horizontal Posts Accordion in Bricks

Creating a horizontal accordion of featured images from posts output by Bricks' query loop.
Categories:
Tags:
Pro
WooCommerce Product Carousel in Bricks

WooCommerce Product Carousel in Bricks

How to set up a carousel that shows WooCommerce products with the product title, price and Add to Cart button for each.
Categories: