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

Preventing default and stopping propagation #303

Open
chrismcv opened this issue Aug 22, 2024 · 3 comments
Open

Preventing default and stopping propagation #303

chrismcv opened this issue Aug 22, 2024 · 3 comments

Comments

@chrismcv
Copy link

chrismcv commented Aug 22, 2024

Is your feature request related to a problem? Please describe.
I find I'm having to add a lot of e.stopPropagation() or e.preventDefault() on Buttons, MenuItems and DropDownMenu's.

Describe the solution you'd like
e.g.
image
I have an Accordian, with a Dropdown Menu in the header, I handle onClick by triggering an action, and the Accordian collapses without a stopPropagation. It also regularly causes modals to trigger close etc.

I'd like MenuItem/Button/Actionable/DropDownItems to call preventDefault before triggering the onClick event to the consuming component.

Describe alternatives you've considered
I'm considering forking components to add this in by default, but I'd rather not have to.

@blvdmitry
Copy link
Contributor

Preventing this by default could cause side effects that would affect global event handlers. For example, if analytics are tracking user clicks on body elements, stop propagation will prevent it from being tracked when clicking on any button.

Potentially a good middle ground is to have boolean props to preventDefault and stopPropagation for all clickable elements? Would that work for you too?

@chrismcv
Copy link
Author

yeah that would be great - or if could sit on the reshaped provider context that could work too?

@blvdmitry
Copy link
Contributor

blvdmitry commented Oct 24, 2024

Hey Chris, just getting back to this ticket. Having this on the provider level could cause a lot of unexpected side effects so I would recommend adding this property only in the cases when you nested a one clickable element inside another.

This also made me wonder if just having stopPropagation flag would solve the problem in your case. For example, this code snippet seems to be already supporting what you're describing. Clicking on Actionable 2 only triggers its console.log:

<Actionable onClick={() => console.log("Click 1")}>
	Actionable 1{" "}
	<Actionable as="div" stopPropagation onClick={() => console.log("Click 2")}>
		Actionable 2
	</Actionable>
</Actionable>

@blvdmitry blvdmitry changed the title Preventing default and stopping propogation Preventing default and stopping propagation Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Awaiting / Blocked
Development

No branches or pull requests

2 participants