0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【React】ファイル選択ダイアログを閉じた際にdialogが閉じないようにする方法

Last updated at Posted at 2023-12-16

状況

以下のように<dialog>の中に<input type='file'>を記述している場合に、Chromeでファイル選択ダイアログをキャンセルで閉じた際にdialogも閉じてしまう。

// ...

  <dialog>
    <input type="file" />
  </dialog>

// ...

改善方法

ファイル選択ダイアログをキャンセルした際にcancelイベントが発生することが原因。
onCancelで以下のようにev.targetdialogの場合にのみdialogを閉じるようにする。

// ...
  const handleCancel = useCallback(
    (ev: React.SyntheticEvent<HTMLDialogElement, Event>) => {
      if (ev.target instanceof HTMLDialogElement) {
        ev.preventDefault()
        closeModal()
      }
    },
    [closeModal]
  )

// ...
  <dialog onCancel={handleCancel}>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?