diff --git a/src/components/DateCalendarPopup.jsx b/src/components/DateCalendarPopup.jsx index cdf613f..c701fbb 100644 --- a/src/components/DateCalendarPopup.jsx +++ b/src/components/DateCalendarPopup.jsx @@ -17,30 +17,52 @@ export default function DateCalendarPopup({ date, onClose, anchorRef }) { const [month, setMonth] = useState(isValidDate ? selectedDate.getMonth() : new Date().getMonth()); // Двухпроходное позиционирование: - // 1) сразу после монтирования попап невидим (opacity: 0), но уже в DOM — + // 1) сразу после монтирования попап невидим, но уже в DOM — // нужно, чтобы измерить его реальные width/height. - // 2) как только размеры известны — пересчитываем координаты так, чтобы - // попап открывался влево-вверх от якоря, и показываем (opacity: 1). + // 2) как только размеры известны — ставим его влево-вверх от якоря + // (или вниз, если сверху не хватает места) и показываем. + // Координаты — относительно окна (position: fixed): лента листается во + // внутреннем контейнере, и window.scrollY тут не помогает. useLayoutEffect(() => { if (!anchorRef?.current || !ref.current) return; const anchorRect = anchorRef.current.getBoundingClientRect(); const popupRect = ref.current.getBoundingClientRect(); + const GAP = 4; - setCoords({ - top: anchorRect.top + window.scrollY - popupRect.height - 4, - left: anchorRect.right + window.scrollX - popupRect.width, - }); + const above = anchorRect.top - popupRect.height - GAP; + const top = above >= GAP ? above : anchorRect.bottom + GAP; + const left = Math.min( + Math.max(GAP, anchorRect.right - popupRect.width), + window.innerWidth - popupRect.width - GAP + ); + + setCoords({ top, left }); }, [anchorRef, month, year]); // пересчитываем и при смене месяца — высота грид может немного плавать + // Закрываем при клике/тапе вне попапа и кнопки, при любой прокрутке + // (ленты, окна) и при изменении размера окна — иначе попап «отрывается» + // от кнопки и висит поверх ленты. useEffect(() => { - const handler = (e) => { + const onPointerDown = (e) => { const clickedAnchor = anchorRef?.current?.contains(e.target); const clickedPopup = ref.current?.contains(e.target); if (!clickedAnchor && !clickedPopup) onClose(); }; - document.addEventListener("mousedown", handler); - return () => document.removeEventListener("mousedown", handler); + const onScroll = (e) => { + if (ref.current?.contains(e.target)) return; + onClose(); + }; + // capture: срабатывает, даже если кто-то по пути остановил всплытие, + // а scroll внутренних контейнеров до document не всплывает вовсе. + document.addEventListener("pointerdown", onPointerDown, true); + document.addEventListener("scroll", onScroll, true); + window.addEventListener("resize", onClose); + return () => { + document.removeEventListener("pointerdown", onPointerDown, true); + document.removeEventListener("scroll", onScroll, true); + window.removeEventListener("resize", onClose); + }; }, [onClose, anchorRef]); const goToPrevMonth = () => { @@ -79,8 +101,11 @@ export default function DateCalendarPopup({ date, onClose, anchorRef }) {