2022-03-22 20:44:27 +00:00
|
|
|
import { useBackHandler } from '@react-native-community/hooks';
|
|
|
|
import * as Haptics from 'expo-haptics';
|
2022-05-19 16:53:45 +00:00
|
|
|
import React, { forwardRef, isValidElement, useEffect, useImperativeHandle, useRef, useState, useCallback } from 'react';
|
|
|
|
import { Keyboard } from 'react-native';
|
|
|
|
import { Easing } from 'react-native-reanimated';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
2022-05-19 16:53:45 +00:00
|
|
|
import BottomSheet, { BottomSheetBackdrop } from '@gorhom/bottom-sheet';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-03-22 20:44:27 +00:00
|
|
|
import { useDimensions, useOrientation } from '../../dimensions';
|
|
|
|
import { useTheme } from '../../theme';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { isIOS, isTablet } from '../../lib/methods/helpers';
|
2022-03-22 20:44:27 +00:00
|
|
|
import { Handle } from './Handle';
|
2022-05-19 16:53:45 +00:00
|
|
|
import { TActionSheetOptions } from './Provider';
|
|
|
|
import BottomSheetContent from './BottomSheetContent';
|
2022-03-22 20:44:27 +00:00
|
|
|
import styles, { ITEM_HEIGHT } from './styles';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
const HANDLE_HEIGHT = isIOS ? 40 : 56;
|
2022-05-19 16:53:45 +00:00
|
|
|
const MIN_SNAP_HEIGHT = 16;
|
2021-09-13 20:41:05 +00:00
|
|
|
const CANCEL_HEIGHT = 64;
|
|
|
|
|
|
|
|
const ANIMATION_DURATION = 250;
|
|
|
|
|
|
|
|
const ANIMATION_CONFIG = {
|
|
|
|
duration: ANIMATION_DURATION,
|
|
|
|
// https://easings.net/#easeInOutCubic
|
|
|
|
easing: Easing.bezier(0.645, 0.045, 0.355, 1.0)
|
|
|
|
};
|
|
|
|
|
|
|
|
const ActionSheet = React.memo(
|
2022-03-22 20:44:27 +00:00
|
|
|
forwardRef(({ children }: { children: React.ReactElement }, ref) => {
|
2022-05-19 16:53:45 +00:00
|
|
|
const { colors } = useTheme();
|
|
|
|
const bottomSheetRef = useRef<BottomSheet>(null);
|
2022-03-22 20:44:27 +00:00
|
|
|
const [data, setData] = useState<TActionSheetOptions>({} as TActionSheetOptions);
|
2021-09-13 20:41:05 +00:00
|
|
|
const [isVisible, setVisible] = useState(false);
|
2022-03-22 20:44:27 +00:00
|
|
|
const { height } = useDimensions();
|
2021-09-13 20:41:05 +00:00
|
|
|
const { isLandscape } = useOrientation();
|
|
|
|
const insets = useSafeAreaInsets();
|
|
|
|
|
2022-05-19 16:53:45 +00:00
|
|
|
const maxSnap = Math.min(
|
|
|
|
// Items height
|
|
|
|
ITEM_HEIGHT * (data?.options?.length || 0) +
|
2021-09-13 20:41:05 +00:00
|
|
|
// Handle height
|
2022-05-19 16:53:45 +00:00
|
|
|
HANDLE_HEIGHT +
|
2021-09-13 20:41:05 +00:00
|
|
|
// Custom header height
|
2022-05-19 16:53:45 +00:00
|
|
|
(data?.headerHeight || 0) +
|
2021-09-13 20:41:05 +00:00
|
|
|
// Insets bottom height (Notch devices)
|
2022-05-19 16:53:45 +00:00
|
|
|
insets.bottom +
|
2021-09-13 20:41:05 +00:00
|
|
|
// Cancel button height
|
|
|
|
(data?.hasCancel ? CANCEL_HEIGHT : 0),
|
2022-05-19 16:53:45 +00:00
|
|
|
height - MIN_SNAP_HEIGHT
|
2021-09-13 20:41:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if the action sheet cover more
|
|
|
|
* than 60% of the whole screen
|
|
|
|
* and it's not at the landscape mode
|
|
|
|
* we'll provide more one snap
|
|
|
|
* that point 50% of the whole screen
|
|
|
|
*/
|
2022-05-19 16:53:45 +00:00
|
|
|
const snaps = maxSnap > height * 0.6 && !isLandscape && !data.snaps ? [height * 0.5, maxSnap] : [maxSnap];
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
const toggleVisible = () => setVisible(!isVisible);
|
|
|
|
|
|
|
|
const hide = () => {
|
2022-05-19 16:53:45 +00:00
|
|
|
bottomSheetRef.current?.close();
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
|
|
|
|
2022-03-22 20:44:27 +00:00
|
|
|
const show = (options: TActionSheetOptions) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
setData(options);
|
|
|
|
toggleVisible();
|
|
|
|
};
|
|
|
|
|
|
|
|
useBackHandler(() => {
|
|
|
|
if (isVisible) {
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
return isVisible;
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isVisible) {
|
|
|
|
Keyboard.dismiss();
|
|
|
|
Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
|
|
|
|
}
|
|
|
|
}, [isVisible]);
|
|
|
|
|
|
|
|
// Hides action sheet when orientation changes
|
|
|
|
useEffect(() => {
|
|
|
|
setVisible(false);
|
|
|
|
}, [isLandscape]);
|
|
|
|
|
|
|
|
useImperativeHandle(ref, () => ({
|
|
|
|
showActionSheet: show,
|
|
|
|
hideActionSheet: hide
|
|
|
|
}));
|
|
|
|
|
|
|
|
const renderHandle = () => (
|
|
|
|
<>
|
2022-03-22 20:44:27 +00:00
|
|
|
<Handle />
|
2021-09-13 20:41:05 +00:00
|
|
|
{isValidElement(data?.customHeader) ? data.customHeader : null}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
2022-05-19 16:53:45 +00:00
|
|
|
const renderBackdrop = useCallback(
|
|
|
|
props => (
|
|
|
|
<BottomSheetBackdrop
|
|
|
|
{...props}
|
|
|
|
appearsOnIndex={0}
|
|
|
|
// Backdrop should be visible all the time bottom sheet is open
|
|
|
|
disappearsOnIndex={-1}
|
|
|
|
opacity={colors.backdropOpacity}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
[]
|
|
|
|
);
|
2022-03-22 20:44:27 +00:00
|
|
|
|
|
|
|
const bottomSheet = isLandscape || isTablet ? styles.bottomSheet : {};
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{children}
|
|
|
|
{isVisible && (
|
2022-05-19 16:53:45 +00:00
|
|
|
<BottomSheet
|
|
|
|
ref={bottomSheetRef}
|
|
|
|
snapPoints={data?.snaps ? data.snaps : snaps}
|
|
|
|
animationConfigs={ANIMATION_CONFIG}
|
|
|
|
animateOnMount={true}
|
|
|
|
backdropComponent={renderBackdrop}
|
|
|
|
handleComponent={renderHandle}
|
|
|
|
enablePanDownToClose
|
|
|
|
style={{ ...styles.container, ...bottomSheet }}
|
|
|
|
backgroundStyle={{ backgroundColor: colors.focusedBackground }}
|
|
|
|
onChange={index => index === -1 && toggleVisible()}>
|
|
|
|
<BottomSheetContent options={data?.options} hide={hide} children={data?.children} hasCancel={data?.hasCancel} />
|
|
|
|
</BottomSheet>
|
2021-09-13 20:41:05 +00:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
export default ActionSheet;
|