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:
dean 2024-02-20 18:00:00 -07:00
commit 5a9578d554

View 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>
);
};