Menu: animated opening/closingIs this convenient, or

How can I add animation to opening and closing menu items in my project? I am not sure if it can be done in Angular or CSS. I have provided a reproduction of the project for reference => Stackblitz.

HTML file

<div class="wrapper">
   <!-- Top Menu -->
   <div class="sidebar">
      <!-- Menu Item -->
      <ul>
         <li
         *ngFor="let menu of menus; let i = index"
         [class.active]="menu.active"
         >
         <ng-container>
            <a class="item" (click)="selectMenu(menu)">
            <i [class]="menu.iconClass"></i> {{ menu.name }}
            <i class="fa fa-chevron-down"></i>
            </a>
            <ul *ngIf="menu.active">
               <li *ngFor="let submenu of menu.submenu" class="submenu">{{submenu.name}}</li>
            </ul>
         </ng-container>
         </li>
      </ul>
   </div>
</div>

Answer №1

Utilize Angular animations for a smoother user experience. Implement the following animation into your component:

animations:[
    trigger('accordion', [
      transition(':enter', [
        style({ height: 0 }),
        animate('1000ms', style({ "height": '*' })),
      ]),
      transition(':leave', [
        animate('100ms', style({ "height": 0 }))
      ])
    ]),
  ]

When initiating the "enter" event, the style starts at height=0 and animates to reach the normal height represented by *. Conversely, the "leave" event animates the style to set height=0.

To use this animation, include the following code:

      <ul @accordion *ngIf="menu.active">
        <li *ngFor="let submenu of menu.submenu" class="submenu">
           {{submenu.name}}
        </li>
      </ul>

IMPORTANT: Remember to import the BrowserAnimationsModule in your module.

Additionally, ensure to add the following CSS to your stylesheet:

.wrapper ul li ul{
  overflow:hidden;
}

View your modified stackblitz

Answer №2

To improve functionality, consider modifying the app.component.ts file as follows:

  updateMenuSelection(parentMenu: { name: string, active:boolean }): void {
    this.menus.forEach((menu) => {
      if (menu.name === parentMenu.name) {
        menu.active = !menu.active;
      }
    });
  }
}

This change ensures that each time the active state is toggled, the boolean value will update accordingly. Have a wonderful day!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Deleting a division with the help of media queries

As a challenge, I am attempting to eliminate the .navbar-brand>img element once the screen size exceeds 768px by using media queries. Unfortunately, my efforts have not been successful so far. Although the styling applied to the img is successfully remo ...

Overlap of columns within a nested flexbox arrangement

While working with React and Styled Components, I encountered a problem of elements overlapping each other: https://i.stack.imgur.com/RuCYu.png There are a few instances of overlap, including: The padding below the <ul> element is colliding with ...

Why won't the display: block CSS style apply to my div element?

Take a look at my code: https://jsfiddle.net/r62p4209/ I created a search bar with company brand name and some buttons on the right, positioned in the center. My aim is for the search bar (from "search by:" to the "Go!" button) to move onto the next lin ...

enhancing the style of my Express project with CSS and EJS

Currently, I am facing challenges with linking CSS to my EJS files in a project I am developing using Node/Express/EJS. Despite creating a public folder with a CSS subfolder and the main.css file inside, I am unable to get the CSS to display in the browser ...

Problem with fetching Grails

I have a table nested within an anchor tag. The table is centered on the page, with each row containing an image. When the page loads, I noticed the following: a) The table initially appears at the top-left corner of the screen. b) As there are multiple v ...

Issue occurred when trying to load controllers during the migration process from AngularJS1 to Angular6

Currently, I am in the process of upgrading AngularJS1 components to Angular6. My strategy involves creating wrappers for all existing AngularJS1 components by extending "UpgradeComponent" and storing them under the folder "directive-wrappers". However, wh ...

Transferring information between a pair of input fields using ngModel

There are two input fields named input1 and input2. An event has been created where anything typed in input1 is displayed in input2. However, if something is manually changed or typed into input2, the event should not trigger. I think I may need to use a ...

How to Align the Button Vertically with the TextField in React Material-UI

Utilizing the Material-UI library for React, I have been working on creating a simple form with the following design: https://i.stack.imgur.com/LY3ZN.png My challenge lies in aligning the button with the TextField element. Adjusting the margin-top proper ...

Clicking on the mat-icon-button with the matSuffix in the password field will always trigger a

Seeking assistance with implementing mat-icon-button matSuffix in Angular for a login page. This is my first time working with Angular and I am facing an issue with the password field. I have used mat-icon-button matSuffix, but whenever I click on the ico ...

Adjusting Text Color on Buttons

I am looking for a simple button with clear color and black text, but the default button has white text. I need a way to change the text color. Check out my current app <View style={styles.fastlog}> <Button style={styles.bouton} title= "Con ...

How to create nested nav menus that are optimized for touch-screen devices?

When it comes to creating responsive websites, one challenge I frequently encounter is designing a multi-level nav menu that functions effectively for touch devices. Many plugins and methods exist, but most fall short because they do not allow a second-l ...

Angular2 fills up the table with data for a particular row

I have a specific requirement where I need to display only some columns of a table within a div. When I click on 'See' for a particular row, I want to reveal the remaining columns of that table (currently it reveals all rows by default). https:/ ...

The CORS policy has restricted access to the XMLHttpRequest from the specified origin at "http://...." to 'http://localhost:4200'

I'm currently facing a challenge while trying to make an API call, as I encountered a CORS error. When attempting with "https://....", the service failed and threw the following error: Status Code: 422. OPTIONS 422 (Unprocessable Entity) Access to ...

Round corners, images in the background, gradients in the background, and compatibility with Internet Explorer versions 8 and

I've encountered an issue where my div with border radius, background gradient, and background image are working fine in FireFox but not displaying correctly in IE8 or IE10. In IE8, the gradient and background image work properly, but as soon as I app ...

Validating Components that Adhere to an Interface

When working with Angular, there is a need to specify the type for a component that implements a particular interface and is passed into a class. For example, consider Class A with the following signature: class A { constructor(public component: ?) {} } ...

The Dropdownlist menu from Metro UI CSS does not function properly when placed in an ASP page within a subfolder

I'm currently facing an issue with my asp.net application in C# that utilizes Metro UI CSS for the Dropdownlist menu. The problem arises when the Dropdownlist menu works properly on pages within the same folder as the master page, but fails to functio ...

My CSS is giving me a bit of trouble with alignment

I have been experimenting with various approaches such as adjusting the size of the divs to tiny dimensions and trying out inline-block or float left and right properties, but I am still unable to achieve the desired result of having my divs positioned s ...

What is the impact on positioning when including a class in Angular?

I've encountered a peculiar bug while trying to add a class to a ui-router element. When I apply the active class, an absolutely positioned element that spans the entire view suddenly snaps to the width of its parent. To see the issue in action, chec ...

Divide Footer Information into Two Sections: Left and Right

<footer> <div class="copyrights-left"> <p>&copy 2015. <a style="color: #fff;" href="index.html">Free Xbox Live Gold Membership and Free Xbox Live Gold Codes</a>. All rights reserved.</p></div> <div class="co ...

What steps should I take to customize the design of my Stripe subscription payment page?

I am having trouble with the alignment of the subscription payment page, which is currently displaying all information in a single line. I would like to format it like a traditional payment form with card number, CVV, and expiry on separate lines rather th ...