-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlightboxViewport.ts
More file actions
42 lines (39 loc) · 1.47 KB
/
Copy pathlightboxViewport.ts
File metadata and controls
42 lines (39 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Lightbox viewport + "match screen orientation" heuristics.
*
* Browser-only helpers (guarded with `typeof window`). Safe to import from
* Node tests if callers avoid calling `getLightboxViewportSize` without
* mocking `window`.
*
* Gene: `sdk.media.gen1` · pairs with `createLightbox` + app-level PhotoSwipe.
*/
/** Treat near-square image / viewport as square (no 90° offer). */
export const LIGHTBOX_ORIENTATION_ASPECT_EPS = 1.02;
export function getLightboxViewportSize(): { w: number; h: number } {
if (typeof window === 'undefined') return { w: 0, h: 0 };
const vv = window.visualViewport;
if (vv && vv.width > 0 && vv.height > 0) {
return { w: vv.width, h: vv.height };
}
return { w: window.innerWidth, h: window.innerHeight };
}
/**
* True when a single 90° rotation can swap the image's long axis with the
* viewport's long axis (portrait viewport + landscape image, or the converse).
*/
export function shouldOfferOrientationMatch(
naturalW: number,
naturalH: number,
viewportW: number,
viewportH: number
): boolean {
if (naturalW <= 0 || naturalH <= 0 || viewportW <= 0 || viewportH <= 0) {
return false;
}
const e = LIGHTBOX_ORIENTATION_ASPECT_EPS;
const imagePortrait = naturalH >= naturalW * e;
const imageLandscape = naturalW >= naturalH * e;
const screenPortrait = viewportH >= viewportW * e;
const screenLandscape = viewportW >= viewportH * e;
return (screenPortrait && imageLandscape) || (screenLandscape && imagePortrait);
}