-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add string support for aspectRatio (#34629)
Summary: This updates `aspectRatio` to support string values and ratio formats, i.e., `'16 / 9'`, thus aligning it with the [CSS Box Sizing Module Level 4](https://drafts.csswg.org/css-sizing-4/#aspect-ratio) specification as requested on #34425. This also adds unit tests to the `processAspectRatio` function ensuring the style processing works as expected. ## Changelog [General] [Added] - Add string support for aspectRatio Pull Request resolved: #34629 Test Plan: This can be tested either through `processAspectRatio-tests` or by using the following code: ```js <View style={{ backgroundColor: '#527FE4', aspectRatio: '16 / 9', }} /> ``` https://user-images.githubusercontent.com/11707729/189029904-da1dc0a6-85de-46aa-8ec2-3567802c8719.mov Reviewed By: jacdebug Differential Revision: D39423304 Pulled By: cipolleschi fbshipit-source-id: d323de93d6524e411e7ab9943335a8ca323b6e61
- Loading branch information
1 parent
71fda5e
commit 14c91cd
Showing
6 changed files
with
120 additions
and
5 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
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
7 changes: 7 additions & 0 deletions
7
Libraries/StyleSheet/__tests__/__snapshots__/processAspectRatio-test.js.snap
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`processAspectRatio should not accept invalid formats 1`] = `"aspectRatio must either be a number, a ratio or \`auto\`. You passed: 0a"`; | ||
|
||
exports[`processAspectRatio should not accept invalid formats 2`] = `"aspectRatio must either be a number, a ratio or \`auto\`. You passed: 1 / 1 1"`; | ||
|
||
exports[`processAspectRatio should not accept invalid formats 3`] = `"aspectRatio must either be a number, a ratio or \`auto\`. You passed: auto 1/1"`; |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @emails oncall+react_native | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const processAspectRatio = require('../processAspectRatio'); | ||
|
||
describe('processAspectRatio', () => { | ||
it('should accept numbers', () => { | ||
expect(processAspectRatio(1)).toBe(1); | ||
expect(processAspectRatio(0)).toBe(0); | ||
expect(processAspectRatio(1.5)).toBe(1.5); | ||
}); | ||
|
||
it('should accept string numbers', () => { | ||
expect(processAspectRatio('1')).toBe(1); | ||
expect(processAspectRatio('0')).toBe(0); | ||
expect(processAspectRatio('1.5')).toBe(1.5); | ||
expect(processAspectRatio('+1.5')).toBe(1.5); | ||
expect(processAspectRatio(' 1')).toBe(1); | ||
expect(processAspectRatio(' 0 ')).toBe(0); | ||
}); | ||
|
||
it('should accept `auto`', () => { | ||
expect(processAspectRatio('auto')).toBe(undefined); | ||
expect(processAspectRatio(' auto')).toBe(undefined); | ||
expect(processAspectRatio(' auto ')).toBe(undefined); | ||
}); | ||
|
||
it('should accept ratios', () => { | ||
expect(processAspectRatio('+1/1')).toBe(1); | ||
expect(processAspectRatio('0 / 10')).toBe(0); | ||
expect(processAspectRatio('117/ 13')).toBe(9); | ||
expect(processAspectRatio('1.5 /1.2')).toBe(1.25); | ||
expect(processAspectRatio('1/0')).toBe(Infinity); | ||
}); | ||
|
||
it('should not accept invalid formats', () => { | ||
expect(() => processAspectRatio('0a')).toThrowErrorMatchingSnapshot(); | ||
expect(() => processAspectRatio('1 / 1 1')).toThrowErrorMatchingSnapshot(); | ||
expect(() => processAspectRatio('auto 1/1')).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const invariant = require('invariant'); | ||
|
||
function processAspectRatio(aspectRatio: number | string): ?number { | ||
if (typeof aspectRatio === 'number') { | ||
return aspectRatio; | ||
} | ||
|
||
const matches = aspectRatio.split('/').map(s => s.trim()); | ||
|
||
if (matches.includes('auto')) { | ||
if (__DEV__) { | ||
invariant( | ||
matches.length, | ||
'aspectRatio does not support `auto <ratio>`. You passed: %s', | ||
aspectRatio, | ||
); | ||
} | ||
return; | ||
} | ||
|
||
const hasNonNumericValues = matches.some(n => Number.isNaN(Number(n))); | ||
if (__DEV__) { | ||
invariant( | ||
!hasNonNumericValues && (matches.length === 1 || matches.length === 2), | ||
'aspectRatio must either be a number, a ratio or `auto`. You passed: %s', | ||
aspectRatio, | ||
); | ||
} | ||
|
||
if (hasNonNumericValues) { | ||
return; | ||
} | ||
|
||
if (matches.length === 2) { | ||
return Number(matches[0]) / Number(matches[1]); | ||
} | ||
|
||
return Number(matches[0]); | ||
} | ||
|
||
module.exports = processAspectRatio; |