Skip to content

Commit 4f7a94d

Browse files
Resolve radial gradient length color stops against the horizontal radius
## Summary: Fixes #59968. Radial gradient length color stops were resolved against the larger radius. Per [spec](https://drafts.csswg.org/css-images-3/#radial-color-stops), the gradient line "extends toward the right", so they should resolve against the horizontal radius. ## Changelog: [GENERAL] [FIXED] - Resolve radial gradient length color stops against the horizontal radius ## Test Plan: Added an RNTester example. Compared with Chrome: ![Before and after on iOS and Android, compared with Chrome](https://raw.githubusercontent.com/intergalacticspacehighway/react-native/14316cfb80c89fd9551e833c4ec83a5ed61184fd/radial-length-stops.png) Differential Revision: D133460463 Pull Request resolved: #60173 (cherry picked from commit 9049482)
1 parent f99a4c7 commit 4f7a94d

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

‎packages/react-native/React/Fabric/Utils/RCTRadialGradient.mm‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ + (CALayer *)gradientLayerWithSize:(CGSize)size gradient:(const RadialGradient &
174174
bool isCircle = (gradient.shape == RadialGradientShape::Circle);
175175
auto [radiusX, radiusY] =
176176
GetRadialGradientRadius(isCircle, gradient.size, centerPoint.x, centerPoint.y, size.width, size.height);
177-
const auto gradientLineLength = std::max(radiusX, radiusY);
178-
auto colorStops = [RCTGradientUtils getFixedColorStops:gradient.colorStops gradientLineLength:gradientLineLength];
177+
// Length stops resolve against radiusX: the gradient line starts at the center and "extends
178+
// toward the right". https://drafts.csswg.org/css-images-3/#radial-color-stops
179+
auto colorStops = [RCTGradientUtils getFixedColorStops:gradient.colorStops gradientLineLength:radiusX];
179180
// Unlike negative stops, stops past the ending shape stay visible outside it, so the radius grows to fit them.
180181
const Float radiusScale = colorStops.empty() ? 1 : std::max<Float>(colorStops.back().position.value_or(1), 1);
181182
colorStops = [RCTGradientUtils clipColorStops:colorStops end:radiusScale];

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ internal class RadialGradient(
244244

245245
val (radiusX, radiusY) = calculateRadius(centerX, centerY, width, height)
246246

247-
val fixedStops = ColorStopUtils.getFixedColorStops(colorStops, max(radiusX, radiusY))
247+
// Length stops resolve against radiusX: the gradient line starts at the center and "extends
248+
// toward the right". https://drafts.csswg.org/css-images-3/#radial-color-stops
249+
val fixedStops = ColorStopUtils.getFixedColorStops(colorStops, radiusX)
248250
// Unlike negative stops, stops past the ending shape stay visible outside it, so the radius
249251
// grows to fit them.
250252
val radiusScale = max(fixedStops.lastOrNull()?.position ?: 1f, 1f)

‎packages/rn-tester/js/examples/RadialGradient/RadialGradientExample.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,25 @@ exports.examples = [
286286
);
287287
},
288288
},
289+
{
290+
title: 'Length color stops on a tall ellipse',
291+
description:
292+
'Length stops are measured along the horizontal radius: red reaches 40px right and 120px down.',
293+
name: 'length-color-stops-tall-ellipse',
294+
render(): React.Node {
295+
return (
296+
<GradientBox
297+
testID="radial-gradient-length-color-stops-tall-ellipse"
298+
style={{
299+
backgroundImage:
300+
'radial-gradient(ellipse 50px 150px at center, red 40px, blue 40px)',
301+
width: 100,
302+
height: 300,
303+
}}
304+
/>
305+
);
306+
},
307+
},
289308
{
290309
title: 'Color stops outside 0–100%',
291310
name: 'out-of-range-color-stops',

0 commit comments

Comments
 (0)