diff --git a/packages/react-native/Libraries/StyleSheet/StyleSheetTypes.d.ts b/packages/react-native/Libraries/StyleSheet/StyleSheetTypes.d.ts index 2eeef10c962..39bbb12e55f 100644 --- a/packages/react-native/Libraries/StyleSheet/StyleSheetTypes.d.ts +++ b/packages/react-native/Libraries/StyleSheet/StyleSheetTypes.d.ts @@ -423,13 +423,10 @@ type RadialGradientValue = { export type BackgroundImageValue = LinearGradientValue | RadialGradientValue; -export type BackgroundSizeValue = - | { - x: string | number; - y: string | number; - } - | 'cover' - | 'contain'; +export type BackgroundSizeValue = { + x: string | number; + y: string | number; +}; export type BackgroundRepeatKeyword = | 'repeat' diff --git a/packages/react-native/Libraries/StyleSheet/StyleSheetTypes.js b/packages/react-native/Libraries/StyleSheet/StyleSheetTypes.js index c0c4b5b825e..a5a9b947d79 100644 --- a/packages/react-native/Libraries/StyleSheet/StyleSheetTypes.js +++ b/packages/react-native/Libraries/StyleSheet/StyleSheetTypes.js @@ -779,13 +779,10 @@ type RadialGradientValue = { export type BackgroundImageValue = LinearGradientValue | RadialGradientValue; -export type BackgroundSizeValue = - | { - x: string | number, - y: string | number, - } - | 'cover' - | 'contain'; +export type BackgroundSizeValue = { + x: string | number, + y: string | number, +}; export type BackgroundRepeatKeyword = | 'repeat' diff --git a/packages/react-native/Libraries/StyleSheet/__tests__/processBackgroundSize-test.js b/packages/react-native/Libraries/StyleSheet/__tests__/processBackgroundSize-test.js index 5733e7529b0..a985732868a 100644 --- a/packages/react-native/Libraries/StyleSheet/__tests__/processBackgroundSize-test.js +++ b/packages/react-native/Libraries/StyleSheet/__tests__/processBackgroundSize-test.js @@ -34,30 +34,19 @@ describe('processBackgroundSize', () => { ]); }); - it('should handle array with cover', () => { - expect(processBackgroundSize(['cover'])).toEqual(['cover']); - }); - - it('should handle array with contain', () => { - expect(processBackgroundSize(['contain'])).toEqual(['contain']); - }); - - it('should handle array with mixed values', () => { - expect(processBackgroundSize([{x: 100, y: 'auto'}, 'cover'])).toEqual([ + it('should handle array with multiple values', () => { + expect( + processBackgroundSize([ + {x: 100, y: 'auto'}, + {x: '50%', y: '50%'}, + ]), + ).toEqual([ {x: 100, y: 'auto'}, - 'cover', + {x: '50%', y: '50%'}, ]); }); // Test string input - single values - it('should parse cover', () => { - expect(processBackgroundSize('cover')).toEqual(['cover']); - }); - - it('should parse contain', () => { - expect(processBackgroundSize('contain')).toEqual(['contain']); - }); - it('should parse single length value', () => { expect(processBackgroundSize('100px')).toEqual([{x: 100, y: 'auto'}]); }); @@ -111,9 +100,9 @@ describe('processBackgroundSize', () => { // Test multiple background sizes (comma-separated) it('should parse multiple background sizes', () => { - expect(processBackgroundSize('100px 200px, cover')).toEqual([ + expect(processBackgroundSize('100px 200px, 50%')).toEqual([ {x: 100, y: 200}, - 'cover', + {x: '50%', y: 'auto'}, ]); }); @@ -125,16 +114,16 @@ describe('processBackgroundSize', () => { }); it('should parse multiple background sizes with newlines', () => { - expect(processBackgroundSize('100px 200px,\nCover')).toEqual([ + expect(processBackgroundSize('100px 200px,\n50%')).toEqual([ {x: 100, y: 200}, - 'cover', + {x: '50%', y: 'auto'}, ]); }); it('should parse multiple background sizes with extra whitespace', () => { - expect(processBackgroundSize(' 100px 200px , cover ')).toEqual([ + expect(processBackgroundSize(' 100px 200px , 50% ')).toEqual([ {x: 100, y: 200}, - 'cover', + {x: '50%', y: 'auto'}, ]); }); @@ -172,11 +161,11 @@ describe('processBackgroundSize', () => { }); it('should handle mixed case keywords', () => { - expect(processBackgroundSize('COVER')).toEqual(['cover']); + expect(processBackgroundSize('AUTO')).toEqual([{x: 'auto', y: 'auto'}]); }); it('should handle mixed case keywords with spaces', () => { - expect(processBackgroundSize(' Cover ')).toEqual(['cover']); + expect(processBackgroundSize(' Auto ')).toEqual([{x: 'auto', y: 'auto'}]); }); it('should handle invalid mixed values', () => { @@ -214,18 +203,26 @@ describe('processBackgroundSize', () => { // Test complex scenarios it('should handle complex multiple background scenario', () => { - expect( - processBackgroundSize('100px 200px, 50% auto, cover, contain'), - ).toEqual([{x: 100, y: 200}, {x: '50%', y: 'auto'}, 'cover', 'contain']); + expect(processBackgroundSize('100px 200px, 50% auto, auto')).toEqual([ + {x: 100, y: 200}, + {x: '50%', y: 'auto'}, + {x: 'auto', y: 'auto'}, + ]); }); it('should handle all valid single value combinations', () => { - expect(processBackgroundSize('100px, 50%, auto, cover, contain')).toEqual([ + expect(processBackgroundSize('100px, 50%, auto')).toEqual([ {x: 100, y: 'auto'}, {x: '50%', y: 'auto'}, {x: 'auto', y: 'auto'}, - 'cover', - 'contain', ]); }); + + it('should return empty array for unsupported cover keyword', () => { + expect(processBackgroundSize('cover')).toEqual([]); + }); + + it('should return empty array for unsupported contain keyword', () => { + expect(processBackgroundSize('contain')).toEqual([]); + }); }); diff --git a/packages/react-native/Libraries/StyleSheet/processBackgroundSize.js b/packages/react-native/Libraries/StyleSheet/processBackgroundSize.js index f95b5ede269..f0be603e721 100644 --- a/packages/react-native/Libraries/StyleSheet/processBackgroundSize.js +++ b/packages/react-native/Libraries/StyleSheet/processBackgroundSize.js @@ -57,19 +57,14 @@ function parseBackgroundSizeCSSString( return []; } } else if (parts.length === 1) { - const part = parts[0].toLowerCase(); - if (part === 'cover' || part === 'contain') { - result.push(part); + const x = getValidLengthPercentageSizeOrNull(parts[0].toLowerCase()); + if (x != null) { + result.push({ + x, + y: 'auto', + }); } else { - const x = getValidLengthPercentageSizeOrNull(parts[0].toLowerCase()); - if (x != null) { - result.push({ - x, - y: 'auto', - }); - } else { - return []; - } + return []; } } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawable.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawable.kt index e8a82750fa3..66e04d3e7cb 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawable.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawable.kt @@ -134,9 +134,11 @@ internal class BackgroundImageDrawable( // So we draw in reverse (last drawn in canvas appears closest) for (index in layers.indices.reversed()) { val backgroundImageLayer = layers[index] - val size = backgroundSize?.let { it.getOrNull(index % it.size) } - val repeat = backgroundRepeat?.let { it.getOrNull(index % it.size) } - val position = backgroundPosition?.let { it.getOrNull(index % it.size) } + val size = backgroundSize?.takeIf { it.isNotEmpty() }?.let { it.getOrNull(index % it.size) } + val repeat = + backgroundRepeat?.takeIf { it.isNotEmpty() }?.let { it.getOrNull(index % it.size) } + val position = + backgroundPosition?.takeIf { it.isNotEmpty() }?.let { it.getOrNull(index % it.size) } // 2. Calculate the size of a single tile. val (tileWidth, tileHeight) = diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.kt index 2421c99a8a2..a59c4ef8cc6 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.kt @@ -167,7 +167,7 @@ public open class ReactViewManager : ReactClippingViewManager() backgroundSizes.add(parsedBackgroundSize) } } - BackgroundStyleApplicator.setBackgroundSize(view, backgroundSizes) + BackgroundStyleApplicator.setBackgroundSize(view, backgroundSizes.ifEmpty { null }) } } else { BackgroundStyleApplicator.setBackgroundSize(view, null) @@ -186,7 +186,7 @@ public open class ReactViewManager : ReactClippingViewManager() backgroundPositions.add(parsedBackgroundPosition) } } - BackgroundStyleApplicator.setBackgroundPosition(view, backgroundPositions) + BackgroundStyleApplicator.setBackgroundPosition(view, backgroundPositions.ifEmpty { null }) } else { BackgroundStyleApplicator.setBackgroundPosition(view, null) } @@ -205,7 +205,7 @@ public open class ReactViewManager : ReactClippingViewManager() backgroundRepeats.add(parsedBackgroundRepeat) } } - BackgroundStyleApplicator.setBackgroundRepeat(view, backgroundRepeats) + BackgroundStyleApplicator.setBackgroundRepeat(view, backgroundRepeats.ifEmpty { null }) } else { BackgroundStyleApplicator.setBackgroundRepeat(view, null) } diff --git a/packages/rn-tester/js/examples/BackgroundImage/BackgroundImageExample.js b/packages/rn-tester/js/examples/BackgroundImage/BackgroundImageExample.js index faf8c05b31e..4857fed46e6 100644 --- a/packages/rn-tester/js/examples/BackgroundImage/BackgroundImageExample.js +++ b/packages/rn-tester/js/examples/BackgroundImage/BackgroundImageExample.js @@ -361,7 +361,6 @@ exports.examples = [ style={{ experimental_backgroundImage: 'linear-gradient(45deg, #667eea, #764ba2)', - experimental_backgroundSize: 'cover', borderRadius: 20, }} testID="background-image-borders-1" @@ -373,7 +372,6 @@ exports.examples = [ style={{ experimental_backgroundImage: 'radial-gradient(circle, #f093fb, #f5576c)', - experimental_backgroundSize: 'cover', borderWidth: 10, borderColor: 'red', }} @@ -386,7 +384,6 @@ exports.examples = [ style={{ experimental_backgroundImage: 'radial-gradient(circle, #f093fb, #f5576c)', - experimental_backgroundSize: 'cover', borderTopLeftRadius: 10, borderTopRightRadius: 20, borderBottomLeftRadius: 30, @@ -401,7 +398,6 @@ exports.examples = [ style={{ experimental_backgroundImage: 'radial-gradient(circle, #f093fb, #f5576c)', - experimental_backgroundSize: 'cover', borderTopWidth: 10, borderTopColor: 'red', borderBottomWidth: 20,