Skip to content

Commit f3071b4

Browse files
add screen-info module; implement functions to retrieve and listen for screen size changes
1 parent 3f5a426 commit f3071b4

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/browser/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from "./prevent-close";
88
export * from "./network";
99
export * from "./vibration";
1010
export * from "./geolocation";
11+
export * from "./screen-info";

src/browser/screen-info/index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export interface ScreenInfo {
2+
width: number;
3+
height: number;
4+
availWidth: number;
5+
availHeight: number;
6+
devicePixelRatio: number;
7+
orientation: string;
8+
isLandscape: boolean;
9+
}
10+
11+
/**
12+
* Get current screen and viewport info
13+
*/
14+
export function getScreenInfo(): ScreenInfo {
15+
const orientation =
16+
(screen.orientation || {}).type || "unknown";
17+
18+
return {
19+
width: window.innerWidth,
20+
height: window.innerHeight,
21+
availWidth: screen.availWidth,
22+
availHeight: screen.availHeight,
23+
devicePixelRatio: window.devicePixelRatio,
24+
orientation: orientation,
25+
isLandscape: window.innerWidth > window.innerHeight
26+
};
27+
}
28+
29+
/**
30+
* Listen for screen size changes (resize/orientation change)
31+
*/
32+
export function onScreenResize(
33+
callback: (info: ScreenInfo) => void
34+
): void {
35+
const handler = () => callback(getScreenInfo());
36+
37+
window.addEventListener("resize", handler);
38+
window.addEventListener("orientationchange", handler);
39+
}

0 commit comments

Comments
 (0)