Skip to content

Event bubbling on disabled form elements #5886

Open

Description

@josepharhar

I came across this issue while re-triaging this chrome bug.

When clicking an element inside a disabled button element, chrome firefox and safari all don't dispatch click events on the disabled form element itself. However, chrome and safari both allow the click event to keep bubbling up the dom, whereas firefox stops the bubbling after the disabled button:

<div id=parent>
  <button disabled>
    <span id=child>click me</span>
  </button>
</div>
chrome safari firefox
#parent click click none
button none none none
#child click click click

In the case that there is no element inside the button, there is no bubbling that occurs in any of the browsers:

<div id=parent>
  <button disabled>click me</button>
</div>
chrome safari firefox
#parent none none none
button none none none

Considering that in the case where there is no element inside the button the bubbling stops, I think that we should probably follow the firefox behavior - why should adding an element inside the button change what events the parent elements of the dom see...?

The spec says that "A form control that is disabled must prevent any click events that are queued on the user interaction task source from being dispatched on the element."
The chrome implementation of this just checks if the target element is a disabled form control before dispatching events on any element in general inside the event dispatcher, which more or less seems to follow the wording of the spec since it doesn't say anything about stopping the bubbling.

Should I change the chrome behavior to be like firefox?
Should we change what the spec says? If so, what should it say?

Some posts I found about this behavior by searching:

cc @jakearchibald @tkent-google

Activity

  1. domenic commented on Sep 2, 2020

    @domenic
    Member

    It'd be good to solve this in tandem with #2368. It looks like there @rniwa was in agreement with you that the Firefox behavior is better.

    It seems like the proposal is to truncate the event path for all mouse events (and pointer events?) to remove disabled form controls, and any of their (shadow-including??) parents.

    @annevk, can you think of any way to spec this without modifying event dispatch?

    The current spec seems to be advocating for a third behavior, which is to don't dispatch the event when the target would be a disabled form control. This would result in clicking on children of a disabled button firing the event, but in that case the event should reach the disabled form control (since dispatch was not prevented). And no browsers seem to follow that.

    The current spec is nice and simple and does not need to get into the innards of event dispatch; it just serves as input into the question of whether or not to dispatch. But, it's probably not good for web developers, and it's not what anyone implements, so oh well.

  2. josepharhar commented on Sep 3, 2020

    @josepharhar
    ContributorAuthor

    Thanks, I didn't know about #2368, it sounds like it has the same root cause as this bug.

    I found the SendMouseEventsDisabledFormControls flag in chrome, and I found that it has behavior different from what I've documented so far. I also looked and .click() and physical clicks separately:

    <div id=parent>
      <button disabled>
        <span id=child>click me</span>
      </button>
    </div>
    physical click firefox safari chrome chrome Experimental Web Platform features
    #parent none click click none
    button none none none none
    #child click click click none
    .click() firefox safari chrome chrome Experimental Web Platform features
    #parent click click click click
    button click none none click
    #child click click click click

    @dtapuska it looks like you added the flag, what are your thoughts on this behavior? Do you think this flag will become enabled by default?

  3. annevk commented on Sep 7, 2020

    @annevk
    Member

    See whatwg/dom#687. It seems that a custom get the parent for elements might be a way of achieving this, but we should probably first sort out where we want everyone to end up.

  4. added
    interop
    topic: events
    topic: forms
    on Sep 7, 2020
  5. pruthvi3007 commented on Oct 3, 2020

    @pruthvi3007
  6. mercmobily commented on Mar 2, 2022

    @mercmobily

    Sorry about the noise... I was just wondering if there was progress in this; it affects my code -- I have workaround code, and I would like to figure out when to task the deletion of the workaround. Thanks!

  7. josepharhar commented on Jun 24, 2022

    @josepharhar
    ContributorAuthor

    I believe that the stable chrome and safari behavior is best here. I'll try to make sure that experimental chrome doesn't have the weird behavior where none of the event handlers are fired.
    I think that web-platform-tests/wpt#32381 includes tests for this behavior.
    cc @saschanaz

  8. added
    agenda+
    on Jun 24, 2022
  9. smaug---- commented on Jun 28, 2022

    @smaug----

    What is stable Chrome behavior? Propagating click from child elements of a disable button to the ancestor of the button, but not firing on the button itself. That is just weird. I'm rather strongly against such behavior. I don't think we have other cases where we drop magically an item from the event path. (But I could add that implementing Chrome's behavior in Gecko should be very easy, one would just change the parent target, if the default parent was disabled)

  10. mercmobily commented on Jun 28, 2022

    @mercmobily

    What is stable Chrome behavior? Propagating click from child elements of a disable button to the ancestor of the button, but not firing on the button itself. That is just weird. I'm rather strongly against such behavior. I don't think we have other cases where we drop magically an item from the event path. (But I could add that implementing Chrome's behavior in Gecko should be very easy, one would just change the parent target, if the default parent was disabled)

    I couldn't possibly agree more.

  11. jakearchibald commented on Jun 28, 2022

    @jakearchibald
    Contributor

    Agreed.

    It seems reasonable to not-dispatch a click event if it's performed on a disabled button. "click" is pretty much an activation event, but a disabled button cannot be activated.

    Dispatching "click" but skipping the button in the event path is weird.

  12. saschanaz commented on Jun 28, 2022

    @saschanaz
    Member

    Should we dupe this to #2368?

    I totally agree that it's weird, but Mozilla has received multiple webcompat reports (see https://bugzilla.mozilla.org/show_bug.cgi?id=1653882). Basically websites are trying to observe click events for disabled form elements by observing from their parent elements. One of the examples:

    image

    but if it's too late to do this, the weird behaviour should probably become part of the spec.

    I think it is indeed too late. 😞

    Edit: See also web-platform-tests/interop#27.

  13. smaug---- commented on Jun 28, 2022

    @smaug----

    Have other browsers gotten bug reports about the behavior they have?

  14. 76 remaining items

  15. josepharhar commented on Sep 4, 2023

    @josepharhar
    ContributorAuthor

    Another regression bug was filed: https://bugs.chromium.org/p/chromium/issues/detail?id=1477379
    The breakage so far all looks like websites which wouldn't have worked properly on firefox in the first place, since firefox has never fired click events on disabled <input type=text>s or <textarea>s.

  16. KajSzy commented on Sep 5, 2023

    @KajSzy

    This chrome updated did scare me a lot.
    I just realized that such structure will not fire any onClick event.
    The easiest solution was to change it toonPointerUp

    <div onClick={onClick}>
       <textarea disabled/>
    </div>

    So after workaround

    <div onPointerUp={onClick}>
       <textarea disabled/>
    </div>
  17. saschanaz commented on Sep 18, 2023

    @saschanaz
    Member

    I'll try shipping this on Firefox beta. If we get enough frustration reports then maybe we could discuss whether we can disable click only for button-like elements (including checkboxes and radio buttons) as blocking click makes best sense there and less for others.

  18. seal7766 commented on Apr 22, 2024

    @seal7766

    @KajSzy 
    answer helped me.

    However, the event occurs, but the state does not change when disabled due to React event delegation. I use the code below to circumvent this. I hope my answer helps others who have this problem.

    const [isOpen, setOpen] = useState(false);
    const ref = useRef<HTMLDivElement>(null);
    return (
          <div
            ref={ref}
            onClick={() => {
              // here click event code..
              setOpen((prev) => !prev);
            }}
          >
            <button
              disabled
              onPointerDown={() => {
                ref.current?.click();
              }}
            >
              버튼
            </button>
          </div>
    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interoptopic: eventstopic: forms

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions