Skip to content

Commit 13b3b85

Browse files
shubh73claude
andauthored
Fix per-side border colors drawing outside the view on Android (#59787)
* Fix per-side border colors drawing outside the view on Android When a border without a radius has different colors per side, BorderDrawable draws each side as a filled quadrilateral, but that branch never set the paint's style. A drawable first drawn with one border color leaves its paint on STROKE with the border width, so after a change to per-side colors the sides were stroked and spilled outside the view. Set the style to FILL there, as the rounded branch already does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Add an RNTester example and visual test for changing to per-side border colors Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Crop the per-side border colors visual test to the box Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0091ea6 commit 13b3b85

5 files changed

Lines changed: 86 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ internal class BorderDrawable(
306306
* after drawing is done, we will re-enable it.
307307
*/
308308
borderPaint.isAntiAlias = false
309+
borderPaint.style = Paint.Style.FILL
309310
val width = bounds.width()
310311
val height = bounds.height()
311312
if (borderLeft > 0) {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,34 @@ class BorderDrawableTest {
6666
assertThat(drawBorderPaint(drawable).pathEffect).isNull()
6767
}
6868

69+
@Test
70+
fun unevenBorderColorsAreFilledAfterAUniformBorderColorWasDrawn() {
71+
val drawable = createDrawable()
72+
// A uniform color is drawn as a stroke.
73+
assertThat(drawBorderPaint(drawable).style).isEqualTo(Paint.Style.STROKE)
74+
75+
drawable.setBorderColor(LogicalEdge.LEFT, Color.BLUE)
76+
77+
// Uneven colors are drawn as filled quadrilaterals.
78+
assertThat(drawBorderPaint(drawable).style).isEqualTo(Paint.Style.FILL)
79+
}
80+
81+
private fun createDrawable(): BorderDrawable {
82+
val borderInsets = BorderInsets().apply { setBorderWidth(LogicalEdge.ALL, 8f) }
83+
return BorderDrawable(
84+
context = RuntimeEnvironment.getApplication(),
85+
borderWidth = Spacing(0f),
86+
borderRadius = null,
87+
borderInsets = borderInsets,
88+
borderStyle = null,
89+
)
90+
.apply {
91+
setBounds(0, 0, 240, 120)
92+
setBorderWidth(Spacing.ALL, 8f)
93+
setBorderColor(LogicalEdge.ALL, Color.RED)
94+
}
95+
}
96+
6997
private fun drawBorderPaint(drawable: BorderDrawable): Paint {
7098
val canvas = mock<Canvas>()
7199
drawable.draw(canvas)

‎packages/rn-tester/js/examples/Border/BorderExample.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,43 @@
1313
import type {RNTesterModule} from '../../types/RNTesterTypes';
1414

1515
import hotdog from '../../assets/hotdog.jpg';
16+
import RNTesterText from '../../components/RNTesterText';
1617
import * as React from 'react';
18+
import {useState} from 'react';
1719
import {
1820
DynamicColorIOS,
1921
Image,
2022
Platform,
2123
PlatformColor,
24+
Pressable,
2225
StyleSheet,
2326
View,
2427
} from 'react-native';
2528

29+
function BorderColorChangeExample(): React.Node {
30+
const [perSide, setPerSide] = useState(false);
31+
return (
32+
<Pressable
33+
testID="border-color-change-button"
34+
onPress={() => setPerSide(value => !value)}>
35+
<RNTesterText>
36+
{perSide ? 'Per-side colors' : 'One color'} (tap to change)
37+
</RNTesterText>
38+
<View
39+
testID="border-test-color-change"
40+
style={{alignSelf: 'flex-start', padding: 10}}>
41+
<View
42+
style={[
43+
styles.box,
44+
{borderWidth: 10},
45+
perSide ? styles.border2 : {borderColor: 'black'},
46+
]}
47+
/>
48+
</View>
49+
</Pressable>
50+
);
51+
}
52+
2653
const styles = StyleSheet.create({
2754
box: {
2855
width: 100,
@@ -600,6 +627,15 @@ export default {
600627
);
601628
},
602629
},
630+
{
631+
title: 'Changing to per-side colors',
632+
name: 'color-change',
633+
description:
634+
'A border that changes from one color to per-side colors stays inside the view',
635+
render: function (): React.Node {
636+
return <BorderColorChangeExample />;
637+
},
638+
},
603639
{
604640
title: 'Borders and scaling transforms',
605641
name: 'scaling-transforms',

‎packages/rn-tester/visual-tests/flows/border.yml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,23 @@ appId: ${APP_ID}
4242
path: ${VISUAL_OUTPUT_DIR}/border/equal-width-borders/default
4343
cropOn:
4444
id: 'border-test-equal-width-borders'
45+
- runFlow:
46+
file: ../helpers/open-example.yml
47+
env:
48+
EXAMPLE_URL: rntester://example/BorderExample/color-change
49+
- extendedWaitUntil:
50+
visible:
51+
id: 'border-test-color-change'
52+
timeout: 15000
53+
- tapOn:
54+
id: 'border-color-change-button'
55+
- extendedWaitUntil:
56+
visible:
57+
text: 'Per-side colors.*'
58+
timeout: 15000
59+
- waitForAnimationToEnd:
60+
timeout: 5000
61+
- takeScreenshot:
62+
path: ${VISUAL_OUTPUT_DIR}/border/color-change/per-side-colors
63+
cropOn:
64+
id: 'border-test-color-change'

‎packages/rn-tester/visual-tests/suites/border.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
| `corner-radii` | Initial rendering |
66
| `child-clipping-radius` | Initial rendering |
77
| `equal-width-borders` | Initial rendering |
8+
| `color-change` | After tapping to change from one border color to per-side colors |
89

910
Screenshots use the existing RNTester example crops. Each example starts from a fresh app launch; captures wait for the target and for animations to settle.

0 commit comments

Comments
 (0)