A removed input's onBlur runs during the commit that removes it (Chrome)

オープン
#186 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
30/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
react, typescript
領域
frontend

調査の方向性

Start with commitBeforeMutationEffects and dispatchEvent in the event-system code, which show why Redact's listener runs while React's does not, then look at commitMutation where the proposed depth counter would go. The branch linked in the issue has a fix and tests in tests/deleted-element-events.test.tsx, so reviewing it or running the browser suite via scripts/actions-browser.config.mjs is the practical entry point. Done means the removed-input blur case no longer fires onBlur in Chrome, and the layout-effect blur still dispatches.

索引モデルが issue の本文から書いたものです。

説明

Chrome fires blur on a focused element just before removing it from the DOM. When a Redact commit removes a focused input, Redact runs that input's onBlur mid-commit, with the props of a view the user just closed. If the handler sets state, it can undo the update that removed it.

React never calls it. commitBeforeMutationEffects turns its event system off (_enabled = false) until the mutations finish, and dispatchEvent returns early while it is off (react-dom-client.development.js in react-dom 19.3.1).

function Form() {
  const [open, setOpen] = useState(true)
  if (!open) return <p>closed</p>
  return (
    <div>
      <input onBlur={() => console.log('blur')} />
      <button onMouseDown={e => e.preventDefault()} onClick={() => setOpen(false)}>Done</button>
    </div>
  )
}

Focus the input and click Done in Chrome. React 19.3.1 logs nothing. Redact on main logs blur.

We hit it in a sheet whose field marks itself touched on blur: confirming the sheet removed the field, its onBlur ran, and the sheet reopened the form.

Fix

Branch with the fix and tests, ready to merge (I can't open a PR here): https://github.com/TanStack/redact/compare/main...WolfieLeader:redact:fix/skip-events-on-deleted-elements

What changes
  • commitMutation runs the mutation queue and placements inside runMutations, which raises a depth counter.
  • Redact's event listener returns early while the counter is above zero.
  • Effects, refs and autoFocus run after the mutations, so their focus events still dispatch, as in React.
Testing
  • tests/deleted-element-events.test.tsx has two cases: a click that removes a focused input, and a layout-effect cleanup that blurs an input being removed. The first needs Chrome's removal blur, so it only proves something in the browser run; I added the file to scripts/actions-browser.config.mjs.
  • Both fail on main and pass with the fix, in jsdom and in Chrome. React 19.3.1 passes both in Chrome.
  • onFocus from autoFocus and from focus() in useLayoutEffect still fire, as in React.
  • pnpm test:ci passes on Node 24.22.0. redact/dom-client grows 27 B gzip, within budget.
  • Not run: the Firefox and WebKit browser jobs. scripts/test-react-compatibility-size.mjs fails locally on unchanged main (server/index +67 B), so I left that check to CI.
主要言語
TypeScript
スター
640
フォーク
32
平均マージ
1時間 15分
マージ済み PR(30日)
7

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

TanStack/redact のほかの issue

TanStack/redact の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。