Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cdk/overlay): don't close if scroll is coming from inside overlay #26840

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/cdk/overlay/scroll/close-scroll-strategy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {inject, TestBed, fakeAsync} from '@angular/core/testing';
import {Component, NgZone} from '@angular/core';
import {Component, ElementRef, NgZone} from '@angular/core';
import {Subject} from 'rxjs';
import {ComponentPortal, PortalModule} from '@angular/cdk/portal';
import {ScrollDispatcher, ViewportRuler} from '@angular/cdk/scrolling';
import {CdkScrollable, ScrollDispatcher, ViewportRuler} from '@angular/cdk/scrolling';
import {Overlay, OverlayConfig, OverlayRef, OverlayModule, OverlayContainer} from '../index';

describe('CloseScrollStrategy', () => {
let overlayRef: OverlayRef;
let componentPortal: ComponentPortal<MozarellaMsg>;
let scrolledSubject = new Subject();
let scrolledSubject = new Subject<CdkScrollable | undefined>();
let scrollPosition: number;

beforeEach(fakeAsync(() => {
Expand Down Expand Up @@ -55,6 +55,16 @@ describe('CloseScrollStrategy', () => {
expect(overlayRef.detach).toHaveBeenCalled();
});

it('should not detach if the scrollable is inside the overlay', () => {
overlayRef.attach(componentPortal);
spyOn(overlayRef, 'detach');

scrolledSubject.next({
getElementRef: () => new ElementRef(overlayRef.overlayElement),
} as CdkScrollable);
expect(overlayRef.detach).not.toHaveBeenCalled();
});

it('should not attempt to detach the overlay after it has been detached', () => {
overlayRef.attach(componentPortal);
overlayRef.detach();
Expand Down
10 changes: 9 additions & 1 deletion src/cdk/overlay/scroll/close-scroll-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {ScrollStrategy, getMatScrollStrategyAlreadyAttachedError} from './scroll
import {OverlayReference} from '../overlay-reference';
import {Subscription} from 'rxjs';
import {ScrollDispatcher, ViewportRuler} from '@angular/cdk/scrolling';
import {filter} from 'rxjs/operators';

/**
* Config options for the CloseScrollStrategy.
Expand Down Expand Up @@ -49,7 +50,14 @@ export class CloseScrollStrategy implements ScrollStrategy {
return;
}

const stream = this._scrollDispatcher.scrolled(0);
const stream = this._scrollDispatcher.scrolled(0).pipe(
filter(scrollable => {
return (
!scrollable ||
!this._overlayRef.overlayElement.contains(scrollable.getElementRef().nativeElement)
);
}),
);

if (this._config && this._config.threshold && this._config.threshold > 1) {
this._initialScrollPosition = this._viewportRuler.getViewportScrollPosition().top;
Expand Down