-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.ts
46 lines (36 loc) 路 902 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { useEffect, useState } from 'react';
export interface IOrientationState {
angle: number;
type: string;
}
export const defaultState: IOrientationState = {
angle: 0,
type: 'landscape-primary',
};
const useOrientation = (initialState: IOrientationState = defaultState) => {
const [state, setState] = useState(initialState);
const onOrientationChangeEvent = () => {
const { orientation } = screen as any;
const { angle, type } = orientation;
if (!orientation) {
setState(initialState);
}
setState({ angle, type });
};
useEffect(() => {
window.addEventListener(
'orientationchange',
onOrientationChangeEvent,
true,
);
return () => {
window.addEventListener(
'orientationchange',
onOrientationChangeEvent,
true,
);
};
}, []);
return state;
};
export default useOrientation;