Dashboard sipadu mbip
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Transition.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
  2. import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
  3. import PropTypes from 'prop-types';
  4. import React from 'react';
  5. import ReactDOM from 'react-dom';
  6. import config from './config';
  7. import { timeoutsShape } from './utils/PropTypes';
  8. import TransitionGroupContext from './TransitionGroupContext';
  9. export var UNMOUNTED = 'unmounted';
  10. export var EXITED = 'exited';
  11. export var ENTERING = 'entering';
  12. export var ENTERED = 'entered';
  13. export var EXITING = 'exiting';
  14. /**
  15. * The Transition component lets you describe a transition from one component
  16. * state to another _over time_ with a simple declarative API. Most commonly
  17. * it's used to animate the mounting and unmounting of a component, but can also
  18. * be used to describe in-place transition states as well.
  19. *
  20. * ---
  21. *
  22. * **Note**: `Transition` is a platform-agnostic base component. If you're using
  23. * transitions in CSS, you'll probably want to use
  24. * [`CSSTransition`](https://reactcommunity.org/react-transition-group/css-transition)
  25. * instead. It inherits all the features of `Transition`, but contains
  26. * additional features necessary to play nice with CSS transitions (hence the
  27. * name of the component).
  28. *
  29. * ---
  30. *
  31. * By default the `Transition` component does not alter the behavior of the
  32. * component it renders, it only tracks "enter" and "exit" states for the
  33. * components. It's up to you to give meaning and effect to those states. For
  34. * example we can add styles to a component when it enters or exits:
  35. *
  36. * ```jsx
  37. * import { Transition } from 'react-transition-group';
  38. *
  39. * const duration = 300;
  40. *
  41. * const defaultStyle = {
  42. * transition: `opacity ${duration}ms ease-in-out`,
  43. * opacity: 0,
  44. * }
  45. *
  46. * const transitionStyles = {
  47. * entering: { opacity: 1 },
  48. * entered: { opacity: 1 },
  49. * exiting: { opacity: 0 },
  50. * exited: { opacity: 0 },
  51. * };
  52. *
  53. * const Fade = ({ in: inProp }) => (
  54. * <Transition in={inProp} timeout={duration}>
  55. * {state => (
  56. * <div style={{
  57. * ...defaultStyle,
  58. * ...transitionStyles[state]
  59. * }}>
  60. * I'm a fade Transition!
  61. * </div>
  62. * )}
  63. * </Transition>
  64. * );
  65. * ```
  66. *
  67. * There are 4 main states a Transition can be in:
  68. * - `'entering'`
  69. * - `'entered'`
  70. * - `'exiting'`
  71. * - `'exited'`
  72. *
  73. * Transition state is toggled via the `in` prop. When `true` the component
  74. * begins the "Enter" stage. During this stage, the component will shift from
  75. * its current transition state, to `'entering'` for the duration of the
  76. * transition and then to the `'entered'` stage once it's complete. Let's take
  77. * the following example (we'll use the
  78. * [useState](https://reactjs.org/docs/hooks-reference.html#usestate) hook):
  79. *
  80. * ```jsx
  81. * function App() {
  82. * const [inProp, setInProp] = useState(false);
  83. * return (
  84. * <div>
  85. * <Transition in={inProp} timeout={500}>
  86. * {state => (
  87. * // ...
  88. * )}
  89. * </Transition>
  90. * <button onClick={() => setInProp(true)}>
  91. * Click to Enter
  92. * </button>
  93. * </div>
  94. * );
  95. * }
  96. * ```
  97. *
  98. * When the button is clicked the component will shift to the `'entering'` state
  99. * and stay there for 500ms (the value of `timeout`) before it finally switches
  100. * to `'entered'`.
  101. *
  102. * When `in` is `false` the same thing happens except the state moves from
  103. * `'exiting'` to `'exited'`.
  104. */
  105. var Transition =
  106. /*#__PURE__*/
  107. function (_React$Component) {
  108. _inheritsLoose(Transition, _React$Component);
  109. function Transition(props, context) {
  110. var _this;
  111. _this = _React$Component.call(this, props, context) || this;
  112. var parentGroup = context; // In the context of a TransitionGroup all enters are really appears
  113. var appear = parentGroup && !parentGroup.isMounting ? props.enter : props.appear;
  114. var initialStatus;
  115. _this.appearStatus = null;
  116. if (props.in) {
  117. if (appear) {
  118. initialStatus = EXITED;
  119. _this.appearStatus = ENTERING;
  120. } else {
  121. initialStatus = ENTERED;
  122. }
  123. } else {
  124. if (props.unmountOnExit || props.mountOnEnter) {
  125. initialStatus = UNMOUNTED;
  126. } else {
  127. initialStatus = EXITED;
  128. }
  129. }
  130. _this.state = {
  131. status: initialStatus
  132. };
  133. _this.nextCallback = null;
  134. return _this;
  135. }
  136. Transition.getDerivedStateFromProps = function getDerivedStateFromProps(_ref, prevState) {
  137. var nextIn = _ref.in;
  138. if (nextIn && prevState.status === UNMOUNTED) {
  139. return {
  140. status: EXITED
  141. };
  142. }
  143. return null;
  144. }; // getSnapshotBeforeUpdate(prevProps) {
  145. // let nextStatus = null
  146. // if (prevProps !== this.props) {
  147. // const { status } = this.state
  148. // if (this.props.in) {
  149. // if (status !== ENTERING && status !== ENTERED) {
  150. // nextStatus = ENTERING
  151. // }
  152. // } else {
  153. // if (status === ENTERING || status === ENTERED) {
  154. // nextStatus = EXITING
  155. // }
  156. // }
  157. // }
  158. // return { nextStatus }
  159. // }
  160. var _proto = Transition.prototype;
  161. _proto.componentDidMount = function componentDidMount() {
  162. this.updateStatus(true, this.appearStatus);
  163. };
  164. _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
  165. var nextStatus = null;
  166. if (prevProps !== this.props) {
  167. var status = this.state.status;
  168. if (this.props.in) {
  169. if (status !== ENTERING && status !== ENTERED) {
  170. nextStatus = ENTERING;
  171. }
  172. } else {
  173. if (status === ENTERING || status === ENTERED) {
  174. nextStatus = EXITING;
  175. }
  176. }
  177. }
  178. this.updateStatus(false, nextStatus);
  179. };
  180. _proto.componentWillUnmount = function componentWillUnmount() {
  181. this.cancelNextCallback();
  182. };
  183. _proto.getTimeouts = function getTimeouts() {
  184. var timeout = this.props.timeout;
  185. var exit, enter, appear;
  186. exit = enter = appear = timeout;
  187. if (timeout != null && typeof timeout !== 'number') {
  188. exit = timeout.exit;
  189. enter = timeout.enter; // TODO: remove fallback for next major
  190. appear = timeout.appear !== undefined ? timeout.appear : enter;
  191. }
  192. return {
  193. exit: exit,
  194. enter: enter,
  195. appear: appear
  196. };
  197. };
  198. _proto.updateStatus = function updateStatus(mounting, nextStatus) {
  199. if (mounting === void 0) {
  200. mounting = false;
  201. }
  202. if (nextStatus !== null) {
  203. // nextStatus will always be ENTERING or EXITING.
  204. this.cancelNextCallback();
  205. var node = ReactDOM.findDOMNode(this);
  206. if (nextStatus === ENTERING) {
  207. this.performEnter(node, mounting);
  208. } else {
  209. this.performExit(node);
  210. }
  211. } else if (this.props.unmountOnExit && this.state.status === EXITED) {
  212. this.setState({
  213. status: UNMOUNTED
  214. });
  215. }
  216. };
  217. _proto.performEnter = function performEnter(node, mounting) {
  218. var _this2 = this;
  219. var enter = this.props.enter;
  220. var appearing = this.context ? this.context.isMounting : mounting;
  221. var timeouts = this.getTimeouts();
  222. var enterTimeout = appearing ? timeouts.appear : timeouts.enter; // no enter animation skip right to ENTERED
  223. // if we are mounting and running this it means appear _must_ be set
  224. if (!mounting && !enter || config.disabled) {
  225. this.safeSetState({
  226. status: ENTERED
  227. }, function () {
  228. _this2.props.onEntered(node);
  229. });
  230. return;
  231. }
  232. this.props.onEnter(node, appearing);
  233. this.safeSetState({
  234. status: ENTERING
  235. }, function () {
  236. _this2.props.onEntering(node, appearing);
  237. _this2.onTransitionEnd(node, enterTimeout, function () {
  238. _this2.safeSetState({
  239. status: ENTERED
  240. }, function () {
  241. _this2.props.onEntered(node, appearing);
  242. });
  243. });
  244. });
  245. };
  246. _proto.performExit = function performExit(node) {
  247. var _this3 = this;
  248. var exit = this.props.exit;
  249. var timeouts = this.getTimeouts(); // no exit animation skip right to EXITED
  250. if (!exit || config.disabled) {
  251. this.safeSetState({
  252. status: EXITED
  253. }, function () {
  254. _this3.props.onExited(node);
  255. });
  256. return;
  257. }
  258. this.props.onExit(node);
  259. this.safeSetState({
  260. status: EXITING
  261. }, function () {
  262. _this3.props.onExiting(node);
  263. _this3.onTransitionEnd(node, timeouts.exit, function () {
  264. _this3.safeSetState({
  265. status: EXITED
  266. }, function () {
  267. _this3.props.onExited(node);
  268. });
  269. });
  270. });
  271. };
  272. _proto.cancelNextCallback = function cancelNextCallback() {
  273. if (this.nextCallback !== null) {
  274. this.nextCallback.cancel();
  275. this.nextCallback = null;
  276. }
  277. };
  278. _proto.safeSetState = function safeSetState(nextState, callback) {
  279. // This shouldn't be necessary, but there are weird race conditions with
  280. // setState callbacks and unmounting in testing, so always make sure that
  281. // we can cancel any pending setState callbacks after we unmount.
  282. callback = this.setNextCallback(callback);
  283. this.setState(nextState, callback);
  284. };
  285. _proto.setNextCallback = function setNextCallback(callback) {
  286. var _this4 = this;
  287. var active = true;
  288. this.nextCallback = function (event) {
  289. if (active) {
  290. active = false;
  291. _this4.nextCallback = null;
  292. callback(event);
  293. }
  294. };
  295. this.nextCallback.cancel = function () {
  296. active = false;
  297. };
  298. return this.nextCallback;
  299. };
  300. _proto.onTransitionEnd = function onTransitionEnd(node, timeout, handler) {
  301. this.setNextCallback(handler);
  302. var doesNotHaveTimeoutOrListener = timeout == null && !this.props.addEndListener;
  303. if (!node || doesNotHaveTimeoutOrListener) {
  304. setTimeout(this.nextCallback, 0);
  305. return;
  306. }
  307. if (this.props.addEndListener) {
  308. this.props.addEndListener(node, this.nextCallback);
  309. }
  310. if (timeout != null) {
  311. setTimeout(this.nextCallback, timeout);
  312. }
  313. };
  314. _proto.render = function render() {
  315. var status = this.state.status;
  316. if (status === UNMOUNTED) {
  317. return null;
  318. }
  319. var _this$props = this.props,
  320. children = _this$props.children,
  321. childProps = _objectWithoutPropertiesLoose(_this$props, ["children"]); // filter props for Transtition
  322. delete childProps.in;
  323. delete childProps.mountOnEnter;
  324. delete childProps.unmountOnExit;
  325. delete childProps.appear;
  326. delete childProps.enter;
  327. delete childProps.exit;
  328. delete childProps.timeout;
  329. delete childProps.addEndListener;
  330. delete childProps.onEnter;
  331. delete childProps.onEntering;
  332. delete childProps.onEntered;
  333. delete childProps.onExit;
  334. delete childProps.onExiting;
  335. delete childProps.onExited;
  336. if (typeof children === 'function') {
  337. // allows for nested Transitions
  338. return React.createElement(TransitionGroupContext.Provider, {
  339. value: null
  340. }, children(status, childProps));
  341. }
  342. var child = React.Children.only(children);
  343. return (// allows for nested Transitions
  344. React.createElement(TransitionGroupContext.Provider, {
  345. value: null
  346. }, React.cloneElement(child, childProps))
  347. );
  348. };
  349. return Transition;
  350. }(React.Component);
  351. Transition.contextType = TransitionGroupContext;
  352. Transition.propTypes = process.env.NODE_ENV !== "production" ? {
  353. /**
  354. * A `function` child can be used instead of a React element. This function is
  355. * called with the current transition status (`'entering'`, `'entered'`,
  356. * `'exiting'`, `'exited'`), which can be used to apply context
  357. * specific props to a component.
  358. *
  359. * ```jsx
  360. * <Transition in={this.state.in} timeout={150}>
  361. * {state => (
  362. * <MyComponent className={`fade fade-${state}`} />
  363. * )}
  364. * </Transition>
  365. * ```
  366. */
  367. children: PropTypes.oneOfType([PropTypes.func.isRequired, PropTypes.element.isRequired]).isRequired,
  368. /**
  369. * Show the component; triggers the enter or exit states
  370. */
  371. in: PropTypes.bool,
  372. /**
  373. * By default the child component is mounted immediately along with
  374. * the parent `Transition` component. If you want to "lazy mount" the component on the
  375. * first `in={true}` you can set `mountOnEnter`. After the first enter transition the component will stay
  376. * mounted, even on "exited", unless you also specify `unmountOnExit`.
  377. */
  378. mountOnEnter: PropTypes.bool,
  379. /**
  380. * By default the child component stays mounted after it reaches the `'exited'` state.
  381. * Set `unmountOnExit` if you'd prefer to unmount the component after it finishes exiting.
  382. */
  383. unmountOnExit: PropTypes.bool,
  384. /**
  385. * Normally a component is not transitioned if it is shown when the
  386. * `<Transition>` component mounts. If you want to transition on the first
  387. * mount set `appear` to `true`, and the component will transition in as soon
  388. * as the `<Transition>` mounts.
  389. *
  390. * > **Note**: there are no special appear states like `appearing`/`appeared`, this prop
  391. * > only adds an additional enter transition. However, in the
  392. * > `<CSSTransition>` component that first enter transition does result in
  393. * > additional `.appear-*` classes, that way you can choose to style it
  394. * > differently.
  395. */
  396. appear: PropTypes.bool,
  397. /**
  398. * Enable or disable enter transitions.
  399. */
  400. enter: PropTypes.bool,
  401. /**
  402. * Enable or disable exit transitions.
  403. */
  404. exit: PropTypes.bool,
  405. /**
  406. * The duration of the transition, in milliseconds.
  407. * Required unless `addEndListener` is provided.
  408. *
  409. * You may specify a single timeout for all transitions:
  410. *
  411. * ```jsx
  412. * timeout={500}
  413. * ```
  414. *
  415. * or individually:
  416. *
  417. * ```jsx
  418. * timeout={{
  419. * appear: 500,
  420. * enter: 300,
  421. * exit: 500,
  422. * }}
  423. * ```
  424. *
  425. * - `appear` defaults to the value of `enter`
  426. * - `enter` defaults to `0`
  427. * - `exit` defaults to `0`
  428. *
  429. * @type {number | { enter?: number, exit?: number, appear?: number }}
  430. */
  431. timeout: function timeout(props) {
  432. var pt = timeoutsShape;
  433. if (!props.addEndListener) pt = pt.isRequired;
  434. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  435. args[_key - 1] = arguments[_key];
  436. }
  437. return pt.apply(void 0, [props].concat(args));
  438. },
  439. /**
  440. * Add a custom transition end trigger. Called with the transitioning
  441. * DOM node and a `done` callback. Allows for more fine grained transition end
  442. * logic. **Note:** Timeouts are still used as a fallback if provided.
  443. *
  444. * ```jsx
  445. * addEndListener={(node, done) => {
  446. * // use the css transitionend event to mark the finish of a transition
  447. * node.addEventListener('transitionend', done, false);
  448. * }}
  449. * ```
  450. */
  451. addEndListener: PropTypes.func,
  452. /**
  453. * Callback fired before the "entering" status is applied. An extra parameter
  454. * `isAppearing` is supplied to indicate if the enter stage is occurring on the initial mount
  455. *
  456. * @type Function(node: HtmlElement, isAppearing: bool) -> void
  457. */
  458. onEnter: PropTypes.func,
  459. /**
  460. * Callback fired after the "entering" status is applied. An extra parameter
  461. * `isAppearing` is supplied to indicate if the enter stage is occurring on the initial mount
  462. *
  463. * @type Function(node: HtmlElement, isAppearing: bool)
  464. */
  465. onEntering: PropTypes.func,
  466. /**
  467. * Callback fired after the "entered" status is applied. An extra parameter
  468. * `isAppearing` is supplied to indicate if the enter stage is occurring on the initial mount
  469. *
  470. * @type Function(node: HtmlElement, isAppearing: bool) -> void
  471. */
  472. onEntered: PropTypes.func,
  473. /**
  474. * Callback fired before the "exiting" status is applied.
  475. *
  476. * @type Function(node: HtmlElement) -> void
  477. */
  478. onExit: PropTypes.func,
  479. /**
  480. * Callback fired after the "exiting" status is applied.
  481. *
  482. * @type Function(node: HtmlElement) -> void
  483. */
  484. onExiting: PropTypes.func,
  485. /**
  486. * Callback fired after the "exited" status is applied.
  487. *
  488. * @type Function(node: HtmlElement) -> void
  489. */
  490. onExited: PropTypes.func // Name the function so it is clearer in the documentation
  491. } : {};
  492. function noop() {}
  493. Transition.defaultProps = {
  494. in: false,
  495. mountOnEnter: false,
  496. unmountOnExit: false,
  497. appear: false,
  498. enter: true,
  499. exit: true,
  500. onEnter: noop,
  501. onEntering: noop,
  502. onEntered: noop,
  503. onExit: noop,
  504. onExiting: noop,
  505. onExited: noop
  506. };
  507. Transition.UNMOUNTED = 0;
  508. Transition.EXITED = 1;
  509. Transition.ENTERING = 2;
  510. Transition.ENTERED = 3;
  511. Transition.EXITING = 4;
  512. export default Transition;