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 7.0KB

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