index.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import {
  2. useImperativeHandle,
  3. forwardRef,
  4. useEffect,
  5. useState,
  6. useRef
  7. } from "react";
  8. import {
  9. TouchableWithoutFeedback,
  10. Animated,
  11. Platform,
  12. Easing,
  13. View
  14. } from "react-native";
  15. import type IModalProps from "./type";
  16. import {
  17. type IModalRef
  18. } from "./type";
  19. import stylesheet from "./stylesheet";
  20. import {
  21. NCoreUIKitTheme
  22. } from "../../core/hooks";
  23. import {
  24. type RefForwardingComponent
  25. } from "../../types";
  26. import {
  27. Portal
  28. } from "../../helpers/portalize";
  29. import {
  30. uuid
  31. } from "../../utils";
  32. /**
  33. * A generic modal
  34. * @param props {@link IModalProps}
  35. * @returns Element
  36. */
  37. const Modal: RefForwardingComponent<IModalRef, IModalProps> = ({
  38. isScaleAnimatedOnlyOpen = true,
  39. closeAnimationDelay = 100,
  40. isDisabledOverlay = false,
  41. openAnimationDelay = 100,
  42. isContentRequired = true,
  43. isOverlayVisible = true,
  44. isScaleAnimated = false,
  45. alignContent = "center",
  46. isWorkWithPortal = true,
  47. isActive: isActiveProp,
  48. onContainerLayout,
  49. isAnimated = true,
  50. onOverlayPress,
  51. contentStyle,
  52. overlayProps,
  53. customTheme,
  54. portalName,
  55. id: outID,
  56. children,
  57. onClosed,
  58. style
  59. }, ref) => {
  60. const {
  61. colors
  62. } = NCoreUIKitTheme.useContext(customTheme);
  63. const id = useRef(outID ? outID : uuid());
  64. const scaleAnim = useRef(new Animated.Value(isAnimated && isScaleAnimated ? 0 : 1)).current;
  65. const opacityAnim = useRef(new Animated.Value(isAnimated ? 0 : 1)).current;
  66. const [
  67. isActive,
  68. setIsActive
  69. ] = useState(isActiveProp === undefined ? true : isActiveProp);
  70. useEffect(() => {
  71. if(isAnimated) {
  72. if(isScaleAnimated) {
  73. Animated.timing(scaleAnim, {
  74. duration: openAnimationDelay,
  75. useNativeDriver: true,
  76. easing: Easing.linear,
  77. toValue: 1
  78. }).start();
  79. }
  80. Animated.timing(opacityAnim, {
  81. duration: openAnimationDelay,
  82. useNativeDriver: true,
  83. easing: Easing.linear,
  84. toValue: 1
  85. }).start();
  86. }
  87. }, [isActive]);
  88. useEffect(() => {
  89. if(isActiveProp !== undefined) {
  90. setIsActive(isActiveProp);
  91. }
  92. }, [isActiveProp]);
  93. useImperativeHandle(
  94. ref,
  95. () => ({
  96. closeAnimation,
  97. setIsActive
  98. }),
  99. []
  100. );
  101. const closeAnimation = (_onClosed?: (props: {
  102. id: string;
  103. }) => void) => {
  104. if(isAnimated) {
  105. if(isScaleAnimated && !isScaleAnimatedOnlyOpen) {
  106. Animated.timing(scaleAnim, {
  107. duration: closeAnimationDelay,
  108. useNativeDriver: true,
  109. easing: Easing.linear,
  110. toValue: 0
  111. }).start();
  112. }
  113. Animated.timing(opacityAnim, {
  114. duration: closeAnimationDelay,
  115. useNativeDriver: true,
  116. easing: Easing.linear,
  117. toValue: 0
  118. }).start(({
  119. finished
  120. }) => {
  121. if(finished) {
  122. if(_onClosed) _onClosed({
  123. id: id.current
  124. });
  125. if(onClosed) onClosed({
  126. id: id.current
  127. });
  128. }
  129. });
  130. }
  131. };
  132. const renderOverlay = () => {
  133. if(!isOverlayVisible) {
  134. return null;
  135. }
  136. return <TouchableWithoutFeedback
  137. {...overlayProps}
  138. style={[
  139. stylesheet.overlay
  140. ]}
  141. onPress={() => {
  142. if(isDisabledOverlay) {
  143. return;
  144. }
  145. if(onOverlayPress) onOverlayPress({
  146. closeAnimation
  147. });
  148. }}
  149. >
  150. <View
  151. style={[
  152. stylesheet.overlayContent,
  153. {
  154. backgroundColor: colors.system.scrim
  155. }
  156. ]}
  157. {...Platform.select({
  158. web: {
  159. dataSet: {
  160. disablePressableHoverEffect: "true",
  161. disablePressableDownEffect: "true"
  162. }
  163. },
  164. default: {}
  165. })}
  166. />
  167. </TouchableWithoutFeedback>;
  168. };
  169. const renderContainer = () => {
  170. return <Animated.View
  171. onLayout={(e) => {
  172. if (onContainerLayout) onContainerLayout(e);
  173. }}
  174. style={[
  175. style,
  176. stylesheet.container,
  177. {
  178. justifyContent: alignContent === "center" ? "center" : undefined,
  179. alignItems: alignContent === "center" ? "center" : "baseline",
  180. opacity: opacityAnim,
  181. transform: [{
  182. scale: scaleAnim
  183. }]
  184. }
  185. ]}
  186. >
  187. {renderOverlay()}
  188. {isContentRequired ? renderContent() : children}
  189. </Animated.View>;
  190. };
  191. const renderContent = () => {
  192. if(!children) {
  193. return null;
  194. }
  195. return <View
  196. style={[
  197. contentStyle,
  198. stylesheet.content
  199. ]}
  200. >
  201. {children}
  202. </View>;
  203. };
  204. const renderWithPortal = () => {
  205. return <Portal name={portalName ? portalName : "modal-system"}>
  206. {renderContainer()}
  207. </Portal>;
  208. };
  209. if(!isActive) {
  210. return null;
  211. }
  212. return isWorkWithPortal ? renderWithPortal() : renderContainer();
  213. };
  214. export default forwardRef(Modal);