Modal Component

A modal dialog component built on native HTML dialog element, featuring ref-based control and customizable actions.

Live Preview

Usage

import { useRef } from 'react';
import Modal, { ModalRef, ModalTriggerButton } from '@react-daisyui-kit/components/Modal';

export default function App() {
  const modalRef = useRef<ModalRef>(null);

  return (
    <>
      <ModalTriggerButton id="my-modal">
        Open Modal
      </ModalTriggerButton>
      <Modal
        ref={modalRef}
        id="my-modal"
        title="Modal Title"
        okText="Confirm"
        closeText="Cancel"
        onOk={() => {
          console.log('OK clicked');
          modalRef.current?.close();
        }}
        onClose={() => {
          console.log('Modal closed');
        }}
      >
        <p>Modal content goes here.</p>
      </Modal>
    </>
  );
}

Features

HTML Dialog Element

Built on native HTML <dialog> element for true modality.

Ref-based Control

Use ref with ModalRef to programmatically open/close the modal.

Customizable Actions

Configure confirm and cancel buttons with custom text and callbacks.

Multiple Sizes

Support for XS to 7XL sizes to fit any content requirements.

Props

PropTypeDefaultDescription
idstring-Unique identifier for the modal
titlestring-Modal title
childrenReactNode-Modal content
onOkfunction-Callback when confirm button is clicked
okTextstring'確定'Confirm button text
onClosefunction-Callback when modal is closed
closeTextstring'取消'Cancel button text
sizestring'md'Modal size (xs, sm, md, lg, xl, 2xl, etc.)
loadingbooleanfalseDisable buttons when loading

ModalRef

A ref interface that provides methods to control the modal programmatically. Passed to Modal via the ref prop.

MethodDescription
open()Programmatically open the modal (equivalent to calling showModal())
close()Programmatically close the modal
const modalRef = useRef<ModalRef>(null);

// Open the modal
modalRef.current?.open();

// Close the modal
modalRef.current?.close();

ModalTriggerButton

A button component that triggers the modal to open. It connects to a Modal via the shared id prop.

Props

PropTypeDescription
idstringMust match the Modal's id to connect them
childrenReactNodeButton label or content

Usage

<ModalTriggerButton id="my-modal">
  Open Modal
</ModalTriggerButton>

<Modal id="my-modal" title="Title">
  Content
</Modal>
Back to Components