Skip to content

Instantly share code, notes, and snippets.

@samueljmurray
Created August 30, 2017 15:48
Show Gist options
  • Save samueljmurray/eeb9495146ef0aad24f534cecd17487c to your computer and use it in GitHub Desktop.
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
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