เพิ่มเครื่องมือใหม่แล้ว! ดูสิ่งที่มาใหม่

หน้าแรก/คอมโพเนนต์/Modal Dialog

Modal Dialog examples

Modal ที่ใช้งานการเข้าถึงได้ (accessible) ซึ่งสร้างขึ้นบน native dialog element พร้อมฉากหลัง (backdrop), การจำกัดโฟกัส (focus trapping), และการกดปุ่ม Escape เพื่อปิด

Modal dialog ที่สร้างขึ้นบน native dialog element ของเบราว์เซอร์ ดังนั้น การจำกัดโฟกัส, การเรนเดอร์ฉากหลัง, และการกด Escape เพื่อปิด จึงมาจากแพลตฟอร์มโดยตรงแทนที่จะต้องเขียน JavaScript คอยดูแลรักษาเอง

รวมถึงฟังก์ชันคลิกฉากหลังเพื่อปิด, การใส่ป้ายกำกับ ARIA ที่ถูกต้อง, และ React wrapper ที่ซิงค์ prop open กับ methods ของตัว element เอง

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 คือ panel ที่เปิดขึ้นมาทับหน้าเว็บและบล็อกการโต้ตอบกับทุกสิ่งที่อยู่เบื้องหลังจนกว่าจะจัดการเสร็จสิ้น การบล็อกนี้คือจุดประสงค์หลัก: มีไว้สำหรับช่วงเวลาที่การดำเนินการต่อโดยไม่มีการตัดสินใจถือเป็นเรื่องที่ไม่ถูกต้อง เช่น การยืนยันการลบหรือการทำขั้นตอนที่จำเป็นให้เสร็จสิ้น สิ่งใดก็ตามที่ไม่จำเป็นต้องขัดจังหวะควรเป็นหน้าเพจ, panel, หรือ toast แทน

Modal ขึ้นชื่อว่าสร้างให้ถูกต้องได้ยาก ซึ่งเป็นเหตุผลว่าทำไมจึงมีไลบรารีมากมายสำหรับคอมโพเนนต์นี้ รายการสิ่งที่ต้องทำให้ถูกต้องนั้นยาวเหยียด: โฟกัสต้องย้ายเข้าไปใน dialog เมื่อเปิดและกลับมาที่ trigger เมื่อปิด, การกด Tab ต้องวนอยู่ภายใน panel แทนที่จะหลุดออกไปหน้าเว็บเบื้องหลัง, หน้าเว็บเบื้องหลังห้ามเลื่อน (scroll), ปุ่ม Escape ต้องปิด dialog ได้, dialog ต้องถูกประกาศชื่อและ role, และทุกสิ่งที่อยู่ข้างล่างต้องถูกซ่อนจาก assistive technology ในขณะที่เปิดอยู่

Native dialog element ในปัจจุบันจัดการเรื่องเหล่านี้เกือบทั้งหมดแล้ว การเรียก showModal บน element จะทำการจำกัดโฟกัส, เรนเดอร์ backdrop pseudo-element, ทำให้เอกสารส่วนที่เหลือกลายเป็น inert (ไร้การตอบสนอง), และเชื่อมโยงปุ่ม Escape ให้ปิด dialog - พฤติกรรมที่เคยต้องใช้โค้ด JavaScript ที่ระมัดระวังหลายร้อยบรรทัด การสนับสนุนบนเบราว์เซอร์มีอย่างกว้างขวางตั้งแต่ปี 2022 ซึ่งทำให้การสร้างเวอร์ชันทำมือเองนั้นยากที่จะหาเหตุผลมารองรับสำหรับงานใหม่ๆ

สิ่งที่คุณจะไม่ได้จากแพลตฟอร์มคือการคลิกฉากหลังเพื่อปิด เนื่องจากฉากหลังเป็น pseudo-element ไม่ใช่ node ที่คุณจะดักฟัง event ได้ วิธีแก้ปัญหาคือการดักฟังการคลิกที่ตัว dialog เองและตรวจสอบว่าเป้าหมายของการคลิกคือตัว dialog หรือไม่ มากกว่าที่จะเป็นบางสิ่งที่อยู่ข้างใน - การคลิกที่ตกลงบนตัว dialog element โดยตรงมาจากพื้นที่ padding นอก panel ซึ่งสำหรับผู้ใช้แล้วก็คือฉากหลังนั่นเอง

Anatomy

