×

Ionic Menu

The Ionic menu is the side-menu navigation that can be slide in from the side of the current view. By default, the menu slides in from the left side, but the side can be overridden. Depending on the mode, the menu will be shown differently, but the display style can be changed to any form of the list available. A sibling of the root content element should be the menu element. You can add any number of menus from the content element.

You can use the <ion-menu> component to add the menu in the ionic app. it makes it easy to create the side menu for the navigation. In the <ion-nav> component, there should be added a local variable, and that can be passed to the <ion-menu> content property. The menus can be controlled from the templates, or programmatically by using the menuController. The content of a menu will be disabled when the menu is dismissed.

There are the following elements of the menu component which are as follows:

  1. Menu Button
  2. Menu Controller
  3. Menu Toggle
  4. Split pane

Menu Button

The Menu Button is the component that automatically creates the icon and functionality to open a menu on the page.

Menu Controller

It makes it easy to control the menu. The provided methods can be used to show, enable, toggle the menu, and many more. The menu controller will take a reference to the menu by the side, or ID. If the side and id are not passed to it, then it will grab the first menu it finds.

Menu Toggle

The Ionic menu toggle can be used to open and closed the menu. Menu toggle is only visible when the selected menu is active. When it is opened or closed, the menu is active. If the menu is hidden or displayed as a split-pane, then the menu is marked as inactive, and <ion-menu-toggle> componenthides.

Split-pane

It is very useful when you want to create a multi-view layout. It permits the UI component, like menus, to be shown as the view-port with the increase in the width. If the width of the device screen is less than a specific size, the split pane will be failed, and the menu will be disabled. It is ideal for creating an application that will be presented in the browser and will be implemented through the app store on phones and tablets.

Create Menu

Here you see how you can create the side menu in the ionic application.

Step 1: Firstly, you are required to create a new blank ionic project. By default, it contains only one page that is the home page. Now, you are going to create the two other new pages, which makes it easy to use a side menu to move the other pages. The following commands help you to create pages in your ionic application. The commands are given below:

ionic generate page contacts
 ionic generate page chat  

Step 2: After executing this command, you create app components. Now you are going to the root component that is app.component.ts. Here you can create a function sidemenu(), which contains an array of objects. You can set the URL and icon of each page.

app.component.ts

import { Component } from '@angular/core';  
 import { Platform } from '@ionic/angular';  
 import { SplashScreen } from '@ionic-native/splash-screen/ngx';  
 import { StatusBar } from '@ionic-native/status-bar/ngx'; 
@Component({  
   selector: 'app-root',  
   templateUrl: 'app.component.html',  
   styleUrls: ['app.component.scss']  
 })  
 export class AppComponent {  
   navigate: any;  
   constructor(   
     private platform: Platform,  
     private splashScreen: SplashScreen,  
     private statusBar: StatusBar  
   ) {   
     this.sideMenu();  
     this.initializeApp();  
   }  
   initializeApp() {  
     this.platform.ready().then(() => {  
       this.statusBar.styleDefault();   
       this.splashScreen.hide();  
     });  
   }  
   sideMenu() {  
     this.navigate =   
     [  
         { 
         title : 'App',
         url   : '/apps',
         icon  : 'apps' 
         },
       { 
         title : 'Book',  
         url   : '/book',  
         icon  : 'book'  
       },   
       {  
         title : 'Paint',  
         url   : '/paint',  
         icon  : 'brush'   
       },  
       {  
         title : 'Contacts',  
         url   : '/contacts',  
         icon  : 'contacts'  
       },   
       {
           title : 'Facebook',
           url   : '/facebook.com',
           icon  : 'logo-facebook'
       },
     ];  
   }  
 }   

Step 3: In this step, you can add the <ion-menu> element in the HTML file and it can create the side menu in the ionic application. The <ion-menu-toggle> component is used to create the open and close menu in the side menu. When you click on the menu, then it will dismiss the side menu automatically.

app.component.html

<ion-app>  
     <ion-menu side="end" menuId="first" contentId="content">  
       <ion-header>  
         <ion-toolbar color="success">  
           <ion-title>Content</ion-title>  
         </ion-toolbar>   
       </ion-header>  
       <ion-content color="light">  
         <ion-list *ngFor="let pages of navigate">  
           <ion-menu-toggle auto-hide="true">  
             <ion-item [routerLink]="pages.url" routerDirection="forward">  
               <ion-icon [name]="pages.icon" slot="start"></ion-icon>  
                 {{pages.title}}   
             </ion-item>  
           </ion-menu-toggle>  
         </ion-list>  
       </ion-content>   
     </ion-menu>  
     <ion-router-outlet Id="content"></ion-router-outlet>  
   </ion-app>   

Step 4: You can open and close the side menu after creating the side menu in the main component. You are required to use the <ion-menu-button> component in the html file to every page. It can able to generate icon and functionality to open a menu on every page. You can use the <ion-menu-button> component in the home.page.html.

Home.page.html

<ion-header>  
   <ion-toolbar color="primary">  
     <ion-buttons slot="start">  
       <ion-menu-button color="dark"></ion-menu-button>   
     </ion-buttons>  
     <ion-title>  
       Ionic Menu  Example
     </ion-title>  
   </ion-toolbar>   
 </ion-header>  

