Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular CDK Overlay configure overlay position

I am using the Angular CDK to show an element with overlay. My problem is that I want to change the position to be in the top right, but none of the arguments to the connectedTo function is doing this.

@Component({
  template: `
    <button cdk-overlay-origin (click)="openSpaghettiPanel()">
      Pasta 3
    </button>
   `
})
export class AppComponent  {
  @ViewChild(OverlayOrigin) _overlayOrigin: OverlayOrigin;
  constructor(private _overlay: Overlay, 
              public viewContainerRef: ViewContainerRef) {

  }

  openSpaghettiPanel() {
    let strategy = this._overlay.position()
        .connectedTo(
            this._overlayOrigin.elementRef,
   {originX: 'start', originY: 'bottom'},
            {overlayX: 'end', overlayY: 'top'} );
    let config = new OverlayConfig({ width: '100px', height: '100px', positionStrategy: strategy});
    const overlayRef = this._overlay.create(config);
    const userProfilePortal = new ComponentPortal(HelloComponent, this.viewContainerRef);
    overlayRef.attach(userProfilePortal);
  }
}

Which values I need to set in the connectedTo function for this to work?

like image 677
JsFan Avatar asked Oct 19 '25 22:10

JsFan


1 Answers

By looking at Angular Material Tooltip's source code and playing with it, it looks like you can't achieve the "top-right" positioning with only using cdk/overlay's API.

But a simple way to accomplish this is to place you component "above" your origin element using the following snippet and add left margin in it. The margin's value it depends on your use case. Also you don't need to set width and height of your overlay configuration.

// above position
let originPos = {
  originX: 'center',
  originY: 'bottom'
}

let overlayPos = {
  overlayX: 'center',
  overlayY: 'bottom'
}

A useful source on how to use @angular/cdk/overlay is this.

like image 123
tasos Avatar answered Oct 22 '25 13:10

tasos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!