Trigger
ปุ่มที่เปิด dialog โฟกัสจะกลับมาที่นี่โดยอัตโนมัติเมื่อปิด ซึ่งเป็นเหตุผลว่าทำไม trigger จึงควรเป็นปุ่มจริงๆ แทนที่จะเป็น div ที่ใส่ handler
Dialog element
Native dialog รองรับการจำกัดความกว้างและสไตล์ฉากหลัง โดยตั้งค่า padding เป็นศูนย์เพื่อให้ container ด้านในควบคุมระยะห่าง (spacing) แทน
Backdrop
จัดสไตล์ผ่าน backdrop pseudo-class แทนที่จะใช้ element แยกต่างหาก ฉากหลังนี้จะมีอยู่เฉพาะในขณะที่ dialog เปิดด้วย showModal เท่านั้น
Title
ชื่อของ dialog ที่รองรับการเข้าถึงได้ เชื่อมต่อด้วย aria-labelledby เขียนในรูปของคำถามหรือภารกิจ ไม่ใช่ในฐานะป้ายกำกับหมวดหมู่
Description
ข้อความอธิบายเพิ่มเติม (ไม่บังคับ) เชื่อมต่อด้วย aria-describedby สำหรับการกระทำที่มีความเสี่ยงหรือทำลายล้าง (destructive actions) นี่คือจุดที่ควรใส่ผลลัพธ์ที่จะตามมา
Action row
ปุ่มที่อยู่ส่วนท้าย จัดชิดขวาโดยให้ปุ่มการกระทำหลักหรือปุ่มการกระทำที่มีความเสี่ยงอยู่ท้ายสุด เพื่อให้อยู่ใกล้กับนิ้วโป้งและจุดสิ้นสุดของสายตามากที่สุด

How to Use

  1. 1

    คัดลอกมาร์กอัป

    นำเวอร์ชัน HTML ไปใช้สำหรับหน้าเว็บแบบ static หรือ React wrapper สำหรับคอมโพเนนต์ทรี ทั้งสองแบบใช้ native element ตัวเดียวกันเบื้องหลัง

  2. 2

    เปิดใช้งานด้วย showModal

    เรียกใช้ showModal แทนที่จะเป็น show เมธอด show แบบธรรมดาจะเปิด dialog โดยไม่มีฉากหลัง, ไม่มีจำกัดโฟกัส, และไม่ทำให้หน้าเว็บส่วนที่เหลือกลายเป็น inert

  3. 3

    เชื่อมโยงเส้นทางการปิด

    การกด Escape จะถูกจัดการโดยเบราว์เซอร์ เพิ่มปุ่มปิดและตัวจัดการคลิกที่ backdrop เพื่อให้มีสามวิธีในการออก และส่งเส้นทางทั้งหมดผ่าน onClose เดียวกัน

  4. 4

    ใส่ป้ายกำกับให้ dialog

    ชี้ aria-labelledby ไปที่ id ของ title และ aria-describedby ไปที่ description หากมี หากไม่มีชื่อ dialog จะถูกประกาศออกมาเป็น dialog ที่ไม่มีป้ายกำกับ

  5. 5

    จัดสไตล์ panel

    กำหนด padding ที่คอนเทนเนอร์ชั้นในแทนที่จะเป็นที่ตัว dialog เอง เพื่อให้การตรวจสอบการคลิก backdrop ยังคงเชื่อถือได้ - padding บนตัว dialog จะกลายเป็นส่วนหนึ่งของพื้นที่คลิกของตัวมันเอง

Features

การดักจับ focus แบบเนทีฟ

Focus จะย้ายเข้าไปใน dialog เมื่อเปิดและวนรอบภายในนั้น ซึ่งจัดการโดยเบราว์เซอร์แทนที่จะใช้ keydown listener ที่คุณต้องคอยดูแลให้ถูกต้อง

Focus กลับไปยัง trigger

เมื่อปิด focus จะกลับไปยังอิลิเมนต์ที่เปิด dialog โดยอัตโนมัติ ทำให้ผู้ใช้คีย์บอร์ดทำงานต่อจากจุดที่ค้างไว้ได้

Backdrop โดยไม่ต้องใช้อิลิเมนต์เสริม

Backdrop คือ pseudo-element ที่จัดสไตล์ด้วย backdrop variant จึงไม่มี overlay div แยกต่างหากให้ต้องจัดตำแหน่ง จัดลำดับชั้น (stack) หรือคอยซิงค์

Escape ใช้ปิด

มีมาในตัวอิลิเมนต์ React wrapper จะคอยฟัง event การปิดที่เกิดขึ้น เพื่อให้ prop open ยังคงซิงค์อยู่เมื่อเบราว์เซอร์ปิด dialog ด้วยตัวเอง

ส่วนที่เหลือของหน้าจะกลายเป็น inert

