u-tooltip.vue 10.0 KB

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