Created
August 30, 2017 15:48
-
-
Save samueljmurray/eeb9495146ef0aad24f534cecd17487c to your computer and use it in GitHub Desktop.
A helper function for React Native which returns values based on screen height
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Dimensions } from "react-native"; | |
// Height | |
const screenSizes = [ | |
{ | |
name: "xs", | |
height: 568 | |
}, | |
{ | |
name: "s", | |
height: 667 | |
} | |
]; | |
// Example usage: | |
// screenSize({xs: 8, s: 12}, 16) | |
// screenSize({s: 12}, 16) | |
// screenSize({xs: 8}, 16) | |
export function screenSize(screenSizeOptions, defaultValue) { | |
const matchedScreenSizes = screenSizes.filter((screenSize) => { | |
return Dimensions.get("window").height < screenSize.height; | |
}); | |
let value; | |
const hasScreenSizeOption = matchedScreenSizes.some((matchedScreenSize) => { | |
if (screenSizeOptions.hasOwnProperty(matchedScreenSize.name)) { | |
value = screenSizeOptions[matchedScreenSize.name]; | |
return true; | |
} else { | |
return false; | |
} | |
}); | |
if (!hasScreenSizeOption) { | |
value = defaultValue; | |
} | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment