-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: drawer body scroll 되는 문제 해결 - drawer style fixed로 수정 * fix: useBodyScrollLock 훅 개선
- Loading branch information
Showing
4 changed files
with
51 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
|
||
type Options = { | ||
enabled?: boolean; | ||
}; | ||
|
||
export function useBodyScrollLock(options?: Options) { | ||
const enabled = options?.enabled; | ||
const scrollRef = useRef(0); | ||
|
||
const lockScroll = useCallback(() => { | ||
scrollRef.current = window.scrollY; | ||
document.body.style.overflow = 'hidden'; | ||
document.body.style.marginTop = `-${scrollRef.current}px`; | ||
}, []); | ||
|
||
const openScroll = useCallback(() => { | ||
document.body.style.removeProperty('overflow'); | ||
document.body.style.removeProperty('margin-top'); | ||
window.scrollTo(0, scrollRef.current); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (enabled === undefined) { | ||
return; | ||
} | ||
|
||
if (enabled) { | ||
lockScroll(); | ||
} else { | ||
openScroll(); | ||
} | ||
}, [enabled, lockScroll, openScroll]); | ||
|
||
return { lockScroll, openScroll }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters