Skip to content

Commit

Permalink
fix(Popup): observer the resize of the target element
Browse files Browse the repository at this point in the history
  • Loading branch information
shervinchen committed Oct 26, 2024
1 parent aff8aa1 commit 0a70c12
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions jest-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ afterEach(() => {
});
jest.useRealTimers();
});

global.ResizeObserver = jest.fn().mockImplementation(() => ({
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
}));
3 changes: 3 additions & 0 deletions packages/Popup/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
useMutationObserver,
usePortal,
useResize,
useResizeObserver,
useSSR,
} from '../utils/hooks';

Expand Down Expand Up @@ -48,6 +49,8 @@ const Popup: FC<PropsWithChildren<PopupProps>> = ({

useMutationObserver(targetRef?.current, updatePopupPosition);

useResizeObserver(targetRef?.current, updatePopupPosition);

useMutationObserver(container, updatePopupPosition, {
attributes: true,
childList: false,
Expand Down
15 changes: 15 additions & 0 deletions packages/Popup/__tests__/Popup.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@ describe('Popup', () => {
expect(mockGetPopupPosition).toHaveBeenCalledTimes(2);
});

test('should update popup position when targetRef size change', async () => {
render(
<Component
visible
targetRef={targetRefMock}
getPopupContainer={getPopupContainerMock}
/>
);
expect(mockGetPopupPosition).toHaveBeenCalledTimes(1);
targetRefMock.current.style.height = '200px';
await waitFor(() => {
expect(mockGetPopupPosition).toHaveBeenCalledTimes(2);
});
});

test('should update popup position when document size change', async () => {
render(
<Component visible targetRef={targetRefMock} getPopupContainer={null} />
Expand Down
1 change: 1 addition & 0 deletions packages/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ const Select = forwardRef<SelectRef, PropsWithChildren<SelectProps>>(
multiple,
internalValue,
handleChange,
selectRef,
dropdownHeight,
getPopupContainer,
type,
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { usePortal } from './usePortal';
import { useResize } from './useResize';
import { useClickAnyWhere } from './useClickAnyWhere';
import { useMutationObserver } from './useMutationObserver';
import { useResizeObserver } from './useResizeObserver';

export {
useControlled,
Expand All @@ -14,4 +15,5 @@ export {
useResize,
useClickAnyWhere,
useMutationObserver,
useResizeObserver,
};
17 changes: 17 additions & 0 deletions packages/utils/hooks/useResizeObserver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useEffect } from 'react';

export const useResizeObserver = (
element: HTMLElement | null | undefined,
callback: ResizeObserverCallback
) => {
useEffect(() => {
if (!element) return;

const observer = new ResizeObserver(callback);
observer.observe(element);

return () => {
observer.disconnect();
};
}, [element, callback]);
};

0 comments on commit 0a70c12

Please sign in to comment.