UNPKG

@shopgate/engage

Version:
5 lines 1.44 kB
import MobileDetect from'mobile-detect';import{logger}from'@shopgate/engage/core/helpers';var md=new MobileDetect(navigator.userAgent);var SELECTOR_BODY='html, body';var SELECTOR_CONTENT='#AppContent';var currentRef=null;/** * Toggles the body scroll on mobile phones * @param {boolean} locked The lock state * @param {*} ref A reference parameter to take care that only the locker can unlock */export var toggleBodyScroll=function toggleBodyScroll(){var locked=arguments.length>0&&arguments[0]!==undefined?arguments[0]:true;var ref=arguments.length>1&&arguments[1]!==undefined?arguments[1]:null;if(!ref){logger.error('No caller reference provided');}if(!md.phone()){return;}/* eslint-disable no-param-reassign */if(locked){currentRef=ref;var offsetY=window.pageYOffset;document.querySelectorAll(SELECTOR_BODY).forEach(function(el){el.style.height='100%';el.style.position='fixed';el.style.overflow='hidden';});var content=document.querySelector(SELECTOR_CONTENT);if(content){content.style.top="".concat(offsetY*-1,"px");}}else{if(currentRef!==ref){return;}document.querySelectorAll(SELECTOR_BODY).forEach(function(el){el.style.removeProperty('height');el.style.removeProperty('position');el.style.removeProperty('overflow');});var _content=document.querySelector(SELECTOR_CONTENT);if(_content){window.scrollTo(0,Math.abs(parseInt(_content.style.top||0,10)));_content.style.removeProperty('top');}}/* eslint-enable no-param-reassign */};