@@ -882,23 +882,18 @@ export function useLockBodyScroll() {
882
882
} , [ ] ) ;
883
883
}
884
884
885
- export function useLongPress (
886
- callback ,
887
- { threshold = 400 , onStart, onFinish, onCancel } = { }
888
- ) {
885
+ export function useLongPress ( callback , options = { } ) {
886
+ const { threshold = 400 , onStart, onFinish, onCancel } = options ;
889
887
const isLongPressActive = React . useRef ( false ) ;
890
888
const isPressed = React . useRef ( false ) ;
891
889
const timerId = React . useRef ( ) ;
892
- const cbRef = React . useRef ( callback ) ;
893
890
894
- React . useLayoutEffect ( ( ) => {
895
- cbRef . current = callback ;
896
- } ) ;
897
-
898
- const start = React . useCallback (
899
- ( ) => ( event ) => {
900
- if ( isPressed . current ) return ;
891
+ return React . useMemo ( ( ) => {
892
+ if ( typeof callback !== "function" ) {
893
+ return { } ;
894
+ }
901
895
896
+ const start = ( event ) => {
902
897
if ( ! isMouseEvent ( event ) && ! isTouchEvent ( event ) ) return ;
903
898
904
899
if ( onStart ) {
@@ -907,15 +902,12 @@ export function useLongPress(
907
902
908
903
isPressed . current = true ;
909
904
timerId . current = setTimeout ( ( ) => {
910
- cbRef . current ( event ) ;
905
+ callback ( event ) ;
911
906
isLongPressActive . current = true ;
912
907
} , threshold ) ;
913
- } ,
914
- [ onStart , threshold ]
915
- ) ;
908
+ } ;
916
909
917
- const cancel = React . useCallback (
918
- ( ) => ( event ) => {
910
+ const cancel = ( event ) => {
919
911
if ( ! isMouseEvent ( event ) && ! isTouchEvent ( event ) ) return ;
920
912
921
913
if ( isLongPressActive . current ) {
@@ -934,31 +926,24 @@ export function useLongPress(
934
926
if ( timerId . current ) {
935
927
window . clearTimeout ( timerId . current ) ;
936
928
}
937
- } ,
938
- [ onFinish , onCancel ]
939
- ) ;
940
-
941
- return React . useMemo ( ( ) => {
942
- if ( callback === null ) {
943
- return { } ;
944
- }
929
+ } ;
945
930
946
931
const mouseHandlers = {
947
- onMouseDown : start ( ) ,
948
- onMouseUp : cancel ( ) ,
949
- onMouseLeave : cancel ( ) ,
932
+ onMouseDown : start ,
933
+ onMouseUp : cancel ,
934
+ onMouseLeave : cancel ,
950
935
} ;
951
936
952
937
const touchHandlers = {
953
- onTouchStart : start ( ) ,
954
- onTouchEnd : cancel ( ) ,
938
+ onTouchStart : start ,
939
+ onTouchEnd : cancel ,
955
940
} ;
956
941
957
942
return {
958
943
...mouseHandlers ,
959
944
...touchHandlers ,
960
945
} ;
961
- } , [ callback , cancel , start ] ) ;
946
+ } , [ callback , threshold , onCancel , onFinish , onStart ] ) ;
962
947
}
963
948
964
949
export function useMap ( initialState ) {
@@ -1257,14 +1242,16 @@ export function usePreferredLanguage() {
1257
1242
) ;
1258
1243
}
1259
1244
1260
- export function usePrevious ( newValue ) {
1261
- const previousRef = React . useRef ( ) ;
1245
+ export function usePrevious ( value ) {
1246
+ const [ current , setCurrent ] = React . useState ( value ) ;
1247
+ const [ previous , setPrevious ] = React . useState ( null ) ;
1262
1248
1263
- React . useEffect ( ( ) => {
1264
- previousRef . current = newValue ;
1265
- } ) ;
1249
+ if ( value !== current ) {
1250
+ setPrevious ( current ) ;
1251
+ setCurrent ( value ) ;
1252
+ }
1266
1253
1267
- return previousRef . current ;
1254
+ return previous ;
1268
1255
}
1269
1256
1270
1257
function getRandomNumber ( min , max ) {
@@ -1376,9 +1363,7 @@ export function useScript(src, options = {}) {
1376
1363
const cachedScriptStatuses = React . useRef ( { } ) ;
1377
1364
1378
1365
React . useEffect ( ( ) => {
1379
- if ( ! src ) {
1380
- return ;
1381
- }
1366
+ if ( ! src ) return ;
1382
1367
1383
1368
const cachedScriptStatus = cachedScriptStatuses . current [ src ] ;
1384
1369
if ( cachedScriptStatus === "ready" || cachedScriptStatus === "error" ) {
@@ -1389,26 +1374,12 @@ export function useScript(src, options = {}) {
1389
1374
let script = document . querySelector ( `script[src="${ src } "]` ) ;
1390
1375
1391
1376
if ( script ) {
1392
- setStatus (
1393
- script . getAttribute ( "data-status" ) ?? cachedScriptStatus ?? "loading"
1394
- ) ;
1377
+ setStatus ( cachedScriptStatus ?? "loading" ) ;
1395
1378
} else {
1396
1379
script = document . createElement ( "script" ) ;
1397
1380
script . src = src ;
1398
1381
script . async = true ;
1399
- script . setAttribute ( "data-status" , "loading" ) ;
1400
1382
document . body . appendChild ( script ) ;
1401
-
1402
- const setAttributeFromEvent = ( event ) => {
1403
- const scriptStatus = event . type === "load" ? "ready" : "error" ;
1404
-
1405
- if ( script ) {
1406
- script . setAttribute ( "data-status" , scriptStatus ) ;
1407
- }
1408
- } ;
1409
-
1410
- script . addEventListener ( "load" , setAttributeFromEvent ) ;
1411
- script . addEventListener ( "error" , setAttributeFromEvent ) ;
1412
1383
}
1413
1384
1414
1385
const setStateFromEvent = ( event ) => {
0 commit comments