import {ReactNode, useState, useEffect, useMemo, useRef} from 'react' import Taro from '@tarojs/taro' import { View, Button, Text } from '@tarojs/components' import {ZPX} from '../constants/constants' import {TaroQuickAnimate} from './Components' import './widget.css' interface Modal { children?: ReactNode isShow?: boolean title?: string content?: string onCancel?: () => void onConfirm?: (e?: any) => void cancelTxt?: string confirmTxt?: string isHaveCancel?: boolean isHaveConfirm?: boolean size?: string /** 标题的字体大小 */ titleSize?: string /** 内容的字体大小 */ contentSize?: string /** 按钮颜色 */ buttonColor?: string /** 按钮大小 */ buttonRadius?: string padding?: string, margin?: string, } const animation = Taro.createAnimation({ transformOrigin: "50% 50%", duration: 430, timingFunction: "ease", delay: 0 }) const Modal = (props: Modal) => { const {windowHeight, windowWidth} = useRef(Taro.getSystemInfoSync()).current const { isShow=false, title='', content='', onCancel=() => {}, onConfirm=() => {}, cancelTxt='取消', confirmTxt='确定', isHaveCancel=true, isHaveConfirm=true, children, size='600 auto', titleSize='24', contentSize='32', buttonColor='#1677ff', padding='30', margin='0' } = props const [animate, setAnimate] = useState({actions: []}) const [backSize, setBackSize] = useState({ width: 0, height: 0, }) useEffect(() => { if(isShow){ setBackSize({ width: windowWidth, height: windowHeight }) animation.opacity(1).step() setAnimate(animation.export()) }else{ animation.opacity(0).step() setAnimate(animation.export()) setTimeout(() => { setBackSize({ width: 0, height: 0 }) }, 430); } }, [isShow, windowWidth, windowHeight]) const msize = useMemo(() => { if(backSize.width === 0){ return { width: 0, height: 0 } } return { width: size ? (size.split(' ')[0].includes('auto') ? 'auto' : size.split(' ')[0].includes('%') ? size.split(' ')[0] : size.split(' ')[0] + ZPX) : 'auto', height: size ? (size.split(' ')[1].includes('auto') ? 'auto' : size.split(' ')[1].includes('%') ? size.split(' ')[1] : size.split(' ')[1] + ZPX) : 'auto', } }, [size, backSize.width]) return( { onCancel() }} > { e.stopPropagation() }} > { title && {title} } { children ? children : {content} } { (isHaveCancel || isHaveConfirm) && { isHaveCancel && { onCancel() }} > {cancelTxt} } { isHaveConfirm && } } ) } export default Modal