[d565449] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | exports.getClosestBody = void 0;
|
---|
| 4 | var react_1 = require("react");
|
---|
| 5 | var util_1 = require("./misc/util");
|
---|
| 6 | function getClosestBody(el) {
|
---|
| 7 | if (!el) {
|
---|
| 8 | return null;
|
---|
| 9 | }
|
---|
| 10 | else if (el.tagName === 'BODY') {
|
---|
| 11 | return el;
|
---|
| 12 | }
|
---|
| 13 | else if (el.tagName === 'IFRAME') {
|
---|
| 14 | var document_1 = el.contentDocument;
|
---|
| 15 | return document_1 ? document_1.body : null;
|
---|
| 16 | }
|
---|
| 17 | else if (!el.offsetParent) {
|
---|
| 18 | return null;
|
---|
| 19 | }
|
---|
| 20 | return getClosestBody(el.offsetParent);
|
---|
| 21 | }
|
---|
| 22 | exports.getClosestBody = getClosestBody;
|
---|
| 23 | function preventDefault(rawEvent) {
|
---|
| 24 | var e = rawEvent || window.event;
|
---|
| 25 | // Do not prevent if the event has more than one touch (usually meaning this is a multi touch gesture like pinch to zoom).
|
---|
| 26 | if (e.touches.length > 1)
|
---|
| 27 | return true;
|
---|
| 28 | if (e.preventDefault)
|
---|
| 29 | e.preventDefault();
|
---|
| 30 | return false;
|
---|
| 31 | }
|
---|
| 32 | var isIosDevice = util_1.isBrowser &&
|
---|
| 33 | window.navigator &&
|
---|
| 34 | window.navigator.platform &&
|
---|
| 35 | /iP(ad|hone|od)/.test(window.navigator.platform);
|
---|
| 36 | var bodies = new Map();
|
---|
| 37 | var doc = typeof document === 'object' ? document : undefined;
|
---|
| 38 | var documentListenerAdded = false;
|
---|
| 39 | exports.default = !doc
|
---|
| 40 | ? function useLockBodyMock(_locked, _elementRef) {
|
---|
| 41 | if (_locked === void 0) { _locked = true; }
|
---|
| 42 | }
|
---|
| 43 | : function useLockBody(locked, elementRef) {
|
---|
| 44 | if (locked === void 0) { locked = true; }
|
---|
| 45 | var bodyRef = react_1.useRef(doc.body);
|
---|
| 46 | elementRef = elementRef || bodyRef;
|
---|
| 47 | var lock = function (body) {
|
---|
| 48 | var bodyInfo = bodies.get(body);
|
---|
| 49 | if (!bodyInfo) {
|
---|
| 50 | bodies.set(body, { counter: 1, initialOverflow: body.style.overflow });
|
---|
| 51 | if (isIosDevice) {
|
---|
| 52 | if (!documentListenerAdded) {
|
---|
| 53 | util_1.on(document, 'touchmove', preventDefault, { passive: false });
|
---|
| 54 | documentListenerAdded = true;
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 | else {
|
---|
| 58 | body.style.overflow = 'hidden';
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | else {
|
---|
| 62 | bodies.set(body, {
|
---|
| 63 | counter: bodyInfo.counter + 1,
|
---|
| 64 | initialOverflow: bodyInfo.initialOverflow,
|
---|
| 65 | });
|
---|
| 66 | }
|
---|
| 67 | };
|
---|
| 68 | var unlock = function (body) {
|
---|
| 69 | var bodyInfo = bodies.get(body);
|
---|
| 70 | if (bodyInfo) {
|
---|
| 71 | if (bodyInfo.counter === 1) {
|
---|
| 72 | bodies.delete(body);
|
---|
| 73 | if (isIosDevice) {
|
---|
| 74 | body.ontouchmove = null;
|
---|
| 75 | if (documentListenerAdded) {
|
---|
| 76 | util_1.off(document, 'touchmove', preventDefault);
|
---|
| 77 | documentListenerAdded = false;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | else {
|
---|
| 81 | body.style.overflow = bodyInfo.initialOverflow;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | else {
|
---|
| 85 | bodies.set(body, {
|
---|
| 86 | counter: bodyInfo.counter - 1,
|
---|
| 87 | initialOverflow: bodyInfo.initialOverflow,
|
---|
| 88 | });
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | };
|
---|
| 92 | react_1.useEffect(function () {
|
---|
| 93 | var body = getClosestBody(elementRef.current);
|
---|
| 94 | if (!body) {
|
---|
| 95 | return;
|
---|
| 96 | }
|
---|
| 97 | if (locked) {
|
---|
| 98 | lock(body);
|
---|
| 99 | }
|
---|
| 100 | else {
|
---|
| 101 | unlock(body);
|
---|
| 102 | }
|
---|
| 103 | }, [locked, elementRef.current]);
|
---|
| 104 | // clean up, on un-mount
|
---|
| 105 | react_1.useEffect(function () {
|
---|
| 106 | var body = getClosestBody(elementRef.current);
|
---|
| 107 | if (!body) {
|
---|
| 108 | return;
|
---|
| 109 | }
|
---|
| 110 | return function () {
|
---|
| 111 | unlock(body);
|
---|
| 112 | };
|
---|
| 113 | }, []);
|
---|
| 114 | };
|
---|