@@ -18,15 +18,19 @@ import {
18
18
Component ,
19
19
ContentChild ,
20
20
Directive ,
21
+ ElementRef ,
21
22
Input ,
23
+ Inject ,
22
24
OnChanges ,
23
25
OnDestroy ,
24
26
Optional ,
25
27
SimpleChanges ,
26
28
SkipSelf ,
27
29
ViewContainerRef ,
28
30
ViewEncapsulation ,
31
+ ViewChild ,
29
32
} from '@angular/core' ;
33
+ import { DOCUMENT } from '@angular/common' ;
30
34
import { Subject } from 'rxjs' ;
31
35
import { filter , startWith , take } from 'rxjs/operators' ;
32
36
import { MatAccordion } from './accordion' ;
@@ -68,8 +72,13 @@ let uniqueId = 0;
68
72
'[class.mat-expansion-panel-spacing]' : '_hasSpacing()' ,
69
73
}
70
74
} )
71
- export class MatExpansionPanel extends CdkAccordionItem
72
- implements AfterContentInit , OnChanges , OnDestroy {
75
+ export class MatExpansionPanel extends CdkAccordionItem implements AfterContentInit , OnChanges ,
76
+ OnDestroy {
77
+
78
+ // @breaking -change 8.0.0 Remove `| undefined` from here
79
+ // when the `_document` constructor param is required.
80
+ private _document : Document | undefined ;
81
+
73
82
/** Whether the toggle indicator should be hidden. */
74
83
@Input ( )
75
84
get hideToggle ( ) : boolean { return this . _hideToggle ; }
@@ -87,6 +96,9 @@ export class MatExpansionPanel extends CdkAccordionItem
87
96
/** Content that will be rendered lazily. */
88
97
@ContentChild ( MatExpansionPanelContent ) _lazyContent : MatExpansionPanelContent ;
89
98
99
+ /** Element containing the panel's user-provided content. */
100
+ @ViewChild ( 'body' ) _body : ElementRef < HTMLElement > ;
101
+
90
102
/** Portal holding the user's content. */
91
103
_portal : TemplatePortal ;
92
104
@@ -96,9 +108,11 @@ export class MatExpansionPanel extends CdkAccordionItem
96
108
constructor ( @Optional ( ) @SkipSelf ( ) accordion : MatAccordion ,
97
109
_changeDetectorRef : ChangeDetectorRef ,
98
110
_uniqueSelectionDispatcher : UniqueSelectionDispatcher ,
99
- private _viewContainerRef : ViewContainerRef ) {
111
+ private _viewContainerRef : ViewContainerRef ,
112
+ @Inject ( DOCUMENT ) _document ?: any ) {
100
113
super ( accordion , _changeDetectorRef , _uniqueSelectionDispatcher ) ;
101
114
this . accordion = accordion ;
115
+ this . _document = _document ;
102
116
}
103
117
104
118
/** Whether the expansion indicator should be hidden. */
@@ -159,6 +173,17 @@ export class MatExpansionPanel extends CdkAccordionItem
159
173
classList . remove ( cssClass ) ;
160
174
}
161
175
}
176
+
177
+ /** Checks whether the expansion panel's content contains the currently-focused element. */
178
+ _containsFocus ( ) : boolean {
179
+ if ( this . _body && this . _document ) {
180
+ const focusedElement = this . _document . activeElement ;
181
+ const bodyElement = this . _body . nativeElement ;
182
+ return focusedElement === bodyElement || bodyElement . contains ( focusedElement ) ;
183
+ }
184
+
185
+ return false ;
186
+ }
162
187
}
163
188
164
189
@Directive ( {
0 commit comments