Angular CDK's OverlayModule, cdk-overlay-pane will not set position to absolute?
Asked Answered
K

3

7

I'm using Overlay Module of Angular's CDK. And I just cannot find the way to add position: absolute to the overlay? The module will receive a top and a left position that matches the connected element position but the overlay will not receive position: absolute. I'm unable to add position absolute due to ViewEncapsulation. and I'm really struggling to find a solution.

Question: How can I use cdkOverlayOrigin and cdkConnectedOverlayOrigin

Module:

import { OverlayModule } from '@angular/cdk/overlay';



@NgModule({
  imports: [
    OverlayModule,
  ],
  declarations: [ MenuComponent ],
})
export class MenuModule {
}

Component:

@Component({
  selector: 'app-menu',
  templateUrl: './menu.component.html',
  styleUrls: [ './menu.component.scss' ],
})
export class MenuComponent {
}

Template:

 <div style="margin: 0 auto; width: 30%">
  <h1 cdkOverlayOrigin
      #checkListTrigger="cdkOverlayOrigin"
      (click)="checkListOpen = !checkListOpen"
      style="background: blue">Overlay Origin</h1>
  <ng-template
    cdkConnectedOverlay
    [cdkConnectedOverlayOrigin]="checkListTrigger"
    [cdkConnectedOverlayOpen]="checkListOpen">

    <ng-container>
      <p>Why you no positioned on h1 element</p>
    </ng-container>

  </ng-template>
</div>

See how it's off center enter image description here

If I add position: absolute it now works as intended? enter image description here

Kalman answered 16/12, 2017 at 1:26 Comment(4)
Is there a reason you're using OverlayModule instead of the Angular - Material mat-dialog? material.angular.io/components/dialog/overview (This being the standard for usking cdk items in Angular)Buckeye
Because I'm trying to make a popover overlay/tooltip and not a dialog box persayKalman
The dialog is supposed to be the all-browser standard for anything that requires more than a span for rendering in DOM. If all you want is text, then Material provides the tooltip. You can position the dialog to an element in the view w/ customization, and you can manipulate it many more ways (though the defaults are prettys tandard)Buckeye
It isn't a tooltip either. joejordanbrown.github.io/material2-popover-demo/popover is something more that I'm looking for.Kalman
T
3

This is described in the API documentation. You can specify a panel class for the overlay pane.

Here's some code:

const overlay = overlay.create({
    panelClass: 'pos-absolute'
});
.pos-absolute {
    position: absolute;
}
Tbilisi answered 18/12, 2017 at 1:59 Comment(1)
HI Edric, I got the same example Matthew, but can't figure out how do assign the overlay to the same code ? I created a git repo github.com/frontruk/overaly-module/blob/master/src/app/modules/… , if you could help that would be awesomeLaterite
P
22

The absolute positioning of the generated overlay container is defined in the .cdk-overlay-pane class, which is part of the angular material theme. You might have forgot to include a theme?

If so, add the following line to your style.css file (assuming you use an Angular CLI project). @import "~@angular/material/prebuilt-themes/indigo-pink.css";

Permanence answered 12/1, 2018 at 12:18 Comment(1)
this answer helped me to focus on core problem: missing theme imports in styles.scss. I added @use '@angular/material' as mat; //use tilda '~@angular/..... for angular <13 and @import '@angular/material/theming'; //use tilda '~@angular/..... for angular <13 and @include mat.core();Adowa
T
3

This is described in the API documentation. You can specify a panel class for the overlay pane.

Here's some code:

const overlay = overlay.create({
    panelClass: 'pos-absolute'
});
.pos-absolute {
    position: absolute;
}
Tbilisi answered 18/12, 2017 at 1:59 Comment(1)
HI Edric, I got the same example Matthew, but can't figure out how do assign the overlay to the same code ? I created a git repo github.com/frontruk/overaly-module/blob/master/src/app/modules/… , if you could help that would be awesomeLaterite
F
1

Inspired by niio's comment, I figured out I had not included the core part of angular material theming (Angular 15):

@use '@angular/material' as mat;
@include mat.core();

That put the select panel back to place.

Fishback answered 24/5, 2023 at 11:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.