import type { Placement } from '@floating-ui/react'; export type FloatingPlacement = | 'top' | 'right' | 'bottom' | 'left' | 'top-left' | 'top-right' | 'right-top' | 'right-bottom' | 'bottom-left' | 'bottom-right' | 'left-top' | 'left-bottom'; export const POPOVER_OFFSET = 16; // By default the flip positioning explores only the opposite alternative. So if left is passed and there's no enough space // the right one gets chosen. If there's no space on both sides popover goes back to the initially chosen one left. // This mapping forces popover to try the four available positions before going back to the initial chosen one. export const fallbackPlacements: Record = { top: ['bottom', 'right', 'left'], bottom: ['top', 'right', 'left'], left: ['right', 'top', 'bottom'], right: ['left', 'top', 'bottom'], 'top-start': ['bottom-start', 'top-end', 'bottom-end'], 'top-end': ['bottom-end', 'top-start', 'bottom-start'], 'bottom-start': ['top-start', 'bottom-end', 'top-end'], 'bottom-end': ['top-end', 'bottom-start', 'top-start'], 'left-start': ['right-start', 'left-end', 'right-end'], 'left-end': ['right-end', 'left-start', 'right-start'], 'right-start': ['left-start', 'right-end', 'left-end'], 'right-end': ['left-end', 'right-start', 'left-start'], }; export const positionToPlacement = (position: FloatingPlacement): Placement => { switch (position) { case 'top-left': return 'top-start'; case 'top-right': return 'top-end'; case 'bottom-left': return 'bottom-start'; case 'bottom-right': return 'bottom-end'; case 'left-top': return 'left-start'; case 'left-bottom': return 'left-end'; case 'right-top': return 'right-start'; case 'right-bottom': return 'right-end'; default: return position; } }; export const mirrorPositionForRtl = (position: FloatingPlacement): FloatingPlacement => { switch (position) { case 'left': return 'right'; case 'right': return 'left'; case 'left-top': return 'right-top'; case 'left-bottom': return 'right-bottom'; case 'right-top': return 'left-top'; case 'right-bottom': return 'left-bottom'; default: // top-left/top-right/bottom-left/bottom-right map to start/end via // positionToPlacement, and floating-ui already flips start/end in RTL. return position; } };