cl-popup.uvue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. <template>
  2. <view
  3. class="cl-popup-wrapper"
  4. :class="[`cl-popup-wrapper--${direction}`]"
  5. :style="{
  6. zIndex,
  7. pointerEvents
  8. }"
  9. v-show="visible"
  10. v-if="keepAlive ? true : visible"
  11. @touchmove.stop.prevent
  12. >
  13. <view
  14. class="cl-popup-mask"
  15. :class="[
  16. {
  17. 'is-open': status == 1,
  18. 'is-close': status == 2
  19. },
  20. pt.mask?.className
  21. ]"
  22. @tap="maskClose"
  23. v-if="showMask"
  24. ></view>
  25. <view
  26. class="cl-popup"
  27. :class="[
  28. {
  29. 'is-open': status == 1,
  30. 'is-close': status == 2,
  31. 'is-custom-navbar': router.isCustomNavbarPage(),
  32. 'stop-transition': swipe.isTouch
  33. },
  34. pt.className
  35. ]"
  36. :style="popupStyle"
  37. @touchstart="onTouchStart"
  38. @touchmove="onTouchMove"
  39. @touchend="onTouchEnd"
  40. >
  41. <view
  42. class="cl-popup__inner"
  43. :class="[
  44. {
  45. 'is-dark': isDark
  46. },
  47. pt.inner?.className
  48. ]"
  49. :style="{
  50. paddingBottom
  51. }"
  52. >
  53. <view
  54. class="cl-popup__draw"
  55. :class="[
  56. {
  57. '!bg-surface-400': swipe.isMove
  58. },
  59. pt.draw?.className
  60. ]"
  61. v-if="isSwipeClose"
  62. ></view>
  63. <view class="cl-popup__header" :class="[pt.header?.className]" v-if="showHeader">
  64. <slot name="header">
  65. <cl-text
  66. :pt="{
  67. className: `text-lg font-bold ${pt.header?.text?.className}`
  68. }"
  69. >{{ title }}</cl-text
  70. >
  71. </slot>
  72. <cl-icon
  73. name="close-circle-fill"
  74. :size="40"
  75. :pt="{
  76. className:
  77. 'absolute right-[24rpx] !text-surface-400 dark:!text-surface-50'
  78. }"
  79. @tap="close"
  80. @touchmove.stop
  81. v-if="isOpen && showClose"
  82. ></cl-icon>
  83. </view>
  84. <view class="cl-popup__container" :class="[pt.container?.className]">
  85. <slot></slot>
  86. </view>
  87. </view>
  88. </view>
  89. </view>
  90. </template>
  91. <script lang="ts" setup>
  92. import { computed, reactive, ref, watch, type PropType } from "vue";
  93. import { parsePt, parseRpx, usePage } from "@/cool";
  94. import type { ClPopupDirection, PassThroughProps } from "../../types";
  95. import { isDark, router } from "@/cool";
  96. import { config } from "../../config";
  97. defineOptions({
  98. name: "cl-popup"
  99. });
  100. defineSlots<{
  101. header: () => any;
  102. }>();
  103. // 组件属性定义
  104. const props = defineProps({
  105. // 透传样式配置
  106. pt: {
  107. type: Object,
  108. default: () => ({})
  109. },
  110. // 是否可见
  111. modelValue: {
  112. type: Boolean,
  113. default: false
  114. },
  115. // 标题
  116. title: {
  117. type: String,
  118. default: null
  119. },
  120. // 弹出方向
  121. direction: {
  122. type: String as PropType<ClPopupDirection>,
  123. default: "bottom"
  124. },
  125. // 弹出框宽度
  126. size: {
  127. type: [String, Number],
  128. default: ""
  129. },
  130. // 是否显示头部
  131. showHeader: {
  132. type: Boolean,
  133. default: true
  134. },
  135. // 显示关闭按钮
  136. showClose: {
  137. type: Boolean,
  138. default: true
  139. },
  140. // 是否显示遮罩层
  141. showMask: {
  142. type: Boolean,
  143. default: true
  144. },
  145. // 是否点击遮罩层关闭弹窗
  146. maskClosable: {
  147. type: Boolean,
  148. default: true
  149. },
  150. // 是否开启拖拽关闭
  151. swipeClose: {
  152. type: Boolean,
  153. default: true
  154. },
  155. // 拖拽关闭的阈值
  156. swipeCloseThreshold: {
  157. type: Number,
  158. default: 150
  159. },
  160. // 触摸事件响应方式
  161. pointerEvents: {
  162. type: String as PropType<"auto" | "none">,
  163. default: "auto"
  164. },
  165. // 是否开启缓存
  166. keepAlive: {
  167. type: Boolean,
  168. default: false
  169. }
  170. });
  171. // 定义组件事件
  172. const emit = defineEmits(["update:modelValue", "open", "opened", "close", "closed", "maskClose"]);
  173. // 页面实例
  174. const page = usePage();
  175. type HeaderPassThrough = {
  176. className?: string;
  177. text?: PassThroughProps;
  178. };
  179. // 透传样式类型定义
  180. type PassThrough = {
  181. className?: string;
  182. inner?: PassThroughProps;
  183. header?: HeaderPassThrough;
  184. container?: PassThroughProps;
  185. mask?: PassThroughProps;
  186. draw?: PassThroughProps;
  187. };
  188. // 解析透传样式配置
  189. const pt = computed(() => parsePt<PassThrough>(props.pt));
  190. // 控制弹出层显示/隐藏
  191. const visible = ref(false);
  192. // 0: 初始状态 1: 打开中 2: 关闭中
  193. const status = ref(0);
  194. // 标记弹出层是否处于打开状态(包含动画过程)
  195. const isOpen = ref(false);
  196. // 标记弹出层是否已完全打开(动画结束)
  197. const isOpened = ref(false);
  198. // 弹出层z-index值
  199. const zIndex = ref(config.zIndex);
  200. // 计算弹出层高度
  201. const height = computed(() => {
  202. switch (props.direction) {
  203. case "top":
  204. case "bottom":
  205. return parseRpx(props.size); // 顶部和底部弹出时使用传入的size
  206. case "left":
  207. case "right":
  208. return "100%"; // 左右弹出时占满全高
  209. default:
  210. return "";
  211. }
  212. });
  213. // 计算弹出层宽度
  214. const width = computed(() => {
  215. switch (props.direction) {
  216. case "top":
  217. case "bottom":
  218. return "100%"; // 顶部和底部弹出时占满全宽
  219. case "left":
  220. case "right":
  221. case "center":
  222. return parseRpx(props.size); // 其他方向使用传入的size
  223. default:
  224. return "";
  225. }
  226. });
  227. // 底部安全距离
  228. const paddingBottom = computed(() => {
  229. let h = 0;
  230. if (props.direction == "bottom") {
  231. h += page.hasCustomTabBar() ? page.getTabBarHeight() : page.getSafeAreaHeight("bottom");
  232. }
  233. return h + "px";
  234. });
  235. // 是否显示拖动条
  236. const isSwipeClose = computed(() => {
  237. return props.direction == "bottom" && props.swipeClose;
  238. });
  239. // 动画定时器
  240. let timer: number = 0;
  241. // 打开弹出层
  242. function open() {
  243. // 递增z-index,保证多个弹出层次序
  244. zIndex.value = config.zIndex++;
  245. if (!visible.value) {
  246. // 显示弹出层
  247. visible.value = true;
  248. // 触发事件
  249. emit("update:modelValue", true);
  250. emit("open");
  251. // 等待DOM更新后开始动画
  252. setTimeout(() => {
  253. // 设置打开状态
  254. status.value = 1;
  255. // 动画结束后触发opened事件
  256. // @ts-ignore
  257. timer = setTimeout(() => {
  258. isOpened.value = true;
  259. emit("opened");
  260. }, 350);
  261. }, 50);
  262. }
  263. }
  264. // 关闭弹出层
  265. function close() {
  266. if (status.value == 1) {
  267. // 重置打开状态
  268. isOpened.value = false;
  269. // 设置关闭状态
  270. status.value = 2;
  271. // 触发事件
  272. emit("close");
  273. // 清除未完成的定时器
  274. if (timer != 0) {
  275. clearTimeout(timer);
  276. }
  277. // 动画结束后隐藏弹出层
  278. // @ts-ignore
  279. timer = setTimeout(() => {
  280. // 隐藏弹出层
  281. visible.value = false;
  282. // 重置状态
  283. status.value = 0;
  284. // 触发事件
  285. emit("update:modelValue", false);
  286. emit("closed");
  287. }, 350);
  288. }
  289. }
  290. // 点击遮罩层关闭
  291. function maskClose() {
  292. if (props.maskClosable) {
  293. close();
  294. }
  295. emit("maskClose");
  296. }
  297. // 滑动状态类型定义
  298. type Swipe = {
  299. isMove: boolean; // 是否移动
  300. isTouch: boolean; // 是否处于触摸状态
  301. startY: number; // 开始触摸的Y坐标
  302. offsetY: number; // Y轴偏移量
  303. };
  304. // 初始化滑动状态数据
  305. const swipe = reactive<Swipe>({
  306. isMove: false, // 是否移动
  307. isTouch: false, // 默认非触摸状态
  308. startY: 0, // 初始Y坐标为0
  309. offsetY: 0 // 初始偏移量为0
  310. });
  311. /**
  312. * 触摸开始事件处理
  313. * @param e 触摸事件对象
  314. * 当弹出层获得焦点且允许滑动关闭时,记录触摸起始位置
  315. */
  316. function onTouchStart(e: UniTouchEvent) {
  317. if (isOpened.value && isSwipeClose.value) {
  318. swipe.isTouch = true; // 标记开始触摸
  319. swipe.startY = e.touches[0].clientY; // 记录起始Y坐标
  320. }
  321. }
  322. /**
  323. * 触摸移动事件处理
  324. * @param e 触摸事件对象
  325. * 计算手指移动距离,更新弹出层位置
  326. */
  327. function onTouchMove(e: UniTouchEvent) {
  328. if (swipe.isTouch) {
  329. // 标记为移动状态
  330. swipe.isMove = true;
  331. // 计算Y轴偏移量
  332. const offsetY = (e.touches[0] as UniTouch).pageY - swipe.startY;
  333. // 只允许向下滑动(offsetY > 0)
  334. if (offsetY > 0) {
  335. swipe.offsetY = offsetY;
  336. }
  337. }
  338. }
  339. /**
  340. * 触摸结束事件处理
  341. * 根据滑动距离判断是否关闭弹出层
  342. */
  343. function onTouchEnd() {
  344. if (swipe.isTouch) {
  345. // 结束触摸状态
  346. swipe.isTouch = false;
  347. // 结束移动状态
  348. swipe.isMove = false;
  349. // 如果滑动距离超过阈值,则关闭弹出层
  350. if (swipe.offsetY > props.swipeCloseThreshold) {
  351. close();
  352. }
  353. // 重置偏移量
  354. swipe.offsetY = 0;
  355. }
  356. }
  357. /**
  358. * 计算弹出层样式
  359. * 根据滑动状态动态设置transform属性实现位移动画
  360. */
  361. const popupStyle = computed(() => {
  362. const style = new Map<string, string>();
  363. // 基础样式
  364. style.set("height", height.value);
  365. style.set("width", width.value);
  366. // 处于触摸状态时添加位移效果
  367. if (swipe.isTouch) {
  368. style.set("transform", `translateY(${swipe.offsetY}px)`);
  369. }
  370. return style;
  371. });
  372. // 监听modelValue变化
  373. watch(
  374. computed(() => props.modelValue),
  375. (val: boolean) => {
  376. if (val) {
  377. open();
  378. } else {
  379. close();
  380. }
  381. },
  382. {
  383. immediate: true
  384. }
  385. );
  386. // 监听状态变化
  387. watch(status, (val: number) => {
  388. isOpen.value = val == 1;
  389. });
  390. defineExpose({
  391. isOpened,
  392. isOpen,
  393. open,
  394. close
  395. });
  396. </script>
  397. <style lang="scss" scoped>
  398. .cl-popup-wrapper {
  399. @apply h-full w-full;
  400. @apply fixed top-0 bottom-0 left-0 right-0;
  401. pointer-events: none;
  402. .cl-popup-mask {
  403. @apply absolute top-0 bottom-0 left-0 right-0;
  404. @apply h-full w-full;
  405. @apply bg-black opacity-0;
  406. transition-property: opacity;
  407. &.is-open {
  408. @apply opacity-40;
  409. }
  410. &.is-open,
  411. &.is-close {
  412. transition-duration: 0.3s;
  413. }
  414. }
  415. .cl-popup {
  416. @apply absolute;
  417. &__inner {
  418. @apply bg-white h-full w-full flex flex-col;
  419. &.is-dark {
  420. @apply bg-surface-700;
  421. }
  422. }
  423. &__draw {
  424. @apply bg-surface-200 rounded-md;
  425. @apply absolute top-2 left-1/2;
  426. height: 10rpx;
  427. width: 70rpx;
  428. transform: translateX(-50%);
  429. transition-property: background-color;
  430. transition-duration: 0.2s;
  431. }
  432. &__header {
  433. @apply flex flex-row items-center;
  434. height: 90rpx;
  435. padding: 0 26rpx;
  436. }
  437. &__container {
  438. flex: 1;
  439. }
  440. &.is-open {
  441. transform: translate(0, 0) scale(1);
  442. opacity: 1;
  443. }
  444. &.is-open,
  445. &.is-close {
  446. transition-property: transform, opacity;
  447. transition-duration: 0.3s;
  448. }
  449. &.stop-transition {
  450. transition-property: none;
  451. }
  452. }
  453. &--left {
  454. .cl-popup {
  455. @apply left-0 top-0;
  456. transform: translateX(-100%);
  457. }
  458. }
  459. &--right {
  460. .cl-popup {
  461. @apply right-0 top-0;
  462. transform: translateX(100%);
  463. }
  464. }
  465. &--top {
  466. .cl-popup {
  467. @apply left-0 top-0;
  468. &__inner {
  469. @apply rounded-b-2xl;
  470. }
  471. transform: translateY(-100%);
  472. }
  473. }
  474. &--left,
  475. &--right,
  476. &--top {
  477. .cl-popup {
  478. // #ifdef H5
  479. top: 44px;
  480. // #endif
  481. &.is-custom-navbar {
  482. top: 0;
  483. }
  484. }
  485. }
  486. &--left,
  487. &--right {
  488. .cl-popup {
  489. // #ifdef H5
  490. height: calc(100% - 44px) !important;
  491. // #endif
  492. }
  493. }
  494. &--bottom {
  495. .cl-popup {
  496. @apply left-0 bottom-0;
  497. transform: translateY(100%);
  498. &__inner {
  499. @apply rounded-t-2xl;
  500. }
  501. }
  502. }
  503. &--center {
  504. @apply flex flex-col items-center justify-center;
  505. .cl-popup {
  506. transform: scale(1.3);
  507. opacity: 0;
  508. &__inner {
  509. @apply rounded-2xl;
  510. }
  511. }
  512. }
  513. }
  514. </style>