FloatingPanel.mjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { ref, watch, computed, defineComponent, createVNode as _createVNode } from "vue";
  2. import { addUnit, closest, createNamespace, makeArrayProp, makeNumericProp, preventDefault, truthProp, windowHeight } from "../utils/index.mjs";
  3. import { useEventListener } from "@vant/use";
  4. import { useLockScroll } from "../composables/use-lock-scroll.mjs";
  5. import { useTouch } from "../composables/use-touch.mjs";
  6. import { useSyncPropRef } from "../composables/use-sync-prop-ref.mjs";
  7. const floatingPanelProps = {
  8. height: makeNumericProp(0),
  9. anchors: makeArrayProp(),
  10. duration: makeNumericProp(0.3),
  11. contentDraggable: truthProp,
  12. lockScroll: Boolean,
  13. safeAreaInsetBottom: truthProp
  14. };
  15. const [name, bem] = createNamespace("floating-panel");
  16. var stdin_default = defineComponent({
  17. name,
  18. props: floatingPanelProps,
  19. emits: ["heightChange", "update:height"],
  20. setup(props, {
  21. emit,
  22. slots
  23. }) {
  24. const DAMP = 0.2;
  25. const rootRef = ref();
  26. const contentRef = ref();
  27. const height = useSyncPropRef(() => +props.height, (value) => emit("update:height", value));
  28. const boundary = computed(() => {
  29. var _a, _b;
  30. return {
  31. min: (_a = props.anchors[0]) != null ? _a : 100,
  32. max: (_b = props.anchors[props.anchors.length - 1]) != null ? _b : Math.round(windowHeight.value * 0.6)
  33. };
  34. });
  35. const anchors = computed(() => props.anchors.length >= 2 ? props.anchors : [boundary.value.min, boundary.value.max]);
  36. const dragging = ref(false);
  37. const rootStyle = computed(() => ({
  38. height: addUnit(boundary.value.max),
  39. transform: `translateY(calc(100% + ${addUnit(-height.value)}))`,
  40. transition: !dragging.value ? `transform ${props.duration}s cubic-bezier(0.18, 0.89, 0.32, 1.28)` : "none"
  41. }));
  42. const ease = (moveY) => {
  43. const absDistance = Math.abs(moveY);
  44. const {
  45. min,
  46. max
  47. } = boundary.value;
  48. if (absDistance > max) {
  49. return -(max + (absDistance - max) * DAMP);
  50. }
  51. if (absDistance < min) {
  52. return -(min - (min - absDistance) * DAMP);
  53. }
  54. return moveY;
  55. };
  56. let startY;
  57. let maxScroll = -1;
  58. const touch = useTouch();
  59. const onTouchstart = (e) => {
  60. touch.start(e);
  61. dragging.value = true;
  62. startY = -height.value;
  63. maxScroll = -1;
  64. };
  65. const onTouchmove = (e) => {
  66. var _a;
  67. touch.move(e);
  68. const target = e.target;
  69. if (contentRef.value === target || ((_a = contentRef.value) == null ? void 0 : _a.contains(target))) {
  70. const {
  71. scrollTop
  72. } = contentRef.value;
  73. maxScroll = Math.max(maxScroll, scrollTop);
  74. if (!props.contentDraggable) return;
  75. if (-startY < boundary.value.max) {
  76. preventDefault(e, true);
  77. } else if (!(scrollTop <= 0 && touch.deltaY.value > 0) || maxScroll > 0) {
  78. return;
  79. }
  80. }
  81. const moveY = touch.deltaY.value + startY;
  82. height.value = -ease(moveY);
  83. };
  84. const onTouchend = () => {
  85. maxScroll = -1;
  86. dragging.value = false;
  87. height.value = closest(anchors.value, height.value);
  88. if (height.value !== -startY) {
  89. emit("heightChange", {
  90. height: height.value
  91. });
  92. }
  93. };
  94. watch(boundary, () => {
  95. height.value = closest(anchors.value, height.value);
  96. }, {
  97. immediate: true
  98. });
  99. useLockScroll(rootRef, () => props.lockScroll || dragging.value);
  100. useEventListener("touchmove", onTouchmove, {
  101. target: rootRef
  102. });
  103. const renderHeader = () => {
  104. if (slots.header) {
  105. return slots.header();
  106. }
  107. return _createVNode("div", {
  108. "class": bem("header")
  109. }, [_createVNode("div", {
  110. "class": bem("header-bar")
  111. }, null)]);
  112. };
  113. return () => {
  114. var _a;
  115. return _createVNode("div", {
  116. "class": [bem(), {
  117. "van-safe-area-bottom": props.safeAreaInsetBottom
  118. }],
  119. "ref": rootRef,
  120. "style": rootStyle.value,
  121. "onTouchstartPassive": onTouchstart,
  122. "onTouchend": onTouchend,
  123. "onTouchcancel": onTouchend
  124. }, [renderHeader(), _createVNode("div", {
  125. "class": bem("content"),
  126. "ref": contentRef
  127. }, [(_a = slots.default) == null ? void 0 : _a.call(slots)])]);
  128. };
  129. }
  130. });
  131. export {
  132. stdin_default as default,
  133. floatingPanelProps
  134. };