BlueWallet/components/BottomModal.tsx

78 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-07-01 18:58:19 +02:00
import React, { forwardRef, useImperativeHandle, useRef, ReactElement, ComponentType } from 'react';
2024-07-01 00:27:37 +02:00
import { SheetSize, TrueSheet, TrueSheetProps } from '@lodev09/react-native-true-sheet';
2024-07-01 18:58:19 +02:00
import { StyleSheet, View } from 'react-native';
2024-05-20 11:54:13 +02:00
2024-06-30 19:17:55 +02:00
interface BottomModalProps extends TrueSheetProps {
2024-02-10 05:35:24 +01:00
children?: React.ReactNode;
2024-06-30 19:17:55 +02:00
onClose?: () => void;
name?: string;
isGrabberVisible?: boolean;
2024-07-01 00:27:37 +02:00
sizes?: SheetSize[] | undefined;
2024-07-01 18:58:19 +02:00
footer?: ReactElement | ComponentType<any>;
footerDefaultMargins?: boolean | number;
2024-02-10 05:35:24 +01:00
}
2024-06-30 19:17:55 +02:00
export interface BottomModalHandle {
present: () => Promise<void>;
dismiss: () => Promise<void>;
}
const BottomModal = forwardRef<BottomModalHandle, BottomModalProps>(
2024-07-01 18:58:19 +02:00
(
{ name, onClose, onPresent, onSizeChange, isGrabberVisible = true, sizes = ['auto'], footer, footerDefaultMargins, children, ...props },
ref,
) => {
2024-06-30 19:17:55 +02:00
const trueSheetRef = useRef<TrueSheet>(null);
2024-02-10 05:35:24 +01:00
2024-06-30 19:17:55 +02:00
useImperativeHandle(ref, () => ({
present: async () => {
if (trueSheetRef.current?.present) {
await trueSheetRef.current.present();
} else {
return Promise.resolve();
}
},
dismiss: async () => {
if (trueSheetRef.current?.dismiss) {
await trueSheetRef.current.dismiss();
} else {
return Promise.resolve();
}
},
}));
2024-07-01 18:58:19 +02:00
const stlyes = StyleSheet.create({
footerContainer: { padding: typeof footerDefaultMargins === 'number' ? footerDefaultMargins : 40, alignItems: 'center' },
});
let FooterComponent: ReactElement | ComponentType<any> | undefined;
if (footer) {
if (React.isValidElement(footer)) {
FooterComponent = footerDefaultMargins ? <View style={stlyes.footerContainer}>{footer}</View> : footer;
} else {
FooterComponent = footer;
}
}
2024-06-30 19:17:55 +02:00
return (
<TrueSheet
name={name ?? 'BottomModal'}
ref={trueSheetRef}
cornerRadius={24}
2024-07-01 00:27:37 +02:00
sizes={sizes}
2024-06-30 19:17:55 +02:00
blurTint="regular"
onDismiss={onClose}
onPresent={onPresent}
onSizeChange={onSizeChange}
grabber={isGrabberVisible}
2024-07-01 18:58:19 +02:00
FooterComponent={FooterComponent}
2024-06-30 19:17:55 +02:00
{...props}
>
{children}
</TrueSheet>
);
},
);
2020-11-17 09:43:38 +01:00
export default BottomModal;