De Wet van As
De Wet van As

Reputation: 1000

How to delay opening in CDK overlay?

I'm using the CDK Overlay to display a "popover" when the user hovers over a list item. I currently open the popover when the mouseenter event fires.

My code:

//component.html
<mat-list-item *ngFor="let item of itemList" (mouseenter)="showItemDetail(item)">
    {{item.display}}
</mat-list-item>




//component.ts
showItemDetail(item: IItemDto, event: MouseEvent) {
        this.hideItemDetail(); // Closes any open overlays
        this.itemDetailOverlayRef = this.itemDetailOverlayService.open(item);
}



//itemDetailOverlayService.ts
open(item: IItemDto) {

        // Returns an OverlayRef (which is a PortalHost)

        const overlayRef = this.createOverlay(item);
        const dialogRef = new ItemDetailOverlayRef(overlayRef);
        // Create ComponentPortal that can be attached to a PortalHost
        const itemDetailPortal = new ComponentPortal(ItemDetailOverlayComponent);
        const componentInstance = this.attachDialogContainer(overlayRef, item, dialogRef);
        // Attach ComponentPortal to PortalHost
        return dialogRef;
}


private attachDialogContainer(overlayRef: OverlayRef, item: IItemDto, dialogRef: ItemDetailOverlayRef) {
        const injector = this.createInjector(item, dialogRef);
        const containerPortal = new ComponentPortal(ItemDetailOverlayComponent, null, injector);
        const containerRef: ComponentRef<ItemDetailOverlayComponent> = overlayRef.attach(containerPortal);
        return containerRef.instance;
}

Note that my overlay is dependent on data from list item data.

How can I delay showItemDetail() to only open the overlay after 2s? Keep in mind that only one popover can be open at a time.

setTimeout() obviously won't work as multiple popovers will be opened if the user drags the mouse across the list of items.

Upvotes: 1

Views: 2211

Answers (1)

De Wet van As
De Wet van As

Reputation: 1000

Resolved by opening the overlay without delay while creating the delay effect using css animation/keyframes:

.container {
    animation: fadeIn 1.5s linear;
}

@keyframes fadeIn {
    0% {
        opacity: 0;
    }

    75% {
        opacity: 0;
    }

    100% {
        opacity: 1;
    }
}

Upvotes: 2

Related Questions