Skip to content

Refactor: extract useDialogControl hook for modal open/close logic #97

Description

@wpinrui

Problem

All three modal components contain identical useEffect logic for opening/closing the native <dialog> element:

useEffect(() => {
  const dialog = dialogRef.current
  if (!dialog) return

  if (isOpen && !dialog.open) {
    dialog.showModal()
  } else if (!isOpen && dialog.open) {
    dialog.close()
  }
}, [isOpen])

This is repeated in:

  • DeleteConfirmModal.tsx
  • RenameModal.tsx
  • NewMemberModal.tsx

Proposed Solution

Extract to a custom hook:

function useDialogControl(isOpen: boolean, dialogRef: RefObject<HTMLDialogElement>) {
  useEffect(() => {
    const dialog = dialogRef.current
    if (!dialog) return

    if (isOpen && !dialog.open) {
      dialog.showModal()
    } else if (!isOpen && dialog.open) {
      dialog.close()
    }
  }, [isOpen])
}

Impact

  • 3 files affected
  • Low risk refactor
  • Improves DRY compliance

Discovered during PR review of fix/replace-prompt-with-modal

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions