cl-popup.uvue 11 KB

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