new_modal.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { View, Text, ScrollView } from '@tarojs/components'
  2. import './new_modal.scss'
  3. import React, { useEffect, useRef, useState } from 'react';
  4. import { ModalType } from '@/utils/types';
  5. import Taro from '@tarojs/taro';
  6. import { vibrate } from '@/utils/tools';
  7. import { MainColorType } from '@/context/themes/color';
  8. let ModalRN, Animated
  9. if (process.env.TARO_ENV == 'rn') {
  10. ModalRN = require("react-native").Modal
  11. Animated = require("react-native").Animated
  12. }
  13. export default function NewModal(props: {
  14. children: React.ReactNode,
  15. title?: string,
  16. btnTitle?: string,
  17. dismiss: Function,
  18. confirm?: Function,
  19. themeColor?: string,
  20. cancelCatchMove?: boolean,
  21. themeIsWhite?: boolean
  22. }) {
  23. const [isDismiss, setIsDismiss] = useState(false)
  24. let animation, animatedStyle;
  25. if (process.env.TARO_ENV == 'rn') {
  26. animation = useRef(new Animated.Value(0)).current;
  27. animatedStyle = {
  28. transform: [
  29. {
  30. translateY: animation.interpolate({
  31. inputRange: [0, 1],
  32. outputRange: [300, 0],
  33. }),
  34. },
  35. ],
  36. };
  37. }
  38. useEffect(() => {
  39. if (process.env.TARO_ENV == 'rn') {
  40. startAnimation()
  41. }
  42. }, [])
  43. const startAnimation = () => {
  44. Animated.spring(animation, {
  45. toValue: 1,
  46. duration: 50,
  47. useNativeDriver: true,
  48. }).start();
  49. };
  50. const endAnimation = () => {
  51. Animated.spring(animation, {
  52. toValue: 0,
  53. duration: 50,
  54. useNativeDriver: true,
  55. }).start();
  56. console.log('end')
  57. };
  58. //阻止中间内容点击事件穿透
  59. function click(e) {
  60. if (process.env.TARO_ENV == 'weapp') {
  61. e.stopPropagation()
  62. }
  63. vibrate()
  64. }
  65. function onClick() {
  66. }
  67. function longPress(e) {
  68. if (process.env.TARO_ENV == 'weapp') {
  69. e.stopPropagation()
  70. }
  71. }
  72. function dismiss() {
  73. setIsDismiss(true)
  74. setTimeout(() => {
  75. props.dismiss()
  76. }, 250)
  77. }
  78. function rndismiss() {
  79. endAnimation()
  80. setTimeout(() => {
  81. props.dismiss()
  82. }, 250)
  83. }
  84. global.dismissModal = dismiss;
  85. if (process.env.TARO_ENV == 'rn') {
  86. return <ModalRN transparent
  87. // animationType="slide"
  88. // style={{backgroundColor:'red'}}
  89. >
  90. <View style={{ flex: 1, backgroundColor: props.themeIsWhite ? '#ffffff90' : '#000000cc' }}>
  91. <View style={{ flex: 1, backgroundColor: 'transparent' }} onClick={(e) => {
  92. rndismiss()
  93. }}></View>
  94. <Animated.View className={isDismiss ? 'modal_bottom_content modal_bottom_dismiss' : 'modal_bottom_content'}
  95. style={[{ flexShrink: 0 }, animatedStyle]} onClick={onClick}>
  96. {
  97. props.children
  98. }
  99. </Animated.View>
  100. </View>
  101. </ModalRN>
  102. }
  103. return <View className={isDismiss ? 'modal modal_dismiss' : 'modal'} catchMove onLongPress={longPress}>
  104. <View style={{ flex: 1, width: 375, flexShrink: 0 }} onClick={(e) => {
  105. if (process.env.TARO_ENV == 'weapp') {
  106. e.stopPropagation()
  107. }; dismiss()
  108. }}>
  109. </View>
  110. <View className={isDismiss ? 'modal_bottom_content modal_bottom_dismiss' : 'modal_bottom_content'} style={{ flexShrink: 0 }} onClick={onClick}>
  111. <View className='modal_title'>{props.title}</View>
  112. {
  113. props.children
  114. }
  115. <View className='modal_footer'>
  116. <View className='modal_footer_btn'
  117. onClick={() => {
  118. props.confirm && props.confirm()
  119. }}
  120. style={{ backgroundColor: props.themeColor ?? MainColorType.fast }}>{props.btnTitle ?? '确定'}</View>
  121. </View>
  122. </View>
  123. </View>
  124. }