नए टूल्स जोड़े गए! देखें क्या नया है

होम/कंपोनेंट्स/Modal Dialog

Modal Dialog examples

नेटिव dialog एलिमेंट पर बना एक सुलभ (accessible) modal, जिसमें backdrop, focus trapping, और बंद करने के लिए Escape की सुविधा है।

ब्राउज़र के नेटिव dialog एलिमेंट पर बना एक modal dialog, जिससे focus trapping, backdrop रेंडरिंग, और Escape-to-close की सुविधा जावास्क्रिप्ट को मेंटेन करने के बजाय प्लेटफ़ॉर्म से मिलती है.

इसमें बंद करने के लिए backdrop क्लिक, सही 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.

Preview
"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.

Preview
"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.

Preview
"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.

Preview
"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.

Preview
"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 Modal Dialog?

Modal dialog एक ऐसा पैनल है जो पेज के ऊपर खुलता है और जब तक उस पर कार्रवाई नहीं हो जाती, तब तक उसके पीछे की हर चीज़ के साथ इंटरैक्शन को ब्लॉक कर देता है। ब्लॉक करने वाला हिस्सा ही इसका मुख्य उद्देश्य है: यह उन पलों के लिए है जहाँ बिना किसी निर्णय के आगे बढ़ना गलत होगा, जैसे किसी विलोपन (deletion) की पुष्टि करना या आवश्यक चरण पूरा करना। जो भी चीज़ रुकावट न डाले, उसे इसके बजाय एक पेज, पैनल या टोस्ट होना चाहिए।

Modals को सही ढंग से बनाना notoriously कठिन होता है, यही वजह है कि उनके लिए इतनी सारी लाइब्रेरी मौजूद हैं। सही होने वाली चीज़ों की सूची लंबी है: जब यह खुले तो फोकस dialog के भीतर जाना चाहिए और बंद होने पर वापस ट्रिगर पर लौटना चाहिए, टैबिंग को पीछे के पेज पर भागने के बजाय पैनल के भीतर चक्र चलाना चाहिए, पीछे का पेज स्क्रॉल नहीं होना चाहिए, Escape इसे बंद करना चाहिए, dialog को एक नाम और रोल के साथ घोषित किया जाना चाहिए, और इसके खुले रहने पर नीचे की हर चीज़ को असिस्टिव टेक्नोलॉजी से छिपाया जाना चाहिए।

नेटिव dialog एलिमेंट अब लगभग उन सभी को संभालता है। इस पर showModal कॉल करने से फोकस ट्रैप होता है, एक backdrop pseudo-element रेंडर होता है, बाकि दस्तावेज़ inert हो जाता है, और Escape क्लोज से जुड़ जाता है - ऐसा व्यवहार जिसके लिए पहले सैकड़ों लाइनों की सावधानीपूर्वक जावास्क्रिप्ट की आवश्यकता होती थी। 2022 से ब्राउज़र का समर्थन व्यापक रहा है, जिससे नए काम के लिए हाथ से बनाए गए संस्करण को उचित ठहराना मुश्किल हो जाता है.

प्लेटफ़ॉर्म आपको जो नहीं देता है वह बंद करने के लिए backdrop क्लिक है, क्योंकि backdrop एक ऐसा नोड होने के बजाय एक pseudo-element है जिस पर आप सुन सकते हैं। इसका समाधान dialog पर ही क्लिक सुनना और यह जांचना है कि क्लिक टारगेट dialog है या इसके अंदर की कोई चीज़ - एक क्लिक जो सीधे dialog एलिमेंट पर पड़ता है, वह पैनल के बाहर के पैडिंग क्षेत्र से आया है, जो उपयोगकर्ता के लिहाज से backdrop है।

Anatomy

