fix(reply modal): get mobile scroll position from hook before render

This commit is contained in:
plebeius.eth
2024-04-30 11:08:01 +02:00
parent d797dbd0bd
commit 1e91119687
4 changed files with 17 additions and 9 deletions
+10 -2
View File
@@ -1,9 +1,14 @@
import { useState, useCallback } from 'react';
import useWindowWidth from './use-window-width';
const useReplyModal = () => {
const [showReplyModal, setShowReplyModal] = useState(false);
const [activeCid, setActiveCid] = useState<string | null>(null);
// on mobile, the position is absolute instead of fixed, so we need to calculate the top position
const isMobile = useWindowWidth() < 640;
const [scrollY, setScrollY] = useState<number>(0);
const closeModal = useCallback(() => {
setActiveCid(null);
setShowReplyModal(false);
@@ -11,6 +16,9 @@ const useReplyModal = () => {
const openReplyModal = useCallback(
(cid: string) => {
if (isMobile) {
setScrollY(window.scrollY);
}
if (activeCid && activeCid !== cid) {
closeModal();
setTimeout(() => {
@@ -22,10 +30,10 @@ const useReplyModal = () => {
setShowReplyModal(true);
}
},
[activeCid, closeModal],
[activeCid, closeModal, isMobile],
);
return { showReplyModal, activeCid, openReplyModal, closeModal };
return { activeCid, closeModal, openReplyModal, scrollY, showReplyModal };
};
export default useReplyModal;