fix(resources): lock body on opening sidebar

This commit is contained in:
Jean-Philippe Sirois
2019-09-24 01:31:48 -04:00
parent 0931f9e4ce
commit 94e2489a85
3 changed files with 29 additions and 1 deletions
+23
View File
@@ -0,0 +1,23 @@
import { useState } from "react"
export const useLockBodyScroll = () => {
const documentGlobal = typeof document !== "undefined" && document
const [locked, setLocked] = useState<boolean>(false)
function lock() {
setLocked(true)
}
function unlock() {
setLocked(false)
}
const body = documentGlobal && document.querySelector("body")
const gatsbyNode = documentGlobal && document.querySelector<HTMLDivElement>("#___gatsby")
if (body && gatsbyNode) {
body.style.overflow = locked ? "hidden" : ""
gatsbyNode.style.overflowY = locked ? "scroll" : ""
}
return { locked, lock, unlock }
}