cl-popup.uvue 11 KB

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