Trigger
वह बटन जो dialog को खोलता है। बंद होने पर फोकस यहाँ अपने आप वापस आ जाता है, यही वजह है कि ट्रिगर हैंडलर वाले div के बजाय एक असली बटन होना चाहिए।
Dialog एलिमेंट
नेटिव dialog। चौड़ाई की बाधा और backdrop स्टाइलिंग को वहन करता है, जिसमें पैडिंग को शून्य पर सेट किया गया है ताकि इनर कंटेनर स्पेसिंग को नियंत्रित करे।
Backdrop
एक अलग एलिमेंट के बजाय backdrop pseudo-class के माध्यम से स्टाइल किया गया। यह केवल तभी मौजूद होता है जब dialog showModal के साथ खोला जाता है।
Title
aria-labelledby से जुड़ा हुआ dialog का सुलभ नाम। इसे श्रेणी लेबल के रूप में नहीं, बल्कि प्रश्न या कार्य के रूप में वाक्यांशबद्ध करें।
Description
aria-describedby से जुड़ा वैकल्पिक सपोर्टिंग टेक्स्ट। विनाशकारी (destructive) एक्शंस के लिए, परिणाम यहीं जाता है।
Action row
फुटर बटन, प्राथमिक या विनाशकारी एक्शन के साथ दाईं ओर संरेखित (right-aligned) होता है ताकि यह अंगूठे और आंख के अंत बिंदु के सबसे करीब हो।

How to Use

  1. 1

    मार्कअप कॉपी करें

    स्टेटिक पेज के लिए HTML वर्शन लें या कंपोनेंट ट्री के लिए React रैपर लें। दोनों नीचे एक ही नेटिव एलिमेंट का उपयोग करते हैं।

  2. 2

    इसे showModal के साथ खोलें

    show के बजाय showModal कॉल करें। सादा show मेथड बिना backdrop, बिना focus trapping, और बाकी पेज को inert किए बिना dialog को खोलता है।

  3. 3

    क्लोज पाथ्स (close paths) को जोड़ें

    Escape को ब्राउज़र द्वारा संभाला जाता है। बाहर निकलने के तीन तरीके प्रदान करने के लिए एक क्लोज बटन और बैड्रॉप क्लिक हैंडलर जोड़ें, और उन सभी को एक ही onClose के माध्यम से रूट करें।

  4. 4

    डायलॉग को लेबल दें

    aria-labelledby को टाइटल के id पर पॉइंट करें, और यदि कोई विवरण हो तो aria-describedby को उस विवरण पर पॉइंट करें। नाम के बिना डायलॉग एक अनलेबल्ड डायलॉग के रूप में अनाउंस होता है।

  5. 5

    पैनल को स्टाइल करें

    पैडिंग को डायलॉग पर सीधे लगाने के बजाय एक इनर कंटेनर पर सेट करें, ताकि बैड्रॉप क्लिक चेक भरोसेमंद बना रहे - डायलॉग पर पैडिंग होने से वह उसके अपने क्लिक एरिया का हिस्सा बन जाती है।

Features

नेटिव फोकस ट्रैपिंग

फोकस ओपन होने पर डायलॉग में जाता है और उसी के भीतर घूमता रहता है, जिसे किसी keydown listener को सही रखने की बजाय ब्राउज़र द्वारा संभाला जाता है।

फोकस ट्रिगर पर वापस लौटता है

क्लोज होने पर, फोकस स्वचालित रूप से उस एलिमेंट पर वापस चला जाता है जिसने डायलॉग को ओपन किया था, जिससे कीबोर्ड यूजर वहीं से शुरू कर सके जहाँ उन्होंने छोड़ा था।

बिना अतिरिक्त एलिमेंट के बैड्रॉप

बैड्रॉप एक साइको-एलिमेंट है जिसे बैड्रॉप वेरिएंट के साथ स्टाइल किया जाता है, इसलिए इसमें पोजीशन, स्टैक या सिंक में रखने के लिए कोई अलग ओवरले div नहीं होता।

Escape इसे बंद करता है

एलिमेंट में पहले से ही मौजूद है। React रैपर परिणामी क्लोज इवेंट को सुनता है ताकि जब ब्राउज़र अपने आप डायलॉग बंद करे तो open प्रॉप सिंक में रहे।

पेज का बाकी हिस्सा inert हो जाता है

मोडल डायलॉग के पीछे का कंटेंट फोकस करने योग्य नहीं होता और असिस्टिव टेक्नोलॉजी से छिपा होता है, बिना किसी चीज़ पर हाथ से aria-hidden सेट किए।

