فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
fix(ux): accessible modal dialogs with labeled role, focus management, Escape, Tab trap and focus restoration
davoodmehraban89 committed
Oct 30, 2026 at 23:26 UTC
55bc80eba95a9020144cc4eda8fb8853829c8dfb
1 file changed
+82
-15
src/core/Modal.js
+82
-15
@@ -1,43 +1,92 @@
1
// ============================================================
2
-// Modal — سیستم پنجرهی شناور
2
+// Modal — accessible stacked dialogs and keyboard interaction
3
// ============================================================
4
5
class ModalManager {
6
constructor() {
7
this.stack = [];
8
+ this.sequence = 0;
9
+ this._onKeydown = this._onKeydown.bind(this);
10
+ }
11
+
12
+ _focusable(card) {
13
+ return [...card.querySelectorAll(
14
+ 'a[href], button:not([disabled]), input:not([disabled]), ' +
15
+ 'select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'
16
+ )].filter(el => el.getClientRects().length > 0 &&
17
+ window.getComputedStyle(el).visibility !== 'hidden');
18
+ }
19
+
20
+ _onKeydown(event) {
21
+ const top = this.stack[this.stack.length - 1];
22
+ if (!top) return;
23
+ if (event.key === 'Escape') {
24
+ event.preventDefault();
25
+ event.stopPropagation();
26
+ this.close();
27
+ return;
28
+ }
29
+ if (event.key !== 'Tab') return;
30
+ const focusables = this._focusable(top.card);
31
+ if (!focusables.length) {
32
+ event.preventDefault();
33
+ top.card.focus({ preventScroll: true });
34
+ return;
35
+ }
36
+ const first = focusables[0];
37
+ const last = focusables[focusables.length - 1];
38
+ const active = document.activeElement;
39
+ if (event.shiftKey) {
40
+ if (active === first || !top.card.contains(active)) {
41
+ event.preventDefault();
42
+ last.focus({ preventScroll: true });
43
+ }
44
+ } else if (active === last || !top.card.contains(active)) {
45
+ event.preventDefault();
46
+ first.focus({ preventScroll: true });
47
+ }
48
+ }
49
+
50
+ _refreshStack() {
51
+ for (let i = 0; i < this.stack.length; i++) {
52
+ const top = i === this.stack.length - 1;
53
+ if (top) this.stack[i].card.removeAttribute('aria-hidden');
54
+ else this.stack[i].card.setAttribute('aria-hidden', 'true');
55
+ }
56
}
57
58
_create(options) {
59
const { title, body, footer, size = 'md', onClose, closeOnBackdrop = true } = options;
12
-
60
const backdrop = document.createElement('div');
61
backdrop.className = 'modal-backdrop';
62
16
- // Fix: کلیک فقط زمانی معنی داره که هم mousedown و هم mouseup روی خود backdrop باشه
17
- // (نه وقتی که کاربر داره داخل فرم متن select میکنه و موس به بیرون کشیده میشه)
63
+ // Do not interpret a text selection ending over the backdrop as an intent to close.
64
if (closeOnBackdrop) {
65
let mouseDownTarget = null;
20
- backdrop.addEventListener('mousedown', (e) => {
21
- mouseDownTarget = e.target;
22
- });
23
- backdrop.addEventListener('mouseup', (e) => {
24
- if (e.target === backdrop && mouseDownTarget === backdrop) {
25
- this.close();
26
- }
66
+ backdrop.addEventListener('mousedown', e => { mouseDownTarget = e.target; });
67
+ backdrop.addEventListener('mouseup', e => {
68
+ if (e.target === backdrop && mouseDownTarget === backdrop) this.close();
69
mouseDownTarget = null;
70
});
71
}
72
73
const card = document.createElement('div');
74
card.className = `modal-card modal-${size}`;
75
+ const titleId = 'finora-dialog-title-' + (++this.sequence);
76
+ card.setAttribute('role', 'dialog');
77
+ card.setAttribute('aria-modal', 'true');
78
+ card.setAttribute('aria-labelledby', titleId);
79
+ card.setAttribute('tabindex', '-1');
80
card.innerHTML = `
81
<div class="modal-header">
35
- <div class="modal-title">${title}</div>
36
- <button class="modal-close" aria-label="بستن">×</button>
82
+ <div class="modal-title" id="${titleId}"></div>
83
+ <button type="button" class="modal-close" aria-label="بستن پنجره">×</button>
84
</div>
85
<div class="modal-body"></div>
86
${footer ? '<div class="modal-footer"></div>' : ''}
87
`;
88
+ // Dialog title is text, never HTML from user-provided company/contact names.
89
+ card.querySelector('.modal-title').textContent = String(title ?? '');
90
91
const bodyEl = card.querySelector('.modal-body');
92
if (typeof body === 'string') bodyEl.innerHTML = body;
@@ -51,7 +100,7 @@ class ModalManager {
100
101
card.querySelector('.modal-close').onclick = () => this.close();
102
backdrop.appendChild(card);
54
- return { backdrop, card, onClose };
103
+ return { backdrop, card, onClose, previousFocus: document.activeElement };
104
}
105
106
open(options) {
@@ -59,6 +108,17 @@ class ModalManager {
108
document.body.appendChild(modal.backdrop);
109
document.body.style.overflow = 'hidden';
110
this.stack.push(modal);
111
+ this._refreshStack();
112
+ if (this.stack.length === 1) {
113
+ document.addEventListener('keydown', this._onKeydown, true);
114
+ }
115
+ queueMicrotask(() => {
116
+ if (this.stack[this.stack.length - 1] !== modal || !modal.card.isConnected) return;
117
+ const focusTarget = modal.card.querySelector('[autofocus]') ||
118
+ modal.card.querySelector('input:not([disabled]), select:not([disabled]), textarea:not([disabled])') ||
119
+ this._focusable(modal.card)[0] || modal.card;
120
+ focusTarget.focus({ preventScroll: true });
121
+ });
122
return modal;
123
}
124
@@ -66,8 +126,15 @@ class ModalManager {
126
const modal = this.stack.pop();
127
if (!modal) return;
128
modal.backdrop.remove();
129
+ this._refreshStack();
130
if (this.stack.length === 0) {
131
document.body.style.overflow = '';
132
+ document.removeEventListener('keydown', this._onKeydown, true);
133
+ }
134
+ if (modal.previousFocus?.isConnected && typeof modal.previousFocus.focus === 'function') {
135
+ modal.previousFocus.focus({ preventScroll: true });
136
+ } else if (this.stack.length) {
137
+ this.stack[this.stack.length - 1].card.focus({ preventScroll: true });
138
}
139
if (modal.onClose) modal.onClose();
140
}
@@ -92,4 +159,4 @@ class ModalManager {
159
}
160
}
161
95
-export const Modal = new ModalManager();
\ No newline at end of file
162
+export const Modal = new ModalManager();