Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Jam3/nextjs-boilerplate into main
Browse files Browse the repository at this point in the history
  • Loading branch information
DonghyukJacobJang committed Nov 23, 2021
2 parents 14f23cb + 913dff1 commit b34e0fd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ tsconfig.tsbuildinfo
/public/workbox*

# misc
.idea
.vscode
.DS_Store
*.log*
Expand Down
36 changes: 36 additions & 0 deletions src/services/lock-body-scroll.ts
Original file line number Diff line number Diff line change
@@ -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();
43 changes: 0 additions & 43 deletions src/utils/lock-body-scroll.ts

This file was deleted.

2 changes: 1 addition & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface Window {
type SetTimeout = ReturnType<typeof setTimeout>;

declare module 'no-op' {
export default function noop(): void;
export default function noop(...args): void;
}

declare module 'get-scroll' {
Expand Down

0 comments on commit b34e0fd

Please sign in to comment.