Skip to main content

ion-popover

View Source

A Popover is a dialog that appears on top of the current page. It can be used for anything, but generally it is used for overflow actions that don't fit in the navigation bar.

There are two ways to use ion-popover: inline or via the popoverController. Each method comes with different considerations, so be sure to use the approach that best fits your use case.

Inline Popovers#

ion-popover can be used by writing the component directly in your template. This reduces the number of handlers you need to wire up in order to present the popover. See Usage for an example of how to write a popover inline.

When using ion-popover with Angular, React, or Vue, the component you pass in will be destroyed when the popover is dismissed. As this functionality is provided by the JavaScript framework, using ion-popover without a JavaScript framework will not destroy the component you passed in. If this is a needed functionality, we recommend using the popoverController instead.

Angular#

Since the component you passed in needs to be created when the popover is presented and destroyed when the popover is dismissed, we are unable to project the content using <ng-content> internally. Instead, we use <ng-container> which expects an <ng-template> to be passed in. As a result, when passing in your component you will need to wrap it in an <ng-template>:

<ion-popover [isOpen]="isPopoverOpen">
<ng-template>
<app-popover-content></app-popover-content>
</ng-template>
</ion-popover>

When to use#

Using a popover inline is useful when you do not want to explicitly wire up click events to open the popover. For example, you can use the trigger property to designate a button that should present the popover when clicked. You can also use the trigger-action property to customize whether the popover should be presented when the trigger is left clicked, right clicked, or hovered over.

If you need fine grained control over when the popover is presented and dismissed, we recommend you use the popoverController.

Controller Popovers#

ion-popover can also be presented programmatically by using the popoverController imported from Ionic Framework. This allows you to have complete control over when a popover is presented above and beyond the customization that inline popovers give you. See Usage for an example of how to use the popoverController.

When to use#

We typically recommend that you write your popovers inline as it streamlines the amount of code in your application. You should only use the popoverController for complex use cases where writing a popover inline is impractical. When using a controller, your popover is not created ahead of time, so properties such as trigger and trigger-action are not applicable here. In addition, nested popovers are not compatible with the controller approach because the popover is automatically added to the root of your application when the create method is called.

Interfaces#

Below you will find all of the options available to you when using the popoverController. These options should be supplied when calling popoverController.create().

interface PopoverOptions {
component: any;
componentProps?: { [key: string]: any };
showBackdrop?: boolean;
backdropDismiss?: boolean;
translucent?: boolean;
cssClass?: string | string[];
event?: Event;
animated?: boolean;
mode?: 'ios' | 'md';
keyboardClose?: boolean;
id?: string;
enterAnimation?: AnimationBuilder;
leaveAnimation?: AnimationBuilder;
size?: PopoverSize;
dismissOnSelect?: boolean;
reference?: PositionReference;
side?: PositionSide;
align?: PositionAlign;
}

Types#

Below you will find all of the custom types for ion-popover:

type PopoverSize = 'cover' | 'auto';
type TriggerAction = 'click' | 'hover' | 'context-menu';
type PositionReference = 'trigger' | 'event';
type PositionSide = 'top' | 'right' | 'bottom' | 'left' | 'start' | 'end';
type PositionAlign = 'start' | 'center' | 'end';

Customization#

Popover uses scoped encapsulation, which means it will automatically scope its CSS by appending each of the styles with an additional class at runtime. Overriding scoped selectors in CSS requires a higher specificity selector.

We recommend setting a custom class on the host element if writing a popover inline or supplying a class to the cssClass option if using the popoverController and using that to add custom styles to the host and inner elements. The cssClass option can also accept multiple classes separated by spaces. View the Usage section for an example of how to pass a class using cssClass.

/* DOES NOT WORK - not specific enough */
.popover-content {
background: #222;
}
/* Works - pass "my-custom-class" in cssClass to increase specificity */
.my-custom-class .popover-content {
background: #222;
}

Any of the defined CSS Custom Properties can be used to style the Popover without needing to target individual elements:

.my-custom-class {
--background: #222;
}

If you are building an Ionic Angular app, the styles need to be added to a global stylesheet file. Read Style Placement in the Angular section below for more information.

Triggers#

A trigger for an ion-popover is the element that will open a popover when interacted with. The interaction behavior can be customized by setting the trigger-action property. The following example shows how to create a right click menu using trigger and trigger-action. Note that trigger-action="context-menu" will prevent your system's default context menu from opening.

<ion-button id="trigger-button">Right click me!</ion-button>
<ion-popover
trigger="trigger-button"
trigger-action="context-menu"
>
<ion-content>
<ion-list>
...
</ion-list>
</ion-content>
</ion-popover>

Triggers are not applicable when using the popoverController because the ion-popover is not created ahead of time.

Positioning#

Reference#

