CSSMotion.js
4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _typeof from "@babel/runtime/helpers/esm/typeof";
/* eslint-disable react/default-props-match-prop-types, react/no-multi-comp, react/prop-types */
import * as React from 'react';
import { useRef } from 'react';
import findDOMNode from "rc-util/es/Dom/findDOMNode";
import { fillRef } from "rc-util/es/ref";
import classNames from 'classnames';
import { getTransitionName, supportTransition } from './util/motion';
import { STATUS_NONE, STEP_PREPARE, STEP_START } from './interface';
import useStatus from './hooks/useStatus';
import DomWrapper from './DomWrapper';
import { isActive } from './hooks/useStepQueue';
/**
* `transitionSupport` is used for none transition test case.
* Default we use browser transition event support check.
*/
export function genCSSMotion(config) {
var transitionSupport = config;
if (_typeof(config) === 'object') {
transitionSupport = config.transitionSupport;
}
function isSupportTransition(props) {
return !!(props.motionName && transitionSupport);
}
var CSSMotion = /*#__PURE__*/React.forwardRef(function (props, ref) {
var _props$visible = props.visible,
visible = _props$visible === void 0 ? true : _props$visible,
_props$removeOnLeave = props.removeOnLeave,
removeOnLeave = _props$removeOnLeave === void 0 ? true : _props$removeOnLeave,
forceRender = props.forceRender,
children = props.children,
motionName = props.motionName,
leavedClassName = props.leavedClassName,
eventProps = props.eventProps;
var supportMotion = isSupportTransition(props); // Ref to the react node, it may be a HTMLElement
var nodeRef = useRef(); // Ref to the dom wrapper in case ref can not pass to HTMLElement
var wrapperNodeRef = useRef();
function getDomElement() {
try {
return findDOMNode(nodeRef.current || wrapperNodeRef.current);
} catch (e) {
// Only happen when `motionDeadline` trigger but element removed.
return null;
}
}
var _useStatus = useStatus(supportMotion, visible, getDomElement, props),
_useStatus2 = _slicedToArray(_useStatus, 4),
status = _useStatus2[0],
statusStep = _useStatus2[1],
statusStyle = _useStatus2[2],
mergedVisible = _useStatus2[3]; // ====================== Refs ======================
var originRef = useRef(ref);
originRef.current = ref;
var setNodeRef = React.useCallback(function (node) {
nodeRef.current = node;
fillRef(originRef.current, node);
}, []); // ===================== Render =====================
var motionChildren;
var mergedProps = _objectSpread(_objectSpread({}, eventProps), {}, {
visible: visible
});
if (!children) {
// No children
motionChildren = null;
} else if (status === STATUS_NONE || !isSupportTransition(props)) {
// Stable children
if (mergedVisible) {
motionChildren = children(_objectSpread({}, mergedProps), setNodeRef);
} else if (!removeOnLeave) {
motionChildren = children(_objectSpread(_objectSpread({}, mergedProps), {}, {
className: leavedClassName
}), setNodeRef);
} else if (forceRender) {
motionChildren = children(_objectSpread(_objectSpread({}, mergedProps), {}, {
style: {
display: 'none'
}
}), setNodeRef);
} else {
motionChildren = null;
}
} else {
var _classNames;
// In motion
var statusSuffix;
if (statusStep === STEP_PREPARE) {
statusSuffix = 'prepare';
} else if (isActive(statusStep)) {
statusSuffix = 'active';
} else if (statusStep === STEP_START) {
statusSuffix = 'start';
}
motionChildren = children(_objectSpread(_objectSpread({}, mergedProps), {}, {
className: classNames(getTransitionName(motionName, status), (_classNames = {}, _defineProperty(_classNames, getTransitionName(motionName, "".concat(status, "-").concat(statusSuffix)), statusSuffix), _defineProperty(_classNames, motionName, typeof motionName === 'string'), _classNames)),
style: statusStyle
}), setNodeRef);
}
return /*#__PURE__*/React.createElement(DomWrapper, {
ref: wrapperNodeRef
}, motionChildren);
});
CSSMotion.displayName = 'CSSMotion';
return CSSMotion;
}
export default genCSSMotion(supportTransition);