Clamp a modal position so it stays within the viewport
( x: number, y: number, width: number, height: number, )
| 40 | |
| 41 | /** Clamp a modal position so it stays within the viewport */ |
| 42 | function clampToViewport( |
| 43 | x: number, |
| 44 | y: number, |
| 45 | width: number, |
| 46 | height: number, |
| 47 | ): { x: number; y: number } { |
| 48 | const vw = typeof window !== 'undefined' ? window.innerWidth : VIEWPORT_FALLBACK.width; |
| 49 | const vh = typeof window !== 'undefined' ? window.innerHeight : VIEWPORT_FALLBACK.height; |
| 50 | const padding = MODAL_POSITION.viewportPadding; |
| 51 | return { |
| 52 | x: Math.max(padding, Math.min(x, vw - width - padding)), |
| 53 | y: Math.max(padding, Math.min(y, vh - height - padding)), |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | function getViewportSize(): { width: number; height: number } { |
| 58 | return typeof window !== 'undefined' |
no outgoing calls
no test coverage detected