Skip to content

Commit 33a5893

Browse files
authored
Improve cancellation of events when using disabled or aria-disabled attributes (#2890)
* only cancel certain events when disabled The initial idea was that whenever an element had the `disabled` or `aria-disabled` prop/attribute that we were going to remove all the event listeners. However, this is not ideal because in some scenario's we were actually explicitly adding `onClick()` listeners (for `<a>` elements) to `e.preventDefault()` when the link was marked as "disabled" to prevent it executing the actual link click. This commit will allow all defined listeners as-is, however, if you are using one of the following event listeners: - `onClick` - `onPointerUp` - `onPointerDown` - `onMouseUp` - `onMouseDown` - `onKeyUp` - `onKeyPress` - `onKeyDown` Then we will replace it with `(e) => e.preventDefault()` instead. This way we are still invoking your own listeners, but are explicitly calling `e.preventDefault()` on listeners that should not be executed in the first place when an element is `disabled`. * update CHANGELOG.md * update CHANGELOG.md
1 parent 7a1940e commit 33a5893

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

packages/@headlessui-react/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12-
- Allow horizontal scrolling when scroll locking ([bdf4f8e](https://github.com/tailwindlabs/headlessui/commit/bdf4f8e32358485dd9c437c0d631309250ee5624))
12+
- Allow horizontal scrolling inside the `Dialog` component ([#2889](https://github.com/tailwindlabs/headlessui/pull/2889))
13+
- Improve cancellation of events when using `disabled` or `aria-disabled` attributes ([#2890](https://github.com/tailwindlabs/headlessui/pull/2890))
1314

1415
## [2.0.0-alpha.1] - 2023-12-20
1516

packages/@headlessui-react/src/utils/render.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,16 +318,14 @@ function mergePropsAdvanced(...listOfProps: Props<any, any>[]) {
318318
}
319319
}
320320

321-
// Do not attach any event handlers when there is a `disabled` or `aria-disabled` prop set.
321+
// Ensure event listeners are not called if `disabled` or `aria-disabled` is true
322322
if (target.disabled || target['aria-disabled']) {
323-
return Object.assign(
324-
target,
325-
// Set all event listeners that we collected to `undefined`. This is
326-
// important because of the `cloneElement` from above, which merges the
327-
// existing and new props, they don't just override therefore we have to
328-
// explicitly nullify them.
329-
Object.fromEntries(Object.keys(eventHandlers).map((eventName) => [eventName, undefined]))
330-
)
323+
for (let eventName in eventHandlers) {
324+
// Prevent default events for `onClick`, `onMouseDown`, `onKeyDown`, etc.
325+
if (/^(on(?:Click|Pointer|Mouse|Key)(?:Down|Up|Press)?)$/.test(eventName)) {
326+
eventHandlers[eventName] = [(e: any) => e?.preventDefault?.()]
327+
}
328+
}
331329
}
332330

333331
// Merge event handlers

0 commit comments

Comments
 (0)