|
1 | 1 | import * as React from 'react'; |
| 2 | +import { useState, useEffect } from 'react'; |
2 | 3 | import { CSSMotionProps } from 'rc-motion'; |
| 4 | +import isMobile from 'rc-util/lib/isMobile'; |
3 | 5 | import { |
4 | 6 | StretchType, |
5 | 7 | AlignType, |
6 | 8 | TransitionNameType, |
7 | 9 | AnimationType, |
8 | 10 | Point, |
| 11 | + MobileConfig, |
9 | 12 | } from '../interface'; |
10 | 13 | import Mask from './Mask'; |
11 | 14 | import PopupInner, { PopupInnerRef } from './PopupInner'; |
| 15 | +import MobilePopupInner from './MobilePopupInner'; |
12 | 16 |
|
13 | 17 | export interface PopupProps { |
14 | 18 | visible?: boolean; |
@@ -39,19 +43,41 @@ export interface PopupProps { |
39 | 43 | transitionName: TransitionNameType; |
40 | 44 | maskAnimation: AnimationType; |
41 | 45 | maskTransitionName: TransitionNameType; |
| 46 | + |
| 47 | + // Mobile |
| 48 | + mobile?: MobileConfig; |
42 | 49 | } |
43 | 50 |
|
44 | | -const Popup = React.forwardRef<PopupInnerRef, PopupProps>((props, ref) => { |
45 | | - const { ...cloneProps } = props; |
| 51 | +const Popup = React.forwardRef<PopupInnerRef, PopupProps>( |
| 52 | + ({ visible, mobile, ...props }, ref) => { |
| 53 | + const [innerVisible, serInnerVisible] = useState(visible); |
| 54 | + const [inMobile, setInMobile] = useState(false); |
| 55 | + const cloneProps = { ...props, visible: innerVisible }; |
| 56 | + |
| 57 | + // We check mobile in visible changed here. |
| 58 | + // And this also delay set `innerVisible` to avoid popup component render flash |
| 59 | + useEffect(() => { |
| 60 | + serInnerVisible(visible); |
| 61 | + if (visible && mobile) { |
| 62 | + setInMobile(isMobile()); |
| 63 | + } |
| 64 | + }, [visible, !!mobile]); |
46 | 65 |
|
47 | | - // We can use fragment directly but this may failed some selector usage. Keep as origin logic |
48 | | - return ( |
49 | | - <div> |
50 | | - <Mask {...cloneProps} /> |
| 66 | + const popupNode: React.ReactNode = inMobile ? ( |
| 67 | + <MobilePopupInner {...cloneProps} mobile={mobile} ref={ref} /> |
| 68 | + ) : ( |
51 | 69 | <PopupInner {...cloneProps} ref={ref} /> |
52 | | - </div> |
53 | | - ); |
54 | | -}); |
| 70 | + ); |
| 71 | + |
| 72 | + // We can use fragment directly but this may failed some selector usage. Keep as origin logic |
| 73 | + return ( |
| 74 | + <div> |
| 75 | + <Mask {...cloneProps} /> |
| 76 | + {popupNode} |
| 77 | + </div> |
| 78 | + ); |
| 79 | + }, |
| 80 | +); |
55 | 81 |
|
56 | 82 | Popup.displayName = 'Popup'; |
57 | 83 |
|
|
0 commit comments