cl-popup.uvue 11 KB

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