wangzhibo
4 天以前 10595d8632f959d0954d1939243196f4eed02372
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<template>
    <view
        v-if="visible"
        :class="[
            'cl-popup__wrapper',
            `cl-popup__wrapper--${direction}`,
            `is-${status ? 'open' : 'close'}`,
            {
                'is-modal': modal,
            },
        ]"
        :style="{
            zIndex,
        }"
        @touchmove.stop.prevent
    >
        <!-- 遮罩层 -->
        <view
            class="cl-popup__modal"
            :style="{
                background: modalBackground,
            }"
            @tap="modalClose"
            v-if="modal"
        ></view>
 
        <!-- 内容 -->
        <view
            class="cl-popup"
            :style="{ height, width, backgroundColor, borderRadius: parseRpx(borderRadius) }"
        >
            <view class="cl-popup__header" v-if="title && showHeader">
                <slot name="header">{{ title }}</slot>
            </view>
 
            <view class="cl-popup__container" :style="{ padding: parseRpx(padding), paddingTop }">
                <slot></slot>
            </view>
 
            <view class="cl-popup__close" v-if="status && showCloseBtn" @tap="close">
                <text class="cl-icon-close"></text>
            </view>
        </view>
    </view>
</template>
 
<script lang="ts">
import { computed, defineComponent, ref, watch, type PropType } from "vue";
import { parseRpx } from "/@/cool/utils";
import { router } from "/@/cool";
 
const { statusBarHeight = 0 } = uni.getSystemInfoSync();
 
let id = 1;
 
export default defineComponent({
    name: "cl-popup",
 
    props: {
        // 是否可见
        modelValue: Boolean,
        // 标题
        title: String,
        // 是否显示头部
        showHeader: {
            type: Boolean,
            default: true,
        },
        // 弹出方向
        direction: {
            type: String as PropType<"top" | "right" | "bottom" | "center" | "left">,
            default: "center",
        },
        // 弹出框宽度
        size: {
            type: [String, Number],
            default: "auto",
        },
        // 內间距
        padding: {
            type: [String, Number],
            default: 32,
        },
        // 弹出框圆角
        borderRadius: [Number, Array, String],
        // 显示关闭按钮
        showCloseBtn: Boolean,
        // 背景色
        backgroundColor: {
            type: String,
            default: "#fff",
        },
        // 是否显示遮罩层
        modal: {
            type: Boolean,
            default: true,
        },
        // 遮罩层背景色
        modalBackground: {
            type: String,
            default: "rgba(0, 0, 0, 0.4)",
        },
        // 点击遮罩层是否关闭
        closeOnClickModal: {
            type: Boolean,
            default: true,
        },
        // 层级
        zIndex: {
            type: Number,
            default: 600,
        },
    },
 
    emits: ["update:modelValue", "open", "opened", "close", "closed"],
 
    setup(props, { emit }) {
        // 是否可见
        const visible = ref(false);
 
        // 是否已打开
        const isOpened = ref(false);
 
        // 是否已关闭
        const isClosed = ref(true);
 
        // 动画状态
        const status = ref(false);
 
        // 层级
        const zIndex = ref(0);
 
        // 计时器
        let timer: any;
 
        // 是否聚焦
        const isFocus = computed(() => {
            // #ifdef MP
            return isOpened.value;
            // #endif
 
            // #ifndef MP
            return true;
            // #endif
        });
 
        // 高
        const height = computed(() => {
            switch (props.direction) {
                case "top":
                case "bottom":
                    return parseRpx(props.size);
                case "left":
                case "right":
                    return "100%";
            }
        });
 
        // 宽
        const width = computed(() => {
            switch (props.direction) {
                case "top":
                case "bottom":
                    return "100%";
                case "left":
                case "right":
                case "center":
                    return parseRpx(props.size);
            }
        });
 
        // 顶部距离
        const paddingTop = computed(() => {
            if (["left", "right", "top"].includes(props.direction)) {
                let h = 0;
 
                if (router.info()?.isCustomNavbar) {
                    h += statusBarHeight;
                } else {
                    // #ifdef H5
                    h += 44 + statusBarHeight;
                    // #endif
                }
 
                let [t] = parseRpx(props.padding).split(" ");
 
                if (t == "0rpx") {
                    t = "0px";
                }
 
                return `calc(${h}px + ${t})`;
            } else {
                return 1;
            }
        });
 
        // 打开
        function open() {
            // 层级
            zIndex.value = props.zIndex + id++;
 
            if (!visible.value) {
                // 显示内容
                visible.value = true;
 
                // 未打开
                isClosed.value = false;
 
                emit("update:modelValue", true);
                emit("open");
 
                clearTimeout(timer);
 
                timer = setTimeout(() => {
                    // 开始动画
                    status.value = true;
 
                    // 等待动画结束
                    timer = setTimeout(() => {
                        // 已打开
                        isOpened.value = true;
                        emit("opened");
                    }, 350);
                }, 50);
            }
        }
 
        // 关闭
        function close() {
            if (status.value) {
                const done = () => {
                    isOpened.value = false;
 
                    // 关闭动画
                    status.value = false;
                    emit("close");
 
                    clearTimeout(timer);
 
                    timer = setTimeout(() => {
                        // 隐藏内容
                        visible.value = false;
                        emit("update:modelValue", false);
 
                        // 已关闭
                        isClosed.value = true;
                        emit("closed");
                    }, 300);
                };
 
                done();
            }
        }
 
        // 遮罩层关闭
        function modalClose() {
            if (!props.closeOnClickModal) {
                return false;
            }
 
            close();
        }
 
        watch(
            () => props.modelValue,
            (val: boolean) => {
                if (val) {
                    open();
                } else {
                    close();
                }
            },
            {
                immediate: true,
            },
        );
 
        return {
            visible,
            isOpened,
            isClosed,
            isFocus,
            status,
            height,
            width,
            paddingTop,
            zIndex,
            parseRpx,
            open,
            close,
            modalClose,
        };
    },
});
</script>