Skip to content

Commit f338b79

Browse files
authored
fix: correct animated fab container bg color (#3558)
1 parent f46e49e commit f338b79

File tree

6 files changed

+54
-25
lines changed

6 files changed

+54
-25
lines changed

src/components/FAB/AnimatedFAB.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ const AnimatedFAB = ({
201201
style,
202202
visible = true,
203203
uppercase = !theme.isV3,
204-
testID,
204+
testID = 'animated-fab',
205205
animateFrom = 'right',
206206
extended = false,
207207
iconMode = 'dynamic',
@@ -242,12 +242,15 @@ const AnimatedFAB = ({
242242
}
243243
}, [visible, scale, visibility]);
244244

245+
const { backgroundColor: customBackgroundColor, ...restStyle } =
246+
(StyleSheet.flatten(style) || {}) as ViewStyle;
247+
245248
const { backgroundColor, foregroundColor } = getFABColors({
246249
theme,
247250
variant,
248251
disabled,
249252
customColor,
250-
style,
253+
customBackgroundColor,
251254
});
252255

253256
const rippleColor = color(foregroundColor).alpha(0.12).rgb().string();
@@ -314,6 +317,7 @@ const AnimatedFAB = ({
314317
return (
315318
<Surface
316319
{...rest}
320+
testID={`${testID}-container`}
317321
style={
318322
[
319323
{
@@ -329,7 +333,7 @@ const AnimatedFAB = ({
329333
elevation: md2Elevation,
330334
},
331335
styles.container,
332-
style,
336+
restStyle,
333337
] as StyleProp<ViewStyle>
334338
}
335339
{...(isV3 && { elevation: md3Elevation })}

src/components/FAB/FAB.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,19 @@ const FAB = React.forwardRef<View, Props>(
205205

206206
const IconComponent = animated ? CrossFadeIcon : Icon;
207207

208+
const fabStyle = getFabStyle({ customSize, size, theme });
209+
210+
const {
211+
borderRadius = fabStyle.borderRadius,
212+
backgroundColor: customBackgroundColor,
213+
} = (StyleSheet.flatten(style) || {}) as ViewStyle;
214+
208215
const { backgroundColor, foregroundColor, rippleColor } = getFABColors({
209216
theme,
210217
variant,
211218
disabled,
212219
customColor,
213-
style,
220+
customBackgroundColor,
214221
});
215222

216223
const isLargeSize = size === 'large';
@@ -219,17 +226,12 @@ const FAB = React.forwardRef<View, Props>(
219226
const loadingIndicatorSize = isLargeSize ? 24 : 18;
220227
const font = isV3 ? theme.fonts.labelLarge : theme.fonts.medium;
221228

222-
const fabStyle = getFabStyle({ customSize, size, theme });
223229
const extendedStyle = getExtendedFabStyle({ customSize, theme });
224230
const textStyle = {
225231
color: foregroundColor,
226232
...font,
227233
};
228234

229-
const { borderRadius = fabStyle.borderRadius } = (StyleSheet.flatten(
230-
style
231-
) || {}) as ViewStyle;
232-
233235
const md3Elevation = isFlatMode || disabled ? 0 : 3;
234236

235237
const newAccessibilityState = { ...accessibilityState, disabled };

src/components/FAB/utils.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
Animated,
3-
I18nManager,
4-
StyleProp,
5-
StyleSheet,
6-
ViewStyle,
7-
} from 'react-native';
1+
import { Animated, ColorValue, I18nManager, ViewStyle } from 'react-native';
82

93
import color from 'color';
104

@@ -167,11 +161,10 @@ const getBackgroundColor = ({
167161
theme,
168162
isVariant,
169163
disabled,
170-
style,
171-
}: BaseProps & { style?: StyleProp<ViewStyle> }) => {
172-
const { backgroundColor } = StyleSheet.flatten(style) || {};
173-
if (backgroundColor && !disabled) {
174-
return backgroundColor;
164+
customBackgroundColor,
165+
}: BaseProps & { customBackgroundColor?: ColorValue }) => {
166+
if (customBackgroundColor && !disabled) {
167+
return customBackgroundColor;
175168
}
176169

177170
if (theme.isV3) {
@@ -263,13 +256,13 @@ export const getFABColors = ({
263256
variant,
264257
disabled,
265258
customColor,
266-
style,
259+
customBackgroundColor,
267260
}: {
268261
theme: InternalTheme;
269262
variant: string;
270263
disabled?: boolean;
271264
customColor?: string;
272-
style?: StyleProp<ViewStyle>;
265+
customBackgroundColor?: ColorValue;
273266
}) => {
274267
const isVariant = (variantToCompare: Variant) => {
275268
return variant === variantToCompare;
@@ -279,7 +272,7 @@ export const getFABColors = ({
279272

280273
const backgroundColor = getBackgroundColor({
281274
...baseFABColorProps,
282-
style,
275+
customBackgroundColor,
283276
});
284277

285278
const foregroundColor = getForegroundColor({

src/components/__tests__/AnimatedFAB.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import * as React from 'react';
2+
import { StyleSheet } from 'react-native';
23

4+
import { render } from '@testing-library/react-native';
35
import renderer from 'react-test-renderer';
46

57
import AnimatedFAB from '../FAB/AnimatedFAB';
68

9+
const styles = StyleSheet.create({
10+
background: {
11+
backgroundColor: 'purple',
12+
},
13+
});
14+
715
it('renders animated fab', () => {
816
const tree = renderer
917
.create(<AnimatedFAB onPress={() => {}} icon="plus" />)
@@ -37,3 +45,19 @@ it('renders animated fab with label on the left', () => {
3745

3846
expect(tree).toMatchSnapshot();
3947
});
48+
49+
it('renders animated fab with only transparent container', () => {
50+
const { getByTestId } = render(
51+
<AnimatedFAB
52+
label="text"
53+
animateFrom="left"
54+
onPress={() => {}}
55+
icon="plus"
56+
testID="animated-fab"
57+
style={styles.background}
58+
/>
59+
);
60+
expect(getByTestId('animated-fab-container')).toHaveStyle({
61+
backgroundColor: 'transparent',
62+
});
63+
});

src/components/__tests__/FAB.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ describe('getFABColors - background color', () => {
162162
getFABColors({
163163
theme: getTheme(),
164164
variant: 'primary',
165-
style: { backgroundColor: 'purple' },
165+
customBackgroundColor: 'purple',
166166
})
167167
).toMatchObject({
168168
backgroundColor: 'purple',

src/components/__tests__/__snapshots__/AnimatedFAB.test.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ exports[`renders animated fab 1`] = `
4949
],
5050
}
5151
}
52+
testID="animated-fab-container"
5253
>
5354
<View
5455
collapsable={false}
@@ -174,6 +175,7 @@ exports[`renders animated fab 1`] = `
174175
},
175176
]
176177
}
178+
testID="animated-fab"
177179
>
178180
<View
179181
style={
@@ -335,6 +337,7 @@ exports[`renders animated fab with label on the left 1`] = `
335337
],
336338
}
337339
}
340+
testID="animated-fab-container"
338341
>
339342
<View
340343
collapsable={false}
@@ -461,6 +464,7 @@ exports[`renders animated fab with label on the left 1`] = `
461464
},
462465
]
463466
}
467+
testID="animated-fab"
464468
>
465469
<View
466470
style={
@@ -624,6 +628,7 @@ exports[`renders animated fab with label on the right by default 1`] = `
624628
],
625629
}
626630
}
631+
testID="animated-fab-container"
627632
>
628633
<View
629634
collapsable={false}
@@ -750,6 +755,7 @@ exports[`renders animated fab with label on the right by default 1`] = `
750755
},
751756
]
752757
}
758+
testID="animated-fab"
753759
>
754760
<View
755761
style={

0 commit comments

Comments
 (0)