11import type { RefObject } from 'react'
22
3- type Placements = 'top' | 'right' | 'bottom' | 'left' | 'nested-menu'
3+ const PLACEMENTS = [ 'top' , 'right' , 'bottom' , 'left' , 'nested-menu' ] as const
4+
5+ type Placements = ( typeof PLACEMENTS ) [ number ]
46
57type AddPrefixToUnion < T extends string , P extends string > = T extends string
68 ? `${P } ${T } `
@@ -13,6 +15,10 @@ type AutoPlacements =
1315export type PopupPlacement = AutoPlacements | Placements
1416export type PopupAlign = 'start' | 'center'
1517
18+ const isGenericPlacement = (
19+ placement : PopupPlacement ,
20+ ) : placement is Placements => PLACEMENTS . includes ( placement as Placements )
21+
1622export const DEFAULT_ARROW_WIDTH = 8 // in px
1723const SPACE = 4 // in px
1824const TOTAL_USED_SPACE = 0 // in px
@@ -276,16 +282,16 @@ export const computePositions = ({
276282 popupRef . current as HTMLDivElement
277283 ) . getBoundingClientRect ( )
278284
279- const placementBasedOnWindowSize = placement . startsWith ( 'auto' )
280- ? computePlacement ( {
281- autoPlacement : placement as AutoPlacements ,
285+ const placementBasedOnWindowSize = isGenericPlacement ( placement )
286+ ? placement
287+ : computePlacement ( {
288+ autoPlacement : placement ,
282289 childrenStructuredRef : childrenRect ,
283290 offsetParent,
284291 offsetParentRect,
285292 popupPortalTarget,
286293 popupStructuredRef,
287294 } )
288- : placement
289295
290296 const {
291297 top : childrenTop ,
@@ -320,7 +326,7 @@ export const computePositions = ({
320326 : childrenLeft - parentLeft + childrenWidth
321327
322328 const popupOverflow = getPopupOverflowFromParent (
323- placementBasedOnWindowSize as Placements ,
329+ placementBasedOnWindowSize ,
324330 offsetParentRect ,
325331 childrenRect ,
326332 popupStructuredRef ,
0 commit comments