I'm trying to use CdkConnectedOverlay
to display an overlay when a button is clicked. It's mostly working, but the overlay is not re-positioning on scroll. I'm sure there's something small I'm missing, but I can't for the life of me figure it out.
I'm using Angular 7.2.8
and Angular CDK 7.3.3
Thought it might be related to missing cdk styles (similar to this), but I imported those; if the styles were missing, I would expect it not to display correctly in the first place. Mine just doesn't re-position on scroll.
Template:
<button
(click)="isOpen = !isOpen"
cdkOverlayOrigin
#trigger="cdkOverlayOrigin"
>Show</button>
<ng-template
cdkConnectedOverlay
[cdkConnectedOverlayOrigin]="trigger"
[cdkConnectedOverlayOpen]="isOpen"
>
Popover content
</ng-template>
Component:
@Component ( {
selector: 'app-popover',
templateUrl: './popover.component.html',
styleUrls: [ './popover.component.css' ],
changeDetection: ChangeDetectionStrategy.OnPush,
} )
export class PopoverComponent {
isOpen: boolean = false;
}
And a Plunkr showing the issue: https://stackblitz.com/edit/angular-7-popover
Update:
The scroll re-positioning issue only occurs when the popover is in an element that overflows its parent with overflow: auto
. If the page is overflowing, then it works fine. You can see this behavior with the following template
<div style="height: 300px; overflow-y: auto">
<!-- Scroll re-positioning does not work when scrolling in here -->
<div style="height: 100px"></div>
<app-popover>
Popover content
</app-popover>
<div style="height: 400px"></div>
</div>
<div style="height: 100px;"></div>
<!-- Scroll re-positioning works when scrolling here -->
<app-popover>
Popover content
</app-popover>
<div style="height: 1200px;"></div>
I've also updated the stackblitz to show this issue.