Logo

dev-resources.site

for different kinds of informations.

how to: open a component like a sheet modal using ionic 7

Published at
8/24/2024
Categories
ionic
angular
webdev
Author
mtspadda
Categories
3 categories in total
ionic
open
angular
open
webdev
open
Author
8 person written this
mtspadda
open
how to: open a component like a sheet modal using ionic 7

Hello folks!

Last week, I got a little task at work that kept me busy the whole Friday.

Here, we prefer to use modals as components. In Ionic, we can't use the trigger property, like this:

<ion-modal trigger="open-modal" (willDismiss)="onWillDismiss($event)">
Enter fullscreen mode Exit fullscreen mode

Since the trigger event is not present in the DOM, Ionic will give an exception saying ‘trigger property should be in the DOM,’ or something like that.

My goal was to add breakpoints and an animation and make a sheet modal. If I could use the trigger tag, it would be done this way:

<ion-modal #modal trigger="open-modal" 
[initialBreakpoint]="0.25" 
[breakpoints]="[0, 0.25, 0.5, 0.75]">
Enter fullscreen mode Exit fullscreen mode

But we are not in an ion-modal, nor are we triggering the modal with its own property. To add more context to my problem, the component I want to open was

<ion-header>
    {{title}}
</ion-header>
<ion-content>
     <!-- html -->
</ion-content>

Enter fullscreen mode Exit fullscreen mode

see? no ion-modal

what I did was add my initialBreakpoint and breakpoints in the async function as properties of the modalOptions interface.

export interface ModalOptions<T extends ComponentRef = ComponentRef> {
  component: T;
  componentProps?: ComponentProps<T>;
  presentingElement?: HTMLElement;
  showBackdrop?: boolean;
  backdropDismiss?: boolean;
  cssClass?: string | string[];
  delegate?: FrameworkDelegate;
  animated?: boolean;
  canDismiss?: boolean | ((data?: any, role?: string) => Promise<boolean>);
  mode?: Mode;
  keyboardClose?: boolean;
  id?: string;
  htmlAttributes?: {
    [key: string]: any;
  };
  enterAnimation?: AnimationBuilder;
  leaveAnimation?: AnimationBuilder;
  breakpoints?: number[];
  initialBreakpoint?: number;
  backdropBreakpoint?: number;
  handle?: boolean;
  handleBehavior?: ModalHandleBehavior;
}
Enter fullscreen mode Exit fullscreen mode

More to explore in the future

Thus, my call to open the modal looks like this. Breakpoints go from 0 to 1, and 1 means fully opened.

 async openModal(params: any) {
    const modal = await this.modalController.create({
      component: ComponentBlabla,
      componentProps: {
        params: params
      },
        initialBreakpoint: 1,
        breakpoints: [1, 0.75, 0.25],
        animated: true,
    });

    await modal.present();
  }
Enter fullscreen mode Exit fullscreen mode

and like this, you have a component opened like a sheet modal!
It is easy, but I didn’t find anyone doing it like this or much in the Ionic docs.

Hope this will be helpful to someone one day!
Thank you and cya guys!

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: