PullRefresh.mjs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { ref, watch, reactive, nextTick, defineComponent, createVNode as _createVNode } from "vue";
  2. import { numericProp, getScrollTop, preventDefault, createNamespace, makeNumericProp } from "../utils/index.mjs";
  3. import { useEventListener, useScrollParent } from "@vant/use";
  4. import { useTouch } from "../composables/use-touch.mjs";
  5. import { Loading } from "../loading/index.mjs";
  6. const [name, bem, t] = createNamespace("pull-refresh");
  7. const DEFAULT_HEAD_HEIGHT = 50;
  8. const TEXT_STATUS = ["pulling", "loosing", "success"];
  9. const pullRefreshProps = {
  10. disabled: Boolean,
  11. modelValue: Boolean,
  12. headHeight: makeNumericProp(DEFAULT_HEAD_HEIGHT),
  13. successText: String,
  14. pullingText: String,
  15. loosingText: String,
  16. loadingText: String,
  17. pullDistance: numericProp,
  18. successDuration: makeNumericProp(500),
  19. animationDuration: makeNumericProp(300)
  20. };
  21. var stdin_default = defineComponent({
  22. name,
  23. props: pullRefreshProps,
  24. emits: ["change", "refresh", "update:modelValue"],
  25. setup(props, {
  26. emit,
  27. slots
  28. }) {
  29. let reachTop;
  30. const root = ref();
  31. const track = ref();
  32. const scrollParent = useScrollParent(root);
  33. const state = reactive({
  34. status: "normal",
  35. distance: 0,
  36. duration: 0
  37. });
  38. const touch = useTouch();
  39. const getHeadStyle = () => {
  40. if (props.headHeight !== DEFAULT_HEAD_HEIGHT) {
  41. return {
  42. height: `${props.headHeight}px`
  43. };
  44. }
  45. };
  46. const isTouchable = () => state.status !== "loading" && state.status !== "success" && !props.disabled;
  47. const ease = (distance) => {
  48. const pullDistance = +(props.pullDistance || props.headHeight);
  49. if (distance > pullDistance) {
  50. if (distance < pullDistance * 2) {
  51. distance = pullDistance + (distance - pullDistance) / 2;
  52. } else {
  53. distance = pullDistance * 1.5 + (distance - pullDistance * 2) / 4;
  54. }
  55. }
  56. return Math.round(distance);
  57. };
  58. const setStatus = (distance, isLoading) => {
  59. const pullDistance = +(props.pullDistance || props.headHeight);
  60. state.distance = distance;
  61. if (isLoading) {
  62. state.status = "loading";
  63. } else if (distance === 0) {
  64. state.status = "normal";
  65. } else if (distance < pullDistance) {
  66. state.status = "pulling";
  67. } else {
  68. state.status = "loosing";
  69. }
  70. emit("change", {
  71. status: state.status,
  72. distance
  73. });
  74. };
  75. const getStatusText = () => {
  76. const {
  77. status
  78. } = state;
  79. if (status === "normal") {
  80. return "";
  81. }
  82. return props[`${status}Text`] || t(status);
  83. };
  84. const renderStatus = () => {
  85. const {
  86. status,
  87. distance
  88. } = state;
  89. if (slots[status]) {
  90. return slots[status]({
  91. distance
  92. });
  93. }
  94. const nodes = [];
  95. if (TEXT_STATUS.includes(status)) {
  96. nodes.push(_createVNode("div", {
  97. "class": bem("text")
  98. }, [getStatusText()]));
  99. }
  100. if (status === "loading") {
  101. nodes.push(_createVNode(Loading, {
  102. "class": bem("loading")
  103. }, {
  104. default: getStatusText
  105. }));
  106. }
  107. return nodes;
  108. };
  109. const showSuccessTip = () => {
  110. state.status = "success";
  111. setTimeout(() => {
  112. setStatus(0);
  113. }, +props.successDuration);
  114. };
  115. const checkPosition = (event) => {
  116. reachTop = getScrollTop(scrollParent.value) === 0;
  117. if (reachTop) {
  118. state.duration = 0;
  119. touch.start(event);
  120. }
  121. };
  122. const onTouchStart = (event) => {
  123. if (isTouchable()) {
  124. checkPosition(event);
  125. }
  126. };
  127. const onTouchMove = (event) => {
  128. if (isTouchable()) {
  129. if (!reachTop) {
  130. checkPosition(event);
  131. }
  132. const {
  133. deltaY
  134. } = touch;
  135. touch.move(event);
  136. if (reachTop && deltaY.value >= 0 && touch.isVertical()) {
  137. preventDefault(event);
  138. setStatus(ease(deltaY.value));
  139. }
  140. }
  141. };
  142. const onTouchEnd = () => {
  143. if (reachTop && touch.deltaY.value && isTouchable()) {
  144. state.duration = +props.animationDuration;
  145. if (state.status === "loosing") {
  146. setStatus(+props.headHeight, true);
  147. emit("update:modelValue", true);
  148. nextTick(() => emit("refresh"));
  149. } else {
  150. setStatus(0);
  151. }
  152. }
  153. };
  154. watch(() => props.modelValue, (value) => {
  155. state.duration = +props.animationDuration;
  156. if (value) {
  157. setStatus(+props.headHeight, true);
  158. } else if (slots.success || props.successText) {
  159. showSuccessTip();
  160. } else {
  161. setStatus(0, false);
  162. }
  163. });
  164. useEventListener("touchmove", onTouchMove, {
  165. target: track
  166. });
  167. return () => {
  168. var _a;
  169. const trackStyle = {
  170. transitionDuration: `${state.duration}ms`,
  171. transform: state.distance ? `translate3d(0,${state.distance}px, 0)` : ""
  172. };
  173. return _createVNode("div", {
  174. "ref": root,
  175. "class": bem()
  176. }, [_createVNode("div", {
  177. "ref": track,
  178. "class": bem("track"),
  179. "style": trackStyle,
  180. "onTouchstartPassive": onTouchStart,
  181. "onTouchend": onTouchEnd,
  182. "onTouchcancel": onTouchEnd
  183. }, [_createVNode("div", {
  184. "class": bem("head"),
  185. "style": getHeadStyle()
  186. }, [renderStatus()]), (_a = slots.default) == null ? void 0 : _a.call(slots)])]);
  187. };
  188. }
  189. });
  190. export {
  191. stdin_default as default,
  192. pullRefreshProps
  193. };