@chief-editor/ui
Version:
UI Component for chief editor
1 lines • 1.04 kB
JavaScript
import{getScrollbarWidth,isClient}from"@co-hooks/dom";import{useCallback}from"react";var lockCount=0,locked=!1,documentBodyStyle={},scrollbarWidth=getScrollbarWidth();export function useLockScreen(){return{lockScreen:useCallback((function(){if(lockCount++,!locked&&(locked=!0,isClient())){documentBodyStyle={overflow:document.body.style.getPropertyValue("overflow"),position:document.body.style.getPropertyValue("position"),width:document.body.style.getPropertyValue("width")};var o={overflow:"hidden"};if(document.body.clientWidth<window.innerWidth&&scrollbarWidth){["fixed","absolute"].indexOf(documentBodyStyle.position||"")>=0?o.position=documentBodyStyle.position:o.position="relative";var e=documentBodyStyle.width?getComputedStyle(document.body).getPropertyValue("width"):"100%";o.width="calc("+e+" - "+scrollbarWidth+"px)"}Object.assign(document.body.style,o)}}),[]),unlockScreen:useCallback((function(){locked&&lockCount--,!lockCount&&locked&&(locked=!1,isClient()&&Object.assign(document.body.style,documentBodyStyle))}),[])}}