<template>
    <view :class="['m-popup', ['mode', mode].join('-'), isTopSafeArea && show ? 'top-safe-area' : '', isBottomSafeArea && show ? 'bottom-safe-area' : '', show ? 'open' : 'close']">
        <view
            class="m-popup-mask"
            :style="{ backgroundColor: `rgba(0, 0, 0, ${Math.max(0.1, Math.min(0.9, maskTransparency))})`, zIndex: _zIndex, transition: isTransition ? 'linear 100ms' : '' }"
            @click="handleMaskClick"
            @touchmove.stop.prevent
        ></view>
        <view
            class="m-popup-content"
            :style="Object.assign({}, customStyle, { background, zIndex: _zIndex + 1, transition: isTransition ? 'linear 150ms' : '' })"
            @click.stop
            @touchmove.stop.prevent
        >
            <slot></slot>
        </view>
    </view>
</template>
<script setup lang="ts">
/**
 * @name 弹窗组件
 * @description 顶部弹出/底部弹出/居中弹出/左侧弹出/右侧弹出
 * @author jjp
 * 参数   defineProps
 * 事件   defineEmits
 * 上下文 defineExpose
 * 版本:
 */
import type { PropType, CSSProperties } from 'vue';
import { ref } from 'vue';
import useZIndex from '@/hook/useZIndex';
const props = defineProps({
    /**
     * 层级
     */
    zIndex: {
        type: Number,
        default: undefined
    },
    /**
     * 模式
     */
    mode: {
        type: String as PropType<'top' | 'left' | 'right' | 'bottom' | 'center'>,
        validator: (value: string) => ['top', 'left', 'right', 'bottom', 'center'].includes(value),
        default: 'center'
    },
    /**
     * 是否展示
     *
     * v-model:show="show" 操作
     */
    show: {
        type: Boolean,
        default: false
    },
    /**
     * 顶部安全区适配
     *
     * mode=top|right|left适用
     */
    isTopSafeArea: {
        type: Boolean,
        default: true
    },
    /**
     * 底部安全区适配
     *
     * mode=bottom|right|left适用
     */
    isBottomSafeArea: {
        type: Boolean,
        default: true
    },
    /**
     * 内容背景,默认白色,优先级高于customStyle自定义样式
     */
    background: {
        type: String,
        default: '#ffffff'
    },
    /**
     * 内容自定义样式
     */
    customStyle: {
        type: Object as PropType<CSSProperties>,
        default: () => {}
    },
    /**
     * 蒙版透明度
     *
     * 0.1-0.9
     */
    maskTransparency: {
        type: Number,
        validator: (value: number) => 0.1 <= value && value <= 0.9,
        default: 0.5
    },
    /**
     * 点击蒙蔽是否可以关闭
     */
    isMaskClickClose: {
        type: Boolean,
        default: true
    }
});
const _zIndex = props.zIndex || useZIndex();
const isTransition = ref(false);
setTimeout(function () {
    isTransition.value = true;
});
const emit = defineEmits(['update:show', 'close']);
/**
 * 蒙蔽点击
 */
const handleMaskClick = function () {
    if (!props.isMaskClickClose) return;
    emit('update:show', false);
    emit('close', false);
};
</script>
<style scoped lang="scss">
.m-popup {
    .m-popup-mask {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
    }

    .m-popup-content {
        position: fixed;
        box-sizing: border-box;
        overflow: hidden;
    }

    &.mode-top.top-safe-area,
    &.mode-right.top-safe-area,
    &.mode-left.top-safe-area {
        padding-top: var(--body-top-status-height);
    }

    &.mode-bottom.bottom-safe-area,
    &.mode-right.bottom-safe-area,
    &.mode-left.bottom-safe-area {
        padding-bottom: var(--body-bottom-status-height);
    }

    &.mode-top {
        .m-popup-content {
            top: 0;
            left: 0;
            right: 0;
            border-radius: 0 0 16rpx 16rpx;
        }
    }

    &.mode-left {
        .m-popup-content {
            top: 0;
            left: 0;
            bottom: 0;
            border-radius: 0 16rpx 16rpx 0;
        }
    }

    &.mode-right {
        .m-popup-content {
            top: 0;
            right: 0;
            bottom: 0;
            border-radius: 16rpx 0 0 16rpx;
        }
    }

    &.mode-bottom {
        .m-popup-content {
            bottom: 0;
            left: 0;
            right: 0;
            border-radius: 16rpx 16rpx 0 0;
        }
    }

    &.mode-center {
        .m-popup-content {
            top: 50%;
            left: 50%;
            // width: 80vw;
            min-height: 10vh;
            border-radius: 16rpx;
        }
    }

    &.open {
        .m-popup-mask {
            opacity: 1;
            pointer-events: all;
        }

        .m-popup-content {
            opacity: 1;
            pointer-events: all;
        }

        &.mode-top {
            .m-popup-content {
                transform: translateY(0);
            }
        }

        &.mode-left {
            .m-popup-content {
                transform: translateX(0);
            }
        }

        &.mode-right {
            .m-popup-content {
                transform: translateX(0);
            }
        }

        &.mode-bottom {
            .m-popup-content {
                transform: translateY(0);
            }
        }

        &.mode-center {
            .m-popup-content {
                transform: translate(-50%, -50%) scale(1);
            }
        }
    }

    &.close {
        .m-popup-mask {
            opacity: 0;
            pointer-events: none;
        }

        .m-popup-content {
            opacity: 0;
            pointer-events: none;
        }

        &.mode-top {
            .m-popup-content {
                transform: translateY(-100%);
            }
        }

        &.mode-left {
            .m-popup-content {
                transform: translateX(-100%);
            }
        }

        &.mode-right {
            .m-popup-content {
                transform: translateX(100%);
            }
        }

        &.mode-bottom {
            .m-popup-content {
                transform: translateY(100%);
            }
        }

        &.mode-center {
            .m-popup-content {
                opacity: 0;
                transform: translate(-50%, -50%) scale(0.8);
            }
        }
    }
}
</style>

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
蜀ICP备2022015939号 简书