Skip to content

Commit 79fdb3e

Browse files
authored
feat(fuselage-hooks): usePosition listening to components size changes (#782)
* wip: resize observer * Listen to element size changes * Add observer mock to tests * fix one more test
1 parent 583f759 commit 79fdb3e

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

packages/fuselage-hooks/src/usePosition.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import { withResizeObserverMock } from 'testing-utils/mocks/withResizeObserverMock';
2+
13
import {
24
getPositionStyle,
35
getTargetBoundaries,
46
getVariantBoundaries,
57
} from './usePosition';
68
// TODO: add tests targeting the hook itself
79

10+
withResizeObserverMock();
11+
812
const container = {
913
bottom: 1000,
1014
height: 1000,

packages/fuselage-hooks/src/usePosition.ts

+8
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,18 @@ const useBoundingClientRect = (
140140
const parents = getScrollParents(element.current);
141141
const passive = { passive: true };
142142

143+
const observer = new ResizeObserver(() => {
144+
if (!element.current) {
145+
return;
146+
}
147+
callback();
148+
});
149+
observer.observe(element.current);
143150
window.addEventListener('resize', callback);
144151
parents.forEach((el) => el.addEventListener('scroll', callback, passive));
145152

146153
return () => {
154+
observer.disconnect();
147155
window.removeEventListener('resize', callback);
148156
parents.forEach((el) => el.removeEventListener('scroll', callback));
149157
};

packages/fuselage/src/components/Menu/Menu.spec.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import { composeStories } from '@storybook/testing-react';
22
import { render, screen } from '@testing-library/react';
33
import userEvent from '@testing-library/user-event';
44
import React from 'react';
5+
import { withResizeObserverMock } from 'testing-utils/mocks/withResizeObserverMock';
56

67
import * as stories from './Menu.stories';
78

9+
withResizeObserverMock();
10+
811
const { Simple } = composeStories(stories);
912

1013
describe('[Menu Component]', () => {

0 commit comments

Comments
 (0)