@@ -19,7 +19,9 @@ import {
19
19
ContentChild ,
20
20
Directive ,
21
21
EventEmitter ,
22
+ ElementRef ,
22
23
Input ,
24
+ Inject ,
23
25
OnChanges ,
24
26
OnDestroy ,
25
27
Optional ,
@@ -28,7 +30,9 @@ import {
28
30
SkipSelf ,
29
31
ViewContainerRef ,
30
32
ViewEncapsulation ,
33
+ ViewChild ,
31
34
} from '@angular/core' ;
35
+ import { DOCUMENT } from '@angular/common' ;
32
36
import { Subject } from 'rxjs' ;
33
37
import { filter , startWith , take } from 'rxjs/operators' ;
34
38
import { MatAccordion } from './accordion' ;
@@ -70,8 +74,13 @@ let uniqueId = 0;
70
74
'[class.mat-expansion-panel-spacing]' : '_hasSpacing()' ,
71
75
}
72
76
} )
73
- export class MatExpansionPanel extends CdkAccordionItem
74
- implements AfterContentInit , OnChanges , OnDestroy {
77
+ export class MatExpansionPanel extends CdkAccordionItem implements AfterContentInit , OnChanges ,
78
+ OnDestroy {
79
+
80
+ // @breaking -change 8.0.0 Remove `| undefined` from here
81
+ // when the `_document` constructor param is required.
82
+ private _document : Document | undefined ;
83
+
75
84
/** Whether the toggle indicator should be hidden. */
76
85
@Input ( )
77
86
get hideToggle ( ) : boolean { return this . _hideToggle ; }
@@ -95,6 +104,9 @@ export class MatExpansionPanel extends CdkAccordionItem
95
104
/** Content that will be rendered lazily. */
96
105
@ContentChild ( MatExpansionPanelContent ) _lazyContent : MatExpansionPanelContent ;
97
106
107
+ /** Element containing the panel's user-provided content. */
108
+ @ViewChild ( 'body' ) _body : ElementRef < HTMLElement > ;
109
+
98
110
/** Portal holding the user's content. */
99
111
_portal : TemplatePortal ;
100
112
@@ -104,9 +116,11 @@ export class MatExpansionPanel extends CdkAccordionItem
104
116
constructor ( @Optional ( ) @SkipSelf ( ) accordion : MatAccordion ,
105
117
_changeDetectorRef : ChangeDetectorRef ,
106
118
_uniqueSelectionDispatcher : UniqueSelectionDispatcher ,
107
- private _viewContainerRef : ViewContainerRef ) {
119
+ private _viewContainerRef : ViewContainerRef ,
120
+ @Inject ( DOCUMENT ) _document ?: any ) {
108
121
super ( accordion , _changeDetectorRef , _uniqueSelectionDispatcher ) ;
109
122
this . accordion = accordion ;
123
+ this . _document = _document ;
110
124
}
111
125
112
126
/** Whether the expansion indicator should be hidden. */
@@ -175,6 +189,17 @@ export class MatExpansionPanel extends CdkAccordionItem
175
189
this . afterCollapse . emit ( ) ;
176
190
}
177
191
}
192
+
193
+ /** Checks whether the expansion panel's content contains the currently-focused element. */
194
+ _containsFocus ( ) : boolean {
195
+ if ( this . _body && this . _document ) {
196
+ const focusedElement = this . _document . activeElement ;
197
+ const bodyElement = this . _body . nativeElement ;
198
+ return focusedElement === bodyElement || bodyElement . contains ( focusedElement ) ;
199
+ }
200
+
201
+ return false ;
202
+ }
178
203
}
179
204
180
205
@Directive ( {
0 commit comments