mirror of
https://github.com/bitwarden/browser
synced 2026-02-19 10:54:00 +00:00
This PR migrates `libs/components` to use strict TypeScript. - Remove `@ts-strict-ignore` from each file in `libs/components` and resolved any new compilation errors - Converted ViewChild and ContentChild decorators to use the new signal-based queries using the [Angular signal queries migration](https://angular.dev/reference/migrations/signal-queries) - Made view/content children `required` where appropriate, eliminating the need for additional null checking. This helped simplify the strict migration. --- Co-authored-by: Vicki League <vleague@bitwarden.com>
136 lines
3.4 KiB
TypeScript
136 lines
3.4 KiB
TypeScript
import { Overlay, OverlayConfig, OverlayRef } from "@angular/cdk/overlay";
|
|
import { TemplatePortal } from "@angular/cdk/portal";
|
|
import {
|
|
AfterViewInit,
|
|
Directive,
|
|
ElementRef,
|
|
HostListener,
|
|
OnDestroy,
|
|
ViewContainerRef,
|
|
input,
|
|
model,
|
|
} from "@angular/core";
|
|
import { Observable, Subscription, filter, mergeWith } from "rxjs";
|
|
|
|
import { defaultPositions } from "./default-positions";
|
|
import { PopoverComponent } from "./popover.component";
|
|
|
|
@Directive({
|
|
selector: "[bitPopoverTriggerFor]",
|
|
exportAs: "popoverTrigger",
|
|
host: {
|
|
"[attr.aria-expanded]": "this.popoverOpen()",
|
|
},
|
|
})
|
|
export class PopoverTriggerForDirective implements OnDestroy, AfterViewInit {
|
|
readonly popoverOpen = model(false);
|
|
|
|
readonly popover = input.required<PopoverComponent>({ alias: "bitPopoverTriggerFor" });
|
|
|
|
readonly position = input<string>();
|
|
|
|
private overlayRef: OverlayRef | null = null;
|
|
private closedEventsSub: Subscription | null = null;
|
|
|
|
get positions() {
|
|
if (!this.position()) {
|
|
return defaultPositions;
|
|
}
|
|
|
|
const preferredPosition = defaultPositions.find((position) => position.id === this.position());
|
|
|
|
if (preferredPosition) {
|
|
return [preferredPosition, ...defaultPositions];
|
|
}
|
|
|
|
return defaultPositions;
|
|
}
|
|
|
|
get defaultPopoverConfig(): OverlayConfig {
|
|
return {
|
|
hasBackdrop: true,
|
|
backdropClass: "cdk-overlay-transparent-backdrop",
|
|
scrollStrategy: this.overlay.scrollStrategies.reposition(),
|
|
positionStrategy: this.overlay
|
|
.position()
|
|
.flexibleConnectedTo(this.elementRef)
|
|
.withPositions(this.positions)
|
|
.withLockedPosition(true)
|
|
.withFlexibleDimensions(false)
|
|
.withPush(true),
|
|
};
|
|
}
|
|
|
|
constructor(
|
|
private elementRef: ElementRef<HTMLElement>,
|
|
private viewContainerRef: ViewContainerRef,
|
|
private overlay: Overlay,
|
|
) {}
|
|
|
|
@HostListener("click")
|
|
togglePopover() {
|
|
if (this.popoverOpen()) {
|
|
this.closePopover();
|
|
} else {
|
|
this.openPopover();
|
|
}
|
|
}
|
|
|
|
private openPopover() {
|
|
this.popoverOpen.set(true);
|
|
this.overlayRef = this.overlay.create(this.defaultPopoverConfig);
|
|
|
|
const templatePortal = new TemplatePortal(this.popover().templateRef(), this.viewContainerRef);
|
|
|
|
this.overlayRef.attach(templatePortal);
|
|
this.closedEventsSub = this.getClosedEvents().subscribe(() => {
|
|
this.destroyPopover();
|
|
});
|
|
}
|
|
|
|
private getClosedEvents(): Observable<any> {
|
|
if (!this.overlayRef) {
|
|
throw new Error("Overlay reference is not available");
|
|
}
|
|
|
|
const detachments = this.overlayRef.detachments();
|
|
const escKey = this.overlayRef
|
|
.keydownEvents()
|
|
.pipe(filter((event: KeyboardEvent) => event.key === "Escape"));
|
|
const backdrop = this.overlayRef.backdropClick();
|
|
const popoverClosed = this.popover().closed;
|
|
|
|
return detachments.pipe(mergeWith(escKey, backdrop, popoverClosed));
|
|
}
|
|
|
|
private destroyPopover() {
|
|
if (!this.overlayRef || !this.popoverOpen()) {
|
|
return;
|
|
}
|
|
|
|
this.popoverOpen.set(false);
|
|
this.disposeAll();
|
|
}
|
|
|
|
private disposeAll() {
|
|
this.closedEventsSub?.unsubscribe();
|
|
this.closedEventsSub = null;
|
|
this.overlayRef?.dispose();
|
|
this.overlayRef = null;
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
if (this.popoverOpen()) {
|
|
this.openPopover();
|
|
}
|
|
}
|
|
|
|
ngOnDestroy() {
|
|
this.disposeAll();
|
|
}
|
|
|
|
closePopover() {
|
|
this.destroyPopover();
|
|
}
|
|
}
|