Skip to content
20 changes: 17 additions & 3 deletions app/containers/ActionSheet/ActionSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { GestureHandlerRootView } from 'react-native-gesture-handler';

import { useTheme } from '../../theme';
import { isAndroid, isIOS } from '../../lib/methods/helpers';
import log from '../../lib/methods/helpers/log';
import { Handle } from './Handle';
import { type TActionSheetOptions } from './Provider';
import { type TActionSheetOptions, type TActionSheetOptionsItem } from './Provider';
import BottomSheetContent from './BottomSheetContent';
import { HANDLE_HEIGHT, useActionSheetDetents } from './useActionSheetDetents';
import styles from './styles';
Expand All @@ -24,6 +25,7 @@ const ActionSheet = React.memo(
const [isVisible, setIsVisible] = useState(false);
const [contentHeight, setContentHeight] = useState(0);
const onCloseSnapshotRef = useRef<TActionSheetOptions['onClose']>(undefined);
const deferredPressRef = useRef<TActionSheetOptionsItem['onPress']>(undefined);

// TrueSheet detects the bottom inset for Android 16 and iOS
// To avoid content hiding behind navigation bar on older Android versions
Expand All @@ -35,7 +37,8 @@ const ActionSheet = React.memo(
setContentHeight(layout.height);
};

const hide = () => {
const hide = (deferredPress?: TActionSheetOptionsItem['onPress']) => {
deferredPressRef.current = deferredPress;
sheetRef.current?.dismiss();
Keyboard.dismiss();
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
Expand Down Expand Up @@ -73,8 +76,19 @@ const ActionSheet = React.memo(
setIsVisible(false);
// Keep contentHeight to avoid flickering on next show
const snapshotOnClose = onCloseSnapshotRef.current;
const deferredPress = deferredPressRef.current;
onCloseSnapshotRef.current = undefined;
snapshotOnClose?.();
deferredPressRef.current = undefined;
try {
snapshotOnClose?.();
} catch (e) {
log(e);
}
try {
deferredPress?.();
} catch (e) {
log(e);
}
};

const isPortrait = windowHeight > windowWidth;
Expand Down
4 changes: 2 additions & 2 deletions app/containers/ActionSheet/BottomSheetContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Touch from '../Touch';
interface IBottomSheetContentProps {
hasCancel?: boolean;
options?: TActionSheetOptionsItem[];
hide: () => void;
hide: (deferredPress?: () => void) => void;
children?: React.ReactElement | null;
onLayout: ViewProps['onLayout'];
fullContainer?: boolean;
Expand Down Expand Up @@ -45,7 +45,7 @@ const BottomSheetContent = React.memo(
const renderFooter = () =>
hasCancel ? (
<Touch
onPress={hide}
onPress={() => hide()}
style={[styles.button, { backgroundColor: colors.surfaceHover, height }]}
accessibilityLabel={I18n.t('Cancel')}>
<Text style={[styles.text, { color: colors.fontDefault }]}>{I18n.t('Cancel')}</Text>
Expand Down
12 changes: 9 additions & 3 deletions app/containers/ActionSheet/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Touch from '../Touch';

export interface IActionSheetItem {
item: TActionSheetOptionsItem;
hide(): void;
hide(deferredPress?: () => void): void;
}

export const Item = React.memo(({ item, hide }: IActionSheetItem) => {
Expand All @@ -23,8 +23,14 @@ export const Item = React.memo(({ item, hide }: IActionSheetItem) => {
const { fontScale } = useWindowDimensions();
const onPress = () => {
if (enabled) {
hide();
item?.onPress();
if (item?.runOnSheetClose) {
hide(item?.onPress);
} else {
hide();
if (item?.onPress) {
item.onPress();
}
}
} else {
EventEmitter.emit(LISTENER, { message: item?.disabledReason || I18n.t('You_dont_have_permission_to_perform_this_action') });
}
Expand Down
3 changes: 2 additions & 1 deletion app/containers/ActionSheet/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export type TActionSheetOptionsItem = {
icon?: TIconsName;
danger?: boolean;
testID?: string;
onPress: () => void;
onPress?: () => void;
runOnSheetClose?: boolean;
right?: () => React.ReactElement;
enabled?: boolean;
accessibilityRole?: AccessibilityRole;
Expand Down
34 changes: 10 additions & 24 deletions app/containers/MessageComposer/components/Buttons/ActionsButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const ActionsButton = () => {
tmid,
permissionToUpload
});
const { showActionSheet, hideActionSheet } = useActionSheet();
const { showActionSheet } = useActionSheet();
const isMasterDetail = useAppSelector(state => state.app.isMasterDetail);

const createDiscussion = async () => {
Expand Down Expand Up @@ -51,48 +51,34 @@ export const ActionsButton = () => {
{
title: I18n.t('Take_a_photo'),
icon: 'camera-photo',
onPress: () => {
hideActionSheet();
// This is necessary because the action sheet does not close properly on Android
setTimeout(() => {
takePhoto();
}, 250);
}
onPress: takePhoto,
runOnSheetClose: true
},
{
title: I18n.t('Take_a_video'),
icon: 'camera',
onPress: () => {
hideActionSheet();
// This is necessary because the action sheet does not close properly on Android
setTimeout(() => {
takeVideo();
}, 250);
}
onPress: takeVideo,
runOnSheetClose: true
},
{
title: I18n.t('Choose_from_library'),
icon: 'image',
onPress: () => {
hideActionSheet();
// This is necessary because the action sheet does not close properly on Android
setTimeout(() => {
chooseFromLibrary();
}, 250);
}
onPress: chooseFromLibrary,
runOnSheetClose: true
},
{
title: I18n.t('Choose_file'),
icon: 'attach',
onPress: () => chooseFile()
onPress: chooseFile,
runOnSheetClose: true
}
);
}

options.push({
title: I18n.t('Create_Discussion'),
icon: 'discussions',
onPress: () => createDiscussion()
onPress: createDiscussion
});

closeEmojiKeyboardAndAction(showActionSheet, { options });
Expand Down
Loading