Skip to content

fix(types): add capture variants of toggle, dialog and command events - #5387

Merged
JoviDeCroock merged 2 commits into
preactjs:mainfrom
playlikeboy:types/missing-capture-event-handlers
Nov 19, 2026
Merged

JoviDeCroock merged 2 commits into
preactjs:mainfrom
playlikeboy:types/missing-capture-event-handlers

Conversation

@playlikeboy

@playlikeboy playlikeboy commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

AI disclosure: I used Claude Code (an LLM coding agent) to write this change and this description. I checked the result myself, as described below. This replaces #5333, which I opened without a disclosure and which was closed under the Code of Conduct. Sorry about that. This version is also smaller: it only adds lines, and it leaves out the reordering and the closedby move that were in #5333.

Problem

Preact turns any on*Capture prop into a capturing listener (src/diff/props.js strips the Capture suffix and passes useCapture to addEventListener). The types have no capture variant for these handlers, so each of these lines fails to type-check on main:

<div popover="auto" onBeforeToggleCapture={e => e.newState} onToggleCapture={e => e.oldState} />
<dialog onCancelCapture={e => {}} onCloseCapture={e => {}} />
<details onToggleCapture={e => e.currentTarget.open} />
<div onCommandCapture={e => e.command} />

The error is TS2322: Type '{ onToggleCapture: ... }' is not assignable to type 'DetailsHTMLAttributes<HTMLDetailsElement>', and the same for the others.

Change

  • DOMAttributes: add onBeforeToggleCapture and onToggleCapture (ToggleEventHandler), onCloseCapture and onCancelCapture (GenericEventHandler), and onCommandCapture (CommandEventHandler), next to the existing non-capture handlers.
  • DialogHTMLAttributes: add onCancelCapture and onCloseCapture next to its existing onCancel and onClose.

Verification

  • I checked each new prop by type-checking a component that uses it and reads a field of the event (newState, oldState, command, currentTarget.returnValue). Against main's src/dom.d.ts that fails with 10 errors (4 × TS2322 and 6 × TS7006); with this change it passes. The test block was removed after review.
  • pnpm test:ts (core and compat), pnpm lint and pnpm build pass. pnpm test:vitest:min passes: 110 files, 1484 tests.
  • oxfmt --check . reports compat/test/ts/index.tsx and debug/test/browser/debug.test.jsx. This PR does not touch either file.

🤖 Generated with Claude Code

Preact registers any `on*Capture` prop as a capturing listener, but
`DOMAttributes` and `DialogHTMLAttributes` had no capture variant for
`onBeforeToggle`, `onToggle`, `onCancel`, `onClose` and `onCommand`, so
they failed to type-check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coveralls

coveralls commented Nov 19, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 99.857%. remained the same — playlikeboy:types/missing-capture-event-handlers into preactjs:main

Comment thread test/ts/dom-attributes.test-d.tsx Outdated
Comment on lines +347 to +350

// Capture variants of popover, dialog and command events, which Preact
// registers with `addEventListener(name, handler, true)`
const testCaptureEvents = (

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete these tests, they're not useful. All they test is that we don't delete the interface members which... why would we?

Adding tests for every single basic property add is just noise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in efbf8cf, the PR now only touches src/dom.d.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JoviDeCroock
JoviDeCroock merged commit cdff5f7 into preactjs:main Nov 19, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants