-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuse-body-scroll-lock-upgrade.ts
50 lines (45 loc) · 1.28 KB
/
use-body-scroll-lock-upgrade.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { useEffect, useRef } from "react";
import {
enableBodyScroll,
disableBodyScroll,
clearAllBodyScrollLocks,
} from "./body-scroll-lock";
export function useBodyScrollLockUpgrade(isLock: boolean) {
const scrollLockerUpgradeRef = useRef<any>(null);
useEffect(() => {
if (!scrollLockerUpgradeRef.current) {
clearAllBodyScrollLocks();
return;
}
if (isLock) {
disableBodyScroll(scrollLockerUpgradeRef.current, {
reserveScrollBarGap: false,
allowTouchMove: (el: any) => {
if (!el) return false;
while (el && el !== document.body) {
if (typeof el.className === "string") {
// 弹框内部需要滚动时给该部分添上此类名,即可恢复滚动
if (el.className.indexOf("body-scroll-lock-ignore") > -1) {
return true;
}
}
el = el.parentElement;
}
return false;
},
});
console.log("disableBodyScroll");
} else {
enableBodyScroll(scrollLockerUpgradeRef.current);
}
}, [isLock]);
useEffect(() => {
const ele = scrollLockerUpgradeRef.current;
return () => {
if (ele) {
enableBodyScroll(ele);
}
};
}, []);
return scrollLockerUpgradeRef;
}