Skip to content

Commit 6098687

Browse files
ggazzodougfabris
andauthored
fix(fuselage-hooks): Fix usePosition (#967)
Co-authored-by: Douglas Fabris <devfabris@gmail.com>
1 parent f292111 commit 6098687

File tree

1 file changed

+37
-46
lines changed

1 file changed

+37
-46
lines changed

packages/fuselage-hooks/src/usePosition.ts

+37-46
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { RefObject } from 'react';
2-
import { useEffect, useRef } from 'react';
1+
import type { AllHTMLAttributes, RefObject } from 'react';
2+
import { useState, useEffect, useRef } from 'react';
33

4-
import { useDebouncedState } from './useDebouncedState';
4+
import { useDebouncedCallback } from './useDebouncedCallback';
55
import { useMutableCallback } from './useMutableCallback';
66

77
export type Positions = 'top' | 'left' | 'bottom' | 'right';
@@ -51,26 +51,8 @@ type VariantBoundaries = {
5151
hm: number;
5252
};
5353

54-
type PositionStyle = {
55-
top: string;
56-
left: string;
57-
position?: 'fixed';
58-
ZIndex: '9999';
59-
transition: 'none !important';
60-
bottom?: '0px';
61-
overflowY?: 'auto';
62-
};
63-
64-
type PositionEmptyResult = {
65-
visibility: 'hidden';
66-
top: '-9999px';
67-
left: '-9999px';
68-
position: 'fixed';
69-
overflowY?: 'initial';
70-
};
71-
7254
type PositionResult = {
73-
style: PositionStyle | PositionEmptyResult;
55+
style: AllHTMLAttributes<HTMLElement>['style'];
7456
placement?: Placements;
7557
};
7658

@@ -101,8 +83,6 @@ const emptyStyle: PositionResult = {
10183
style: {
10284
position: 'fixed',
10385
visibility: 'hidden',
104-
top: '-9999px',
105-
left: '-9999px',
10686
},
10787
};
10888

@@ -247,7 +227,7 @@ export const getPositionStyle = ({
247227
bottom: `${margin}px`,
248228
overflowY: 'auto',
249229
}),
250-
ZIndex: '9999',
230+
...({ zIndex: '9999' } as any),
251231
},
252232
placement: `${PlacementMap[placementAttempt]}-${
253233
PlacementMap[variantsAttempts[0]]
@@ -311,30 +291,41 @@ export const usePosition = <T extends Element, R extends Element>(
311291
} = options;
312292
const container = useRef(containerElement);
313293

314-
const [style, setStyle] = useDebouncedState<PositionResult>(emptyStyle, 10);
294+
const [style, setStyle] = useState<PositionResult>(emptyStyle);
315295

316-
const callback = useMutableCallback(() => {
317-
const boundaries = target.current.getBoundingClientRect();
318-
const targetBoundaries = getTargetBoundaries({
319-
referenceBox: reference.current.getBoundingClientRect(),
320-
target: boundaries,
321-
margin,
322-
});
323-
const variantStore = getVariantBoundaries({
324-
referenceBox: reference.current.getBoundingClientRect(),
325-
target: boundaries,
326-
});
327-
setStyle(
328-
getPositionStyle({
329-
placement,
330-
container: container.current.getBoundingClientRect(),
331-
targetBoundaries,
332-
variantStore,
296+
const callback = useDebouncedCallback(
297+
useMutableCallback(() => {
298+
const clone = target.current.cloneNode(true) as HTMLElement;
299+
300+
clone.style.bottom = '';
301+
clone.id = 'clone';
302+
target.current.parentElement.appendChild(clone);
303+
const boundaries = clone.getBoundingClientRect();
304+
target.current.parentElement.removeChild(clone);
305+
306+
const targetBoundaries = getTargetBoundaries({
307+
referenceBox: reference.current.getBoundingClientRect(),
333308
target: boundaries,
334309
margin,
335-
})
336-
);
337-
});
310+
});
311+
const variantStore = getVariantBoundaries({
312+
referenceBox: reference.current.getBoundingClientRect(),
313+
target: boundaries,
314+
});
315+
316+
setStyle(
317+
getPositionStyle({
318+
placement,
319+
container: container.current.getBoundingClientRect(),
320+
targetBoundaries,
321+
variantStore,
322+
target: boundaries,
323+
margin,
324+
})
325+
);
326+
}),
327+
10
328+
);
338329

339330
useBoundingClientRect(target, watch, callback);
340331
useBoundingClientRect(reference, watch, callback);

0 commit comments

Comments
 (0)