fix: focus cancel or ok button on confirm and alert modals
when opening the modal: - remember previously focused element - set focused element to the ok or cancel button when closing the modal: - restore the focus to the previously focused element
This commit is contained in:
@@ -822,6 +822,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
|
||||
function showConfirmModal(message: string): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
const previousActiveEl = document.activeElement as HTMLButtonElement | null;
|
||||
const overlay = document.createElement('div');
|
||||
overlay.className = 'modal-overlay';
|
||||
|
||||
@@ -842,19 +843,30 @@ function showConfirmModal(message: string): Promise<boolean> {
|
||||
overlay.appendChild(modal);
|
||||
modalContainer?.appendChild(overlay);
|
||||
|
||||
modal.querySelector('#modal-cancel')?.addEventListener('click', () => {
|
||||
const modalCancelBtn = modal.querySelector(
|
||||
'#modal-cancel'
|
||||
) as HTMLButtonElement | null;
|
||||
const modalConfirmBtn = modal.querySelector(
|
||||
'#modal-confirm'
|
||||
) as HTMLButtonElement | null;
|
||||
modalCancelBtn?.focus();
|
||||
|
||||
modalCancelBtn?.addEventListener('click', () => {
|
||||
modalContainer?.removeChild(overlay);
|
||||
previousActiveEl?.focus();
|
||||
resolve(false);
|
||||
});
|
||||
|
||||
modal.querySelector('#modal-confirm')?.addEventListener('click', () => {
|
||||
modalConfirmBtn?.addEventListener('click', () => {
|
||||
modalContainer?.removeChild(overlay);
|
||||
previousActiveEl?.focus();
|
||||
resolve(true);
|
||||
});
|
||||
|
||||
overlay.addEventListener('click', (e: MouseEvent) => {
|
||||
if (e.target === overlay) {
|
||||
modalContainer?.removeChild(overlay);
|
||||
previousActiveEl?.focus();
|
||||
resolve(false);
|
||||
}
|
||||
});
|
||||
@@ -863,6 +875,7 @@ function showConfirmModal(message: string): Promise<boolean> {
|
||||
|
||||
function showAlertModal(title: string, message: string): Promise<boolean> {
|
||||
return new Promise((resolve) => {
|
||||
const previousActiveEl = document.activeElement as HTMLButtonElement | null;
|
||||
const overlay = document.createElement('div');
|
||||
overlay.className = 'modal-overlay';
|
||||
|
||||
@@ -882,14 +895,19 @@ function showAlertModal(title: string, message: string): Promise<boolean> {
|
||||
overlay.appendChild(modal);
|
||||
modalContainer?.appendChild(overlay);
|
||||
|
||||
modal.querySelector('#modal-ok')?.addEventListener('click', () => {
|
||||
const okBtn = modal.querySelector('#modal-ok') as HTMLButtonElement | null;
|
||||
okBtn?.focus();
|
||||
|
||||
okBtn?.addEventListener('click', () => {
|
||||
modalContainer?.removeChild(overlay);
|
||||
previousActiveEl?.focus();
|
||||
resolve(true);
|
||||
});
|
||||
|
||||
overlay.addEventListener('click', (e: MouseEvent) => {
|
||||
if (e.target === overlay) {
|
||||
modalContainer?.removeChild(overlay);
|
||||
previousActiveEl?.focus();
|
||||
resolve(true);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user