UNPKG

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