-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
133 additions
and
58 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,9 @@ yarn add react-native-size-matters | |
``` | ||
|
||
## Motivation | ||
When developing with react-native, you need to manually adjust your app to look great on a variety of different screen sizes. That's a tedious job. | ||
react-native-size-matters provides some simple tooling to make your scaling a whole lot easier. | ||
The idea is to develop once on a standard ~5" screen mobile device and then simply apply the provided utils. | ||
When developing with react-native, you need to manually adjust your app to look great on a variety of different screen sizes. That's a tedious job. | ||
react-native-size-matters provides some simple tooling to make your scaling a whole lot easier. | ||
The idea is to develop once on a standard ~5" screen mobile device and then simply apply the provided utils. | ||
📖 You can read more about what led to this library on my blog post, which can be found in [this repo](./examples/BlogPost) or at [Medium](https://medium.com/soluto-engineering/size-matters-5aeeb462900a). | ||
|
||
## Api | ||
|
@@ -37,22 +37,24 @@ const Component = props => | |
``` | ||
|
||
|
||
* `scale(size: number)` | ||
* `scale(size: number)` | ||
Will return a linear scaled result of the provided size, based on your device's screen width. | ||
* `verticalScale(size: number)` | ||
* `verticalScale(size: number)` | ||
Will return a linear scaled result of the provided size, based on your device's screen height. | ||
|
||
* `moderateScale(size: number, factor?: number)` | ||
Sometimes you don't want to scale everything in a linear manner, that's where moderateScale comes in. | ||
The cool thing about it is that you can control the resize factor (default is 0.5). | ||
If normal scale will increase your size by +2X, moderateScale will only increase it by +X, for example: | ||
➡️ scale(10) = 20 | ||
➡️ moderateScale(10) = 15 | ||
➡️ moderateScale(10, 0.1) = 11 | ||
* `moderateScale(size: number, factor?: number)` | ||
Sometimes you don't want to scale everything in a linear manner, that's where moderateScale comes in. | ||
The cool thing about it is that you can control the resize factor (default is 0.5). | ||
If normal scale will increase your size by +2X, moderateScale will only increase it by +X, for example: | ||
➡️ scale(10) = 20 | ||
➡️ moderateScale(10) = 15 | ||
➡️ moderateScale(10, 0.1) = 11 | ||
* `moderateVerticalScale(size: number, factor?: number)` | ||
Same as moderateScale, but using verticalScale instead of scale. | ||
|
||
All scale functions can be imported using their shorthand alias as well: | ||
```js | ||
import { s, vs, ms } from 'react-native-size-matters'; | ||
import { s, vs, ms, mvs } from 'react-native-size-matters'; | ||
``` | ||
|
||
|
||
|
@@ -67,9 +69,11 @@ ScaleSheet will take the same stylesObject a regular StyleSheet will take, plus | |
* `<size>@s` - will apply `scale` function on `size`. | ||
* `<size>@vs` - will apply `verticalScale` function on `size`. | ||
* `<size>@ms` - will apply `moderateScale` function with resize factor of 0.5 on `size`. | ||
* `<size>@mvs` - will apply `moderateVerticalScale` function with resize factor of 0.5 on `size`. | ||
* `<size>@ms<factor>` - will apply `moderateScale` function with resize factor of `factor` on size. | ||
* `<size>@mvs<factor>` - will apply `moderateVerticalScale` function with resize factor of `factor` on size. | ||
|
||
ScaledSheet also supports rounding the result, simply add `r` at the end of the annotation. | ||
ScaledSheet also supports rounding the result, simply add `r` at the end of the annotation. | ||
|
||
Example: | ||
```js | ||
|
@@ -85,12 +89,16 @@ const styles = ScaledSheet.create({ | |
row: { | ||
padding: '[email protected]', // = moderateScale(10, 0.3) | ||
height: '50@ms' // = moderateScale(50) | ||
}, | ||
titleBar: { | ||
paddingBottom: '[email protected]', // = moderateVerticalScale(30, 0.3) | ||
height: '30@mvs' | ||
} | ||
}); | ||
``` | ||
|
||
<hr/> | ||
|
||
* [Changing the Default Guideline Sizes](./examples/change-guideline-sizes.md) | ||
* [Examples](./examples/README.md) | ||
* [Examples](./examples/README.md) | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,72 @@ | ||
jest.mock('react-native'); | ||
import { ScaledSheet, scale, verticalScale, moderateScale } from '..'; | ||
import { ScaledSheet, scale, verticalScale, moderateScale, moderateVerticalScale } from '..'; | ||
|
||
const getRandomInt = (min = 1, max = 100) => Math.floor(Math.random() * (max - min + 1)) + min; | ||
|
||
describe('ScaledSheet', () => { | ||
test('Scale works', () => { | ||
const number = getRandomInt(); | ||
const input = { test: `${number}@s` } | ||
const input = { test: `${number}@s` }; | ||
expect(ScaledSheet.create(input).test).toBe(scale(number)); | ||
}); | ||
|
||
test('verticalScale works', () => { | ||
const number = getRandomInt(); | ||
const input = { test: `${number}@vs` } | ||
const input = { test: `${number}@vs` }; | ||
expect(ScaledSheet.create(input).test).toBe(verticalScale(number)); | ||
}); | ||
|
||
test('moderateScale with default factor works', () => { | ||
const number = getRandomInt(); | ||
const input = { test: `${number}@ms` } | ||
const input = { test: `${number}@ms` }; | ||
expect(ScaledSheet.create(input).test).toBe(moderateScale(number)); | ||
}); | ||
|
||
test('moderateVerticalScale with default factor works', () => { | ||
const number = getRandomInt(); | ||
const input = { test: `${number}@mvs` }; | ||
expect(ScaledSheet.create(input).test).toBe(moderateVerticalScale(number)); | ||
}); | ||
|
||
test('moderateScale with custom factor works', () => { | ||
const number = getRandomInt(); | ||
const input = { test: `${number}@ms0.7` } | ||
const input = { test: `${number}@ms0.7` }; | ||
expect(ScaledSheet.create(input).test).toBe(moderateScale(number, 0.7)); | ||
}); | ||
|
||
test('moderateVerticalScale with custom factor works', () => { | ||
const number = getRandomInt(); | ||
const input = { test: `${number}@mvs0.7` }; | ||
expect(ScaledSheet.create(input).test).toBe(moderateVerticalScale(number, 0.7)); | ||
}); | ||
|
||
test('Scale works with a negative value', () => { | ||
const number = getRandomInt(-100, -1); | ||
const input = { test: `${number}@s` } | ||
const input = { test: `${number}@s` }; | ||
expect(ScaledSheet.create(input).test).toBe(scale(number)); | ||
}); | ||
|
||
test('moderateScale works with a negative value', () => { | ||
const number = getRandomInt(-100, -1); | ||
const input = { test: `${number}@ms0.3` } | ||
const input = { test: `${number}@ms0.3` }; | ||
expect(ScaledSheet.create(input).test).toBe(moderateScale(number, 0.3)); | ||
}); | ||
|
||
test('moderateVerticalScale works with a negative value', () => { | ||
const number = getRandomInt(-100, -1); | ||
const input = { test: `${number}@mvs0.3` }; | ||
expect(ScaledSheet.create(input).test).toBe(moderateVerticalScale(number, 0.3)); | ||
}); | ||
|
||
test('Scale works on a deeply nested object', () => { | ||
const number = getRandomInt(); | ||
const input = { test: { test: { test: `${number}@s` } } } | ||
const input = { test: { test: { test: `${number}@s` } } }; | ||
expect(ScaledSheet.create(input).test.test.test).toBe(scale(number)); | ||
}); | ||
|
||
test('No special annotation should leave the number intact', () => { | ||
const number = getRandomInt(); | ||
const input = { test: number } | ||
const input = { test: number }; | ||
expect(ScaledSheet.create(input).test).toBe(number); | ||
}); | ||
|
||
|
@@ -60,11 +78,13 @@ describe('ScaledSheet', () => { | |
margin: { | ||
width: 12, | ||
height: '12@s', | ||
paddingTop: '6.3@mvs', | ||
paddingBottom: -1 | ||
} | ||
}, | ||
row: { | ||
padding: '[email protected]', | ||
paddingLeft: '[email protected]', | ||
paddingBottom: '[email protected]', | ||
height: '34@ms', | ||
marginRight: '[email protected]', | ||
marginLeft: '[email protected]', | ||
|
@@ -75,7 +95,8 @@ describe('ScaledSheet', () => { | |
top: '11.3@sr', | ||
bottom: '22.75@vsr', | ||
left: '35.1@msr', | ||
right: '[email protected]' | ||
right: '[email protected]', | ||
height: '[email protected]' | ||
} | ||
}; | ||
|
||
|
@@ -86,11 +107,13 @@ describe('ScaledSheet', () => { | |
margin: { | ||
width: 12, | ||
height: scale(12), | ||
paddingTop: moderateVerticalScale(6.3), | ||
paddingBottom: -1 | ||
} | ||
}, | ||
row: { | ||
padding: moderateScale(10, 0.3), | ||
paddingLeft: moderateScale(10, 0.3), | ||
paddingBottom: moderateVerticalScale(10, 0.4), | ||
height: moderateScale(34), | ||
marginRight: moderateScale(0.5, 0.9), | ||
marginLeft: moderateScale(-0.5, 0.9), | ||
|
@@ -101,10 +124,11 @@ describe('ScaledSheet', () => { | |
top: Math.round(scale(11.3)), | ||
bottom: Math.round(verticalScale(22.75)), | ||
left: Math.round(moderateScale(35.1)), | ||
right: Math.round(moderateScale(-20.19, 0.3)) | ||
right: Math.round(moderateScale(-20.19, 0.3)), | ||
height: Math.round(moderateVerticalScale(-14.13, 0.4)) | ||
} | ||
}; | ||
|
||
expect(JSON.stringify(ScaledSheet.create(input))).toBe(JSON.stringify(expectedOutput)); | ||
}); | ||
}) | ||
}); |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import scaledSheetCreator from './lib/ScaledSheet'; | ||
import { scale, verticalScale, moderateScale } from './lib/extend/scaling-utils.extend'; | ||
import { scale, verticalScale, moderateScale, moderateVerticalScale } from './lib/extend/scaling-utils.extend'; | ||
|
||
export const ScaledSheet = scaledSheetCreator(scale, verticalScale, moderateScale); | ||
export const ScaledSheet = scaledSheetCreator(scale, verticalScale, moderateScale, moderateVerticalScale); | ||
export * from './lib/extend/scaling-utils.extend'; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import scaledSheetCreator from './lib/ScaledSheet'; | ||
import { scale, verticalScale, moderateScale } from './lib/scaling-utils'; | ||
import { scale, verticalScale, moderateScale, moderateVerticalScale } from './lib/scaling-utils'; | ||
|
||
export const ScaledSheet = scaledSheetCreator(scale, verticalScale, moderateScale); | ||
export const ScaledSheet = scaledSheetCreator(scale, verticalScale, moderateScale, moderateVerticalScale); | ||
export * from './lib/scaling-utils'; |
Oops, something went wrong.