Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot Close Angular Material Cdkoverlay From BackdropClick

I'm currently using the Angular Material cdkoverlay and want to close it when I click anywhere else on the screen except for the overlay. I've gone aheadand subscribed to backdropClick() but I can't get it to fire.

launchOverlay() {
        let strategy = this.overlay.position()
        .connectedTo(
            this._overlayOrigin,
            {originX: 'end', originY: 'top'},
            {overlayX: 'end', overlayY: 'top'} );
        let config = new OverlayConfig({positionStrategy: strategy, width: '280px', scrollStrategy: this.overlay.scrollStrategies.reposition()});
        this._overlayRef = this.overlay.create(config);
        this._overlayRef.attach(new TemplatePortal(this.filterTemplate, this.viewContainerRef));
        this._overlayRef.backdropClick().subscribe(() => this.close()}, () => console.log("ERROR"), () => console.log("COMPLETE"));
}

 close(){
    this._overlayRef.dispose();
}

<ng-template cdkPortal #columnFilter>
    <mat-card>
        HELLO WORLD
    </mat-card>
</ng-template>

Everything with creating and launching the overlay works fine, it's just responding to the outside click.

If I add hasBackdrop: true to OverlayConfig then it creates the dark grey backdrop and the click outside works, but I don't want a visiable backdrop, like the selete component.

like image 779
DevEng Avatar asked Jun 29 '18 15:06

DevEng


People also ask

How do I close CDK overlay?

the backdropClick event close the Overlay.

What is CdkOverlayOrigin?

CdkOverlayOrigin. Directive applied to an element to make it usable as an origin for an Overlay using a ConnectedPositionStrategy.

What is CDK Global Scrollblock?

cdk-global-scrollblock css class. This class is added to the html tag when the block strategy is used. It is used to block the scroll of the content behind the dialog, especially on mobile devices (iOS) – that's why position: fixed; is used.12-Dec-2017.

What is angular CDK?

The Angular Component Dev Kit (CDK) is a library of predefined behaviors included in Angular Material, a UI component library for Angular developers. It contains reusable logic for many features that are used as common building blocks for the interfaces of Angular applications.


2 Answers

hasBackdrop: true,
backdropClass: 'cdk-overlay-transparent-backdrop'

Adds the transparent backdrop inwhich the select and other components use.

like image 168
DevEng Avatar answered Sep 19 '22 06:09

DevEng


In case if someone is using template and directives then you can use cdkConnectedOverlayHasBackdrop and cdkConnectedOverlayBackdropClass directives.

And for transparent backdrop use the css class cdk-overlay-transparent-backdrop

<ng-template cdkConnectedOverlay [cdkConnectedOverlayOrigin]="trigger" [cdkConnectedOverlayHasBackdrop]="true"
    [cdkConnectedOverlayBackdropClass]="'cdk-overlay-transparent-backdrop'" [cdkConnectedOverlayOpen]="isDropdownOpen"
    (backdropClick)="toggleDropdown()">
like image 23
Sunil Garg Avatar answered Sep 20 '22 06:09

Sunil Garg