uni-cloudPopup.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <view class="u-tooltip" :style="[$u.addStyle(customStyle)]">
  3. <u-overlay :show="showTooltip && tooltipTop !== -10000 && overlay"
  4. customStyle="backgroundColor: rgba(0, 0, 0, 0)" @click="overlayClickHandler"></u-overlay>
  5. <view class="u-tooltip__wrapper">
  6. <text class="u-tooltip__wrapper__text" :id="textId" :ref="textId" :userSelect="false" :selectable="false"
  7. @tap.stop="longpressHandler" :style="{
  8. color: color,
  9. fontSize: $u.addUnit(size),
  10. backgroundColor: bgColor && showTooltip && tooltipTop !== -10000 ? bgColor : 'transparent'
  11. }">{{ text }}</text>
  12. <u-transition mode="fade" :show="showTooltip" duration="300" :customStyle="{
  13. position: 'absolute',
  14. top: $u.addUnit(tooltipTop),
  15. zIndex: zIndex,
  16. ...tooltipStyle
  17. }">
  18. <view class="u-tooltip__wrapper__popup" :id="tooltipId" :ref="tooltipId">
  19. <view class="u-tooltip__wrapper__popup__indicator"
  20. hover-class="u-tooltip__wrapper__popup__indicator--hover" v-if="showCopy || buttons.length"
  21. :style="[indicatorStyle, {
  22. width: $u.addUnit(indicatorWidth),
  23. height: $u.addUnit(indicatorWidth),
  24. }]">
  25. <!-- 由于nvue不支持三角形绘制,这里就做一个四方形,再旋转45deg,得到露出的一个三角 -->
  26. </view>
  27. <view class="u-tooltip__wrapper__popup__list">
  28. <block v-for="(item , index) in buttons" :key="index">
  29. <view class="u-tooltip__wrapper__popup__list__btn" @tap.stop="btnClickHandler(item)"
  30. hover-class="u-tooltip__wrapper__popup__list__btn--hover">
  31. <text class="u-tooltip__wrapper__popup__list__btn__text">{{ item }}</text>
  32. </view>
  33. </block>
  34. </view>
  35. </view>
  36. </u-transition>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. import props from './props.js';
  42. // #ifdef APP-NVUE
  43. const dom = uni.requireNativePlugin('dom')
  44. // #endif
  45. // #ifdef H5
  46. import ClipboardJS from "./clipboard.min.js"
  47. // #endif
  48. /**
  49. * Tooltip
  50. * @description
  51. * @tutorial https://www.uviewui.com/components/tooltip.html
  52. * @property {String | Number} text 需要显示的提示文字
  53. * @property {String | Number} copyText 点击复制按钮时,复制的文本,为空则使用text值
  54. * @property {String | Number} size 文本大小(默认 14 )
  55. * @property {String} color 字体颜色(默认 '#606266' )
  56. * @property {String} bgColor 弹出提示框时,文本的背景色(默认 'transparent' )
  57. * @property {String} direction 弹出提示的方向,top-上方,bottom-下方(默认 'top' )
  58. * @property {String | Number} zIndex 弹出提示的z-index,nvue无效(默认 10071 )
  59. * @property {Boolean} showCopy 是否显示复制按钮(默认 true )
  60. * @property {Array} buttons 扩展的按钮组
  61. * @property {Boolean} overlay 是否显示透明遮罩以防止触摸穿透(默认 true )
  62. * @property {Object} customStyle 定义需要用到的外部样式
  63. *
  64. * @event {Function}
  65. * @example
  66. */
  67. export default {
  68. name: 'u-tooltip',
  69. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  70. data() {
  71. return {
  72. // 是否展示气泡
  73. showTooltip: true,
  74. // 生成唯一id,防止一个页面多个组件,造成干扰
  75. textId: uni.$u.guid(),
  76. tooltipId: uni.$u.guid(),
  77. // 初始时甚至为很大的值,让其移到屏幕外面,为了计算元素的尺寸
  78. tooltipTop: 0,
  79. // 气泡的位置信息
  80. tooltipInfo: {
  81. width: 0,
  82. left: 0
  83. },
  84. // 文本的位置信息
  85. textInfo: {
  86. width: 0,
  87. left: 0
  88. },
  89. // 三角形指示器的样式
  90. indicatorStyle: {},
  91. // 气泡在可能超出屏幕边沿范围时,重新定位后,距离屏幕边沿的距离
  92. screenGap: 12,
  93. // 三角形指示器的宽高,由于对元素进行了角度旋转,精确计算指示器位置时,需要用到其尺寸信息
  94. indicatorWidth: 14,
  95. }
  96. },
  97. watch: {
  98. propsChange() {
  99. this.getElRect()
  100. }
  101. },
  102. computed: {
  103. // 特别处理H5的复制,因为H5浏览器是自带系统复制功能的,在H5环境
  104. // 当一些依赖参数变化时,需要重新计算气泡和指示器的位置信息
  105. propsChange() {
  106. return [this.text, this.buttons]
  107. },
  108. // 计算气泡和指示器的位置信息
  109. tooltipStyle() {
  110. const style = {
  111. transform: `translateY(${this.direction === 'top' ? '-100%' : '0'})`,
  112. },
  113. sys = uni.$u.sys(),
  114. getPx = uni.$u.getPx,
  115. addUnit = uni.$u.addUnit
  116. if (this.tooltipInfo.width / 2 > this.textInfo.left + this.textInfo.width / 2 - this.screenGap) {
  117. this.indicatorStyle = {}
  118. style.left = `-${addUnit(this.textInfo.left - this.screenGap)}`
  119. this.indicatorStyle.left = addUnit(this.textInfo.width / 2 - getPx(style.left) - this.indicatorWidth /
  120. 2)
  121. } else if (this.tooltipInfo.width / 2 > sys.windowWidth - this.textInfo.right + this.textInfo.width / 2 -
  122. this.screenGap) {
  123. this.indicatorStyle = {}
  124. style.right = `-${addUnit(sys.windowWidth - this.textInfo.right - this.screenGap)}`
  125. this.indicatorStyle.right = addUnit(this.textInfo.width / 2 - getPx(style.right) - this
  126. .indicatorWidth / 2)
  127. } else {
  128. const left = Math.abs(this.textInfo.width / 2 - this.tooltipInfo.width / 2)
  129. style.left = this.textInfo.width > this.tooltipInfo.width ? addUnit(left) : -addUnit(left)
  130. this.indicatorStyle = {}
  131. }
  132. if (this.direction === 'top') {
  133. this.indicatorStyle.bottom = '-4px'
  134. } else {
  135. this.indicatorStyle.top = '-4px'
  136. }
  137. return style
  138. }
  139. },
  140. mounted() {
  141. this.init()
  142. },
  143. methods: {
  144. init() {
  145. this.getElRect()
  146. },
  147. // 长按触发事件
  148. async longpressHandler() {
  149. if (this.direction === 'top') {
  150. this.tooltipTop = -10
  151. } else {
  152. this.tooltipTop = 25
  153. }
  154. this.showTooltip = true
  155. },
  156. // 点击透明遮罩
  157. overlayClickHandler() {
  158. this.showTooltip = false
  159. },
  160. // 点击弹出按钮
  161. btnClickHandler(index) {
  162. this.showTooltip = false
  163. // 如果需要展示复制按钮,此处index需要加1,因为复制按钮在第一个位置
  164. this.$emit('click', index)
  165. },
  166. // 查询内容高度
  167. queryRect(ref) {
  168. // #ifndef APP-NVUE
  169. // $uGetRect为uView自带的节点查询简化方法,详见文档介绍:https://www.uviewui.com/js/getRect.html
  170. // 组件内部一般用this.$uGetRect,对外的为uni.$u.getRect,二者功能一致,名称不同
  171. return new Promise(resolve => {
  172. this.$uGetRect(`#${ref}`).then(size => {
  173. resolve(size)
  174. })
  175. })
  176. // #endif
  177. // #ifdef APP-NVUE
  178. // nvue下,使用dom模块查询元素高度
  179. // 返回一个promise,让调用此方法的主体能使用then回调
  180. return new Promise(resolve => {
  181. dom.getComponentRect(this.$refs[ref], res => {
  182. resolve(res.size)
  183. })
  184. })
  185. // #endif
  186. },
  187. // 元素尺寸
  188. getElRect() {
  189. // 调用之前,先将指示器调整到屏幕外,方便获取尺寸
  190. this.showTooltip = true
  191. this.tooltipTop = -10000
  192. uni.$u.sleep(500).then(() => {
  193. this.queryRect(this.tooltipId).then(size => {
  194. this.tooltipInfo = size
  195. // 获取气泡尺寸之后,将其隐藏,为了让下次切换气泡显示与隐藏时,有淡入淡出的效果
  196. this.showTooltip = false
  197. })
  198. this.queryRect(this.textId).then(size => {
  199. this.textInfo = size
  200. })
  201. })
  202. },
  203. }
  204. }
  205. </script>
  206. <style lang="scss" scoped>
  207. uni-text {
  208. white-space: unset;
  209. }
  210. .u-tooltip {
  211. position: relative;
  212. @include flex;
  213. &__wrapper {
  214. @include flex;
  215. justify-content: center;
  216. /* #ifndef APP-NVUE */
  217. white-space: nowrap;
  218. /* #endif */
  219. &__text {
  220. font-size: 14px;
  221. }
  222. &__popup {
  223. @include flex;
  224. position: relative;
  225. justify-content: center;
  226. filter: drop-shadow(0 0 16rpx rgba(0, 0, 0, 0.1));
  227. &__list {
  228. background-color: #fff;
  229. position: relative;
  230. flex: 1;
  231. border-radius: 5px;
  232. padding: 0px 0;
  233. display: flex;
  234. flex-direction: column;
  235. overflow: hidden;
  236. &__btn {
  237. padding: 21rpx 64rpx;
  238. box-sizing: border-box;
  239. border-bottom: 1rpx solid #eee;
  240. text-align: center;
  241. &--hover {
  242. background-color: #fff;
  243. }
  244. &__text {
  245. font-size: 28rpx;
  246. color: #333;
  247. }
  248. }
  249. &__btn:last-child {
  250. border: none;
  251. }
  252. }
  253. &__indicator {
  254. position: absolute;
  255. background-color: #fff;
  256. width: 14px;
  257. height: 14px;
  258. bottom: -4px;
  259. transform: rotate(45deg);
  260. border-radius: 2px;
  261. z-index: -1;
  262. &--hover {
  263. background-color: #58595B;
  264. }
  265. }
  266. }
  267. }
  268. }
  269. </style>