बंद करने के लिए बैड्रॉप क्लिक

एक क्लिक टारगेट चेक पैडिंग एरिया को पैनल के कंटेंट से अलग करता है, जिससे बिना किसी अतिरिक्त ओवरले नोड के लोगों द्वारा अपेक्षित डिसमिस व्यवहार मिलता है।

Common Uses

एक डिस्ट्रक्टिव एक्शन की पुष्टि करना

खाता, प्रोजेक्ट या फाइल डिलीट करना - ऐसे मामले जहाँ गलती से हुआ क्लिक ऐसे परिणाम लाता है जिन्हें टोस्ट से पूर्ववत (undo) नहीं किया जा सकता।

छोटे फॉर्म्स

किसी आइटम का नाम बदलना, किसी टीम के साथी को आमंत्रित करना, या सिंगल वैल्यू दर्ज करना, जहाँ किसी यूजर को अलग पेज पर भेजने से उनका स्थान खो जाएगा।

आवश्यक ऑनबोर्डिंग स्टेप्स

एक ऐसा स्टेप जिसे आगे बढ़ने से पहले वास्तव में पूरा किया जाना चाहिए, जैसे अपडेट की गई शर्तों को स्वीकार करना या वर्कस्पेस चुनना।

मीडिया और डिटेल व्यूज

मौजूदा पेज के ऊपर एक बड़ी इमेज या रिकॉर्ड की डिटेल्स दिखाना, जहाँ उसी स्क्रॉल पोजीशन पर वापस लौटना मायने रखता है।

Accessibility

Customization

Props

PropTypeDefaultDescription
open *boolean-dialog दिख रहा है या नहीं। यह कंपोनेंट इसे सशर्त रेंडर करने के बजाय नेटिव showModal और close मेथड्स के साथ सिंक करता है।
onClose *() => void-जब dialog किसी भी तरीके से बंद होता है - Escape की, backdrop क्लिक, या क्लोज बटन से - तब यह कॉल होता है। open को वापस false सेट करने के लिए इसका इस्तेमाल करें।
title *string-aria-labelledby के माध्यम से जुड़ा हुआ dialog का सुलभ नाम। इसे कभी न छोड़ें; बिना नाम वाले dialog को केवल एक dialog के रूप में घोषित किया जाता है।
descriptionstring-वैकल्पिक सपोर्टिंग टेक्स्ट, जो aria-describedby के माध्यम से जुड़ा होता है ताकि इसे छोड़े जाने के बजाय शीर्षक के बाद पढ़ा जाए।
childrenReactNode-dialog की बॉडी और एक्शंस। एक्शंस को अंत में रखें ताकि उन तक पहुँचने से पहले टैब ऑर्डर कंटेंट से होकर गुजरे।

Why Use These Modal Dialogs?

यह रीइम्प्लीमेंट करने के बजाय प्लेटफॉर्म का उपयोग करता है। फोकस ट्रैपिंग, इनर्ट बैकग्राउंड, और Escape हैंडलिंग वे चीजें हैं जिन्हें हाथ से बनाए गए मॉडल्स गलत कर देते हैं, और यहाँ वे आपके बजाय ब्राउज़र की ज़िम्मेदारी हैं।

इसका मतलब यह भी है कि कोई डिपेंडेंसी नहीं है। एक हेडलेस डायलॉग लाइब्रेरी एक उचित विकल्प है, लेकिन एक कन्फर्मेशन प्रॉम्प्ट के लिए इसका मतलब है एक पैकेज शिप करना, इसके वर्जन को ट्रैक करना, और उस व्यवहार को पाने برای इसके API को सीखना जो एलिमेंट पहले से प्रदान करता है।

React रैपर इसे कंडीशनल रूप से रेंडर करने के बजाय एलिमेंट की अपनी स्टेट से सिंक होता है, इसलिए Escape द्वारा बंद किया गया डायलॉग भी onClose को ट्रिगर करता है और open प्रॉप स्क्रीन पर मौजूद चीज़ों से सिंक से बाहर नहीं होता है।

Tips

Frequently Asked Questions

Related Components