Skip to content

Commit 5281ead

Browse files
authored
Merge pull request #120 from ShaMan123/master
fix withOrientationChange typing + expose useMobileOrientation hook
2 parents 9ad10d6 + 9419658 commit 5281ead

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ declare namespace ReactDeviceDetect {
8080
condition?: boolean;
8181
}
8282

83-
export function deviceDetect (): any;
83+
export function deviceDetect(): any;
8484

85-
export function withOrientationChange (Component: any): any;
85+
export function withOrientationChange<P, S = {}>(Component: React.Component<P, S> | React.FC<P>): React.ComponentClass<P, S>;
86+
87+
export function useMobileOrientation(): { isPortrait: boolean, isLandscape: boolean, orientation: 'portrait' | 'landscape' };
8688

8789
export class BrowserView extends React.Component<ViewProps> {
8890
}

package-lock.json

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"devDependencies": {
5151
"@babel/preset-env": "^7.5.5",
5252
"@babel/preset-react": "^7.0.0",
53+
"@types/react": "^17.0.0",
5354
"enzyme": "^3.1.0",
5455
"enzyme-adapter-react-16": "^1.0.1",
5556
"jest": "^24.8.0",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React, { useCallback, useEffect, useState } from "react";
2+
import { isMobile } from "./selectors";
3+
4+
export function useMobileOrientation() {
5+
const [state, setState] = useState(() => {
6+
const orientation = window.innerWidth > window.innerHeight ? 90 : 0;
7+
return {
8+
isPortrait: orientation === 0,
9+
isLandscape: orientation === 90,
10+
orientation: orientation === 0 ? 'portrait' : 'landscape'
11+
}
12+
});
13+
const handleOrientationChange = useCallback(() => {
14+
const orientation = window.innerWidth > window.innerHeight ? 90 : 0;
15+
const next = {
16+
isPortrait: orientation === 0,
17+
isLandscape: orientation === 90,
18+
orientation: orientation === 0 ? 'portrait' : 'landscape'
19+
}
20+
state.orientation !== next.orientation && setState(next);
21+
}, [state.orientation]);
22+
useEffect(() => {
23+
if (typeof window !== undefined && isMobile) {
24+
handleOrientationChange();
25+
window.addEventListener("load", handleOrientationChange, false);
26+
window.addEventListener("resize", handleOrientationChange, false);
27+
}
28+
return () => {
29+
window.removeEventListener("resize", handleOrientationChange, false);
30+
window.removeEventListener("load", handleOrientationChange, false);
31+
}
32+
}, [handleOrientationChange]);
33+
return state;
34+
}
35+
36+
export { useMobileOrientation };

0 commit comments

Comments
 (0)