Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 5 Popover Position

I am using ion-popover.

In the example in the docs, when you click the three dots at the top right, the popover is shown right next to the clicked button.

What would be a good way of reproducing this? Is there a built-in way of doing it?

Since I didn't find a way, I am trying to set the styles for the popover manually, but that doesnt work either.

My page.ts

const popover = await this.popoverController.create({
  component: OptionsComponent,
  cssClass: 'my-custom-class',
  event: ev
});
return await popover.present();

My global.scss

.my-custom-class .popover-content {
  position: absolute;
  right: 0;
  top: 0;
}

Am I doing something wrong? Is there a better way to approach this?

like image 612
onzinsky Avatar asked Jun 20 '20 11:06

onzinsky


People also ask

How do you make an ionic popover?

A popover can be created by calling the create method. The view to display in the popover should be passed as the first argument. Any data to pass to the popover view can optionally be passed in the second argument. Options for the popover can optionally be passed in the third argument.

Which element is used for creating popover in ionic?

It uses create() method for creating popover. You can customize the controller by setting popover options in the create method.


1 Answers

It's explained in the docs (emphasis mine) :

To present a popover, call the present method on a popover instance. In order to position the popover relative to the element clicked, a click event needs to be passed into the options of the the present method.

HTML

<div (click)="showPopover($event)">
    <div>AAA</div>
</div>

In your class, pass the event as an argument to your method:

showPopover(event) {
    const popover = await this.popoverController.create({
      event,
      component: OptionsComponent,
      cssClass: 'my-custom-class', // optional
    });
    return await popover.present();
}

No need for extra CSS unless you want to style the content of your modal.

like image 172
Sébastien Avatar answered Oct 27 '22 14:10

Sébastien