คอนเทนต์เบื้องหลัง modal dialog จะไม่สามารถโฟกัสได้และถูกซ่อนจาก assistive technology โดยไม่ต้องตั้งค่า aria-hidden ที่ตัวไหนด้วยมือ

คลิก backdrop เพื่อปิด

การตรวจสอบจุดคลิกจะแยกระหว่างพื้นที่ padding และคอนเทนต์ใน panel มอบพฤติกรรมการปิดตามที่ผู้คนคาด ใช้งานได้โดยไม่ต้องมี overlay node เพิ่มเติม

Common Uses

การยืนยันการกระทำที่มีผลทำลายข้อมูล

การลบบัญชี โปรเจกต์ หรือไฟล์ - กรณีที่การคลิกโดยอุบัติเหตุมีผลลัพธ์ที่ไม่สามารถกู้คืนได้ด้วย toast

ฟอร์มขนาดสั้น

การเปลี่ยนชื่อไอเทม การเชิญเพื่อนร่วมทีม หรือการกรอกค่าเดี่ยว ซึ่งการส่งผู้ใช้ไปยังหน้าแยกต่างหากจะทำให้หลุดจากจุดที่กำลังทำอยู่

ขั้นตอน onboarding ที่จำเป็น

ขั้นตอนที่ต้องทำให้เสร็จสิ้นก่อนจึงจะดำเนินการต่อได้ เช่น การยอมรับเงื่อนไขที่อัปเดตแล้วหรือการเลือก workspace

มุมมองสื่อและรายละเอียด

การแสดงภาพขนาดใหญ่หรือรายละเอียดของเรกคอร์ดทับหน้าปัจจุบัน ซึ่งการกลับมาที่ตำแหน่ง scroll เดิมเป็นสิ่งสำคัญ

Accessibility

Customization

Props

PropTypeDefaultDescription
open *boolean-กำหนดว่า dialog กำลังแสดงอยู่หรือไม่ คอมโพเนนต์จะซิงค์สถานะนี้กับ native showModal และ close methods แทนที่จะใช้วิธี conditional rendering ตัว element
onClose *() => void-เรียกใช้เมื่อ dialog ปิดลงไม่ว่าจะด้วยวิธีใดก็ตาม - ปุ่ม Escape, การคลิกที่ฉากหลัง, หรือปุ่มปิด ใช้สิ่งนี้เพื่อตั้งค่า open กลับเป็น false
title *string-ชื่อของ dialog ที่รองรับการเข้าถึงได้ เชื่อมต่อผ่าน aria-labelledby ห้ามละเว้นเด็ดขาด; dialog ที่ไม่มีชื่อจะถูกประกาศเพียงแค่ว่าเป็น dialog เท่านั้น
descriptionstring-ข้อความอธิบายเพิ่มเติม (ไม่บังคับ) เชื่อมต่อผ่าน aria-describedby เพื่อให้อ่านต่อจากชื่อเรื่องแทนที่จะถูกข้ามไป
childrenReactNode-ส่วนเนื้อหาและปุ่มการกระทำ (actions) ของ dialog วางปุ่มการกระทำไว้ท้ายสุดเพื่อให้ลำดับการกด Tab เคลื่อนผ่านเนื้อหาก่อนที่จะถึงปุ่มเหล่านี้

Why Use These Modal Dialogs?

มันใช้ระบบของแพลตฟอร์มแทนที่จะเขียนขึ้นมาใหม่ ระบบจัดการโฟกัส (focus trapping), พื้นหลังแบบ inert และการจัดการปุ่ม Escape คือส่วนที่โมดัลที่สร้างเองมักทำผิดพลาด แต่ในที่นี้สิ่งเหล่านี้คือหน้าที่ของเบราว์เซอร์ไม่ใช่ของคุณ

นั่นหมายความว่าไม่มีดีพนดอนซี (dependency) ด้วย ไลบรารี headless dialog เป็นตัวเลือกที่สมเหตุสมผล แต่สำหรับปุ่มยืนยัน มันหมายถึงการต้องส่งมอบแพ็กเกจ ติดตามเวอร์ชัน และเรียนรู้ API ของมันเพื่อให้ได้พฤติกรรมที่ตัวองค์ประกอบมีให้อยู่แล้ว

แร็ปเปอร์ (wrapper) ของ React จะซิงค์กับสถานะขององค์ประกอบเองแทนที่จะเรนเดอร์แบบมีเงื่อนไข ดังนั้น dialog ที่ปิดด้วยปุ่ม Escape จึงยังคงทริกเกอร์ onClose และ prop ตัว open ก็จะไม่หลุดซิงค์กับสิ่งที่แสดงอยู่บนหน้าจอ

Tips

Frequently Asked Questions

Related Components