Skip to content

Commit

Permalink
[Upd] Overlay placement is calculated only when the component is moun…
Browse files Browse the repository at this point in the history
…ted or updated for performance matters
  • Loading branch information
LaChope committed Jan 26, 2022
1 parent 38537c5 commit dc0f316
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/components/comment/QuestionCommentIcon.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ import {motion} from 'framer-motion/dist/framer-motion';

const QuestionCommentIcon = (props) => {
const context = useContext(ConfigurationContext);

const target = useRef(null);
const dragRef = useRef(null);
const overlayTarget = useRef(null);

const [show, setShow] = useState(false);
const [overlayPlacement, setOverlayPlacement] = useState("right");

useEffect(() => {
getOverlayPlacement(overlayTarget.current)
});

const hideOverlay = () => {
setShow(false);
}
Expand Down Expand Up @@ -57,16 +64,16 @@ const QuestionCommentIcon = (props) => {
return _getComments().length;
}

const getOverlayPlacement = (el) => {
if (!el) return;
const getOverlayPlacement = (overlayTarget) => {
if (!overlayTarget) return;

if (el.getBoundingClientRect().x > window.innerWidth / 2) {
if (overlayTarget.getBoundingClientRect().x > window.innerWidth / 2) {
setOverlayPlacement("left");
} else setOverlayPlacement("right");
};

return (
<div ref={el => getOverlayPlacement(el)}>
<div ref={overlayTarget}>
<span ref={target} onClick={onClickHandler}>
<CommentBubble/>
{getCommentsLength() > 0 ? <Badge className="comment-badge" pill variant="primary">{getCommentsLength()}</Badge> : null}
Expand Down

0 comments on commit dc0f316

Please sign in to comment.