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

fix(useEventListener): avoid double handlers calls in React 17 #16514

Merged
merged 3 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/fluentui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Fix RadioGroup visual issues. Add `RadioButtonIcon`. Fix vertical RadioGroup alignment issue with custom content. Fix focus frame alignment for bare indicator. @TanelVari ([#16478](https://github.com/microsoft/fluentui/pull/16478))
- Fix type for `Accessibility` to allow function without props @ling1726 ([#16322](https://github.com/microsoft/fluentui/pull/16322))
- Optimize `NumberListIcon` for bundled size @ling1726 ([#16473](https://github.com/microsoft/fluentui/pull/16473))
- Fix event handlers behavior and compatibility with portals in React 17 @layershifter ([#16514](https://github.com/microsoft/fluentui/pull/16514))

### Features
- Add 2.0 light and dark themes @jurokapsiar ([#15867](https://github.com/microsoft/fluentui/pull/15867))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,23 @@ export const useEventListener = <T extends EventTypes>(options: EventListenerOpt
React.useEffect(() => {
const element: Target | null | undefined = typeof targetRef === 'undefined' ? target : targetRef.current;

// Store the current event to avoid triggering handlers immediately
// Note this depends on a deprecated but extremely well supported quirk of the web platform
// https://github.com/facebook/react/issues/20074
let currentEvent = window.event;

const conditionalHandler = (event: DocumentEventMap[T]) => {
ling1726 marked this conversation as resolved.
Show resolved Hide resolved
// Skip if this event is the same as the one running when we added the handlers
if (event === currentEvent) {
currentEvent = undefined;
return;
}

eventHandler(event);
};

if (isActionSupported(element, 'addEventListener')) {
element.addEventListener(type, eventHandler, capture);
element.addEventListener(type, conditionalHandler, capture);
} else if (process.env.NODE_ENV !== 'production') {
throw new Error(
'@fluentui/react-component-event-listener: Passed `element` is not valid or does not support `addEventListener()` method.',
Expand All @@ -45,7 +60,7 @@ export const useEventListener = <T extends EventTypes>(options: EventListenerOpt

return () => {
if (isActionSupported(element, 'removeEventListener')) {
element.removeEventListener(type, eventHandler, capture);
element.removeEventListener(type, conditionalHandler, capture);
} else if (process.env.NODE_ENV !== 'production') {
throw new Error(
'@fluentui/react-component-event-listener: Passed `element` is not valid or does not support `removeEventListener()` method.',
Expand Down