Skip to content

Commit 28f89eb

Browse files
committed
fix: typing
1 parent 671281e commit 28f89eb

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

packages/ui/src/components/Popup/helpers.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 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

57
type AddPrefixToUnion<T extends string, P extends string> = T extends string
68
? `${P}${T}`
@@ -13,6 +15,10 @@ type AutoPlacements =
1315
export type PopupPlacement = AutoPlacements | Placements
1416
export type PopupAlign = 'start' | 'center'
1517

18+
const isGenericPlacement = (
19+
placement: PopupPlacement,
20+
): placement is Placements => PLACEMENTS.includes(placement as Placements)
21+
1622
export const DEFAULT_ARROW_WIDTH = 8 // in px
1723
const SPACE = 4 // in px
1824
const 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

Comments
 (0)