popover.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <template>
  2. <view class="zbPopover" :style="{
  3. '--theme-bg-color': bgStyleColor
  4. }">
  5. <view class="mask" @click.stop="close" v-if="show" :style="{ backgroundColor: overlayBg }"></view>
  6. <view @click.stop="handleClick" class="zb-button-popover">
  7. <slot></slot>
  8. </view>
  9. <view class="zb-popover" v-show="inited" ref="zb-transition" :class="[classes, `zb-popover-${placement}`]"
  10. :style="[mergeStyle, { ...popoverStyle }]" @touchmove="noop">
  11. <view class="zb-popover-arrow" :style="[arrowStyle]" :class="[{
  12. 'zb_popper__up': placement.indexOf('bottom') === 0,
  13. 'zb_popper__arrow': placement.indexOf('top') === 0,
  14. 'zb_popper__right': placement.indexOf('right') === 0,
  15. 'zb_popper__left': placement.indexOf('left') === 0,
  16. }]" v-if="isArrow">
  17. </view>
  18. <slot name="content">
  19. <view :class="[{
  20. 'horizontal__action': actionsDirection === 'horizontal'
  21. }]">
  22. <view @click.stop="actionAction(item)" v-for="item, index in options" class="zb-popover__action" :class="[{
  23. 'dark__action': theme === 'dark'
  24. }]" :key="index">
  25. <slot name="item" :item="item">
  26. <i class="icon-font" :class="item.icon" :style="{ fontSize: iconSize }"></i>
  27. </slot>
  28. <view class="zb-popover__action-text">
  29. <trans class="text" :_t="keyName ? item[keyName] : item" />
  30. </view>
  31. </view>
  32. </view>
  33. </slot>
  34. </view>
  35. </view>
  36. </template>
  37. <script setup>
  38. import { ref, onMounted, computed, nextTick, watch } from "vue"
  39. const tranClass = {
  40. enter: "zb-fade-zoom-enter zb-fade-zoom-enter-active",
  41. 'enter-to': "zb-fade-zoom-enter-to zb-fade-zoom-enter-active",
  42. leave: "zb-fade-zoom-leave zb-fade-zoom-leave-active",
  43. 'leave-to': "zb-fade-zoom-leave-to zb-fade-zoom-leave-active",
  44. }
  45. const props = defineProps({
  46. options: {
  47. type: Array,
  48. default: () => []
  49. },
  50. placement: {
  51. type: String,
  52. default: 'bottom-start'
  53. },
  54. bgColor: {
  55. type: String,
  56. },
  57. theme: {
  58. type: String,
  59. default: 'light' // light dark
  60. },
  61. actionsDirection: {
  62. type: String,
  63. default: 'vertical' // horizontal vertical
  64. },
  65. keyName: {
  66. type: String,
  67. default: ''
  68. },
  69. overlayBg: {
  70. type: String,
  71. default: 'transparent'
  72. },
  73. iconSize: {
  74. type: String,
  75. default: '30px'
  76. },
  77. isArrow: Boolean
  78. })
  79. const emit = defineEmits(['handleClick', 'select'])
  80. const show = ref(false);
  81. const inited = ref(false);
  82. const classes = ref('');
  83. const duration = ref(100);
  84. const popoverStyle = ref({});
  85. const arrowOldStyle = ref({});
  86. const bgStyleColor = computed(() => {
  87. if (props.bgColor) {
  88. return props.bgColor
  89. }
  90. if (props.theme === 'light') {
  91. return 'white'
  92. }
  93. if (props.theme === 'dark') {
  94. return '#4a4a4a'
  95. }
  96. })
  97. const mergeStyle = computed(() => {
  98. return {
  99. transitionDuration: `${duration.value}ms`,
  100. transitionTimingFunction: `ease-out`,
  101. ...popoverStyle.value
  102. }
  103. })
  104. const arrowStyle = computed(() => {
  105. return { ...arrowOldStyle.value }
  106. })
  107. onMounted(() => {
  108. // // #ifdef H5
  109. // window.addEventListener('click', () => {
  110. // show.value = false
  111. // })
  112. // // #endif
  113. })
  114. const handleClick = () => {
  115. if (show.value) {
  116. show.value = false
  117. } else {
  118. show.value = true
  119. }
  120. emit('handleClick', show.value)
  121. }
  122. const close = () => {
  123. show.value = false
  124. }
  125. const actionAction = (item) => {
  126. emit('select', item)
  127. show.value = false
  128. }
  129. const sleep = (value) => {
  130. return new Promise((resolve) => {
  131. setTimeout(() => {
  132. resolve()
  133. }, value)
  134. })
  135. }
  136. const vueEnter = () => {
  137. inited.value = true
  138. getPosition()
  139. classes.value = tranClass.enter
  140. nextTick(async () => {
  141. await sleep(30)
  142. classes.value = tranClass['enter-to']
  143. })
  144. }
  145. const vueLeave = () => {
  146. classes.value = tranClass.leave
  147. nextTick(async () => {
  148. classes.value = tranClass['leave-to']
  149. await sleep(120)
  150. inited.value = false
  151. })
  152. }
  153. const preventEvent = (e) => {
  154. e && typeof (e.stopPropagation) === 'function' && e.stopPropagation()
  155. }
  156. const getPosition = () => {
  157. return new Promise((resolve) => {
  158. nextTick(() => {
  159. let selectorQuery = uni.createSelectorQuery().in(this).selectAll('.zb-button-popover,.zb-popover')
  160. let popoverStyles = {}
  161. let arrowOldStyles = {}
  162. selectorQuery.boundingClientRect(async (data) => {
  163. let { left, bottom, right, top, width, height } = data[0]
  164. let popoverClientRect = data[1]
  165. switch (props.placement) {
  166. case 'top':
  167. if (popoverClientRect.width > width) {
  168. popoverStyles.left = `-${(popoverClientRect.width - width) / 2}px`
  169. } else {
  170. popoverStyles.left = `${Math.abs(popoverClientRect.width - width) / 2}px`
  171. }
  172. popoverStyles.bottom = `${height + 8}px`
  173. arrowOldStyles.left = (popoverClientRect.width / 2 - 6) + 'px'
  174. break;
  175. case 'top-start':
  176. popoverStyles.left = `0px`
  177. popoverStyles.bottom = `${height + 8}px`
  178. arrowOldStyles.left = '16px'
  179. break;
  180. case 'top-end':
  181. popoverStyles.right = `0px`
  182. popoverStyles.bottom = `${height + 8}px`
  183. arrowOldStyles.right = '16px'
  184. break;
  185. case 'bottom':
  186. if (popoverClientRect.width > width) {
  187. popoverStyles.left = `-${(popoverClientRect.width - width) / 2}px`
  188. } else {
  189. popoverStyles.left = `${Math.abs(popoverClientRect.width - width) / 2}px`
  190. }
  191. popoverStyles.top = `${height + 8}px`
  192. arrowOldStyles.left = (popoverClientRect.width / 2 - 6) + 'px'
  193. break;
  194. case 'bottom-start':
  195. popoverStyles.top = `${height + 8}px`
  196. popoverStyles.left = `0px`
  197. arrowOldStyles.left = '16px'
  198. break;
  199. case 'bottom-end':
  200. popoverStyles.top = `${height + 8}px`
  201. popoverStyles.right = `0px`
  202. arrowOldStyles.right = '16px'
  203. break;
  204. case 'right':
  205. popoverStyles.left = `${width + 8}px`
  206. if (popoverClientRect.height > height) {
  207. popoverStyles.top = `-${(popoverClientRect.height - height) / 2}px`
  208. } else {
  209. popoverStyles.top = `${Math.abs((popoverClientRect.height - height) / 2)}px`
  210. }
  211. arrowOldStyles.top = `${popoverClientRect.height / 2 - 6}px`
  212. break;
  213. case 'right-start':
  214. popoverStyles.left = `${width + 8}px`
  215. popoverStyles.top = `0px`
  216. arrowOldStyles.top = `8px`
  217. break;
  218. case 'right-end':
  219. popoverStyles.left = `${width + 8}px`
  220. popoverStyles.bottom = `0px`
  221. arrowOldStyles.bottom = `8px`
  222. break;
  223. case 'left':
  224. popoverStyles.right = `${width + 8}px`
  225. if (popoverClientRect.height > height) {
  226. popoverStyles.top = `-${(popoverClientRect.height - height) / 2}px`
  227. } else {
  228. popoverStyles.top = `${Math.abs((popoverClientRect.height - height) / 2)}px`
  229. }
  230. arrowOldStyles.top = `${popoverClientRect.height / 2 - 6}px`
  231. break;
  232. case 'left-start':
  233. popoverStyles.right = `${width + 8}px`
  234. popoverStyles.top = `0px`
  235. arrowOldStyles.top = `8px`
  236. break;
  237. case 'left-end':
  238. popoverStyles.right = `${width + 8}px`
  239. popoverStyles.bottom = `0px`
  240. arrowOldStyles.bottom = `8px`
  241. break;
  242. }
  243. popoverStyle.value = popoverStyles;
  244. arrowOldStyle.value = arrowOldStyles
  245. resolve()
  246. }).exec()
  247. })
  248. })
  249. }
  250. const noop = (e) => {
  251. preventEvent(e)
  252. }
  253. watch(show, (newVal) => {
  254. newVal ? vueEnter() : vueLeave()
  255. }, { immediate: true })
  256. </script>
  257. <style lang="scss" scoped>
  258. $theme-bg-color: var(--theme-bg-color);
  259. .mask {
  260. position: fixed;
  261. top: 0;
  262. left: 0;
  263. right: 0;
  264. bottom: 0;
  265. z-index: 2044;
  266. }
  267. .zbPopover {
  268. position: relative;
  269. }
  270. .zb-button-popover {
  271. display: flex;
  272. align-items: center;
  273. }
  274. .zb-popover {
  275. border-radius: 8px;
  276. z-index: 2144;
  277. position: absolute;
  278. background-color: $theme-bg-color;
  279. box-shadow: 0 2px 12px #3232331f;
  280. overflow: hidden;
  281. }
  282. .zb-popover-top {
  283. transform-origin: 50% bottom;
  284. }
  285. .zb-popover-top-start {
  286. transform-origin: 50% bottom;
  287. }
  288. .zb-popover-top-end {
  289. transform-origin: 0 bottom;
  290. }
  291. .zb-popover-bottom {
  292. transform-origin: 50% 0;
  293. }
  294. .zb-popover-bottom-end {
  295. transform-origin: 100% 0;
  296. }
  297. .zb-popover-bottom-start {
  298. transform-origin: 0 0;
  299. }
  300. .zb-popover-right {
  301. transform-origin: left 50%;
  302. }
  303. .zb-popover-right-start {
  304. transform-origin: left 0;
  305. }
  306. .zb-popover-right-end {
  307. transform-origin: left 100%;
  308. }
  309. .zb-popover-left {
  310. transform-origin: right 50%;
  311. }
  312. .zb-popover-left-start {
  313. transform-origin: right 0;
  314. }
  315. .zb-popover-left-end {
  316. transform-origin: right 100%;
  317. }
  318. .zb-popover-arrow {
  319. position: absolute;
  320. width: 0;
  321. height: 0;
  322. border-color: transparent;
  323. border-style: solid;
  324. border-width: 6px;
  325. color: $theme-bg-color;
  326. }
  327. .zb_popper__up {
  328. border-top-width: 0;
  329. border-bottom-color: currentColor;
  330. top: -5px;
  331. }
  332. .zb_popper__right {
  333. border-left-width: 0;
  334. border-right-color: currentColor;
  335. left: -5px;
  336. }
  337. .zb_popper__left {
  338. border-right-width: 0;
  339. border-left-color: currentColor;
  340. right: -5px;
  341. }
  342. .zb_popper__arrow {
  343. border-bottom-width: 0;
  344. border-top-color: currentColor;
  345. bottom: -6px;
  346. }
  347. .zb-popover__action {
  348. position: relative;
  349. display: flex;
  350. align-items: center;
  351. box-sizing: border-box;
  352. height: 88rpx;
  353. padding: 0 24rpx;
  354. font-size: 28rpx;
  355. cursor: pointer;
  356. }
  357. .zb-popover__action-text {
  358. display: flex;
  359. flex: 1;
  360. align-items: center;
  361. height: 100%;
  362. padding: 0 24rpx;
  363. // border-bottom: 1rpx solid #ebedf0;
  364. word-wrap: break-word;
  365. white-space: nowrap;
  366. .text{
  367. display: inline-block;
  368. word-wrap: break-word;
  369. white-space: nowrap;
  370. color: #62708c;
  371. }
  372. }
  373. .zb-popover__action:last-child {
  374. .zb-popover__action-text {
  375. border-bottom: none;
  376. }
  377. }
  378. .dark__action {
  379. color: white;
  380. .zb-popover__action-text {
  381. // border-bottom: 1rpx solid #ebedf033
  382. }
  383. }
  384. .horizontal__action {
  385. display: flex;
  386. .zb-popover__action {
  387. padding: 0 20rpx;
  388. border-right: 1rpx solid #ebedf0;
  389. }
  390. .zb-popover__action-text {
  391. padding: 0;
  392. //border-right:1rpx solid #ebedf0;
  393. }
  394. }
  395. $u-zoom-scale: scale(0.95);
  396. .zb-fade-enter-active,
  397. .zb-fade-leave-active {
  398. transition-property: opacity;
  399. }
  400. .zb-fade-enter,
  401. .zb-fade-leave-to {
  402. opacity: 0
  403. }
  404. .zb-fade-zoom-enter,
  405. .zb-fade-zoom-leave-to {
  406. transform: $u-zoom-scale;
  407. opacity: 0;
  408. }
  409. .zb-fade-zoom-enter-active,
  410. .zb-fade-zoom-leave-active {
  411. transition-property: transform, opacity;
  412. }
  413. .zb-fade-down-enter-active,
  414. .zb-fade-down-leave-active,
  415. .zb-fade-left-enter-active,
  416. .zb-fade-left-leave-active,
  417. .zb-fade-right-enter-active,
  418. .zb-fade-right-leave-active,
  419. .zb-fade-up-enter-active,
  420. .zb-fade-up-leave-active {
  421. transition-property: opacity, transform;
  422. }
  423. .zb-fade-up-enter,
  424. .zb-fade-up-leave-to {
  425. transform: translate3d(0, 100%, 0);
  426. opacity: 0
  427. }
  428. .zb-fade-down-enter,
  429. .zb-fade-down-leave-to {
  430. transform: translate3d(0, -100%, 0);
  431. opacity: 0
  432. }
  433. .zb-fade-left-enter,
  434. .zb-fade-left-leave-to {
  435. transform: translate3d(-100%, 0, 0);
  436. opacity: 0
  437. }
  438. .zb-fade-right-enter,
  439. .zb-fade-right-leave-to {
  440. transform: translate3d(100%, 0, 0);
  441. opacity: 0
  442. }
  443. .zb-popover__action {
  444. .icon-font {
  445. color: #62708c;
  446. }
  447. &:active {
  448. background-color: rgba(0, 0, 0, 0.2);
  449. }
  450. &--disabled {
  451. color: var(--van-popover-dark-action-disabled-text-color);
  452. &:active {
  453. background-color: transparent;
  454. }
  455. }
  456. }
  457. </style>