mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix(reply modal): get mobile scroll position from hook before render
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user