MotionThumb.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _typeof = require("@babel/runtime/helpers/typeof");
  4. Object.defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = MotionThumb;
  8. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  9. var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
  10. var _classnames = _interopRequireDefault(require("classnames"));
  11. var _rcMotion = _interopRequireDefault(require("rc-motion"));
  12. var _useLayoutEffect = _interopRequireDefault(require("rc-util/lib/hooks/useLayoutEffect"));
  13. var _ref2 = require("rc-util/lib/ref");
  14. var React = _interopRequireWildcard(require("react"));
  15. function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
  16. function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
  17. var calcThumbStyle = function calcThumbStyle(targetElement, vertical) {
  18. if (!targetElement) return null;
  19. var style = {
  20. left: targetElement.offsetLeft,
  21. right: targetElement.parentElement.clientWidth - targetElement.clientWidth - targetElement.offsetLeft,
  22. width: targetElement.clientWidth,
  23. top: targetElement.offsetTop,
  24. bottom: targetElement.parentElement.clientHeight - targetElement.clientHeight - targetElement.offsetTop,
  25. height: targetElement.clientHeight
  26. };
  27. if (vertical) {
  28. // Adjusts positioning and size for vertical layout by setting horizontal properties to 0 and using vertical properties from the style object.
  29. return {
  30. left: 0,
  31. right: 0,
  32. width: 0,
  33. top: style.top,
  34. bottom: style.bottom,
  35. height: style.height
  36. };
  37. }
  38. return {
  39. left: style.left,
  40. right: style.right,
  41. width: style.width,
  42. top: 0,
  43. bottom: 0,
  44. height: 0
  45. };
  46. };
  47. var toPX = function toPX(value) {
  48. return value !== undefined ? "".concat(value, "px") : undefined;
  49. };
  50. function MotionThumb(props) {
  51. var prefixCls = props.prefixCls,
  52. containerRef = props.containerRef,
  53. value = props.value,
  54. getValueIndex = props.getValueIndex,
  55. motionName = props.motionName,
  56. onMotionStart = props.onMotionStart,
  57. onMotionEnd = props.onMotionEnd,
  58. direction = props.direction,
  59. _props$vertical = props.vertical,
  60. vertical = _props$vertical === void 0 ? false : _props$vertical;
  61. var thumbRef = React.useRef(null);
  62. var _React$useState = React.useState(value),
  63. _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
  64. prevValue = _React$useState2[0],
  65. setPrevValue = _React$useState2[1];
  66. // =========================== Effect ===========================
  67. var findValueElement = function findValueElement(val) {
  68. var _containerRef$current;
  69. var index = getValueIndex(val);
  70. var ele = (_containerRef$current = containerRef.current) === null || _containerRef$current === void 0 ? void 0 : _containerRef$current.querySelectorAll(".".concat(prefixCls, "-item"))[index];
  71. return (ele === null || ele === void 0 ? void 0 : ele.offsetParent) && ele;
  72. };
  73. var _React$useState3 = React.useState(null),
  74. _React$useState4 = (0, _slicedToArray2.default)(_React$useState3, 2),
  75. prevStyle = _React$useState4[0],
  76. setPrevStyle = _React$useState4[1];
  77. var _React$useState5 = React.useState(null),
  78. _React$useState6 = (0, _slicedToArray2.default)(_React$useState5, 2),
  79. nextStyle = _React$useState6[0],
  80. setNextStyle = _React$useState6[1];
  81. (0, _useLayoutEffect.default)(function () {
  82. if (prevValue !== value) {
  83. var prev = findValueElement(prevValue);
  84. var next = findValueElement(value);
  85. var calcPrevStyle = calcThumbStyle(prev, vertical);
  86. var calcNextStyle = calcThumbStyle(next, vertical);
  87. setPrevValue(value);
  88. setPrevStyle(calcPrevStyle);
  89. setNextStyle(calcNextStyle);
  90. if (prev && next) {
  91. onMotionStart();
  92. } else {
  93. onMotionEnd();
  94. }
  95. }
  96. }, [value]);
  97. var thumbStart = React.useMemo(function () {
  98. if (vertical) {
  99. var _prevStyle$top;
  100. return toPX((_prevStyle$top = prevStyle === null || prevStyle === void 0 ? void 0 : prevStyle.top) !== null && _prevStyle$top !== void 0 ? _prevStyle$top : 0);
  101. }
  102. if (direction === 'rtl') {
  103. return toPX(-(prevStyle === null || prevStyle === void 0 ? void 0 : prevStyle.right));
  104. }
  105. return toPX(prevStyle === null || prevStyle === void 0 ? void 0 : prevStyle.left);
  106. }, [vertical, direction, prevStyle]);
  107. var thumbActive = React.useMemo(function () {
  108. if (vertical) {
  109. var _nextStyle$top;
  110. return toPX((_nextStyle$top = nextStyle === null || nextStyle === void 0 ? void 0 : nextStyle.top) !== null && _nextStyle$top !== void 0 ? _nextStyle$top : 0);
  111. }
  112. if (direction === 'rtl') {
  113. return toPX(-(nextStyle === null || nextStyle === void 0 ? void 0 : nextStyle.right));
  114. }
  115. return toPX(nextStyle === null || nextStyle === void 0 ? void 0 : nextStyle.left);
  116. }, [vertical, direction, nextStyle]);
  117. // =========================== Motion ===========================
  118. var onAppearStart = function onAppearStart() {
  119. if (vertical) {
  120. return {
  121. transform: 'translateY(var(--thumb-start-top))',
  122. height: 'var(--thumb-start-height)'
  123. };
  124. }
  125. return {
  126. transform: 'translateX(var(--thumb-start-left))',
  127. width: 'var(--thumb-start-width)'
  128. };
  129. };
  130. var onAppearActive = function onAppearActive() {
  131. if (vertical) {
  132. return {
  133. transform: 'translateY(var(--thumb-active-top))',
  134. height: 'var(--thumb-active-height)'
  135. };
  136. }
  137. return {
  138. transform: 'translateX(var(--thumb-active-left))',
  139. width: 'var(--thumb-active-width)'
  140. };
  141. };
  142. var onVisibleChanged = function onVisibleChanged() {
  143. setPrevStyle(null);
  144. setNextStyle(null);
  145. onMotionEnd();
  146. };
  147. // =========================== Render ===========================
  148. // No need motion when nothing exist in queue
  149. if (!prevStyle || !nextStyle) {
  150. return null;
  151. }
  152. return /*#__PURE__*/React.createElement(_rcMotion.default, {
  153. visible: true,
  154. motionName: motionName,
  155. motionAppear: true,
  156. onAppearStart: onAppearStart,
  157. onAppearActive: onAppearActive,
  158. onVisibleChanged: onVisibleChanged
  159. }, function (_ref, ref) {
  160. var motionClassName = _ref.className,
  161. motionStyle = _ref.style;
  162. var mergedStyle = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, motionStyle), {}, {
  163. '--thumb-start-left': thumbStart,
  164. '--thumb-start-width': toPX(prevStyle === null || prevStyle === void 0 ? void 0 : prevStyle.width),
  165. '--thumb-active-left': thumbActive,
  166. '--thumb-active-width': toPX(nextStyle === null || nextStyle === void 0 ? void 0 : nextStyle.width),
  167. '--thumb-start-top': thumbStart,
  168. '--thumb-start-height': toPX(prevStyle === null || prevStyle === void 0 ? void 0 : prevStyle.height),
  169. '--thumb-active-top': thumbActive,
  170. '--thumb-active-height': toPX(nextStyle === null || nextStyle === void 0 ? void 0 : nextStyle.height)
  171. });
  172. // It's little ugly which should be refactor when @umi/test update to latest jsdom
  173. var motionProps = {
  174. ref: (0, _ref2.composeRef)(thumbRef, ref),
  175. style: mergedStyle,
  176. className: (0, _classnames.default)("".concat(prefixCls, "-thumb"), motionClassName)
  177. };
  178. if (process.env.NODE_ENV === 'test') {
  179. motionProps['data-test-style'] = JSON.stringify(mergedStyle);
  180. }
  181. return /*#__PURE__*/React.createElement("div", motionProps);
  182. });
  183. }