Skip to content

Commit fa8c878

Browse files
authored
fix: root-close firing immediately in react 17 (#880)
1 parent 0168d4a commit fa8c878

File tree

3 files changed

+696
-466
lines changed

3 files changed

+696
-466
lines changed

package.json

+11-11
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@
6161
"conventionalCommits": true
6262
},
6363
"dependencies": {
64-
"@babel/runtime": "^7.4.5",
65-
"@popperjs/core": "^2.0.0",
66-
"@restart/hooks": "^0.3.12",
64+
"@babel/runtime": "^7.12.1",
65+
"@popperjs/core": "^2.5.3",
66+
"@restart/hooks": "^0.3.25",
6767
"@types/warning": "^3.0.0",
68-
"dom-helpers": "^5.1.0",
68+
"dom-helpers": "^5.2.0",
6969
"prop-types": "^15.7.2",
7070
"uncontrollable": "^7.0.0",
7171
"warning": "^4.0.3"
@@ -91,8 +91,8 @@
9191
"@types/classnames": "^2.2.10",
9292
"@types/react": "^16.9.53",
9393
"@types/react-dom": "^16.9.8",
94-
"@typescript-eslint/eslint-plugin": "^4.4.1",
95-
"@typescript-eslint/parser": "^4.4.1",
94+
"@typescript-eslint/eslint-plugin": "^4.5.0",
95+
"@typescript-eslint/parser": "^4.5.0",
9696
"babel-eslint": "^10.1.0",
9797
"babel-plugin-add-module-exports": "^1.0.4",
9898
"babel-plugin-istanbul": "^6.0.0",
@@ -102,14 +102,14 @@
102102
"enzyme": "^3.11.0",
103103
"enzyme-adapter-react-16": "^1.15.5",
104104
"eslint": "^7.11.0",
105-
"eslint-config-4catalyzer-typescript": "^2.0.4",
105+
"eslint-config-4catalyzer-typescript": "^3.0.1",
106106
"eslint-config-prettier": "^6.13.0",
107107
"eslint-plugin-import": "^2.22.1",
108108
"eslint-plugin-jsx-a11y": "^6.3.1",
109109
"eslint-plugin-mocha": "^8.0.0",
110110
"eslint-plugin-prettier": "^3.1.4",
111-
"eslint-plugin-react": "^7.21.4",
112-
"eslint-plugin-react-hooks": "^4.1.2",
111+
"eslint-plugin-react": "^7.21.5",
112+
"eslint-plugin-react-hooks": "^4.2.0",
113113
"gh-pages": "^3.1.0",
114114
"husky": "^4.3.0",
115115
"jquery": "^3.5.1",
@@ -129,13 +129,13 @@
129129
"react-dom": "^16.14.0",
130130
"react-transition-group": "^4.4.1",
131131
"rimraf": "^3.0.2",
132-
"rollup": "^2.32.0",
132+
"rollup": "^2.32.1",
133133
"simulant": "^0.2.2",
134134
"sinon": "^9.2.0",
135135
"sinon-chai": "^3.5.0",
136136
"typescript": "^4.0.3",
137137
"webpack": "^4.44.2",
138-
"webpack-atoms": "^13.1.0",
138+
"webpack-atoms": "^14.0.0",
139139
"webpack-cli": "^3.3.12"
140140
},
141141
"bugs": {

src/useRootClose.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ function useRootClose(
8686
useEffect(() => {
8787
if (disabled || ref == null) return undefined;
8888

89+
// Store the current event to avoid triggering handlers immediately
90+
// https://github.com/facebook/react/issues/20074
91+
let currentEvent = window.event;
92+
8993
const doc = ownerDocument(getRefTarget(ref));
9094

9195
// Use capture for this listener so it fires before React's listener, to
@@ -98,8 +102,23 @@ function useRootClose(
98102
true,
99103
);
100104

101-
const removeMouseListener = listen(doc as any, clickTrigger, handleMouse);
102-
const removeKeyupListener = listen(doc as any, 'keyup', handleKeyUp);
105+
const removeMouseListener = listen(doc as any, clickTrigger, (e) => {
106+
// skip if this event is the same as the one running when we added the handlers
107+
if (e === currentEvent) {
108+
currentEvent = undefined;
109+
return;
110+
}
111+
handleMouse(e);
112+
});
113+
114+
const removeKeyupListener = listen(doc as any, 'keyup', (e) => {
115+
// skip if this event is the same as the one running when we added the handlers
116+
if (e === currentEvent) {
117+
currentEvent = undefined;
118+
return;
119+
}
120+
handleKeyUp(e);
121+
});
103122

104123
let mobileSafariHackListeners = [] as Array<() => void>;
105124
if ('ontouchstart' in doc.documentElement) {

0 commit comments

Comments
 (0)