mui / mui/base-ui

[all components] stopPropagation() logic doesn't truly stop

Open
#3,588 1 comment 0 reactions 0 assignees View on GitHub
external dependency scope: all components
Dominant language
TypeScript
Stars
10.9k
Forks
543
Avg merge
1d 20h
Merged PRs (30d)
101

Description

## Reproducible example

https://github.com/mui/base-ui/blob/e1d92a4bcd95057b2d04dca08fb086caec18ae1c/packages/react/src/composite/root/useCompositeRoot.ts#L306

## Current behavior

When React renders the whole page (e.g. Next.js),`event.stopPropagation()` in a synthetic event only stops propagation on the synthetic React event. This leaves the native event untouched. See https://github.com/facebook/react/issues/4335#issuecomment-3678205960 for more details and a minimal reproduction.

## Expected behavior

Shouldn't we stop propagation on the native React event, too?

To truly stop events so they can't interfere with third-party code, we need this:

```jsx
event.stopPropagation();
event.nativeEvent.stopImmediatePropagation();
```

## Base UI version

v1.0.0

## Additional context

- Since it's a bug in React (https://github.com/facebook/react/issues/4335#issuecomment-3678205960) one could argue that we should do nothing about it here. This GitHub issue is enough for the developers to understand what's going on. We can close after React gives a clear direction.
- I have noticed this from https://github.com/ncdai/chanhdai.com/pull/528. It's using https://github.com/JohannesKlauss/react-hotkeys-hook, which adds a direct event listener on the `document`, so even though our code calls stopPropagation(), it doesn't truly stop it, hence the bug.
- Seeing all the stopPropagation() calls in the component source that we have, I wonder if we are not abusing its purpose in several cases. https://css-tricks.com/dangers-stopping-event-propagation/ feels like is waiting around the corner to bite us.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.