Skip to content

Commit 47a11b9

Browse files
committed
Merge branch 'main' into experimental
2 parents 1096459 + 3529e5c commit 47a11b9

File tree

1 file changed

+27
-56
lines changed

1 file changed

+27
-56
lines changed

index.js

Lines changed: 27 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -882,23 +882,18 @@ export function useLockBodyScroll() {
882882
}, []);
883883
}
884884

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;
889887
const isLongPressActive = React.useRef(false);
890888
const isPressed = React.useRef(false);
891889
const timerId = React.useRef();
892-
const cbRef = React.useRef(callback);
893890

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+
}
901895

896+
const start = (event) => {
902897
if (!isMouseEvent(event) && !isTouchEvent(event)) return;
903898

904899
if (onStart) {
@@ -907,15 +902,12 @@ export function useLongPress(
907902

908903
isPressed.current = true;
909904
timerId.current = setTimeout(() => {
910-
cbRef.current(event);
905+
callback(event);
911906
isLongPressActive.current = true;
912907
}, threshold);
913-
},
914-
[onStart, threshold]
915-
);
908+
};
916909

917-
const cancel = React.useCallback(
918-
() => (event) => {
910+
const cancel = (event) => {
919911
if (!isMouseEvent(event) && !isTouchEvent(event)) return;
920912

921913
if (isLongPressActive.current) {
@@ -934,31 +926,24 @@ export function useLongPress(
934926
if (timerId.current) {
935927
window.clearTimeout(timerId.current);
936928
}
937-
},
938-
[onFinish, onCancel]
939-
);
940-
941-
return React.useMemo(() => {
942-
if (callback === null) {
943-
return {};
944-
}
929+
};
945930

946931
const mouseHandlers = {
947-
onMouseDown: start(),
948-
onMouseUp: cancel(),
949-
onMouseLeave: cancel(),
932+
onMouseDown: start,
933+
onMouseUp: cancel,
934+
onMouseLeave: cancel,
950935
};
951936

952937
const touchHandlers = {
953-
onTouchStart: start(),
954-
onTouchEnd: cancel(),
938+
onTouchStart: start,
939+
onTouchEnd: cancel,
955940
};
956941

957942
return {
958943
...mouseHandlers,
959944
...touchHandlers,
960945
};
961-
}, [callback, cancel, start]);
946+
}, [callback, threshold, onCancel, onFinish, onStart]);
962947
}
963948

964949
export function useMap(initialState) {
@@ -1257,14 +1242,16 @@ export function usePreferredLanguage() {
12571242
);
12581243
}
12591244

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);
12621248

1263-
React.useEffect(() => {
1264-
previousRef.current = newValue;
1265-
});
1249+
if (value !== current) {
1250+
setPrevious(current);
1251+
setCurrent(value);
1252+
}
12661253

1267-
return previousRef.current;
1254+
return previous;
12681255
}
12691256

12701257
function getRandomNumber(min, max) {
@@ -1376,9 +1363,7 @@ export function useScript(src, options = {}) {
13761363
const cachedScriptStatuses = React.useRef({});
13771364

13781365
React.useEffect(() => {
1379-
if (!src) {
1380-
return;
1381-
}
1366+
if (!src) return;
13821367

13831368
const cachedScriptStatus = cachedScriptStatuses.current[src];
13841369
if (cachedScriptStatus === "ready" || cachedScriptStatus === "error") {
@@ -1389,26 +1374,12 @@ export function useScript(src, options = {}) {
13891374
let script = document.querySelector(`script[src="${src}"]`);
13901375

13911376
if (script) {
1392-
setStatus(
1393-
script.getAttribute("data-status") ?? cachedScriptStatus ?? "loading"
1394-
);
1377+
setStatus(cachedScriptStatus ?? "loading");
13951378
} else {
13961379
script = document.createElement("script");
13971380
script.src = src;
13981381
script.async = true;
1399-
script.setAttribute("data-status", "loading");
14001382
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);
14121383
}
14131384

14141385
const setStateFromEvent = (event) => {

0 commit comments

Comments
 (0)