@@ -41,7 +41,7 @@ import {
41
41
MockNgZone ,
42
42
} from '@angular/cdk/testing/private' ;
43
43
import { Subject } from 'rxjs' ;
44
- import { ScrollDispatcher } from '@angular/cdk/scrolling' ;
44
+ import { ScrollDispatcher , ViewportRuler } from '@angular/cdk/scrolling' ;
45
45
import { FocusMonitor } from '@angular/cdk/a11y' ;
46
46
import {
47
47
MAT_MENU_SCROLL_STRATEGY ,
@@ -58,6 +58,7 @@ describe('MDC-based MatMenu', () => {
58
58
let overlayContainer : OverlayContainer ;
59
59
let overlayContainerElement : HTMLElement ;
60
60
let focusMonitor : FocusMonitor ;
61
+ let viewportRuler : ViewportRuler ;
61
62
62
63
function createComponent < T > ( component : Type < T > ,
63
64
providers : Provider [ ] = [ ] ,
@@ -68,11 +69,13 @@ describe('MDC-based MatMenu', () => {
68
69
providers
69
70
} ) . compileComponents ( ) ;
70
71
71
- inject ( [ OverlayContainer , FocusMonitor ] , ( oc : OverlayContainer , fm : FocusMonitor ) => {
72
- overlayContainer = oc ;
73
- overlayContainerElement = oc . getContainerElement ( ) ;
74
- focusMonitor = fm ;
75
- } ) ( ) ;
72
+ inject ( [ OverlayContainer , FocusMonitor , ViewportRuler ] ,
73
+ ( oc : OverlayContainer , fm : FocusMonitor , vr : ViewportRuler ) => {
74
+ overlayContainer = oc ;
75
+ overlayContainerElement = oc . getContainerElement ( ) ;
76
+ focusMonitor = fm ;
77
+ viewportRuler = vr ;
78
+ } ) ( ) ;
76
79
77
80
return TestBed . createComponent < T > ( component ) ;
78
81
}
@@ -1059,6 +1062,28 @@ describe('MDC-based MatMenu', () => {
1059
1062
expect ( document . activeElement ) . toBe ( items [ 3 ] , 'Expected fourth item to be focused' ) ;
1060
1063
} ) ) ;
1061
1064
1065
+ it ( 'should keep the panel in the viewport when more items are added while open' , ( ) => {
1066
+ const fixture = createComponent ( SimpleMenu , [ ] , [ FakeIcon ] ) ;
1067
+ fixture . detectChanges ( ) ;
1068
+
1069
+ const triggerEl = fixture . componentInstance . triggerEl . nativeElement ;
1070
+ triggerEl . style . position = 'absolute' ;
1071
+ triggerEl . style . left = '200px' ;
1072
+ triggerEl . style . bottom = '300px' ;
1073
+ triggerEl . click ( ) ;
1074
+ fixture . detectChanges ( ) ;
1075
+
1076
+ const panel = overlayContainerElement . querySelector ( '.mat-mdc-menu-panel' ) ! ;
1077
+ const viewportHeight = viewportRuler . getViewportSize ( ) . height ;
1078
+ let panelRect = panel . getBoundingClientRect ( ) ;
1079
+ expect ( Math . floor ( panelRect . bottom ) ) . toBeLessThan ( viewportHeight ) ;
1080
+
1081
+ fixture . componentInstance . extraItems = new Array ( 50 ) . fill ( 'Hello there' ) ;
1082
+ fixture . detectChanges ( ) ;
1083
+ panelRect = panel . getBoundingClientRect ( ) ;
1084
+ expect ( Math . floor ( panelRect . bottom ) ) . toBe ( viewportHeight ) ;
1085
+ } ) ;
1086
+
1062
1087
describe ( 'lazy rendering' , ( ) => {
1063
1088
it ( 'should be able to render the menu content lazily' , fakeAsync ( ( ) => {
1064
1089
const fixture = createComponent ( SimpleLazyMenu ) ;
0 commit comments