Output: After executing this code, you will get the output, as shown below:

Ionic Menu

When you click on the menu button, then it opens the side menu at the right side of the screen and shows the following output as shown below:

Ionic Menu2

There are many methods used in the ionic menu component.

  Method     Signature   Description  
Close() Close( animated?: boolean) => Promise<boolean> This method closes the menu. If already menu is closed, or cannot be closed, it returns false.
isActive() isActive() => Promise<boolean> If the menu is active then it returns true.
isOpen() isOpen() => Promise<boolean> If the menu is open then it returns true.
Open() open(animated?: boolean) => Promise<boolean> It is used to open the menu. If already menu is open, or cannot be opened then it returns false.
setOpen() setOpen(shouldOpen: Boolean, animated?: boolean) => Promise<boolean> This method is used to open or close the button. If the operation is not completed then it returns false.
Toogle() toogle(animated?:  boolean) => Promise<boolean> This method is used to toggle the menu. If operation is not completed successfully then it returns false.

Related Topics

History of Ionic

It is a complete open-source platform for hybrid mobile application development created by Ben Sperry, Max Lynch, and Adam Bradley of Drifty Co. in 2013. The first alpha version of the framework was...

2 minutes read.

Ionic vs Apache Cordova

Ionic Vs Apache Cordova Ionic and Cordova are two popular technology going on in the market for mobile application development. The combination of these two technologies creates a development platform that is known as...

3 minutes read.

Ionic Chip

Ionic chip displays the complex entities in small entities in small blocks, such as the contacts. Ionic chip has several types of elements like avatars, thumbnails, text, and icons. It is a bubble-like...

3 minutes read.

Ionic Modal

Ionic modal is displayed as a temporary UI that slides into the screen. The ionic modal is a dialog box that is displayed at the top of the app’s content, and it...

5 minutes read.

Ionic Tutorial for Beginners

What is Ionic? Ionic is an open-source framework that is used for the development of mobile applications. It is also used to build android applications, iOS, applications, desktop applications, and web applications using web...

4 minutes read.

Ionic Splash Screen

The Apache Cordova plugin helps to displays and hides a splash screen during the application launch. The Ionic Splash Screen is similar to a home page on a website. The ionic splash...

2 minutes read.

Ionic Range

The Range slider lets users choose from a range of values by moving the slider knob. You can accept two knobs, but by default, one-knob controls the range value. Range labels You can put down on...

2 minutes read.

Ionic Toolbar

A toolbar is a general toolbar that can be used in an application such as a header, sub-header, footer, or even a sub-footer. Because the ion-toolbar is flexbox-dependent, no matter how many toolbars...

3 minutes read.

Ionic vs Xamarin

Ionic vs Xamarin Ionic and Xamarin are both the most popular frameworks used to develop a hybrid mobile application. Both frameworks are used in large enterprises by the developers. Here, we will discuss the...

4 minutes read.

Ionic Reorder

The Ionic offers the <ion-reorder> component to reordering features. Ionic reorder component adds the Drag and Drop feature to list items, in which the user can change the order of the items. Reorder...

3 minutes read.

Ionic Colors

Ionic Colors Before we start with the actual elements available in the Ionic frame, let's understand a little bit how Ionic uses colors for different elements. Ionic Color Classes Ionic framework is a set of nine predefined...

3 minutes read.

Ionic Popover

The Ionic popover is a small dialog box that displays at the top of the screen. It can be used for anything, but it is usually used for a button, an action,...

4 minutes read.

Ionic Checkbox

Ionic Checkbox Ionic Checkbox is nearly the same as a toggle. It can be placed in an ionic element or used as a separate checkbox. It is designed differently for every platform, such...

2 minutes read.

Ionic Slides

The ionic slides component is the multi-section container. You can swipe or drag any segment between them. This contains any number of slides components. Slides help it easy to make galleries, tutorials, and...

2 minutes read.

Ionic Spinner

The ionic spinner component provides the various SVG (Scalable Vector Graphic) spinners. Ionic spinners are the visual indicators that display the app which is loading content or performing another process that the user...

5 minutes read.

Ionic Geolocation

In today’s time, the mobile location plays an essential role in the mobile application. The Ionic Cordova geolocation plugin provides information about the location of the device, like as latitude and longitude. The...

5 minutes read.

Ionic progress bar

The ionic progress bar element is a horizontal progress bar used to visualize the progression of operation and activity. You can use the <ion-progress-bar> component to create a progress bar in the ionic...

4 minutes read.

Ionic Cards

Ionic Cards: Ionic cards are a standard part of the user interface that acts as an entry point for more specific information. These are the best components for displaying information...

4 minutes read.

Difference between Ionic 4 and Ionic 3

The ionic framework is an open-source platform that is used to develop mobile applications. It is also used to build android applications, desktop applications, iOS, and web applications using web technologies like HTML,...

5 minutes read.

Ionic-fab Button

Ionic-fab Button The Ionic FABs (Floating Action Buttons) are standard material design compounds. It is a container element that contains one or more fab buttons. They are required to be placed in a fixed position,...

2 minutes read.