Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Popover): move position with target when in scroll container #2749

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/gamut/src/Popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ import {
} from './elements';
import { PopoverProps } from './types';

const findScrollingParent = ({
parentElement,
}: HTMLElement): HTMLElement | null => {
if (parentElement) {
const { overflow, overflowY, overflowX } = getComputedStyle(parentElement);
if (
[overflow, overflowY, overflowX].some((val) =>
['scroll', 'auto'].includes(val)
)
) {
return parentElement;
}
return findScrollingParent(parentElement);
}
return null;
};

export const Popover: React.FC<PopoverProps> = ({
animation,
align = 'left',
Expand Down Expand Up @@ -58,6 +75,23 @@ export const Popover: React.FC<PopoverProps> = ({
setTargetRect(targetRef?.current?.getBoundingClientRect());
}, [targetRef, isOpen, width, height, x, y]);

useEffect(() => {
if (!targetRef.current) {
return;
}
const scrollingParent = findScrollingParent(
targetRef.current as HTMLElement
);
if (!scrollingParent?.addEventListener) {
return;
}
const handler = () => {
setTargetRect(targetRef?.current?.getBoundingClientRect());
};
scrollingParent.addEventListener('scroll', handler);
return () => scrollingParent.removeEventListener('scroll', handler);
}, [targetRef]);

useEffect(() => {
if (targetRect) {
const inView =
Expand Down
Loading