Popover component for Angular
npm install @ncstate/sat-popover

Demo |
StackBlitz Template |
Development App
sat-popover has a peer dependency on the Angular CDK to leverage its overlay API.
``bash`
npm install --save @ncstate/sat-popover @angular/cdk
`ts
import { AppRootComponent } from './app-root.component';
import { bootstrapApplication } from '@angular/platform-browser';
import { importProvidersFrom } from '@angular/core';
import { provideAnimations, provideNoopAnimations } from '@angular/platform-browser/animations';
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
import { SatPopoverModule } from '@ncstate/sat-popover';
bootstrapApplication(AppRootComponent, {
// AppConfig
providers: [
// If you want the popover animations to work, you must use provideAnimations or provideAnimationsAsync.
provideAnimationsAsync(),
// If your application requires animations on immediate load, use provideAnimations instead.
// provideAnimations(),
// If you prefer to not have animations, you can use provideNoopAnimations.
// provideNoopAnimations(),
importProvidersFrom(SatPopoverModule)
]
});
`
Finally, import the SatPopoverModule as needed to provide the necessary components and directives.
`ts
import { Component } from '@angular/core';
import { SatPopoverAnchorDirective, SatPopoverComponent } from '@ncstate/sat-popover';
@Component({
imports: [ SatPopoverAnchorDirective, SatPopoverComponent ],
selector: 'my-component',
template:
(mouseenter)="myPopover.open()"
(mouseleave)="myPopover.close()">Hover me
`
})
Wrap any component you want to display in a popover with an component.
`html`
Next, apply the satPopoverAnchor directive to the element you wish to be the popover anchor and pass the component as an argument to the satPopoverAnchor directive.
`html
`
> Note: hasBackdrop is explained below
Alternatively, supply an anchor element to the popover.
`html
`
By default, the popover will appear centered over the anchor. If you instead want the popover
to appear below the anchor:
`html`
You can use the following to align the popover around the anchor:
| Input | Type | Default |
| ----------------- | --------------------------------------------------- | -------- |
| horizontalAlign | 'before' \| 'start' \| 'center' \| 'end' \| 'after' | 'center' |verticalAlign
| | 'above' \| 'start' \| 'center' \| 'end' \| 'below' | 'center' |
For convenience, you can also use xAlign and yAlign as shorthand for horizontalAlignverticalAlign
and , respectively.
By default, if the popover cannot fully fit within the viewport, it will use a fallback
alignment. You can use forceAlignment to ensure that the popover always displays
with the alignment you've specified.
`html`
Also by default, as the user scrolls or changes the viewport size, the popover will attempt
to stay within the viewport by using a fallback position (provided forceAlignment is notlockAlignment
set). You can use to ensure the popover does not change its alignment once
opened.
`html`
You are in full control of when the popover opens and closes. You can hook into any event or
trigger that fits your application's needs.
#### SatPopover has the following methods and outputs
| Method | Description |
| ------- | -------------------------------------------- |
| open | Open the popover. |
| close | Close the popover. Optionally takes a value. |
| toggle | Toggle the popover open or closed. |
| isOpen | Get whether the popover is presently open. |
| realign | Realign the popover to the anchor. |
| Output | Description |
| --------------- | ----------------------------------------------------------------- |
| opened | Emits when the popover is opened. |
| closed | Emits when the popover is closed. |
| afterOpen | Emits when the popover has finished opening. |
| afterClose | Emits when the popover has finished closing. |
| backdropClicked | Emits when the popover's backdrop (if enabled) is clicked. |
| overlayKeydown | Emits when a keydown event is targeted to this popover's overlay. |
#### SatPopoverAnchor has the following properties
| Property | Description |
| ------------------------- | ------------------------------------------------- |
| popover | A handle to the associated popover. |
| satPopoverAnchor (setter) | An @Input() for setting the associated popover. |
| elementRef | The ElementRef for with the anchor. |
| viewContainerRef | The ViewContainerRef for the anchor. |
By default, the popover will apply focus to the first tabbable element when opened and trap focus
within the popover until closed. If the popover does not contain any focusable elements, focus
will remain on the most recently focused element.
You can target a different element for initial focus using the cdkFocusInitial attribute.
To prevent focus from automatically moving into the popover, you can set the autoFocus propertyfalse
to .
`html`
Once the popover is closed, focus will return to the most recently focused element prior to
opening the popover. To disable this, you can set the restoreFocus property to false.
`html`
Alternatively the open method supports an optional SatPopoverOpenOptionsautoFocus
object where and restoreFocus options can be set while opening the popover. NoterestoreFocus
that these options do no take precendence over the component inputs. For example, if false
is set to either in the open options or via the component input, focus will not be
restored.
`html`
You can add a fullscreen backdrop that appears behind the popover when it is open. It prevents
interaction with the rest of the application and will automatically close the popover when
clicked. To add it to your popover, use hasBackdrop.
`html`
If used, the default backdrop will be transparent. You can add any custom backdrop class with
backdropClass.
`html`
> Note: if you plan on using mouseenter and mouseleave events to open and close your popover,mouseleave
> keep in mind that a backdrop will block pointer events once it is open, immediately triggering
> a event.
You can add custom css classes to the overlay panel that wraps the popover.
`html`
If your popover has a backdrop, it will automatically close when clicked. The popover will also
automatically close when esc is pressed. These two behaviors are wrapped in the
interactiveClose property, which defaults to true. Set interactiveClose to false to prevent
the popover from automatically closing on these user interactions.
`html`
If you wish to only disable the automatic esc behavior, you must disable all
interactive close options and then manually react to backdropClicked events.
`html`
By default, when a popover is open and the user scrolls the container, the popover will reposition
itself to stay attached to its anchor. You can adjust this behavior with scrollStrategy.
`html`
| Strategy | Description |
| -------------- | ------------------------------------------- |
| 'noop' | Don't update position. |'block'
| | Block page scrolling while open. |'reposition'
| | Reposition the popover on scroll (default). |'close'
| | Close the popover on scroll. |
> Note: if your popover fails to stay anchored with the reposition strategy, you may need to addcdkScrollable
> the directive to your
> scrolling container. This will ensure scroll events are dispatched to the popover's positioning
> service.
By default, the opening and closing animations of a popover are quick with a simple easing curve.
You can modify these animation curves using openTransition and closeTransition.
`html`
You can also modify the default transition globally by adding a custom value to the
DEFAULT_TRANSITION provider.
`ts
import { SatPopoverModule, DEFAULT_TRANSITION } from '@ncstate/sat-popover';
@NgModule({
...
imports: [ SatPopoverModule ],
providers: [
{ provide: DEFAULT_TRANSITION, useValue: '300ms ease' }
]
...
})
export class AppModule { }
`
Additionally you can modify the scale values for the opening (startAtScale) and closing (endAtScale) animations.
`html`
The component only provides styles to affect its own transform origin. It is
the responsibility of the elements you project inside the popover to style themselves. This
includes background color, box shadows, margin offsets, etc.
The SatPopoverHoverDirective is available as a way to automatically add hover logic to yourSatPopoverHoverDirective
anchor with an optional delay. The must be used in conjunctionSatPopoverAnchor
with .
`html` Hover to show tooltip after 1 second
`html`` Hover this text to show tooltip immediately