الإغلاق بمفتاح Escape واستعادة التركيز
وفّروا طريقة خروج موثوقة تعيد المستخدم إلى موضعه
الإغلاق بمفتاح Escape واستعادة التركيز درس مجاني في Web Accessibility Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Web Accessibility Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Web Accessibility Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Every Modal Needs an Exit
A modal that you cannot leave is a trap. Users expect a fast, reliable way to close it without hunting for a tiny button.
Escape Is the Universal Key
The Escape key is the agreed shortcut to dismiss a dialog. Keyboard and screen reader users reach for it first, so always support it.
Native dialog Handles Escape
A native dialog opened with showModal() closes on Escape automatically, firing a cancel event you can listen to if needed.
dialog.addEventListener('cancel', onClose);Wiring Escape Yourself
For a custom modal, listen for the keydown event and close when the key is Escape. It is a few lines, but never skip it.
if (e.key === 'Escape') closeModal();Remember Where Focus Was
Before you open the modal, save the element that had focus. document.activeElement is the trigger you will return to later.
const opener = document.activeElement;Restore Focus on Close
When the modal closes, send focus back to that saved trigger. This restore focus step is what makes closing feel seamless.
closeModal();
opener.focus();Why Restoring Matters
Without restore, focus jumps to the top of the page. The user loses their place and must tab all the way back. Frustrating and avoidable. 😕
A Visible Close Button Too
Escape is not enough on its own. Offer a clearly labeled close button so mouse and touch users have an obvious exit as well.
<button aria-label="Close dialog">×</button>Clicking the Backdrop
Many modals also close when you click the dimmed backdrop. It is a nice extra, but it must never replace Escape or the button.
Be Careful With Destructive Actions
For dialogs that confirm something risky, you may suppress quick dismissal so a stray Escape does not skip an important choice.
Test the Full Loop
Open with the keyboard, press Escape, and confirm focus lands back on the exact trigger you started from. That round trip is the goal. ✅
Quick Check
A user opens a modal from a button and presses Escape. Where should focus land?
Recap: Close Cleanly
You learned to support Escape, offer a visible close button, and restore focus to the opener so closing never strands the user. 🎯
الأسئلة الشائعة
هل درس «الإغلاق بمفتاح Escape واستعادة التركيز» مجاني؟
نعم — نص درس «الإغلاق بمفتاح Escape واستعادة التركيز» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Web Accessibility Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Web Accessibility Academy 4 دروس في المجموع.
ماذا ستتعلم في «الإغلاق بمفتاح Escape واستعادة التركيز»؟
وفّروا طريقة خروج موثوقة تعيد المستخدم إلى موضعه تتمرن على Web Accessibility Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Web Accessibility Academy؟
لا تُشترط خبرة سابقة. Web Accessibility Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «الإغلاق بمفتاح Escape واستعادة التركيز»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Web Accessibility Academy هذا؟
نعم. كل درس في Web Accessibility Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- أدوار مربعات الحوار وعنصر dialog الأصلي
- حصر التركيز داخل النافذة المشروطة
- الإغلاق بمفتاح Escape واستعادة التركيز
- الخلفية غير النشطة وaria-modal