Logo

dev-resources.site

for different kinds of informations.

How To Easily Expand or Collapse All Accordion Items in IonAccordionGroup at Once Without Adding Complexity

Published at
6/19/2024
Categories
angular
ionic
Author
rensjaspers
Categories
2 categories in total
angular
open
ionic
open
Author
11 person written this
rensjaspers
open
How To Easily Expand or Collapse All Accordion Items in IonAccordionGroup at Once Without Adding Complexity

The Ionic Accordion Group and the ion-accordion are awesome. Accordions provide collapsible sections in your content to reduce vertical space while organizing and grouping information.

However, I feel there's an important feature that's missing: the ability to easily expand or collapse all accordion items at once.

While it's technically possible to expand or collapse all items at once, the current method is quite cumbersome. You have to use <ion-accordion-group [value]="valuesOfTheAccordionsThatShouldBeExpanded"> and pass an array of all the values of the accordions (or an empty array if you want to collapse all). This approach is problematic for several reasons:

  1. Typically, you don't want to assign a value to each accordion manually. Ionic already handles this automatically, but as a developer, you can't easily see these values.

  2. Your component suddenly has an extra responsibility: managing the state of each accordion group. This adds unnecessary complexity to your app.

Generally, I appreciate that the Ionic team keeps things lean and leaves extra functionalities to the developer. However, in this case, I would have liked to see an accordionGroup.toggleAll / expandAll / collapseAll method.

Fortunately, with Angular directives, it's relatively simple to add this functionality cleanly.

Here's an example:

import { ContentChildren, Directive, QueryList } from "@angular/core";
import { IonAccordion, IonAccordionGroup } from "@ionic/angular/standalone";

@Directive({
  selector: "[appAccordionGroupToggle]",
  exportAs: "appAccordionGroupToggle",
  standalone: true,
})
export class AccordionGroupToggleDirective {
  @ContentChildren(IonAccordion) accordions!: QueryList<IonAccordion>;

  get isCompletelyExpanded() {
    const groupValue = this.host.value;
    return (
      Array.isArray(groupValue) && groupValue?.length === this.accordions.length
    );
  }

  constructor(private host: IonAccordionGroup) {}

  toggle() {
    if (this.isCompletelyExpanded) {
      this.collapse();
    } else {
      this.expand();
    }
  }

  expand() {
    this.host.value = this.accordions.map((accordion) => accordion.value);
  }

  collapse() {
    this.host.value = [];
  }
}
Enter fullscreen mode Exit fullscreen mode

The directive works by using @ContentChildren to get a reference to all ion-accordion items within the accordion group. This allows the directive to access the state (and thus the values) of all the accordions collectively. The constructor injects the host IonAccordionGroup, providing access to the group’s properties and methods.

In the expand method, the directive sets the value of the host accordion group to an array containing the values of all the individual accordions, effectively expanding all of them. In the collapse method, it sets the value to an empty array, collapsing all the accordions.

The toggle method first checks if all accordions are already open by checking the value of isCompletelyExpanded. If it equals true, it collapses them; if not, it expands them.

You can use the directive as follows (don't forget to import the directive in your component):

<ion-content>
  <ion-button (click)="toggler.toggle()">Toggle all</ion-button>
  <ion-button (click)="toggler.expand()">Expand all</ion-button>
  <ion-button (click)="toggler.collapse()">Collapse all</ion-button>
  <ion-accordion-group
    appAccordionGroupToggle
    #toggler="appAccordionGroupToggle"
  >
    <ion-accordion>
      <ion-item slot="header">
        <ion-label>First Accordion</ion-label>
      </ion-item>
      <div slot="content">😎</div>
    </ion-accordion>
    <ion-accordion>
      <ion-item slot="header">
        <ion-label>Second Accordion</ion-label>
      </ion-item>
      <div slot="content">🤩</div>
    </ion-accordion>
    <ion-accordion>
      <ion-item slot="header">
        <ion-label>Third Accordion</ion-label>
      </ion-item>
      <div slot="content">💪</div>
    </ion-accordion>
  </ion-accordion-group>
</ion-content>
Enter fullscreen mode Exit fullscreen mode

In the above example, we use #toggler to create a reference to the instance of the Group toggle. This is possible because we set exportAs: 'appAccordionGroupToggle' in our directive. Now, we can call any of the public methods (toggle, expand, and collapse) directly from our template.

That's it! Now you can use this directive to easily toggle all accordions at once anywhere you like. While I hope the Ionic team will add expand, collapse, and toggle methods to ion-accordion-group in the near future, it's great that Angular components make it so easy to add the desired functionality yourself.

ionic Article's
30 articles in total
Favicon
Parental Control Solutions: iOS VPNs for Family Safety
Favicon
Domina el arte de la personalización en Ionic: Crea paletas de colores únicas paso a paso
Favicon
Implementacion de videollamadas multiplataforma iOS - Android
Favicon
Ionic: Angular 18, CapacitorJS & SQLite
Favicon
Crafting the Face of the Web: 10 Quotes on Front-End Development
Favicon
Hello ionic
Favicon
How to Easily Notify Users About Task Progress in Angular
Favicon
Quick Guide to Installing Android SDK Platform Tools on macOS and Windows
Favicon
How to edit angular ionic app in locally? the project angular version 13 and ionic version 5?
Favicon
how to: open a component like a sheet modal using ionic 7
Favicon
Why Ionic Outperforms Flutter in 2024: 7 Data-Driven Reasons to Choose Ionic
Favicon
Besoin d'aide pour créer une application d'enquête générique
Favicon
Desktop Application Development: Why It Still Matters in a Digital World?
Favicon
Integrating Capacitor with Next.js: A Step-by-Step Guide
Favicon
Top Ionic Interview Questions and Answers
Favicon
How to Build a Vue App To Show Your GitHub Repositories
Favicon
How To Easily Expand or Collapse All Accordion Items in IonAccordionGroup at Once Without Adding Complexity
Favicon
Ionic loader, without clicking event (loading.dismiss())
Favicon
How I built a rhymes dictionary ?
Favicon
Building Native Applications with Capacitor and ReactJS
Favicon
Setting Up ESLint and Prettier in an Ionic Angular Project
Favicon
Remède V1.1.5
Favicon
Creating a Mobile App with Ionic and Vue.js 🚀📱
Favicon
Running a Phaser Game on Mobile Devices
Favicon
Working with Scenes and Data in Phaser
Favicon
Diving Into Capacitor 6: What’s New, What’s Improved, and How to Upgrade
Favicon
Announcing Ionstarter
Favicon
Ionic v8 - Create Even Better Hybrid Apps
Favicon
Ionic + Capacitor Security Tips
Favicon
Responsive Ionic app development services

Featured ones: