react-native-utils-scale
Version:
Provide solutions to make your app flexible for different screen sizes, different devices.
92 lines (78 loc) • 2.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useScale = exports.useDetectDevice = void 0;
var _reactNative = require("react-native");
var _devicesWithNotch = require("./devicesWithNotch");
const {
width,
height
} = _reactNative.Dimensions.get('window');
const {
UtilsScale
} = _reactNative.NativeModules;
const {
checkSmallDevice,
checkhasNotch,
getModel,
getBrand,
deviceInch
} = UtilsScale.getConstants();
const pixelDensity = _reactNative.PixelRatio.get();
const isTablet = () => {
const adjustedWidth = width * pixelDensity;
const adjustedHeight = height * pixelDensity;
if (pixelDensity < 2 && (adjustedWidth >= 1000 || adjustedHeight >= 1000)) {
return true;
} else {
return pixelDensity === 2 && (adjustedWidth >= 1920 || adjustedHeight >= 1920);
}
};
const hasNotch = () => {
if (_reactNative.Platform.OS === 'ios') {
if (isTablet()) {
return false;
} else {
return checkhasNotch;
}
} else {
const model = getModel;
const brand = getBrand;
const notch = _devicesWithNotch.devicesWithNotch.findIndex(item => item.brand.toLowerCase() === brand.toLowerCase() && item.model.toLowerCase() === model.toLowerCase()) !== -1;
return notch;
}
};
const useScale = {
fontScale: function () {
let number = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
if (deviceInch > 0) {
const value = (deviceInch + pixelDensity) / 10;
const scale = number * Number(value.toFixed(1));
return scale;
}
return number;
},
scale: function () {
let number = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
if (deviceInch > 0) {
const value = (deviceInch + (pixelDensity + 0.5)) / 10;
const scale = number * Number(value.toFixed(1));
return scale;
}
return number;
}
};
exports.useScale = useScale;
const useDetectDevice = {
isTablet: isTablet(),
isSmallDevice: checkSmallDevice,
isAndroid: _reactNative.Platform.OS === 'android',
isIOS: _reactNative.Platform.OS === 'ios',
hasNotch: hasNotch(),
deviceInch: Number(deviceInch.toFixed(1)),
width: _reactNative.Dimensions.get('window').width,
height: _reactNative.Dimensions.get('window').height
};
exports.useDetectDevice = useDetectDevice;
//# sourceMappingURL=index.js.map