When presenting a popover, Ionic Framework needs a reference point to present the popover relative to. With reference="event", the popover will be presented relative to the x-y coordinates of the pointer event that was dispatched on your trigger element. With reference="trigger", the popover will be presented relative to the bounding box of your trigger element.

Side#

Regardless of what you choose for your reference point, you can position a popover to the top, right, left, or bottom of your reference point by using the side property. You can also use the start or end values if you would like the side to switch based on LTR or RTL modes.

Alignment#

The alignment property allows you to line up an edge of your popover with a corresponding edge on your trigger element. The exact edge that is used depends on the value of the side property.

Offsets#

If you need finer grained control over the positioning of your popover you can use the --offset-x and --offset-y CSS Variables. For example, --offset-x: 10px will move your popover content to the right by 10px.

Sizing#

When making dropdown menus, you may want to have the width of the popover match the width of the trigger element. Doing this without knowing the trigger width ahead of time is tricky. You can set the size property to 'cover' and Ionic Framework will ensure that the width of the popover matches the width of your trigger element. If you are using the popoverController, you must provide an event via the event option and Ionic Framework will use event.target as the reference element.

Nested Popovers#

When using ion-popover inline, you can nested popovers to create nested dropdown menus. When doing this, only the backdrop on the first popover will appear so that the screen does not get progressively darker as you open more popovers. See the Usage section for an example on how to write a nested popover.

You can use the dismissOnSelect property to automatically close the popover when the popover content has been clicked. This behavior does not apply when clicking a trigger element for another popover.

Nested popovers cannot be created when using the popoverController because the popover is automatically added to the root of your application when the create method is called.

Accessibility#

Keyboard Navigation#

ion-popover has basic keyboard support for navigating between focusable elements inside of the popover. The following table details what each key does:

KeyFunction
TabMoves focus to the next focusable element.
Shift + TabMoves focus to the previous focusable element.
EscCloses the popover.
Space or EnterClicks the focusable element.

ion-popover has full arrow key support for navigating between ion-item elements with the button property. The most common use case for this is as a dropdown menu in a desktop-focused application. In addition to the basic keyboard support, the following table details arrow key support for dropdown menus:

KeyFunction
ArrowUpMoves focus to the previous focusable element.
ArrowDownMoves focus to the next focusable element.
ArrowLeftWhen used in a child popover, closes the popover and returns focus to the parent popover.
Space, Enter, and ArrowRightWhen focusing a trigger element, opens the associated popover.

Usage#

import { Component } from '@angular/core';
import { PopoverController } from '@ionic/angular';
import { PopoverComponent } from '../../component/popover/popover.component';
@Component({
selector: 'popover-example',
templateUrl: 'popover-example.html',
styleUrls: ['./popover-example.css']
})
export class PopoverExample {
constructor(public popoverController: PopoverController) {}
async presentPopover(ev: any) {
const popover = await this.popoverController.create({
component: PopoverComponent,
cssClass: 'my-custom-class',
event: ev,
translucent: true
});
await popover.present();
const { role } = await popover.onDidDismiss();
console.log('onDidDismiss resolved with role', role);
}
}

Style Placement#

In Angular, the CSS of a specific page is scoped only to elements of that page. Even though the Popover can be presented from within a page, the ion-popover element is appended outside of the current page. This means that any custom styles need to go in a global stylesheet file. In an Ionic Angular starter this can be the src/global.scss file or you can register a new global style file by adding to the styles build option in angular.json.

Properties#

alignment#

DescriptionDescribes how to align the popover content with the reference point.
Attributealignment
Type"center" \| "end" \| "start"
Default'start'

animated#

DescriptionIf true, the popover will animate.
Attributeanimated
Typeboolean
Defaulttrue

arrow#

DescriptionIf true, the popover will display an arrow
that points at the reference when running in ios mode
on mobile. Does not apply in md mode or on desktop.
Attributearrow
Typeboolean
Defaulttrue

backdropDismiss#

DescriptionIf true, the popover will be dismissed when the backdrop is clicked.
Attributebackdrop-dismiss
Typeboolean
Defaulttrue

component#

DescriptionThe component to display inside of the popover.
You only need to use this if you are not using
a JavaScript framework. Otherwise, you can just
slot your component inside of ion-popover.
Attributecomponent
TypeFunction \| HTMLElement \| null \| string \| undefined
Defaultundefined

componentProps#

DescriptionThe data to pass to the popover component.
You only need to use this if you are not using
a JavaScript framework. Otherwise, you can just
set the props directly on your component.
Attributeundefined
Typeundefined \| { [key: string]: any; }
Defaultundefined

dismissOnSelect#

DescriptionIf true, the popover will be automatically
dismissed when the content has been clicked.
Attributedismiss-on-select
Typeboolean
Defaultfalse

enterAnimation#

DescriptionAnimation to use when the popover is presented.
Attributeundefined
Type((baseEl: any, opts?: any) => Animation) \| undefined
Defaultundefined

event#

