نافذة حوارية منبثقة examples
نافذة منبثقة سهلة الوصول مبنية على عنصر dialog الأصلي، بخلفية وحصر للتركيز وEscape للإغلاق.
نافذة حوارية منبثقة مبنية على عنصر dialog الأصلي في المتصفح، فيأتي حصر التركيز وعرض الخلفية والإغلاق بـ Escape من المنصة لا من JavaScript عليك صيانتها.
تتضمن النقر على الخلفية للإغلاق، ووسم ARIA صحيحًا، وغلاف React يزامن خاصية open مع دوال العنصر نفسه.
Confirmation modal dialog
Two actions with the destructive one in red and placed last. The button names the action, so it reads correctly on its own.
"use client";
import { useRef } from "react";
export default function ConfirmDialog() {
const ref = useRef<HTMLDialogElement>(null);
return (
<>
<button
type="button"
onClick={() => ref.current?.showModal()}
className="rounded-md bg-gray-900 px-4 py-2 text-sm font-medium text-white hover:bg-gray-700"
>
Delete project
</button>
<dialog
ref={ref}
onClick={(e) => {
if (e.target === ref.current) ref.current?.close();
}}
aria-labelledby="dialog-title"
aria-describedby="dialog-description"
className="m-auto w-full max-w-md rounded-xl p-0 backdrop:bg-gray-900/50"
>
<div className="p-6">
<h2 id="dialog-title" className="text-lg font-semibold text-gray-900">Delete this project?</h2>
<p id="dialog-description" className="mt-2 text-sm text-gray-600">
This removes the project and all of its files. This action cannot be undone.
</p>
<div className="mt-6 flex justify-end gap-3">
<button type="button" onClick={() => ref.current?.close()} className="rounded-md border border-gray-300 px-4 py-2 text-sm font-medium text-gray-900 hover:bg-gray-50">Cancel</button>
<button type="button" onClick={() => ref.current?.close()} className="rounded-md bg-red-600 px-4 py-2 text-sm font-medium text-white hover:bg-red-500">Delete project</button>
</div>
</div>
</dialog>
</>
);
}Modal dialog with a form
A single-field form using method=dialog, so submitting closes the dialog and reports the clicked button's value without extra wiring.
"use client";
import { useRef } from "react";
export default function RenameDialog() {
const ref = useRef<HTMLDialogElement>(null);
return (
<>
<button
type="button"
onClick={() => ref.current?.showModal()}
className="rounded-md bg-gray-900 px-4 py-2 text-sm font-medium text-white hover:bg-gray-700"
>
Rename project
</button>
<dialog
ref={ref}
onClick={(e) => {
if (e.target === ref.current) ref.current?.close();
}}
aria-labelledby="dialog-title"
aria-describedby="dialog-description"
className="m-auto w-full max-w-md rounded-xl p-0 backdrop:bg-gray-900/50"
>
<form method="dialog" className="p-6">
<h2 id="dialog-title" className="text-lg font-semibold text-gray-900">Rename project</h2>
<p id="dialog-description" className="mt-2 text-sm text-gray-600">Choose a name your team will recognize.</p>
<label htmlFor="project-name" className="mt-4 block text-sm font-medium text-gray-900">Project name</label>
<input id="project-name" name="name" type="text" required autoComplete="off" className="mt-1.5 block w-full rounded-md border border-gray-300 px-3 py-2 text-sm text-gray-900 focus:border-gray-900 focus:outline-none focus:ring-1 focus:ring-gray-900" />
<div className="mt-6 flex justify-end gap-3">
<button type="button" onClick={() => ref.current?.close()} className="rounded-md border border-gray-300 px-4 py-2 text-sm font-medium text-gray-900 hover:bg-gray-50">Cancel</button>
<button type="submit" className="rounded-md bg-gray-900 px-4 py-2 text-sm font-medium text-white hover:bg-gray-700">Save name</button>
</div>
</form>
</dialog>
</>
);
}Bottom sheet modal on mobile
Pinned to the bottom edge with a drag-handle cue below the sm breakpoint, and a centered dialog above it.
"use client";
import { useRef } from "react";
export default function ShareSheet() {
const ref = useRef<HTMLDialogElement>(null);
return (
<>
<button
type="button"
onClick={() => ref.current?.showModal()}
className="rounded-md bg-gray-900 px-4 py-2 text-sm font-medium text-white hover:bg-gray-700"
>
Share
</button>
<dialog
ref={ref}
onClick={(e) => {
if (e.target === ref.current) ref.current?.close();
}}
aria-labelledby="dialog-title"
aria-describedby="dialog-description"
className="m-0 mt-auto w-full max-w-none rounded-b-none rounded-t-2xl p-0 backdrop:bg-gray-900/50 sm:m-auto sm:max-w-md sm:rounded-xl"
>
<div className="p-6">
<div className="mx-auto mb-4 h-1.5 w-10 rounded-full bg-gray-300 sm:hidden" aria-hidden="true"></div>
<h2 id="dialog-title" className="text-lg font-semibold text-gray-900">Share this page</h2>
<p id="dialog-description" className="mt-1 text-sm text-gray-600">Anyone with the link can view it.</p>
<ul className="mt-4 space-y-1">
<li><button type="button" onClick={() => ref.current?.close()} className="block w-full rounded-md px-3 py-2.5 text-left text-sm text-gray-900 hover:bg-gray-100">Copy link</button></li>
<li><button type="button" onClick={() => ref.current?.close()} className="block w-full rounded-md px-3 py-2.5 text-left text-sm text-gray-900 hover:bg-gray-100">Send by email</button></li>
<li><button type="button" onClick={() => ref.current?.close()} className="block w-full rounded-md px-3 py-2.5 text-left text-sm text-gray-900 hover:bg-gray-100">Open in a new tab</button></li>
</ul>
<button type="button" onClick={() => ref.current?.close()} className="mt-4 w-full rounded-md border border-gray-300 px-4 py-2 text-sm font-medium text-gray-900 hover:bg-gray-50">Cancel</button>
</div>
</dialog>
</>
);
}Side drawer modal
A full-height panel on the right for filters or settings. Still a native modal dialog, so focus trapping and Escape work the same way.
"use client";
import { useRef } from "react";
export default function FilterDrawer() {
const ref = useRef<HTMLDialogElement>(null);
return (
<>
<button
type="button"
onClick={() => ref.current?.showModal()}
className="rounded-md bg-gray-900 px-4 py-2 text-sm font-medium text-white hover:bg-gray-700"
>
Open filters
</button>
<dialog
ref={ref}
onClick={(e) => {
if (e.target === ref.current) ref.current?.close();
}}
aria-labelledby="dialog-title"
className="m-0 ml-auto h-screen max-h-none w-full max-w-sm rounded-none p-0 backdrop:bg-gray-900/50"
>
<div className="flex h-full flex-col">
<div className="flex items-center justify-between border-b border-gray-200 px-6 py-4">
<h2 id="dialog-title" className="text-lg font-semibold text-gray-900">Filters</h2>
<button type="button" onClick={() => ref.current?.close()} aria-label="Close filters" className="rounded-md p-2 text-gray-500 hover:bg-gray-100">
<svg className="h-5 w-5" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M6 18 18 6M6 6l12 12" /></svg>
</button>
</div>
<div className="flex-1 overflow-y-auto px-6 py-5">
<fieldset>
<legend className="text-sm font-medium text-gray-900">Category</legend>
<div className="mt-3 space-y-2 text-sm text-gray-700">
<label className="flex items-center gap-2"><input type="checkbox" className="h-4 w-4 rounded border-gray-300" /> Design</label>
<label className="flex items-center gap-2"><input type="checkbox" className="h-4 w-4 rounded border-gray-300" /> Engineering</label>
<label className="flex items-center gap-2"><input type="checkbox" className="h-4 w-4 rounded border-gray-300" /> Marketing</label>
</div>
</fieldset>
</div>
<div className="flex gap-3 border-t border-gray-200 px-6 py-4">
<button type="button" onClick={() => ref.current?.close()} className="flex-1 rounded-md border border-gray-300 px-4 py-2 text-sm font-medium text-gray-900 hover:bg-gray-50">Clear</button>
<button type="button" onClick={() => ref.current?.close()} className="flex-1 rounded-md bg-gray-900 px-4 py-2 text-sm font-medium text-white hover:bg-gray-700">Show results</button>
</div>
</div>
</dialog>
</>
);
}Scrollable modal with sticky header and footer
Long content scrolls inside the panel while the title and actions stay visible. Uses open:flex so the closed dialog stays hidden.
"use client";
import { useRef } from "react";
export default function TermsDialog() {
const ref = useRef<HTMLDialogElement>(null);
return (
<>
<button
type="button"
onClick={() => ref.current?.showModal()}
className="rounded-md bg-gray-900 px-4 py-2 text-sm font-medium text-white hover:bg-gray-700"
>
Review terms
</button>
<dialog
ref={ref}
onClick={(e) => {
if (e.target === ref.current) ref.current?.close();
}}
aria-labelledby="dialog-title"
className="m-auto max-h-[80vh] w-full max-w-lg flex-col rounded-xl p-0 backdrop:bg-gray-900/50 open:flex"
>
<div className="flex items-center justify-between border-b border-gray-200 px-6 py-4">
<h2 id="dialog-title" className="text-lg font-semibold text-gray-900">Terms of service</h2>
<button type="button" onClick={() => ref.current?.close()} aria-label="Close terms" className="rounded-md p-2 text-gray-500 hover:bg-gray-100">
<svg className="h-5 w-5" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M6 18 18 6M6 6l12 12" /></svg>
</button>
</div>
<div className="min-h-0 flex-1 space-y-3 overflow-y-auto px-6 py-4 text-sm text-gray-600">
<p>Section 1. These terms describe how the service may be used, what you can expect from us, and what we expect from you. Read each section before continuing.</p>
<p>Section 2. These terms describe how the service may be used, what you can expect from us, and what we expect from you. Read each section before continuing.</p>
<p>Section 3. These terms describe how the service may be used, what you can expect from us, and what we expect from you. Read each section before continuing.</p>
<p>Section 4. These terms describe how the service may be used, what you can expect from us, and what we expect from you. Read each section before continuing.</p>
<p>Section 5. These terms describe how the service may be used, what you can expect from us, and what we expect from you. Read each section before continuing.</p>
<p>Section 6. These terms describe how the service may be used, what you can expect from us, and what we expect from you. Read each section before continuing.</p>
</div>
<div className="flex justify-end gap-3 border-t border-gray-200 px-6 py-4">
<button type="button" onClick={() => ref.current?.close()} className="rounded-md border border-gray-300 px-4 py-2 text-sm font-medium text-gray-900 hover:bg-gray-50">Decline</button>
<button type="button" onClick={() => ref.current?.close()} className="rounded-md bg-gray-900 px-4 py-2 text-sm font-medium text-white hover:bg-gray-700">Accept</button>
</div>
</dialog>
</>
);
}What is a نافذة حوارية منبثقة?
النافذة الحوارية المنبثقة لوحة تُفتح فوق الصفحة وتمنع التفاعل مع كل ما خلفها حتى يُتعامل معها. المنع هو جوهر الفكرة: فهي للحظات التي يكون فيها المتابعة دون قرار خطأً، كتأكيد حذف أو إكمال خطوة مطلوبة. وكل ما لا يحتاج إلى مقاطعة ينبغي أن يكون صفحة أو لوحة أو إشعارًا منبثقًا بدلها.
النوافذ المنبثقة صعبة البناء الصحيح بشكل معروف، ولهذا توجد مكتبات كثيرة لها. فقائمة الأشياء التي يجب أن تكون صحيحة طويلة: يجب أن ينتقل التركيز إلى داخل النافذة عند فتحها وأن يعود إلى المحفّز عند إغلاقها، وأن يدور التنقل بمفتاح Tab داخل اللوحة بدل الهروب إلى الصفحة خلفها، وألا تتمرر الصفحة خلفها، وأن يغلقها Escape، وأن تُعلن النافذة باسم ودور، وأن يُخفى كل ما تحتها عن التقنيات المساعدة أثناء فتحها.
يتولى عنصر dialog الأصلي الآن معظم ذلك. فاستدعاء showModal عليه يحصر التركيز ويعرض عنصر الخلفية الزائف ويجعل بقية المستند خاملًا ويربط Escape بالإغلاق - سلوك كان يتطلب عدة مئات من أسطر JavaScript المتقنة. ودعم المتصفحات واسع منذ 2022، مما يجعل النسخة المبنية يدويًا صعبة التبرير للأعمال الجديدة.
ما لا تمنحك إياه المنصة هو النقر على الخلفية للإغلاق، لأن الخلفية عنصر زائف لا عقدة يمكنك الاستماع إليها. والحل البديل هو الاستماع إلى النقرات على النافذة نفسها وفحص ما إذا كان هدف النقرة هو النافذة لا شيء بداخلها - فالنقرة التي تقع على عنصر dialog مباشرة جاءت من منطقة الحشوة خارج اللوحة، وهي الخلفية في نظر المستخدم.
Anatomy
- المحفّز
- الزر الذي يفتح النافذة. يعود التركيز إليه تلقائيًا عند الإغلاق، ولهذا ينبغي أن يكون المحفّز زرًا حقيقيًا لا div بمعالج.
- عنصر dialog
- عنصر dialog الأصلي. يحمل قيد العرض وتنسيق الخلفية، مع حشوة صفرية ليتحكم الحاوي الداخلي في المسافات.
- الخلفية
- تُنسَّق عبر الفئة الزائفة backdrop لا عبر عنصر منفصل. وتوجد فقط أثناء فتح النافذة بـ showModal.
- العنوان
- الاسم المتاح للنافذة، موصول بـ aria-labelledby. صغه كسؤال أو كمهمة لا كتسمية فئة.
- الوصف
- نص داعم اختياري موصول بـ aria-describedby. وللإجراءات المدمّرة هنا تُوضع العواقب.
- صف الإجراءات
- أزرار التذييل، محاذاة لليمين مع الإجراء الأساسي أو المدمّر أخيرًا ليكون الأقرب إلى الإبهام وإلى نقطة نهاية نظر العين.
How to Use
- 1
انسخ الشيفرة
خذ نسخة HTML لصفحة ثابتة أو غلاف React لشجرة مكوّنات. كلاهما يستخدم العنصر الأصلي نفسه تحتهما.
- 2
افتحها بـ showModal
استدعِ showModal لا show. فدالة show العادية تفتح النافذة دون خلفية ودون حصر للتركيز ودون جعل بقية الصفحة خاملة.
- 3
اربط مسارات الإغلاق
يتولى المتصفح Escape. أضف زر إغلاق ومعالج النقر على الخلفية ليكون هناك ثلاث طرق للخروج، ومرّرها جميعًا عبر onClose نفسها.
- 4
سمِّ النافذة
وجّه aria-labelledby إلى معرّف العنوان، وaria-describedby إلى الوصف إن وُجد. فبلا اسم تُعلن النافذة كنافذة بلا تسمية.
- 5
نسّق اللوحة
اضبط الحشوة على حاوية داخلية لا على النافذة نفسها، ليبقى فحص النقر على الخلفية موثوقًا - فالحشوة على النافذة تصير جزءًا من منطقة نقرها هي.
Features
حصر تركيز أصلي
ينتقل التركيز إلى داخل النافذة عند الفتح ويدور فيها، ويتولاه المتصفح بدل مستمع keydown عليك إبقاؤه صحيحًا.
يعود التركيز إلى المحفّز
عند الإغلاق يعود التركيز تلقائيًا إلى العنصر الذي فتح النافذة، فيستأنف مستخدم لوحة المفاتيح من حيث توقف.
خلفية دون عنصر إضافي
الخلفية عنصر زائف يُنسَّق بمتغير backdrop، فلا يوجد div تراكب منفصل لتموضعه أو ترتيبه أو إبقائه متزامنًا.
Escape يغلقها
مدمج في العنصر. يستمع غلاف React إلى حدث close الناتج لتبقى خاصية open متزامنة حين يغلق المتصفح النافذة من تلقاء نفسه.
بقية الصفحة تصبح خاملة
المحتوى خلف النافذة المنبثقة لا يمكن تركيزه ومخفي عن التقنيات المساعدة، دون ضبط aria-hidden يدويًا على أي شيء.
النقر على الخلفية للإغلاق
يميّز فحص هدف النقر بين منطقة الحشوة ومحتويات اللوحة، فيمنح سلوك الإغلاق الذي يتوقعه الناس دون عقدة تراكب إضافية.
Common Uses
تأكيد إجراء مدمّر
حذف حساب أو مشروع أو ملف - حالات يكون لنقرة عرضية فيها عواقب لا يمكن التراجع عنها بإشعار منبثق.
نماذج قصيرة
إعادة تسمية عنصر أو دعوة زميل أو إدخال قيمة واحدة، حيث يؤدي إرسال الشخص إلى صفحة منفصلة إلى فقدان مكانه.
خطوات تهيئة مطلوبة
خطوة يجب فعلًا إكمالها قبل المتابعة، مثل قبول الشروط المحدّثة أو اختيار مساحة عمل.
عرض الوسائط والتفاصيل
إظهار صورة أكبر أو تفاصيل سجل فوق الصفحة الحالية، حيث تهم العودة إلى موضع التمرير نفسه.
Accessibility
- يحمل عنصر dialog دور dialog ضمنيًا، وعند فتحه بـ showModal حالة modal ضمنية، فلا حاجة إلى إضافة role أو aria-modal يدويًا.
- يشير aria-labelledby إلى العنوان لتُعلن النافذة باسم لا كنافذة عامة.
- يشير aria-describedby إلى النص الداعم لتُقرأ عواقب الإجراء المدمّر بدل تخطيها.
- ينتقل التركيز إلى داخل النافذة عند الفتح ويعود إلى المحفّز عند الإغلاق، ويتولى المتصفح الأمرين.
- كل ما خارج النافذة خامل أثناء فتحها، فلا يمكن الوصول إلى الصفحة خلفها بـ Tab ولا تقرؤها قارئات الشاشة.
- يغلق Escape النافذة افتراضيًا. لا تمنعه؛ فإن كانت نافذة يجب ألا تُغلق فالمشكلة أن التفاعل لا ينبغي أن يكون نافذة منبثقة.
- يحتاج زر الإغلاق إلى تسمية مرئية أو aria-label. فزر إغلاق بأيقونة فقط دون اسم متاح من أكثر عيوب النوافذ شيوعًا.
Customization
- نسّق الخلفية بمتغير backdrop - backdrop:bg-gray-900/50 للتعتيم، إضافة إلى backdrop:backdrop-blur-sm إن أردت تنعيم الصفحة خلفها.
- أبقِ الحشوة على حاوية داخلية لا على عنصر dialog، وإلا صارت الحشوة جزءًا من منطقة نقر النافذة وتوقف الإغلاق بالنقر على الخلفية عن التصرف بشكل متوقع.
- اجعلها لوحة سفلية على الشاشات الصغيرة بتثبيتها على الحافة السفلية وإعادة التوسيط فوق sm.
- حرّك الدخول بقاعدة starting-style على حالة الفتح. أبقِها دون 200ms؛ فالنافذة البطيئة تبدو غير مستجيبة لأن المحتوى لا يمكن قراءته حتى يستقر.
- لنافذة أعرض غيّر max-width. وبعد max-w-2xl فكّر في ما إذا كان ينبغي أن يكون المحتوى صفحة بدلها - فالنوافذ الطويلة تتمرر بشكل سيئ وتخفي إجراءاتها.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| open * | boolean | - | ما إذا كانت النافذة ظاهرة. يزامن المكوّن هذه القيمة مع الدالتين الأصليتين showModal وclose بدل العرض الشرطي للعنصر. |
| onClose * | () => void | - | تُستدعى حين تُغلق النافذة بأي طريقة - مفتاح Escape أو النقر على الخلفية أو زر إغلاق. استخدمها لإعادة open إلى false. |
| title * | string | - | الاسم المتاح للنافذة، موصول عبر aria-labelledby. لا تحذفه أبدًا؛ فالنافذة بلا اسم تُعلن كنافذة فقط. |
| description | string | - | نص داعم اختياري، موصول عبر aria-describedby ليُقرأ بعد العنوان بدل تخطيه. |
| children | ReactNode | - | جسم النافذة وإجراءاتها. ضع الإجراءات في النهاية ليتحرك ترتيب Tab عبر المحتوى قبل الوصول إليها. |
Why Use These نافذة حوارية منبثقةs?
يستخدم المنصة بدل إعادة تنفيذها. فحصر التركيز والخلفية الخاملة والتعامل مع Escape هي الأجزاء التي تخطئ فيها النوافذ المبنية يدويًا، وهنا هي مسؤولية المتصفح لا مسؤوليتك.
وهذا يعني أيضًا لا اعتماديات. مكتبة حوار بلا رأس خيار معقول، لكنها لمطالبة تأكيد تعني شحن حزمة وتتبع إصدارها وتعلّم واجهتها للحصول على سلوك يوفره العنصر أصلًا.
يزامن غلاف React حالة العنصر نفسه بدل عرضه شرطيًا، فالنافذة التي يغلقها Escape تُطلق onClose أيضًا ولا تنحرف خاصية open عن ما هو على الشاشة.
Tips
- استخدم showModal دائمًا ولا تستخدم show أبدًا. الفرق هو مجموعة سلوكيات النافذة المنبثقة كلها - فمع show تحصل على لوحة تصادف أنها في الأعلى ولا شيء غير ذلك.
- اضبط الحشوة على الحاوية الداخلية لا على النافذة، وإلا أُطلق فحص النقر على الخلفية على نقرات تقع فيما يبدو جزءًا من اللوحة.
- سمِّ الإجراء في الزر. فـ حذف المشروع مقروء بمعزل عن غيره؛ أما تأكيد فيتطلب أن تكون قد قرأت العنوان، وقد لا يكون في تركيز مستخدم قارئ الشاشة.
- لا تُعشّش النوافذ المنبثقة. فإن احتاجت نافذة إلى فتح نافذة أخرى فالتدفق يحتاج إعادة تفكير لا طبقة ثانية.
- تمرير الجسم مقفل أصلًا بـ showModal في المتصفحات الحالية، فإضافة فئة overflow-hidden فوقه قد تسبب قفزة في التخطيط إذ يختفي شريط التمرير مرتين.
- اختبر بلوحة المفاتيح فقط: افتح، وتنقل عبر كل عنصر تحكم، واضغط Escape. فإن انتهى التركيز في أي مكان غير المحفّز فهناك شيء يعرض النافذة شرطيًا بدل مزامنتها.