Skip to content

Commit c533b97

Browse files
committed
feat: 🎸 add useOrientation hook
1 parent 4ea0277 commit c533b97

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This is port of [`libreact`](https://github.com/streamich/libreact) to React Hoo
1818
- [`useLocation`](./docs/useLocation.md)
1919
- [`useMedia`](./docs/useMedia.md)
2020
- [`useMediaDevices`](./docs/useMediaDevices.md)
21+
- [`useOrientation`](./docs/useOrientation.md)
2122
- [`useSize`](./docs/useSize.md)
2223
- [`useWindowSize`](./docs/useWindowSize.md)
2324
- Side effects

docs/useOrientation.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# `useOrientation`
2+
3+
React sensor hook that tracks screen orientation of user's device.
4+
5+
Returns state in the following shape
6+
7+
```js
8+
{
9+
angle: 0,
10+
type: 'landscape-primary'
11+
}
12+
```
13+
14+
15+
## Usage
16+
17+
```jsx
18+
import {useOrientation} from 'react-use';
19+
20+
const Demo = () => {
21+
const state = useOrientation();
22+
23+
return (
24+
<pre>
25+
{JSON.stringify(state, null, 2)}
26+
</pre>
27+
);
28+
};
29+
```
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as React from 'react';
2+
import {storiesOf} from '@storybook/react';
3+
import {useOrientation} from '..';
4+
5+
const Demo = () => {
6+
const state = useOrientation();
7+
8+
return (
9+
<pre>
10+
{JSON.stringify(state, null, 2)}
11+
</pre>
12+
);
13+
};
14+
15+
storiesOf('useOrientation', module)
16+
.add('Example', () =>
17+
<Demo/>
18+
)

src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import useLocation from './useLocation';
77
import useMap from './useMap';
88
import useMedia from './useMedia';
99
import useMediaDevices from './useMediaDevices';
10+
import useOrientation from './useOrientation';
1011
import useSize from './useSize';
1112
import useTitle from './useTitle';
1213
import useToggle from './useToggle';
@@ -22,6 +23,7 @@ export {
2223
useMap,
2324
useMedia,
2425
useMediaDevices,
26+
useOrientation,
2527
useSize,
2628
useTitle,
2729
useToggle,

src/useOrientation.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {useState, useEffect} from './react';
2+
import {on, off} from './util';
3+
4+
export interface State {
5+
angle: number;
6+
type: string;
7+
}
8+
9+
const defaultState: State = {
10+
angle: 0,
11+
type: 'landscape-primary'
12+
};
13+
14+
const useOrientation = (initialState: State = defaultState) => {
15+
const [state, setState] = useState(initialState);
16+
17+
useEffect(() => {
18+
let mounted = true;
19+
20+
const onChange = () => {
21+
if (mounted) {
22+
const {orientation} = screen as any;
23+
24+
if (!orientation) {
25+
setState(initialState);
26+
}
27+
28+
const {angle, type} = orientation;
29+
setState({angle, type});
30+
}
31+
};
32+
33+
on(window, 'orientationchange', onChange);
34+
onChange();
35+
36+
return () => {
37+
mounted = false;
38+
off(window, 'orientationchange', onChange);
39+
};
40+
}, [0]);
41+
42+
return state;
43+
};
44+
45+
export default useOrientation;

0 commit comments

Comments
 (0)