Skip to content

Commit ac6baa1

Browse files
Abbondanzometa-codesync[bot]
authored andcommitted
Support cover and contain backgroundSize for url() images on Android
Summary: Android only parsed length and percentage values for `backgroundSize`, so `cover` and `contain` were dropped and a `url()` image drew at its natural size. They now scale an image that has an aspect ratio to cover or fit inside the background area. Gradients have no aspect ratio, so they keep filling it. Today the keywords only reach Android as items of an array value with native CSS parsing on. The JS style processor drops them, and the native parser drops a bare string. Changelog: [Internal] Differential Revision: D135152982
1 parent bc5f563 commit ac6baa1

3 files changed

Lines changed: 85 additions & 3 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawable.kt‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ internal fun calculateBackgroundImageSize(
7777
finalWidth = finalHeight * ratio
7878
}
7979
}
80+
} else if (
81+
keepsRatio &&
82+
(backgroundSize is BackgroundSize.Cover || backgroundSize is BackgroundSize.Contain)
83+
) {
84+
xAuto = false
85+
yAuto = false
86+
val widthScale = containerWidth / imageWidth
87+
val heightScale = containerHeight / imageHeight
88+
val scale =
89+
if (backgroundSize is BackgroundSize.Cover) maxOf(widthScale, heightScale)
90+
else minOf(widthScale, heightScale)
91+
finalWidth = imageWidth * scale
92+
finalHeight = imageHeight * scale
8093
}
8194

8295
if (repeat?.x == BackgroundRepeatKeyword.Round && finalWidth > 0) {

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/style/BackgroundSize.kt‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ internal class BackgroundSizeLengthPercentage(
110110
/**
111111
* Sealed class representing CSS background-size property values.
112112
*
113-
* This class models the different ways a background size can be specified in CSS, currently
114-
* supporting length/percentage/auto values for both dimensions.
113+
* This class models the different ways a background size can be specified in CSS: length,
114+
* percentage, or auto values for both dimensions, or the `cover` and `contain` keywords.
115115
*
116116
* @see BackgroundSizeLengthPercentage
117117
*/
@@ -124,11 +124,17 @@ internal sealed class BackgroundSize {
124124
class LengthPercentageAuto(val lengthPercentage: BackgroundSizeLengthPercentage) :
125125
BackgroundSize()
126126

127+
/** Scales an image that has an aspect ratio to cover the whole background area. */
128+
object Cover : BackgroundSize()
129+
130+
/** Scales an image that has an aspect ratio to fit inside the background area. */
131+
object Contain : BackgroundSize()
132+
127133
companion object {
128134
/**
129135
* Parses a Dynamic value into a BackgroundSize.
130136
*
131-
* Currently supports map values containing x/y dimensions.
137+
* Supports map values containing x/y dimensions, and the `cover` and `contain` keywords.
132138
*
133139
* @param backgroundSizeValue The dynamic value to parse
134140
* @return A BackgroundSize instance, or null if parsing fails
@@ -146,6 +152,12 @@ internal sealed class BackgroundSize {
146152
null
147153
}
148154
}
155+
ReadableType.String ->
156+
when (backgroundSizeValue.asString()) {
157+
"cover" -> Cover
158+
"contain" -> Contain
159+
else -> null
160+
}
149161
else -> null
150162
}
151163
}

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/drawable/BackgroundImageDrawableTest.kt‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ package com.facebook.react.uimanager.drawable
99

1010
import com.facebook.react.uimanager.LengthPercentage
1111
import com.facebook.react.uimanager.LengthPercentageType
12+
import com.facebook.react.uimanager.style.BackgroundRepeat
13+
import com.facebook.react.uimanager.style.BackgroundRepeatKeyword
1214
import com.facebook.react.uimanager.style.BackgroundSize
1315
import com.facebook.react.uimanager.style.BackgroundSizeLengthPercentage
1416
import org.assertj.core.api.Assertions.assertThat
@@ -44,4 +46,59 @@ class BackgroundImageDrawableTest {
4446

4547
assertThat(result).isEqualTo(50f to 12f)
4648
}
49+
50+
@Test
51+
fun coverFillsTheAreaKeepingTheRatio() {
52+
val result =
53+
calculateBackgroundImageSize(
54+
300f,
55+
200f,
56+
100f,
57+
100f,
58+
BackgroundSize.Cover,
59+
null,
60+
hasIntrinsicRatio = true,
61+
)
62+
63+
assertThat(result).isEqualTo(300f to 300f)
64+
}
65+
66+
@Test
67+
fun containFitsInsideTheAreaKeepingTheRatio() {
68+
val result =
69+
calculateBackgroundImageSize(
70+
300f,
71+
200f,
72+
100f,
73+
100f,
74+
BackgroundSize.Contain,
75+
null,
76+
hasIntrinsicRatio = true,
77+
)
78+
79+
assertThat(result).isEqualTo(200f to 200f)
80+
}
81+
82+
@Test
83+
fun coverWithoutARatioKeepsTheGivenSize() {
84+
val result = calculateBackgroundImageSize(300f, 200f, 300f, 200f, BackgroundSize.Cover, null)
85+
86+
assertThat(result).isEqualTo(300f to 200f)
87+
}
88+
89+
@Test
90+
fun roundOnOneAxisDoesNotRescaleContainOnTheOther() {
91+
val result =
92+
calculateBackgroundImageSize(
93+
300f,
94+
200f,
95+
100f,
96+
100f,
97+
BackgroundSize.Contain,
98+
BackgroundRepeat(BackgroundRepeatKeyword.Round, BackgroundRepeatKeyword.NoRepeat),
99+
hasIntrinsicRatio = true,
100+
)
101+
102+
assertThat(result).isEqualTo(150f to 200f)
103+
}
47104
}

0 commit comments

Comments
 (0)