Skip to content

Commit e340ace

Browse files
Abraham Rosa VargasAbraham Rosa Vargas
Abraham Rosa Vargas
authored and
Abraham Rosa Vargas
committed
Added improvements for responsive methods, released version 0.0.2
1 parent 7d8c77d commit e340ace

File tree

4 files changed

+51
-28
lines changed

4 files changed

+51
-28
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rn-responsive-pixels",
3-
"version": "0.0.1-beta.7.7",
3+
"version": "0.0.2",
44
"description": "A React Native library for adaptive pixel scaling.",
55
"source": "./src/index.tsx",
66
"main": "./lib/commonjs/index.js",
@@ -177,4 +177,4 @@
177177
"type": "library",
178178
"version": "0.41.0"
179179
}
180-
}
180+
}

src/__tests__/dimensions.test.ts

+16-11
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,49 @@ const mockDimensions = {
99
};
1010

1111
describe('Responsive utility functions', () => {
12+
let consoleWarnSpy: jest.SpyInstance;
13+
1214
beforeAll(() => {
1315
// Mock Dimensions to return fixed values
1416
Dimensions.get = jest.fn(() => mockDimensions);
17+
// Mock console.warn to suppress warnings during tests
18+
consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
19+
});
20+
21+
afterAll(() => {
22+
jest.restoreAllMocks();
23+
consoleWarnSpy.mockRestore();
1524
});
1625

17-
it('should return an integer value for pixel sizes', () => {
26+
it('should return a numeric value for pixel sizes', () => {
1827
const value = 10;
1928
const result = resPx(value);
2029

21-
expect(Number.isInteger(result)).toBe(true);
30+
expect(typeof result).toBe('number');
2231
expect(result).toBeGreaterThanOrEqual(0);
2332
});
2433

25-
it('should return an integer value for text sizes', () => {
34+
it('should return a numeric value for text sizes', () => {
2635
const pixels = 20;
2736
const result = resText(pixels);
2837

29-
expect(Number.isInteger(result)).toBe(true);
38+
expect(typeof result).toBe('number');
3039
expect(result).toBeGreaterThanOrEqual(0);
3140
});
3241

33-
it('should return an integer value for platform-specific size', () => {
42+
it('should return a numeric value for platform-specific size', () => {
3443
Platform.OS = 'ios';
3544
const iosSize = 30;
3645
const androidSize = 40;
3746
const resultIos = resPlatformSize(iosSize, androidSize);
3847

39-
expect(Number.isInteger(resultIos)).toBe(true);
48+
expect(typeof resultIos).toBe('number');
4049
expect(resultIos).toBeGreaterThanOrEqual(0);
4150

4251
Platform.OS = 'android';
4352
const resultAndroid = resPlatformSize(iosSize, androidSize);
4453

45-
expect(Number.isInteger(resultAndroid)).toBe(true);
54+
expect(typeof resultAndroid).toBe('number');
4655
expect(resultAndroid).toBeGreaterThanOrEqual(0);
4756
});
4857

@@ -54,8 +63,4 @@ describe('Responsive utility functions', () => {
5463
expect(resPlatformSize(NaN, -30)).toBe(0);
5564
expect(resPlatformSize(-10, NaN)).toBe(0);
5665
});
57-
58-
afterAll(() => {
59-
jest.restoreAllMocks();
60-
});
6166
});

src/index.tsx src/index.ts

+31-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Dimensions, Platform, PixelRatio } from 'react-native';
22

33
const windowDimensions = Dimensions.get('window');
4-
const { height: windowScreenHeight, width: windowScreenWidth } =
5-
windowDimensions;
4+
const { height: windowScreenHeight, width: windowScreenWidth } = windowDimensions;
65

76
// Reference dimensions for a variety of devices
87
const referenceDimensions = [
@@ -33,7 +32,6 @@ const aspectRatioHeight = windowScreenHeight / referenceHeight;
3332
const adjustedRatio = Math.min(aspectRatioWidth, aspectRatioHeight) * minRatio;
3433

3534
// Optional scale factor (adjustable as needed)
36-
// This can be used to further adjust the final ratio
3735
const scaleFactor = 1.0;
3836

3937
// Calculate the final ratio considering the scale factor
@@ -66,28 +64,48 @@ const isValidValue = (value: number): boolean => {
6664
return valid;
6765
};
6866

67+
/**
68+
* Calculates a moderate scaling value.
69+
* @param {number} value - The original value to scale.
70+
* @param {number} factor - The scaling factor (default is 0.5 for moderate scaling).
71+
* @returns {number} The scaled value.
72+
*/
73+
const moderateScale = (value: number, factor: number = 0.5): number => {
74+
return value + (Math.round(value * ratio) - value) * factor;
75+
};
76+
6977
const responsivePixels = (value: number): number => {
70-
if (!isPlatformSupported()) return value;
71-
if (!isValidValue(value)) return 0;
78+
if (!isPlatformSupported()) {
79+
return value;
80+
}
81+
if (!isValidValue(value)) {
82+
return 0;
83+
}
7284

73-
return Math.round(value * ratio);
85+
return moderateScale(value);
7486
};
7587

7688
const textResponsive = (value: number): number => {
77-
if (!isPlatformSupported()) return value;
78-
if (!isValidValue(value)) return 0;
89+
if (!isPlatformSupported()) {
90+
return value;
91+
}
92+
if (!isValidValue(value)) {
93+
return 0;
94+
}
7995

8096
const scaleWithWidth = windowScreenWidth / referenceWidth; // Base size for text
8197
const size = value * scaleWithWidth;
8298

83-
const adjustedSize = PixelRatio.roundToNearestPixel(size) - 3;
84-
85-
return Math.round(adjustedSize);
99+
return PixelRatio.roundToNearestPixel(size);
86100
};
87101

88102
const getSizeForPlatform = (iosSize: number, androidSize: number): number => {
89-
if (!isPlatformSupported()) return iosSize;
90-
if (!isValidValue(iosSize) || !isValidValue(androidSize)) return 0;
103+
if (!isPlatformSupported()) {
104+
return iosSize;
105+
}
106+
if (!isValidValue(iosSize) || !isValidValue(androidSize)) {
107+
return 0;
108+
}
91109

92110
return responsivePixels(Platform.OS === 'ios' ? iosSize : androidSize);
93111
};

yarn.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -14735,7 +14735,7 @@ __metadata:
1473514735

1473614736
"typescript@patch:[email protected]#~builtin<compat/typescript>":
1473714737
version: 5.5.3
14738-
resolution: "typescript@patch:typescript@npm%3A5.5.3#~builtin<compat/typescript>::version=5.5.3&hash=14eedb"
14738+
resolution: "typescript@patch:typescript@npm%3A5.5.3#~builtin<compat/typescript>::version=5.5.3&hash=29ae49"
1473914739
bin:
1474014740
tsc: bin/tsc
1474114741
tsserver: bin/tsserver
@@ -14745,7 +14745,7 @@ __metadata:
1474514745

1474614746
"typescript@patch:typescript@^4.6.4 || ^5.2.2#~builtin<compat/typescript>":
1474714747
version: 5.5.4
14748-
resolution: "typescript@patch:typescript@npm%3A5.5.4#~builtin<compat/typescript>::version=5.5.4&hash=14eedb"
14748+
resolution: "typescript@patch:typescript@npm%3A5.5.4#~builtin<compat/typescript>::version=5.5.4&hash=29ae49"
1474914749
bin:
1475014750
tsc: bin/tsc
1475114751
tsserver: bin/tsserver

0 commit comments

Comments
 (0)