From 023d84c3505202e5ccc40ff50abb6307eacf4ba3 Mon Sep 17 00:00:00 2001 From: "plebeius.eth" Date: Sat, 10 Jun 2023 12:28:21 +0200 Subject: [PATCH] improve PostOnHover positioning logic --- src/components/PostOnHover.jsx | 12 ++- src/components/StateLabel.jsx | 2 - src/components/views/All.jsx | 102 +++++++++++++++++------ src/components/views/Board.jsx | 109 ++++++++++++++++++------- src/components/views/Subscriptions.jsx | 102 +++++++++++++++++------ src/components/views/Thread.jsx | 85 +++++++++++++------ 6 files changed, 298 insertions(+), 114 deletions(-) diff --git a/src/components/PostOnHover.jsx b/src/components/PostOnHover.jsx index 095eddce..5683234a 100644 --- a/src/components/PostOnHover.jsx +++ b/src/components/PostOnHover.jsx @@ -22,8 +22,16 @@ const PostOnHover = ({ cid, feed }) => { const stateString = useStateString(reply); return ( - +
diff --git a/src/components/StateLabel.jsx b/src/components/StateLabel.jsx index 57243607..b4c69f0a 100644 --- a/src/components/StateLabel.jsx +++ b/src/components/StateLabel.jsx @@ -15,8 +15,6 @@ const StateLabel = ({ commentIndex, className }) => { if (commentIndex === undefined) return null; - console.log(commentIndex, stateString, comment); - return ( commentIndex && stateString !== "Succeeded" ? ( (comment.state === "failed" || (stateString === undefined && !isLoading)) ? ( diff --git a/src/components/views/All.jsx b/src/components/views/All.jsx index b83b8f5c..62a2d8e0 100755 --- a/src/components/views/All.jsx +++ b/src/components/views/All.jsx @@ -198,8 +198,8 @@ const All = () => { useLayoutEffect(() => { if (postOnHoverRef.current) { - const rect = postOnHoverRef.current.getBoundingClientRect(); - setPostOnHoverHeight(rect.height); + const rect = postOnHoverRef.current.getBoundingClientRect(); + setPostOnHoverHeight(rect.height); } }, [outOfViewCid]); @@ -542,10 +542,22 @@ const All = () => { handleQuoteHover(reply, null, () => { setOutOfViewCid(reply.cid); const rect = backlinkRefs.current[reply.cid].getBoundingClientRect(); - setOutOfViewPosition({ - top: rect.top + window.scrollY - rect.height / 2, - left: rect.left + rect.width + 5, - }); + const distanceToRight = window.innerWidth - rect.right; + + if (distanceToRight < 200) { + setOutOfViewPosition({ + top: rect.top + window.scrollY - rect.height / 2, + left: rect.left - rect.innerWidth - 5, + maxWidth: rect.left + }); + + } else { + setOutOfViewPosition({ + top: rect.top + window.scrollY - rect.height / 2, + left: rect.left + rect.width - 5, + maxWidth: window.innerWidth - rect.left - rect.width + }); + } }); }} onMouseLeave={() => { @@ -747,10 +759,22 @@ const All = () => { handleQuoteHover(reply, null, () => { setOutOfViewCid(reply.cid); const rect = backlinkRefs.current[reply.cid].getBoundingClientRect(); - setOutOfViewPosition({ - top: rect.top + window.scrollY - rect.height / 2, - left: rect.left + rect.width + 5, - }); + const distanceToRight = window.innerWidth - rect.right; + + if (distanceToRight < 200) { + setOutOfViewPosition({ + top: rect.top + window.scrollY - rect.height / 2, + left: rect.left - rect.innerWidth - 5, + maxWidth: rect.left + }); + + } else { + setOutOfViewPosition({ + top: rect.top + window.scrollY - rect.height / 2, + left: rect.left + rect.width - 5, + maxWidth: window.innerWidth - rect.left - rect.width + }); + } }); }} onMouseLeave={() => { @@ -836,10 +860,22 @@ const All = () => { setOutOfViewCid(reply.parentCid); console.log("risposta: ", reply.parentCid); const rect = quoteRefs.current[shortParentCid].getBoundingClientRect(); + const distanceToRight = window.innerWidth - rect.right; + + if (distanceToRight < 200) { setOutOfViewPosition({ top: rect.top + window.scrollY - rect.height / 2, - left: rect.left + rect.width + 5, + left: rect.left - rect.innerWidth - 5, + maxWidth: rect.left }); + + } else { + setOutOfViewPosition({ + top: rect.top + window.scrollY - rect.height / 2, + left: rect.left + rect.width - 5, + maxWidth: window.innerWidth - rect.left - rect.width + }); + } } }); }} @@ -871,10 +907,22 @@ const All = () => { } else { setOutOfViewCid(reply.parentCid); const rect = quoteRefs.current[shortParentCid].getBoundingClientRect(); - setOutOfViewPosition({ - top: rect.top + window.scrollY - rect.height / 2, - left: rect.left + rect.width + 5, - }); + const distanceToRight = window.innerWidth - rect.right; + + if (distanceToRight < 200) { + setOutOfViewPosition({ + top: rect.top + window.scrollY - rect.height / 2, + left: rect.left - rect.innerWidth - 5, + maxWidth: rect.left + }); + + } else { + setOutOfViewPosition({ + top: rect.top + window.scrollY - rect.height / 2, + left: rect.left + rect.width - 5, + maxWidth: window.innerWidth - rect.left - rect.width + }); + } } }); }} @@ -1247,23 +1295,23 @@ const All = () => { )}
- {createPortal( + {outOfViewCid && outOfViewPosition && createPortal(
- -
- , document.body - )} + > + +
+ , document.body)}