Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Transition.js 17KB

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