import { useImperativeHandle, forwardRef, useEffect, useRef } from "react"; import { Animated, Easing, View } from "react-native"; import type IDialogProps from "./type"; import type { IDialogRef } from "./type"; import stylesheet, { useStyles } from "./stylesheet"; import { NCoreUIKitLocalize, NCoreUIKitTheme } from "../../core/hooks"; import type { RefForwardingComponent } from "../../types"; import type { IModalRef } from "../modal/type"; import { X as XIcon } from "lucide-react-native"; import Button from "../button"; import Modal from "../modal"; import Text from "../text"; import { uuid } from "../../utils"; const Dialog: RefForwardingComponent = ({ bottomContentContainerStyle, contentJustify = "centered", closeAnimationDelay = 100, openAnimationDelay = 100, contentContainerStyle, bottomContainerStyle, headerContainerStyle, secondaryButtonStyle, secondaryButtonProps, primaryButtonStyle, primaryButtonProps, closeIconProps = { color: "low", size: 22 }, isVisible = false, withModal = true, headerComponent, bottomComponent, onOverlayPress, variant = "ok", modalProps, id: outID, onClosed, children, content, style, title }, ref) => { const { radiuses, colors, spaces } = NCoreUIKitTheme.useContext(); const { localize } = NCoreUIKitLocalize.useContext(); const id = useRef(outID ? outID : uuid()); const { bottomContentContainer: bottomContentContainerDynamicStyle, headerContainer: headerContainerDynamicStyle, bottomContainer: bottomContainerDynamicStyle, primaryButton: primaryButtonDynamicStyle, contentText: contentTextDynamicStyle, headerTitle: headerTitleDynamicStyle, container: containerDynamicStyle, closeIcon: closeIconDynamicStyle, content: contentDynamicStyle } = useStyles({ contentJustify, isVisible, radiuses, variant, colors, spaces }); const scaleAnim = useRef(new Animated.Value(0)).current; const modalRef = useRef(null); useEffect(() => { if(isVisible) { Animated.timing(scaleAnim, { duration: openAnimationDelay, useNativeDriver: true, easing: Easing.linear, toValue: 1 }).start(); } }, [isVisible]); useImperativeHandle( ref, () => ({ closeAnimation }), [] ); const closeAnimation = (_onClosed?: (props: { id: string; }) => void) => { Animated.timing(scaleAnim, { duration: closeAnimationDelay, useNativeDriver: true, easing: Easing.linear, toValue: 0 }).start(({ finished }) => { if(finished) { if(_onClosed) _onClosed({ id: id.current }); if(onClosed) onClosed({ id: id.current }); if(modalRef && modalRef.current) modalRef.current.closeAnimation(); } }); }; const renderHeader = () => { return {headerComponent || {title} } ; }; const renderBottom = () => { if(variant === "info") { return null; } return {bottomComponent || {secondaryButton()} {primaryButton()} } ; }; const secondaryButton = () => { if(variant !== "yes-no") { return null; } return