import { createElement, CSSProperties, forwardRef, HTMLAttributes, RaxElement, RefForwardingComponent, useState, } from 'rax'; import cloneElement from 'rax-clone-element'; import isValidElement from 'rax-is-valid-element'; import { StyledComponentProps, useStyles } from '@rax-ui/styles'; import Transition, { TransitionStyles, TransitionType } from '@rax-ui/transition'; import View from 'rax-view'; import { isMiniApp, isWeb, isWeex } from 'universal-env'; import StylesProvider from './style'; import Backdrop from './backdrop'; import Portal from './portal'; export interface ModalProps extends HTMLAttributes, StyledComponentProps { view?: boolean; visible?: boolean; container?: HTMLElement | RaxElement | (() => HTMLElement | RaxElement); containerStyle?: CSSProperties; disablePortal?: boolean; keepMounted?: boolean; hideBackdrop?: boolean; transition?: { transition: { enter: TransitionType; exit: TransitionType; }; styles: TransitionStyles; }; disableBackdropClose?: boolean; onBackdropClick?: (e: any) => void; onEnter?: () => void; onExit?: () => void; onEntering?: () => void; onExiting?: () => void; onEntered?: () => void; onExited?: () => void; onClose?: (reason: string, e: any) => void; } function getHasTransition(props) { // todo: close transition in miniapp runtime temporarily if (isMiniApp && !isWeb) { return false; } return ( props.transition || (props.children ? Object.prototype.hasOwnProperty.call(props.children.props, 'in') : false) ); } export function createChainedFunction(...funcs) { return funcs.reduce( (acc, func) => { if (func == null) { return acc; } return function chainedFunction(...args) { acc.apply(this, args); func.apply(this, args); }; }, () => {} ); } const getExitedByProps = (props) => { if (props.visible) { return false; } if (!getHasTransition(props)) { return true; } return null; }; const Modal: RefForwardingComponent = (props, ref) => { const { view = false, visible = false, container = true, disablePortal = false, keepMounted = false, hideBackdrop = false, disableBackdropClose = false, onBackdropClick, onClose = () => {}, containerStyle = {}, transition, style = {}, className = '', styles: stylesProp, onEnter, onExit, onEntering, onExiting, onEntered, onExited, children, ...others } = props; const [exited, setExited] = useState(!visible); const hasTransition = getHasTransition(props); const exitedByProps = getExitedByProps(props); if (!keepMounted && !visible && (!hasTransition || exited)) { return null; } if (exited !== exitedByProps && exitedByProps !== null) { setExited(exitedByProps); } const handleExited = () => { setExited(true); if (typeof onExited === 'function') { onExited(); } }; const handleBackdropClick = (e) => { if (onBackdropClick) { onBackdropClick(e); } if (!disableBackdropClose && onClose) { onClose('backdropClick', e); } }; let transitionEvents = {}; if (hasTransition) { transitionEvents = { onEnter: transition ? onEnter : createChainedFunction(onEnter, (children as any).props.onEnter), onExit: transition ? onExit : createChainedFunction(onExit, (children as any).props.onExit), onEntering: transition ? onEntering : createChainedFunction(onEntering, (children as any).props.onEntering), onExiting: transition ? onExiting : createChainedFunction(onExiting, (children as any).props.onExiting), onEntered: transition ? onEntered : createChainedFunction(onEntered, (children as any).props.onEntered), onExited: createChainedFunction(handleExited, transition ? null : (children as any).props.onExited), }; } const styles = useStyles(StylesProvider, { styles: stylesProp }, (classNames) => { return { modal: classNames([ 'modal', { 'modal--view': view, 'modal--hidden': exited, }, ]), }; }); let content; if (transition) { content = ( {children} ); } else { content = isValidElement(children) ? cloneElement(children, { ...transitionEvents }) : children; } const element = ( {hideBackdrop ? null : } {content} ); if (disablePortal || isWeex || view) { return element; } return {element}; }; export default forwardRef(Modal);