Skip to content

Commit

Permalink
Merge pull request #1223 from Wizleap-Inc/fix/popup-position
Browse files Browse the repository at this point in the history
fix: ポップアップの座標計算修正(React)
  • Loading branch information
ichi-h authored Feb 27, 2024
2 parents 736839e + dc45aec commit 81ccf0d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-fishes-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wizleap-inc/wiz-ui-react": patch
---

popup の座標計算タイミングを追加
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {
FC,
ReactNode,
RefObject,
useCallback,
useEffect,
useLayoutEffect,
useRef,
useState,
} from "react";
Expand Down Expand Up @@ -81,38 +83,42 @@ const Popup: FC<Props> = ({

const isPopupFixed = hasFixedOrStickyParent(anchorElement.current);

const updatePopupPosition = useCallback(() => {
if (!anchorElement.current || !popupRef.current) {
return;
}

const fontSize = window.getComputedStyle(document.body).fontSize;
const contentRect = popupRef.current.getBoundingClientRect();

setPopupPosition(
getPopupPosition({
anchorRect: anchorElement.current.getBoundingClientRect(),
popupSize: {
width: contentRect.width,
height: contentRect.height,
},
directionKey: direction,
gap: parseFloat(getSpacingCss(gap) || "0") * parseFloat(fontSize),
screenSize: {
width: document.body.clientWidth,
height: Math.max(document.body.clientHeight, window.innerHeight),
},
scroll: {
x: isPopupFixed ? 0 : window.scrollX,
y: isPopupFixed ? 0 : window.scrollY,
},
isDirectionFixed,
})
);
}, [anchorElement, direction, gap, isDirectionFixed, isPopupFixed]);

useEffect(() => {
const anchor = anchorElement.current;
const content = popupRef.current;
if (!isActuallyOpen || !anchor || !content) {
return;
}
const updatePopupPosition = () => {
const fontSize = window.getComputedStyle(document.body).fontSize;
const contentRect = content.getBoundingClientRect();

setPopupPosition(
getPopupPosition({
anchorRect: anchor.getBoundingClientRect(),
popupSize: {
width: contentRect.width,
height: contentRect.height,
},
directionKey: direction,
gap: parseFloat(getSpacingCss(gap) || "0") * parseFloat(fontSize),
screenSize: {
width: document.body.clientWidth,
height: Math.max(document.body.clientHeight, window.innerHeight),
},
scroll: {
x: isPopupFixed ? 0 : window.scrollX,
y: isPopupFixed ? 0 : window.scrollY,
},
isDirectionFixed,
})
);
};

updatePopupPosition();
window.addEventListener("scroll", updatePopupPosition);
window.addEventListener("resize", updatePopupPosition);
Expand All @@ -123,14 +129,13 @@ const Popup: FC<Props> = ({
window.removeEventListener("scroll", updatePopupPosition);
anchorResizeObserver.disconnect();
};
}, [
anchorElement,
direction,
gap,
isActuallyOpen,
isDirectionFixed,
isPopupFixed,
]);
}, [anchorElement, isActuallyOpen, updatePopupPosition]);

useLayoutEffect(() => {
if (children) {
updatePopupPosition();
}
}, [children, updatePopupPosition]);

return (
<WizPortal container={document.body}>
Expand Down

0 comments on commit 81ccf0d

Please sign in to comment.