Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix withOrientationChange typing + expose useMobileOrientation hook #120

Merged
merged 2 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ declare namespace ReactDeviceDetect {
condition?: boolean;
}

export function deviceDetect (): any;
export function deviceDetect(): any;

export function withOrientationChange (Component: any): any;
export function withOrientationChange<P, S = {}>(Component: React.Component<P, S> | React.FC<P>): React.ComponentClass<P, S>;

export function useMobileOrientation(): { isPortrait: boolean, isLandscape: boolean, orientation: 'portrait' | 'landscape' };

export class BrowserView extends React.Component<ViewProps> {
}
Expand Down
24 changes: 23 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"devDependencies": {
"@babel/preset-env": "^7.5.5",
"@babel/preset-react": "^7.0.0",
"@types/react": "^17.0.0",
"enzyme": "^3.1.0",
"enzyme-adapter-react-16": "^1.0.1",
"jest": "^24.8.0",
Expand Down
36 changes: 36 additions & 0 deletions src/components/helpers/useOrientationChange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { useCallback, useEffect, useState } from "react";
import { isMobile } from "./selectors";

export function useMobileOrientation() {
const [state, setState] = useState(() => {
const orientation = window.innerWidth > window.innerHeight ? 90 : 0;
return {
isPortrait: orientation === 0,
isLandscape: orientation === 90,
orientation: orientation === 0 ? 'portrait' : 'landscape'
}
});
const handleOrientationChange = useCallback(() => {
const orientation = window.innerWidth > window.innerHeight ? 90 : 0;
const next = {
isPortrait: orientation === 0,
isLandscape: orientation === 90,
orientation: orientation === 0 ? 'portrait' : 'landscape'
}
state.orientation !== next.orientation && setState(next);
}, [state.orientation]);
useEffect(() => {
if (typeof window !== undefined && isMobile) {
handleOrientationChange();
window.addEventListener("load", handleOrientationChange, false);
window.addEventListener("resize", handleOrientationChange, false);
}
return () => {
window.removeEventListener("resize", handleOrientationChange, false);
window.removeEventListener("load", handleOrientationChange, false);
}
}, [handleOrientationChange]);
return state;
}

export { useMobileOrientation };