From 3692e44700e9f314a5f04a053158a314b3e0e001 Mon Sep 17 00:00:00 2001 From: namniak Date: Mon, 22 Nov 2021 16:04:13 -0500 Subject: [PATCH] feature: make it a service --- .gitignore | 1 + src/services/lock-body-scroll.ts | 36 ++++++++++++++++++++++++++ src/utils/lock-body-scroll.ts | 43 -------------------------------- types.d.ts | 2 +- 4 files changed, 38 insertions(+), 44 deletions(-) create mode 100644 src/services/lock-body-scroll.ts delete mode 100644 src/utils/lock-body-scroll.ts diff --git a/.gitignore b/.gitignore index bcd833f6..e96d01d3 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ codeship.aes /public/workbox* # misc +.idea .vscode .DS_Store *.log* diff --git a/src/services/lock-body-scroll.ts b/src/services/lock-body-scroll.ts new file mode 100644 index 00000000..6c9b6496 --- /dev/null +++ b/src/services/lock-body-scroll.ts @@ -0,0 +1,36 @@ +import { getScrollTop } from 'get-scroll'; +import noop from 'no-op'; + +import scrollPage from '@/utils/scroll-page'; + +/** + * Lock and unlock body scroll with page position restoration + */ +class LockBodyScroll { + scrollPosY = 0; + isLocked = false; + + lock = + typeof window === 'undefined' + ? noop + : () => { + this.scrollPosY = getScrollTop(); + document.body.style.position = 'fixed'; + document.body.style.overflowY = 'scroll'; + document.body.style.marginTop = `-${this.scrollPosY}px`; + this.isLocked = true; + }; + + unlock = + typeof window === 'undefined' + ? noop + : (skipPositionRestore: Boolean = false) => { + document.body.style.position = ''; + document.body.style.overflowY = ''; + document.body.style.marginTop = ''; + !skipPositionRestore && scrollPage({ y: this.scrollPosY, duration: 0 }); + this.isLocked = false; + }; +} + +export default new LockBodyScroll(); diff --git a/src/utils/lock-body-scroll.ts b/src/utils/lock-body-scroll.ts deleted file mode 100644 index 80e276be..00000000 --- a/src/utils/lock-body-scroll.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { getScrollTop } from 'get-scroll'; - -import scrollPage from './scroll-page'; - -/** - * Lock and unlock body scroll with page position restoration - */ -function lockBodyScroll() { - let scrollPosY = 0; - let isLocked = false; - - /** - * Lock body scroll - */ - function lock() { - if (!isLocked) { - scrollPosY = getScrollTop(); - document.body.style.position = 'fixed'; - document.body.style.overflowY = 'scroll'; - document.body.style.marginTop = `-${scrollPosY}px`; - isLocked = true; - } - } - - /** - * Unlock body scroll - * - * @param {boolean} [skipPositionRestore=false] - Skip page position restoration flag - */ - function unlock(skipPositionRestore = false) { - if (isLocked) { - document.body.style.position = ''; - document.body.style.overflowY = ''; - document.body.style.marginTop = ''; - !skipPositionRestore && scrollPage({ y: scrollPosY }); - isLocked = false; - } - } - - return { isLocked, lock, unlock }; -} - -export default lockBodyScroll(); diff --git a/types.d.ts b/types.d.ts index 53ea7486..d6130479 100644 --- a/types.d.ts +++ b/types.d.ts @@ -5,7 +5,7 @@ interface Window { type SetTimeout = ReturnType; declare module 'no-op' { - export default function noop(): void; + export default function noop(...args): void; } declare module 'get-scroll' {