@@ -640,23 +640,18 @@ export function useLockBodyScroll() {
640
640
} , [ ] ) ;
641
641
}
642
642
643
- export function useLongPress (
644
- callback ,
645
- { threshold = 400 , onStart, onFinish, onCancel } = { }
646
- ) {
643
+ export function useLongPress ( callback , options = { } ) {
644
+ const { threshold = 400 , onStart, onFinish, onCancel } = options ;
647
645
const isLongPressActive = React . useRef ( false ) ;
648
646
const isPressed = React . useRef ( false ) ;
649
647
const timerId = React . useRef ( ) ;
650
- const cbRef = React . useRef ( callback ) ;
651
648
652
- React . useLayoutEffect ( ( ) => {
653
- cbRef . current = callback ;
654
- } ) ;
655
-
656
- const start = React . useCallback (
657
- ( ) => ( event ) => {
658
- if ( isPressed . current ) return ;
649
+ return React . useMemo ( ( ) => {
650
+ if ( typeof callback !== "function" ) {
651
+ return { } ;
652
+ }
659
653
654
+ const start = ( event ) => {
660
655
if ( ! isMouseEvent ( event ) && ! isTouchEvent ( event ) ) return ;
661
656
662
657
if ( onStart ) {
@@ -665,15 +660,12 @@ export function useLongPress(
665
660
666
661
isPressed . current = true ;
667
662
timerId . current = setTimeout ( ( ) => {
668
- cbRef . current ( event ) ;
663
+ callback ( event ) ;
669
664
isLongPressActive . current = true ;
670
665
} , threshold ) ;
671
- } ,
672
- [ onStart , threshold ]
673
- ) ;
666
+ } ;
674
667
675
- const cancel = React . useCallback (
676
- ( ) => ( event ) => {
668
+ const cancel = ( event ) => {
677
669
if ( ! isMouseEvent ( event ) && ! isTouchEvent ( event ) ) return ;
678
670
679
671
if ( isLongPressActive . current ) {
@@ -692,31 +684,24 @@ export function useLongPress(
692
684
if ( timerId . current ) {
693
685
window . clearTimeout ( timerId . current ) ;
694
686
}
695
- } ,
696
- [ onFinish , onCancel ]
697
- ) ;
698
-
699
- return React . useMemo ( ( ) => {
700
- if ( callback === null ) {
701
- return { } ;
702
- }
687
+ } ;
703
688
704
689
const mouseHandlers = {
705
- onMouseDown : start ( ) ,
706
- onMouseUp : cancel ( ) ,
707
- onMouseLeave : cancel ( ) ,
690
+ onMouseDown : start ,
691
+ onMouseUp : cancel ,
692
+ onMouseLeave : cancel ,
708
693
} ;
709
694
710
695
const touchHandlers = {
711
- onTouchStart : start ( ) ,
712
- onTouchEnd : cancel ( ) ,
696
+ onTouchStart : start ,
697
+ onTouchEnd : cancel ,
713
698
} ;
714
699
715
700
return {
716
701
...mouseHandlers ,
717
702
...touchHandlers ,
718
703
} ;
719
- } , [ callback , cancel , start ] ) ;
704
+ } , [ callback , threshold , onCancel , onFinish , onStart ] ) ;
720
705
}
721
706
722
707
export function useMap ( initialState ) {
0 commit comments