Skip to content

Commit 204f0ca

Browse files
committed
2 parents 4a73196 + 8ed1470 commit 204f0ca

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/modal/modal.component.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { ModalService } from "./modal.service";
22
import {
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

src/modal/modal.stories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { Modal, ModalService } from "../";
1919
<p class="bx--modal-content__text">{{modalText}}</p>
2020
</section>
2121
<ibm-modal-footer>
22-
<button class="bx--btn bx--btn--primary" (click)="closeModal()">Close</button>
22+
<button class="bx--btn bx--btn--primary" modal-primary-focus (click)="closeModal()">Close</button>
2323
</ibm-modal-footer>
2424
</ibm-modal>
2525
`

0 commit comments

Comments
 (0)