Skip to content

Commit bf880da

Browse files
fix(progress-bar): preserve fill border radius while animating progress (#4950)
1 parent bf20edb commit bf880da

3 files changed

Lines changed: 55 additions & 60 deletions

File tree

src/components/ProgressBar.tsx

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,42 @@ const ProgressBar = ({
194194
const trackTintColor = theme.isV3
195195
? theme.colors.surfaceVariant
196196
: setColor(tintColor).alpha(0.38).rgb().string();
197+
const progressBarStyle = indeterminate
198+
? {
199+
width,
200+
backgroundColor: tintColor,
201+
transform: [
202+
{
203+
translateX: timer.interpolate({
204+
inputRange: [0, 0.5, 1],
205+
outputRange: [
206+
(isRTL ? 1 : -1) * 0.5 * width,
207+
(isRTL ? 1 : -1) * 0.5 * INDETERMINATE_MAX_WIDTH * width,
208+
(isRTL ? -1 : 1) * 0.7 * width,
209+
],
210+
}),
211+
},
212+
{
213+
// Workaround for workaround for https://github.com/facebook/react-native/issues/6278
214+
scaleX: timer.interpolate({
215+
inputRange: [0, 0.5, 1],
216+
outputRange: [0.0001, INDETERMINATE_MAX_WIDTH, 0.0001],
217+
}),
218+
},
219+
],
220+
}
221+
: {
222+
width,
223+
backgroundColor: tintColor,
224+
transform: [
225+
{
226+
translateX: timer.interpolate({
227+
inputRange: [0, 1],
228+
outputRange: [(isRTL ? 1 : -1) * width, 0],
229+
}),
230+
},
231+
],
232+
};
197233

198234
return (
199235
<View
@@ -220,54 +256,7 @@ const ProgressBar = ({
220256
{width ? (
221257
<Animated.View
222258
testID={`${testID}-fill`}
223-
style={[
224-
styles.progressBar,
225-
{
226-
width,
227-
backgroundColor: tintColor,
228-
transform: [
229-
{
230-
translateX: timer.interpolate(
231-
indeterminate
232-
? {
233-
inputRange: [0, 0.5, 1],
234-
outputRange: [
235-
(isRTL ? 1 : -1) * 0.5 * width,
236-
(isRTL ? 1 : -1) *
237-
0.5 *
238-
INDETERMINATE_MAX_WIDTH *
239-
width,
240-
(isRTL ? -1 : 1) * 0.7 * width,
241-
],
242-
}
243-
: {
244-
inputRange: [0, 1],
245-
outputRange: [(isRTL ? 1 : -1) * 0.5 * width, 0],
246-
}
247-
),
248-
},
249-
{
250-
// Workaround for workaround for https://github.com/facebook/react-native/issues/6278
251-
scaleX: timer.interpolate(
252-
indeterminate
253-
? {
254-
inputRange: [0, 0.5, 1],
255-
outputRange: [
256-
0.0001,
257-
INDETERMINATE_MAX_WIDTH,
258-
0.0001,
259-
],
260-
}
261-
: {
262-
inputRange: [0, 1],
263-
outputRange: [0.0001, 1],
264-
}
265-
),
266-
},
267-
],
268-
},
269-
fillStyle,
270-
]}
259+
style={[styles.progressBar, progressBarStyle, fillStyle]}
271260
/>
272261
) : null}
273262
</Animated.View>

src/components/__tests__/ProgressBar.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react';
22
import { Animated, Platform, StyleSheet } from 'react-native';
3+
import type { ViewStyle } from 'react-native';
34

45
import { act, render } from '@testing-library/react-native';
56

@@ -97,3 +98,17 @@ it('renders progress bar with custom style of filled part', async () => {
9798
borderRadius: 4,
9899
});
99100
});
101+
102+
it('does not scale the determinate fill width', async () => {
103+
const tree = render(<ProgressBar progress={0.2} testID="progress-bar" />);
104+
await triggerLayout(tree);
105+
106+
const fillStyle = StyleSheet.flatten(
107+
tree.getByTestId('progress-bar-fill').props.style
108+
) as ViewStyle;
109+
const transform = fillStyle.transform;
110+
111+
expect(
112+
Array.isArray(transform) && transform.some((value) => 'scaleX' in value)
113+
).toBe(false);
114+
});

src/components/__tests__/__snapshots__/ProgressBar.test.tsx.snap

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ exports[`renders colored progress bar 1`] = `
3939
"flex": 1,
4040
"transform": [
4141
{
42-
"translateX": -50,
43-
},
44-
{
45-
"scaleX": 0.0001,
42+
"translateX": -100,
4643
},
4744
],
4845
"width": 100,
@@ -93,10 +90,7 @@ exports[`renders hidden progress bar 1`] = `
9390
"flex": 1,
9491
"transform": [
9592
{
96-
"translateX": -50,
97-
},
98-
{
99-
"scaleX": 0.0001,
93+
"translateX": -100,
10094
},
10195
],
10296
"width": 100,
@@ -195,10 +189,7 @@ exports[`renders progress bar with specific progress 1`] = `
195189
"flex": 1,
196190
"transform": [
197191
{
198-
"translateX": -50,
199-
},
200-
{
201-
"scaleX": 0.0001,
192+
"translateX": -100,
202193
},
203194
],
204195
"width": 100,

0 commit comments

Comments
 (0)