-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve cancellation of events when using disabled
or aria-disabled
attributes
#2890
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
RobinMalfait
changed the title
Improve cancellation of event when using
Improve cancellation of events when using Dec 20, 2023
disabled
or aria-disabled
attributesdisabled
or aria-disabled
attributes
RobinMalfait
force-pushed
the
fix/cancel-events-when-disabled
branch
from
December 20, 2023 23:03
7068e8f
to
304c184
Compare
This was referenced May 28, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes and improves the
disabled
andaria-disabled
event handling that we introduced in#1265.
We wrongly assumed that we don't want to attach any event listeners if the
disabled
oraria-disabled
props were used. While in most cases this is true, and is even a no-op (the browserdoesn't call
onClick
on a<button disabled>
) it is not true in other cases.For example
<a disabled>
doesn't exist, instead we use<a aria-hidden="">
. If we now want to"disabled" the native click behaviour we have to add
onClick={e => e.preventDefault()}
.In the linked PR, we were removing the
onClick
we could have added. In this PR we make sure toalways add
e => e.preventDefault()
instead of theonXYZ
handler we or you provided. We only dothis for
onClick
,onMouseDown
,onMouseUp
,onPointerDown
,onPoiterUp
,onKeyDown
,onKeyUp
andonKeyPress
. These are typically the events that cause an actual action that we wantto prevent instead.