11import { ModalService } from "./modal.service" ;
22import {
3+ AfterViewInit ,
34 Component ,
45 EventEmitter ,
56 HostListener ,
67 Input ,
7- OnInit ,
88 OnDestroy ,
9+ OnInit ,
910 Output ,
1011 ElementRef ,
1112 ViewChild
@@ -110,7 +111,7 @@ import { cycleTabs } from "./../common/tab.service";
110111 ] )
111112 ]
112113} )
113- export class ModalComponent implements OnInit , OnDestroy {
114+ export class ModalComponent implements AfterViewInit , OnInit , OnDestroy {
114115 /**
115116 * Size of the modal to display.
116117 * (size `"default"` is being deprecated as of neutrino v1.2.0, please use `"md"` instead)
@@ -148,6 +149,12 @@ export class ModalComponent implements OnInit, OnDestroy {
148149 */
149150 modalState = "out" ;
150151
152+ /**
153+ * An element should have 'data-modal-primary-focus' as an attribute to receive initial focus within the `Modal` component.
154+ * @memberof ModalComponent
155+ */
156+ selectorPrimaryFocus = "[modal-primary-focus]" ;
157+
151158 /**
152159 * Creates an instance of `ModalComponent`.
153160 * @param {ModalService } modalService
@@ -156,11 +163,23 @@ export class ModalComponent implements OnInit, OnDestroy {
156163 constructor ( public modalService : ModalService ) { }
157164
158165 /**
159- * Set document focus to be on the modal component when it is initialized.
166+ * Set modalState on the modal component when it is initialized.
160167 * @memberof ModalComponent
161168 */
162169 ngOnInit ( ) {
163170 this . modalState = "in" ;
171+ }
172+
173+ /**
174+ * Set document focus to be on the modal component after it is initialized.
175+ * @memberof ModalComponent
176+ */
177+ ngAfterViewInit ( ) {
178+ const primaryFocusElement = this . modal . nativeElement . querySelector ( this . selectorPrimaryFocus ) ;
179+ if ( primaryFocusElement && primaryFocusElement . focus ) {
180+ primaryFocusElement . focus ( ) ;
181+ return ;
182+ }
164183 this . modal . nativeElement . focus ( ) ;
165184 }
166185
0 commit comments