Skip to content

Commit 9060c3c

Browse files
authored
fix: hide actions after closing fab group (#4692)
1 parent ff58b00 commit 9060c3c

File tree

1 file changed

+44
-26
lines changed

1 file changed

+44
-26
lines changed

src/components/FAB/FABGroup.tsx

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,18 @@ const FABGroup = ({
222222
rippleColor,
223223
}: Props) => {
224224
const theme = useInternalTheme(themeOverrides);
225+
const { top, bottom, right, left } = useSafeAreaInsets();
226+
225227
const { current: backdrop } = React.useRef<Animated.Value>(
226228
new Animated.Value(0)
227229
);
228230
const animations = React.useRef<Animated.Value[]>(
229231
actions.map(() => new Animated.Value(open ? 1 : 0))
230232
);
231233

234+
const [isClosingAnimationFinished, setIsClosingAnimationFinished] =
235+
React.useState(false);
236+
232237
const [prevActions, setPrevActions] = React.useState<
233238
| {
234239
icon: IconSource;
@@ -247,6 +252,7 @@ const FABGroup = ({
247252

248253
React.useEffect(() => {
249254
if (open) {
255+
setIsClosingAnimationFinished(false);
250256
Animated.parallel([
251257
Animated.timing(backdrop, {
252258
toValue: 1,
@@ -280,14 +286,33 @@ const FABGroup = ({
280286
useNativeDriver: true,
281287
})
282288
),
283-
]).start();
289+
]).start(({ finished }) => {
290+
if (finished) {
291+
setIsClosingAnimationFinished(true);
292+
}
293+
});
284294
}
285295
}, [open, actions, backdrop, scale, isV3]);
286296

287297
const close = () => onStateChange({ open: false });
288-
289298
const toggle = () => onStateChange({ open: !open });
290299

300+
const handlePress = (e: GestureResponderEvent) => {
301+
onPress?.(e);
302+
if (!toggleStackOnLongPress || open) {
303+
toggle();
304+
}
305+
};
306+
307+
const handleLongPress = (e: GestureResponderEvent) => {
308+
if (!open || enableLongPressWhenStackOpened) {
309+
onLongPress?.(e);
310+
if (toggleStackOnLongPress) {
311+
toggle();
312+
}
313+
}
314+
};
315+
291316
const { labelColor, backdropColor, stackedFABBackgroundColor } =
292317
getFABGroupColors({ theme, customBackdropColor });
293318

@@ -325,14 +350,17 @@ const FABGroup = ({
325350
: -8
326351
);
327352

328-
const { top, bottom, right, left } = useSafeAreaInsets();
329353
const containerPaddings = {
330354
paddingBottom: bottom,
331355
paddingRight: right,
332356
paddingLeft: left,
333357
paddingTop: top,
334358
};
335359

360+
const actionsContainerVisibility: ViewStyle = {
361+
display: isClosingAnimationFinished ? 'none' : 'flex',
362+
};
363+
336364
if (actions.length !== prevActions?.length) {
337365
animations.current = actions.map(
338366
(_, i) => animations.current[i] || new Animated.Value(open ? 1 : 0)
@@ -358,7 +386,10 @@ const FABGroup = ({
358386
]}
359387
/>
360388
<View pointerEvents="box-none" style={styles.safeArea}>
361-
<View pointerEvents={open ? 'box-none' : 'none'}>
389+
<View
390+
pointerEvents={open ? 'box-none' : 'none'}
391+
style={actionsContainerVisibility}
392+
>
362393
{actions.map((it, i) => {
363394
const labelTextStyle = {
364395
color: it.labelTextColor ?? labelColor,
@@ -372,6 +403,11 @@ const FABGroup = ({
372403
: it.label;
373404
const size = typeof it.size !== 'undefined' ? it.size : 'small';
374405

406+
const handleActionPress = (e: GestureResponderEvent) => {
407+
it.onPress(e);
408+
close();
409+
};
410+
375411
return (
376412
<View
377413
key={i} // eslint-disable-line react/no-array-index-key
@@ -392,10 +428,7 @@ const FABGroup = ({
392428
<View>
393429
<Card
394430
mode={isV3 ? 'contained' : 'elevated'}
395-
onPress={(e) => {
396-
it.onPress(e);
397-
close();
398-
}}
431+
onPress={handleActionPress}
399432
accessibilityHint={it.accessibilityHint}
400433
importantForAccessibility="no-hide-descendants"
401434
accessibilityElementsHidden={true}
@@ -440,10 +473,7 @@ const FABGroup = ({
440473
]}
441474
accessibilityElementsHidden={true}
442475
theme={theme}
443-
onPress={(e) => {
444-
it.onPress(e);
445-
close();
446-
}}
476+
onPress={handleActionPress}
447477
importantForAccessibility="no-hide-descendants"
448478
testID={it.testID}
449479
visible={open}
@@ -454,20 +484,8 @@ const FABGroup = ({
454484
})}
455485
</View>
456486
<FAB
457-
onPress={(e) => {
458-
onPress?.(e);
459-
if (!toggleStackOnLongPress || open) {
460-
toggle();
461-
}
462-
}}
463-
onLongPress={(e) => {
464-
if (!open || enableLongPressWhenStackOpened) {
465-
onLongPress?.(e);
466-
if (toggleStackOnLongPress) {
467-
toggle();
468-
}
469-
}
470-
}}
487+
onPress={handlePress}
488+
onLongPress={handleLongPress}
471489
delayLongPress={delayLongPress}
472490
icon={icon}
473491
color={colorProp}

0 commit comments

Comments
 (0)