Skip to content

Commit 512b533

Browse files
authored
fix: improve the mouse tracker hook (#1666)
1 parent c2f79b4 commit 512b533

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

src/hooks/useCombobox/index.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,22 @@ function useCombobox(userProps = {}) {
9696
previousResultCountRef.current = items.length
9797
}
9898
})
99+
100+
const handleBlurInTracker = useCallback(
101+
function handleBlur() {
102+
if (latest.current.state.isOpen) {
103+
dispatch({
104+
type: stateChangeTypes.InputBlur,
105+
})
106+
}
107+
},
108+
[dispatch, latest],
109+
)
110+
const downshiftRefs = useMemo(() => [menuRef, toggleButtonRef, inputRef], [])
99111
const mouseAndTouchTrackers = useMouseAndTouchTracker(
100112
environment,
101-
useCallback(
102-
function handleBlur() {
103-
if (latest.current.state.isOpen) {
104-
dispatch({
105-
type: stateChangeTypes.InputBlur,
106-
selectItem: false,
107-
})
108-
}
109-
},
110-
[dispatch, latest],
111-
),
112-
useMemo(
113-
() => [menuRef, toggleButtonRef, inputRef],
114-
// dependencies can be left empty because refs are getting mutated
115-
[],
116-
),
113+
handleBlurInTracker,
114+
downshiftRefs
117115
)
118116
const setGetterPropCallInfo = useGetterPropsCalledChecker(
119117
'getInputProps',

src/hooks/useSelect/index.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,21 @@ function useSelect(userProps = {}) {
119119
// eslint-disable-next-line react-hooks/exhaustive-deps
120120
}, [])
121121

122+
const handleBlurInTracker = useCallback(
123+
function handleBlur() {
124+
if (latest.current.state.isOpen) {
125+
dispatch({
126+
type: stateChangeTypes.ToggleButtonBlur,
127+
})
128+
}
129+
},
130+
[dispatch, latest],
131+
)
132+
const downshiftRefs = useMemo(() => [menuRef, toggleButtonRef], [])
122133
const mouseAndTouchTrackers = useMouseAndTouchTracker(
123134
environment,
124-
useCallback(
125-
function handleBlur() {
126-
if (latest.current.state.isOpen) {
127-
dispatch({
128-
type: stateChangeTypes.ToggleButtonBlur,
129-
})
130-
}
131-
},
132-
[dispatch, latest],
133-
),
134-
useMemo(
135-
() => [menuRef, toggleButtonRef],
136-
// dependencies can be left empty because refs are getting mutated
137-
[],
138-
),
135+
handleBlurInTracker,
136+
downshiftRefs,
139137
)
140138
const setGetterPropCallInfo = useGetterPropsCalledChecker(
141139
'getMenuProps',

src/hooks/utils.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,18 +374,18 @@ function getHighlightedIndexOnOpen(props, state, offset) {
374374
function useMouseAndTouchTracker(
375375
environment,
376376
handleBlur,
377-
downshiftElementsRefs,
377+
downshiftRefs,
378378
) {
379379
const mouseAndTouchTrackersRef = useRef({
380380
isMouseDown: false,
381381
isTouchMove: false,
382382
isTouchEnd: false,
383383
})
384384

385-
// the elements should be retrieved the moment they are required because these are refs - they can be mutated
386-
function getDownshiftElements() {
387-
return downshiftElementsRefs.map(ref => ref.current)
388-
}
385+
const getDownshiftElements = useCallback(
386+
() => downshiftRefs.map(ref => ref.current),
387+
[downshiftRefs],
388+
);
389389

390390
useEffect(() => {
391391
if (isReactNative || !environment) {
@@ -441,7 +441,7 @@ function useMouseAndTouchTracker(
441441
environment.removeEventListener('touchmove', onTouchMove)
442442
environment.removeEventListener('touchend', onTouchEnd)
443443
}
444-
}, [downshiftElementsRefs, environment, handleBlur])
444+
}, [environment, getDownshiftElements, handleBlur])
445445

446446
return mouseAndTouchTrackersRef.current
447447
}

0 commit comments

Comments
 (0)