-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(FullscreenModal): fix hooks implementation
- Loading branch information
Showing
6 changed files
with
94 additions
and
76 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
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useCallback, useState } from 'react'; | ||
|
||
import { useStickyObserver } from './useStickyObserver'; | ||
|
||
export const useStickyHeader = ( | ||
modalRef: React.MutableRefObject<HTMLElement>, | ||
modalHeaderRef: React.MutableRefObject<HTMLElement>, | ||
): { isFixed: boolean } => { | ||
const [isFixed, setIsFixed] = useState(false); | ||
|
||
const isInView = useCallback(() => { | ||
if (modalRef.current === null || modalHeaderRef.current === null) return; | ||
const scrollOffset = modalRef.current.scrollTop; | ||
const headerHeight = modalHeaderRef.current.scrollHeight; | ||
|
||
if (isFixed && scrollOffset <= headerHeight) { | ||
setIsFixed(false); | ||
} else if (!isFixed && scrollOffset > headerHeight) { | ||
setIsFixed(true); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [isFixed]); | ||
|
||
useStickyObserver(modalRef, modalHeaderRef, isInView); | ||
|
||
return { | ||
isFixed, | ||
}; | ||
}; |
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,47 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import debounce from 'lodash.debounce'; | ||
|
||
export const useStickyObserver = ( | ||
modalRef: React.MutableRefObject<HTMLElement>, | ||
elementRef: React.MutableRefObject<HTMLElement>, | ||
isInView: () => void, | ||
): void => { | ||
const observerRef = useRef(null); | ||
const initialRender = useRef(true); | ||
const scrollListener = debounce(isInView, 100); | ||
const resizeListener = debounce(isInView, 200); | ||
|
||
useEffect(() => { | ||
let currentModalRef; | ||
|
||
if (observerRef.current !== null) { | ||
observerRef.current.disconnect(); | ||
} | ||
observerRef.current = new MutationObserver(() => isInView()); | ||
|
||
if (modalRef.current !== null && elementRef.current !== null) { | ||
currentModalRef = modalRef.current; | ||
observerRef.current.observe(currentModalRef, { | ||
childList: true, | ||
subtree: true, | ||
attributes: true, | ||
}); | ||
isInView(); | ||
|
||
if (!initialRender) { | ||
currentModalRef.removeEventListener('scroll', scrollListener); | ||
window.removeEventListener('resize', resizeListener); | ||
} | ||
initialRender.current = false; | ||
|
||
currentModalRef.addEventListener('scroll', scrollListener); | ||
window.addEventListener('resize', resizeListener); | ||
} | ||
return () => { | ||
observerRef.current.disconnect(); | ||
currentModalRef.removeEventListener('scroll', scrollListener); | ||
window.removeEventListener('resize', resizeListener); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [isInView]); | ||
}; |