Skip to content

Commit

Permalink
fix(between): Between supports unitless values
Browse files Browse the repository at this point in the history
between (and therefore fluidRange) now supports unitless values for fromSize and toSize.

fix #444
  • Loading branch information
bhough committed Jun 15, 2019
1 parent 93b313f commit 1d9eb2a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
12 changes: 5 additions & 7 deletions src/mixins/between.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import PolishedError from '../internalHelpers/_errors'
* }
*/
export default function between(
fromSize: string,
toSize: string,
fromSize: string | number,
toSize: string | number,
minScreen?: string = '320px',
maxScreen?: string = '1200px',
): string {
Expand All @@ -49,8 +49,6 @@ export default function between(
if (
typeof unitlessFromSize !== 'number'
|| typeof unitlessToSize !== 'number'
|| !fromSizeUnit
|| !toSizeUnit
|| fromSizeUnit !== toSizeUnit
) {
throw new PolishedError(48)
Expand All @@ -59,7 +57,7 @@ export default function between(
const slope = (unitlessFromSize - unitlessToSize)
/ (unitlessMinScreen - unitlessMaxScreen)
const base = unitlessToSize - slope * unitlessMaxScreen
return `calc(${base.toFixed(2)}${fromSizeUnit} + ${(100 * slope).toFixed(
2,
)}vw)`
return `calc(${base.toFixed(2)}${fromSizeUnit || ''} + ${(
100 * slope
).toFixed(2)}vw)`
}
4 changes: 4 additions & 0 deletions src/mixins/test/__snapshots__/between.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
exports[`between should return a valid calc formula when not passed min/max screen sizes 1`] = `"calc(-9.09px + 9.09vw)"`;

exports[`between should return a valid calc formula when passed min/max screen sizes 1`] = `"calc(-33.33px + 13.33vw)"`;

exports[`between should return a valid calc formula when passed unitless to/from values as numbers 1`] = `"calc(-9.09 + 9.09vw)"`;

exports[`between should return a valid calc formula when passed unitless to/from values as strings 1`] = `"calc(-9.09 + 9.09vw)"`;
17 changes: 8 additions & 9 deletions src/mixins/test/between.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ describe('between', () => {
it('should return a valid calc formula when not passed min/max screen sizes', () => {
expect(between('20px', '100px')).toMatchSnapshot()
})

it('should return a valid calc formula when passed unitless to/from values as numbers', () => {
expect(between(20, 100)).toMatchSnapshot()
})

it('should return a valid calc formula when passed unitless to/from values as strings', () => {
expect(between('20', '100')).toMatchSnapshot()
})
// Errors
it('should throw an error when not passed min/max screen size as a string', () => {
expect(() => {
Expand All @@ -28,15 +36,6 @@ describe('between', () => {
)
})

it('should throw an error when not passed from/to size as a string', () => {
expect(() => {
// $FlowFixMe
between(20, 100, '400px', '1000px')
}).toThrow(
'fromSize and toSize must be provided as stringified numbers with the same units.',
)
})

it('should throw an error when passed to/from size with different units', () => {
expect(() => {
// $FlowFixMe
Expand Down
4 changes: 2 additions & 2 deletions src/types/fluidRangeConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
*/
export type FluidRangeConfiguration = {|
prop: string,
fromSize: string,
toSize: string,
fromSize: string | number,
toSize: string | number,
|}

0 comments on commit 1d9eb2a

Please sign in to comment.