improve PostOnHover positioning logic

This commit is contained in:
plebeius.eth
2023-06-10 12:28:21 +02:00
parent bb6bb379ca
commit 023d84c350
6 changed files with 298 additions and 114 deletions
+10 -2
View File
@@ -22,8 +22,16 @@ const PostOnHover = ({ cid, feed }) => {
const stateString = useStateString(reply);
return (
<Container selectedStyle={selectedStyle} style={{margin: '0', padding: '0',
whiteSpace: 'nowrap', overflow: 'visible'}}>
<Container selectedStyle={selectedStyle} style={{
margin: '0',
padding: '0',
whiteSpace: 'normal',
maxWidth: '100vw',
overflowWrap: 'break-word',
wordWrap: 'break-word',
wordBreak: 'break-all',
boxSizing: 'border-box',
}}>
<BoardForm selectedStyle={selectedStyle} style={{margin: '0', padding: '0'}}>
<div className="board" style={{margin: '0', padding: '0'}}>
<div className="thread" style={{margin: '0', padding: '0'}}>
-2
View File
@@ -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)) ? (
+75 -27
View File
@@ -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 = () => {
<PostLoader />
)}
</div>
{createPortal(
{outOfViewCid && outOfViewPosition && createPortal(
<div
ref={postOnHoverRef}
ref={postOnHoverRef}
style={{
display: outOfViewCid ? "block" : "none",
display: "block",
position: "absolute",
top: outOfViewPosition.top - postOnHoverHeight / 2 + 10,
left: outOfViewPosition.left,
left: outOfViewPosition.left,
maxWidth: outOfViewPosition.maxWidth,
}}
>
<PostOnHover
cid={outOfViewCid}
feed={feed}
/>
</div>
, document.body
)}
>
<PostOnHover
cid={outOfViewCid}
feed={feed}
/>
</div>
, document.body)}
</BoardForm>
<Footer selectedStyle={selectedStyle}>
<Break id="break" selectedStyle={selectedStyle} style={{
+79 -30
View File
@@ -528,14 +528,16 @@ const Board = () => {
}
};
useLayoutEffect(() => {
if (postOnHoverRef.current) {
const rect = postOnHoverRef.current.getBoundingClientRect();
setPostOnHoverHeight(rect.height);
const rect = postOnHoverRef.current.getBoundingClientRect();
setPostOnHoverHeight(rect.height);
}
}, [outOfViewCid]);
return (
<>
<Helmet>
@@ -989,14 +991,26 @@ const Board = () => {
onMouseOver={(event) => {
event.stopPropagation();
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
});
}
setOutOfViewCid(reply.cid);
});
}}
}}
onMouseLeave={() => {
removeHighlight();
setOutOfViewCid(null);
@@ -1228,10 +1242,22 @@ const Board = () => {
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={() => {
@@ -1315,12 +1341,23 @@ const Board = () => {
return;
} else {
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
});
}
}
});
}}
@@ -1359,10 +1396,22 @@ const Board = () => {
} 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
});
}
}
});
}}
@@ -1743,23 +1792,23 @@ const Board = () => {
) : (<PostLoader />)
)}
</div>
{createPortal(
{outOfViewCid && outOfViewPosition && createPortal(
<div
ref={postOnHoverRef}
ref={postOnHoverRef}
style={{
display: outOfViewCid ? "block" : "none",
display: "block",
position: "absolute",
top: outOfViewPosition.top - postOnHoverHeight / 2 + 10,
left: outOfViewPosition.left,
left: outOfViewPosition.left,
maxWidth: outOfViewPosition.maxWidth,
}}
>
<PostOnHover
cid={outOfViewCid}
feed={feed}
/>
</div>
, document.body
)}
>
<PostOnHover
cid={outOfViewCid}
feed={feed}
/>
</div>
, document.body)}
</BoardForm>
<Footer selectedStyle={selectedStyle}>
<Break id="break" selectedStyle={selectedStyle} style={{
+75 -27
View File
@@ -197,8 +197,8 @@ const Subscriptions = () => {
useLayoutEffect(() => {
if (postOnHoverRef.current) {
const rect = postOnHoverRef.current.getBoundingClientRect();
setPostOnHoverHeight(rect.height);
const rect = postOnHoverRef.current.getBoundingClientRect();
setPostOnHoverHeight(rect.height);
}
}, [outOfViewCid]);
@@ -549,10 +549,22 @@ const Subscriptions = () => {
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={() => {
@@ -754,10 +766,22 @@ const Subscriptions = () => {
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={() => {
@@ -843,10 +867,22 @@ const Subscriptions = () => {
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
});
}
}
});
}}
@@ -878,10 +914,22 @@ const Subscriptions = () => {
} 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
});
}
}
});
}}
@@ -1252,23 +1300,23 @@ const Subscriptions = () => {
/>
)}
</div>
{createPortal(
{outOfViewCid && outOfViewPosition && createPortal(
<div
ref={postOnHoverRef}
ref={postOnHoverRef}
style={{
display: outOfViewCid ? "block" : "none",
display: "block",
position: "absolute",
top: outOfViewPosition.top - postOnHoverHeight / 2 + 10,
left: outOfViewPosition.left,
left: outOfViewPosition.left,
maxWidth: outOfViewPosition.maxWidth,
}}
>
<PostOnHover
cid={outOfViewCid}
feed={feed}
/>
</div>
, document.body
)}
>
<PostOnHover
cid={outOfViewCid}
feed={feed}
/>
</div>
, document.body)}
</BoardForm>
<Footer selectedStyle={selectedStyle}>
<Break id="break" selectedStyle={selectedStyle} style={{
+59 -26
View File
@@ -482,8 +482,8 @@ const Thread = () => {
useLayoutEffect(() => {
if (postOnHoverRef.current) {
const rect = postOnHoverRef.current.getBoundingClientRect();
setPostOnHoverHeight(rect.height);
const rect = postOnHoverRef.current.getBoundingClientRect();
setPostOnHoverHeight(rect.height);
}
}, [outOfViewCid]);
@@ -949,10 +949,21 @@ const Thread = () => {
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={() => {
@@ -1150,10 +1161,21 @@ const Thread = () => {
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={() => {
@@ -1233,10 +1255,21 @@ const Thread = () => {
} 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
});
}
}
});
}}
@@ -1639,23 +1672,23 @@ const Thread = () => {
) : (null)}
</>
)) : null}
{createPortal(
{outOfViewCid && outOfViewPosition && createPortal(
<div
ref={postOnHoverRef}
ref={postOnHoverRef}
style={{
display: outOfViewCid ? "block" : "none",
display: "block",
position: "absolute",
top: outOfViewPosition.top - postOnHoverHeight / 2 + 10,
left: outOfViewPosition.left,
left: outOfViewPosition.left,
maxWidth: outOfViewPosition.maxWidth,
}}
>
<PostOnHover
cid={outOfViewCid}
feed={comment}
/>
</div>
, document.body
)}
>
<PostOnHover
cid={outOfViewCid}
feed={comment}
/>
</div>
, document.body)}
</BoardForm>
<Footer selectedStyle={selectedStyle}>
<Break id="break" selectedStyle={selectedStyle} style={{