Merge pull request 'Orientation Context' (#124) from dean/orientation-context into master
Reviewed-on: railbird/railbird-mobile#124 Reviewed-by: Ivan Malison <ivanmalison@gmail.com>
This commit is contained in:
commit
5a9578d554
35
src/context/OrientationContext.tsx
Normal file
35
src/context/OrientationContext.tsx
Normal file
@ -0,0 +1,35 @@
|
||||
import React, { createContext, useContext, useEffect, useState } from "react";
|
||||
import { Dimensions } from "react-native";
|
||||
|
||||
const OrientationContext = createContext<string | null>(null);
|
||||
|
||||
export const useOrientation = (): string | null =>
|
||||
useContext(OrientationContext);
|
||||
|
||||
export const OrientationProvider = ({ children }) => {
|
||||
const [orientation, setOrientation] = useState(getOrientation());
|
||||
|
||||
function getOrientation() {
|
||||
const { width, height } = Dimensions.get("window");
|
||||
return width < height ? "portrait" : "landscape";
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
const updateOrientation = () => {
|
||||
setOrientation(getOrientation());
|
||||
};
|
||||
|
||||
const subscription = Dimensions.addEventListener(
|
||||
"change",
|
||||
updateOrientation,
|
||||
);
|
||||
|
||||
return () => subscription.remove();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<OrientationContext.Provider value={orientation}>
|
||||
{children}
|
||||
</OrientationContext.Provider>
|
||||
);
|
||||
};
|
Loading…
Reference in New Issue
Block a user