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.

SwitchTransition.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
  2. var _leaveRenders, _enterRenders;
  3. import React from 'react';
  4. import PropTypes from 'prop-types';
  5. import { ENTERED, ENTERING, EXITING } from './Transition';
  6. import TransitionGroupContext from './TransitionGroupContext';
  7. function areChildrenDifferent(oldChildren, newChildren) {
  8. if (oldChildren === newChildren) return false;
  9. if (React.isValidElement(oldChildren) && React.isValidElement(newChildren) && oldChildren.key != null && oldChildren.key === newChildren.key) {
  10. return false;
  11. }
  12. return true;
  13. }
  14. /**
  15. * Enum of modes for SwitchTransition component
  16. * @enum { string }
  17. */
  18. export var modes = {
  19. out: 'out-in',
  20. in: 'in-out'
  21. };
  22. var callHook = function callHook(element, name, cb) {
  23. return function () {
  24. var _element$props;
  25. element.props[name] && (_element$props = element.props)[name].apply(_element$props, arguments);
  26. cb();
  27. };
  28. };
  29. var leaveRenders = (_leaveRenders = {}, _leaveRenders[modes.out] = function (_ref) {
  30. var current = _ref.current,
  31. changeState = _ref.changeState;
  32. return React.cloneElement(current, {
  33. in: false,
  34. onExited: callHook(current, 'onExited', function () {
  35. changeState(ENTERING, null);
  36. })
  37. });
  38. }, _leaveRenders[modes.in] = function (_ref2) {
  39. var current = _ref2.current,
  40. changeState = _ref2.changeState,
  41. children = _ref2.children;
  42. return [current, React.cloneElement(children, {
  43. in: true,
  44. onEntered: callHook(children, 'onEntered', function () {
  45. changeState(ENTERING);
  46. })
  47. })];
  48. }, _leaveRenders);
  49. var enterRenders = (_enterRenders = {}, _enterRenders[modes.out] = function (_ref3) {
  50. var children = _ref3.children,
  51. changeState = _ref3.changeState;
  52. return React.cloneElement(children, {
  53. in: true,
  54. onEntered: callHook(children, 'onEntered', function () {
  55. changeState(ENTERED, React.cloneElement(children, {
  56. in: true
  57. }));
  58. })
  59. });
  60. }, _enterRenders[modes.in] = function (_ref4) {
  61. var current = _ref4.current,
  62. children = _ref4.children,
  63. changeState = _ref4.changeState;
  64. return [React.cloneElement(current, {
  65. in: false,
  66. onExited: callHook(current, 'onExited', function () {
  67. changeState(ENTERED, React.cloneElement(children, {
  68. in: true
  69. }));
  70. })
  71. }), React.cloneElement(children, {
  72. in: true
  73. })];
  74. }, _enterRenders);
  75. /**
  76. * A transition component inspired by the [vue transition modes](https://vuejs.org/v2/guide/transitions.html#Transition-Modes).
  77. * You can use it when you want to control the render between state transitions.
  78. * Based on the selected mode and the child's key which is the `Transition` or `CSSTransition` component, the `SwitchTransition` makes a consistent transition between them.
  79. *
  80. * If the `out-in` mode is selected, the `SwitchTransition` waits until the old child leaves and then inserts a new child.
  81. * If the `in-out` mode is selected, the `SwitchTransition` inserts a new child first, waits for the new child to enter and then removes the old child
  82. *
  83. * ```jsx
  84. *
  85. * function App() {
  86. * const [state, setState] = useState(false);
  87. * return (
  88. * <SwitchTransition>
  89. * <FadeTransition key={state ? "Goodbye, world!" : "Hello, world!"}
  90. * addEndListener={(node, done) => node.addEventListener("transitionend", done, false)}
  91. * classNames='fade' >
  92. * <button onClick={() => setState(state => !state)}>
  93. * {state ? "Goodbye, world!" : "Hello, world!"}
  94. * </button>
  95. * </FadeTransition>
  96. * </SwitchTransition>
  97. * )
  98. * }
  99. * ```
  100. */
  101. var SwitchTransition =
  102. /*#__PURE__*/
  103. function (_React$Component) {
  104. _inheritsLoose(SwitchTransition, _React$Component);
  105. function SwitchTransition() {
  106. var _this;
  107. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  108. args[_key] = arguments[_key];
  109. }
  110. _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
  111. _this.state = {
  112. status: ENTERED,
  113. current: null
  114. };
  115. _this.appeared = false;
  116. _this.changeState = function (status, current) {
  117. if (current === void 0) {
  118. current = _this.state.current;
  119. }
  120. _this.setState({
  121. status: status,
  122. current: current
  123. });
  124. };
  125. return _this;
  126. }
  127. var _proto = SwitchTransition.prototype;
  128. _proto.componentDidMount = function componentDidMount() {
  129. this.appeared = true;
  130. };
  131. SwitchTransition.getDerivedStateFromProps = function getDerivedStateFromProps(props, state) {
  132. if (props.children == null) {
  133. return {
  134. current: null
  135. };
  136. }
  137. if (state.status === ENTERING && props.mode === modes.in) {
  138. return {
  139. status: ENTERING
  140. };
  141. }
  142. if (state.current && areChildrenDifferent(state.current, props.children)) {
  143. return {
  144. status: EXITING
  145. };
  146. }
  147. return {
  148. current: React.cloneElement(props.children, {
  149. in: true
  150. })
  151. };
  152. };
  153. _proto.render = function render() {
  154. var _this$props = this.props,
  155. children = _this$props.children,
  156. mode = _this$props.mode,
  157. _this$state = this.state,
  158. status = _this$state.status,
  159. current = _this$state.current;
  160. var data = {
  161. children: children,
  162. current: current,
  163. changeState: this.changeState,
  164. status: status
  165. };
  166. var component;
  167. switch (status) {
  168. case ENTERING:
  169. component = enterRenders[mode](data);
  170. break;
  171. case EXITING:
  172. component = leaveRenders[mode](data);
  173. break;
  174. case ENTERED:
  175. component = current;
  176. }
  177. return React.createElement(TransitionGroupContext.Provider, {
  178. value: {
  179. isMounting: !this.appeared
  180. }
  181. }, component);
  182. };
  183. return SwitchTransition;
  184. }(React.Component);
  185. SwitchTransition.propTypes = process.env.NODE_ENV !== "production" ? {
  186. /**
  187. * Transition modes.
  188. * `out-in`: Current element transitions out first, then when complete, the new element transitions in.
  189. * `in-out: New element transitions in first, then when complete, the current element transitions out.`
  190. *
  191. * @type {'out-in'|'in-out'}
  192. */
  193. mode: PropTypes.oneOf([modes.in, modes.out]),
  194. /**
  195. * Any `Transition` or `CSSTransition` component
  196. */
  197. children: PropTypes.oneOfType([PropTypes.element.isRequired])
  198. } : {};
  199. SwitchTransition.defaultProps = {
  200. mode: modes.out
  201. };
  202. export default SwitchTransition;