DescriptionThe event to pass to the popover animation.
Attributeevent
Typeany
Defaultundefined

isOpen#

DescriptionIf true, the popover will open. If false, the popover will close.
Use this if you need finer grained control over presentation, otherwise
just use the popoverController or the trigger property.
Note: isOpen will not automatically be set back to false when
the popover dismisses. You will need to do that in your code.
Attributeis-open
Typeboolean
Defaultfalse

keyboardClose#

DescriptionIf true, the keyboard will be automatically dismissed when the overlay is presented.
Attributekeyboard-close
Typeboolean
Defaulttrue

leaveAnimation#

DescriptionAnimation to use when the popover is dismissed.
Attributeundefined
Type((baseEl: any, opts?: any) => Animation) \| undefined
Defaultundefined

mode#

DescriptionThe mode determines which platform styles to use.
Attributemode
Type"ios" \| "md"
Defaultundefined

reference#

DescriptionDescribes what to position the popover relative to.
If 'trigger', the popover will be positioned relative
to the trigger button. If passing in an event, this is
determined via event.target.
If 'event', the popover will be positioned relative
to the x/y coordinates of the trigger action. If passing
in an event, this is determined via event.clientX and event.clientY.
Attributereference
Type"event" \| "trigger"
Default'trigger'

showBackdrop#

DescriptionIf true, a backdrop will be displayed behind the popover.
Attributeshow-backdrop
Typeboolean
Defaulttrue

side#

DescriptionDescribes which side of the reference point to position
the popover on. The 'start' and 'end' values are RTL-aware,
and the 'left' and 'right' values are not.
Attributeside
Type"bottom" \| "end" \| "left" \| "right" \| "start" \| "top"
Default'bottom'

size#

DescriptionDescribes how to calculate the popover width.
If 'cover', the popover width will match the width of the trigger.
If 'auto', the popover width will be determined by the content in
the popover.
Attributesize
Type"auto" \| "cover"
Default'auto'

translucent#

DescriptionIf true, the popover will be translucent.
Only applies when the mode is "ios" and the device supports
backdrop-filter.
Attributetranslucent
Typeboolean
Defaultfalse

trigger#

DescriptionAn ID corresponding to the trigger element that
causes the popover to open. Use the trigger-action
property to customize the interaction that results in
the popover opening.
Attributetrigger
Typestring \| undefined
Defaultundefined

triggerAction#

DescriptionDescribes what kind of interaction with the trigger that
should cause the popover to open. Does not apply when the trigger
property is undefined.
If 'click', the popover will be presented when the trigger is left clicked.
If 'hover', the popover will be presented when a pointer hovers over the trigger.
If 'context-menu', the popover will be presented when the trigger is right
clicked on desktop and long pressed on mobile. This will also prevent your
device's normal context menu from appearing.
Attributetrigger-action
Type"click" \| "context-menu" \| "hover"
Default'click'

Events#

NameDescription
didDismissEmitted after the popover has dismissed.
Shorthand for ionPopoverDidDismiss.
didPresentEmitted after the popover has presented.
Shorthand for ionPopoverWillDismiss.
ionPopoverDidDismissEmitted after the popover has dismissed.
ionPopoverDidPresentEmitted after the popover has presented.
ionPopoverWillDismissEmitted before the popover has dismissed.
ionPopoverWillPresentEmitted before the popover has presented.
willDismissEmitted before the popover has dismissed.
Shorthand for ionPopoverWillDismiss.
willPresentEmitted before the popover has presented.
Shorthand for ionPopoverWillPresent.

Methods#

dismiss#

DescriptionDismiss the popover overlay after it has been presented.
Signaturedismiss(data?: any, role?: string \| undefined, dismissParentPopover?: boolean) => Promise<boolean>

onDidDismiss#

DescriptionReturns a promise that resolves when the popover did dismiss.
SignatureonDidDismiss<T = any>() => Promise<OverlayEventDetail<T>>

onWillDismiss#

DescriptionReturns a promise that resolves when the popover will dismiss.
SignatureonWillDismiss<T = any>() => Promise<OverlayEventDetail<T>>

present#

DescriptionPresent the popover overlay after it has been created.
Signaturepresent() => Promise<void>

CSS Shadow Parts#

NameDescription
arrowThe arrow that points to the reference element. Only applies on ios mode.
backdropThe ion-backdrop element.
contentThe wrapper element for the default slot.

CSS Custom Properties#

NameDescription
--backdrop-opacityOpacity of the backdrop
--backgroundBackground of the popover
--box-shadowBox shadow of the popover
--heightHeight of the popover
--max-heightMaximum height of the popover
--max-widthMaximum width of the popover
--min-heightMinimum height of the popover
--min-widthMinimum width of the popover
--offset-xThe amount to move the popover by on the x-axis
--offset-yThe amount to move the popover by on the y-axis
--widthWidth of the popover

Slots#

NameDescription
``Content is placed inside of the .popover-content element.