Skip to content

Commit 18877e0

Browse files
committed
fix(alert): focus first button when alert opens
1 parent 9f30b79 commit 18877e0

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

core/src/components/alert/alert.tsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,34 @@ export class Alert implements ComponentInterface, OverlayInterface {
400400

401401
await this.delegateController.attachViewToDom();
402402

403-
await present(this, 'alertEnter', iosEnterAnimation, mdEnterAnimation);
403+
const ariaDescribedBy = this.el.getAttribute('aria-describedby');
404+
const ariaLabelledBy = this.el.getAttribute('aria-labelledby');
405+
406+
if (ariaDescribedBy) {
407+
this.el.removeAttribute('aria-describedby');
408+
}
409+
410+
if (ariaLabelledBy) {
411+
this.el.removeAttribute('aria-labelledby');
412+
}
413+
414+
await present(this, 'alertEnter', iosEnterAnimation, mdEnterAnimation).then(() => {
415+
const firstButton =
416+
this.el.querySelector<HTMLElement>('.alert-button-role-cancel') ||
417+
this.el.querySelector<HTMLElement>('.alert-button');
418+
419+
if (firstButton) {
420+
firstButton.focus();
421+
}
422+
423+
if (ariaDescribedBy) {
424+
this.el.setAttribute('aria-describedby', ariaDescribedBy);
425+
}
426+
427+
if (ariaLabelledBy) {
428+
this.el.setAttribute('aria-labelledby', ariaLabelledBy);
429+
}
430+
});
404431

405432
unlock();
406433
}

0 commit comments

Comments
 (0)