File tree 5 files changed +95
-0
lines changed
5 files changed +95
-0
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ This is port of [`libreact`](https://github.com/streamich/libreact) to React Hoo
18
18
- [ ` useLocation ` ] ( ./docs/useLocation.md )
19
19
- [ ` useMedia ` ] ( ./docs/useMedia.md )
20
20
- [ ` useMediaDevices ` ] ( ./docs/useMediaDevices.md )
21
+ - [ ` useOrientation ` ] ( ./docs/useOrientation.md )
21
22
- [ ` useSize ` ] ( ./docs/useSize.md )
22
23
- [ ` useWindowSize ` ] ( ./docs/useWindowSize.md )
23
24
- Side effects
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change
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
+ )
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import useLocation from './useLocation';
7
7
import useMap from './useMap' ;
8
8
import useMedia from './useMedia' ;
9
9
import useMediaDevices from './useMediaDevices' ;
10
+ import useOrientation from './useOrientation' ;
10
11
import useSize from './useSize' ;
11
12
import useTitle from './useTitle' ;
12
13
import useToggle from './useToggle' ;
@@ -22,6 +23,7 @@ export {
22
23
useMap ,
23
24
useMedia ,
24
25
useMediaDevices ,
26
+ useOrientation ,
25
27
useSize ,
26
28
useTitle ,
27
29
useToggle ,
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments