cl-popup.uvue 11 KB

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