Skip to content

Commit 4662cf0

Browse files
authored
fix(fuselage-hooks): usePosition fallback (#588)
1 parent 4a7ae0b commit 4662cf0

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

packages/fuselage-hooks/src/usePosition.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ type PositionStyle = {
5757
zIndex?: '9999';
5858
transition?: 'none !important';
5959
opacity?: 0 | 1;
60+
bottom?: 0;
61+
display?: 'flex';
6062
};
6163

6264
type PositionResult = {
@@ -85,7 +87,7 @@ const fallbackOrder = {
8587
bottom: 'btrl',
8688
right: 'rltb',
8789
left: 'lrbt',
88-
};
90+
} as const;
8991

9092
const getParents = function (element: Element): Array<Element | Window> {
9193
// Set up a parent array
@@ -136,12 +138,14 @@ export const getPositionStyle = ({
136138
targetBoundaries,
137139
variantStore,
138140
target,
141+
margin = 0,
139142
}: {
140143
placement: Placements;
141144
target: DOMRect;
142145
container: DOMRect;
143146
targetBoundaries: Boundaries;
144147
variantStore?: VariantBoundaries;
148+
margin?: number;
145149
}): PositionResult => {
146150
if (!targetBoundaries) {
147151
return {};
@@ -197,11 +201,11 @@ export const getPositionStyle = ({
197201
opacity: 1,
198202
},
199203
placement: `${PlacementMap[placementAttempt]}-${PlacementMap[v]}`,
200-
} as PositionResult;
204+
} as unknown as PositionResult;
201205
}
202206
}
203207

204-
const placementAttempt = targetBoundaries[placementAttempts[0]];
208+
const placementAttempt = placementAttempts[0];
205209

206210
const directionVertical = ['t', 'b'].includes(placementAttempt);
207211

@@ -214,6 +218,10 @@ export const getPositionStyle = ({
214218
top: `${point}px`,
215219
left: `${variantPoint}px`,
216220
position: 'fixed',
221+
...(bottom < target.height + point && {
222+
bottom: margin,
223+
display: 'flex',
224+
}),
217225
zIndex: '9999',
218226
opacity: 1,
219227
},
@@ -279,7 +287,7 @@ export const usePosition = (
279287
} = options;
280288
const container = useRef(containerElement);
281289

282-
const [style, setStyle] = useDebouncedState({} as PositionResult, 10);
290+
const [style, setStyle] = useDebouncedState<PositionResult>({}, 10);
283291

284292
const callback = useMutableCallback(() => {
285293
const boundaries = target.current.getBoundingClientRect();
@@ -299,6 +307,7 @@ export const usePosition = (
299307
targetBoundaries,
300308
variantStore,
301309
target: boundaries,
310+
margin,
302311
})
303312
);
304313
});

0 commit comments

Comments
 (0)