u-tooltip.vue 11 KB

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