Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Apr 24, 2022
1 parent ef4b57b commit 6705af8
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/utilities/dom/src/body-scroll-lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ export function preventBodyScroll(opts?: PreventScrollOptions) {
const docEl = doc.documentElement

function preventScrollStandard() {
if (docEl.getAttribute("data-prevent-scroll") === "true") return
if (docEl.hasAttribute("scroll-lock")) return

const fn = pipe(
setStyle(docEl, "paddingRight", `${win.innerWidth - docEl.clientWidth}px`),
setStyle(docEl, "overflow", "hidden"),
() => docEl.setAttribute("data-prevent-scroll", "true"),
() => docEl.setAttribute("scroll-lock", "true"),
)

return () => fn?.()
return pipe(fn, () => docEl.removeAttribute("scroll-lock"))
}

function preventScrollMobileSafari() {
if (docEl.getAttribute("data-prevent-scroll") === "true") return
if (docEl.hasAttribute("scroll-lock")) return

let scrollable: HTMLElement | undefined
let lastY = 0
Expand Down Expand Up @@ -121,13 +121,14 @@ export function preventBodyScroll(opts?: PreventScrollOptions) {
addDomEvent(doc, "touchend", onTouchEnd, { passive: false, capture: true }),
addDomEvent(doc, "focus", onFocus, true),
addDomEvent(win, "scroll", onWindowScroll),
() => docEl.setAttribute("data-prevent-scroll", "true"),
() => docEl.setAttribute("scroll-lock", "true"),
)

return () => {
restoreStyles()
removeEvents()
win.scrollTo(scrollX, scrollY)
docEl.removeAttribute("scroll-lock")
}
}

Expand Down

0 comments on commit 6705af8

Please sign in to comment.