Skip to content
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

Change click eventListener timing on elements outside offcanvas #33471

Closed
wants to merge 1 commit into from

Conversation

GeoSot
Copy link
Member

@GeoSot GeoSot commented Mar 25, 2021

On initialization the offcanvas registers click listeners.
Although it was good for simple buttons, if a button contains another element and user clicks on this, the click seems that through delegation, is triggering two times, so the offcanvas instantly toggles itself

Closes #33457

could be fixed by #33545

@GeoSot GeoSot requested a review from a team as a code owner March 25, 2021 00:57
@GeoSot GeoSot added this to Inbox in v5.0.0 via automation Mar 25, 2021
@alpadev
Copy link
Contributor

alpadev commented Mar 25, 2021

I think you can keep your current implementation but you'd have to add event.stopPropagation() to this event handler. The delegation event handler work on the capturing phase while the handler you're using to check for clicks outside is a regular event handler that works on the bubbling phase.

What happens is that a click event in the capturing phase initializes the offcanvas (that happens while the capturing is at the document) and adds the bubbling phase handler. After that, the event continues to propagate through the DOM to your button (or the image) and bubbles back to the document and there it triggers the event handler you just added. That handler checks for the event.target that in case of an image can't get a valid target (since the image doesn't have the data-bs-target attribute) so the first part of the if condition is true because the image isn't in the offcanvas and the second one is true because target (null) !== offcanvas._element.

Hope that explains the behaviour.

@GeoSot
Copy link
Member Author

GeoSot commented Mar 25, 2021

Your explanation is precise, and your solution seems valid too :)

The only reason I chose not to put event.stopPropagation() ,is to allow other listeners to work.
As I can recall we had an open issue on this, during the previous days.

@alpadev
Copy link
Contributor

alpadev commented Mar 26, 2021

Well nvm then 😊

I thought I would explain the behavior in more detail, just in case there were any questions left.

@GeoSot GeoSot marked this pull request as draft April 4, 2021 00:18
@GeoSot GeoSot closed this Apr 14, 2021
@XhmikosR XhmikosR removed this from Inbox in v5.0.0 Apr 15, 2021
@XhmikosR XhmikosR deleted the gs-fix-offcanvas-close-on-img-click branch May 13, 2021 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

offcanvas Toggle doesn't trigger if it has image inside (Event doesn't bubble?)
3 participants