u-tooltip.vue 11 KB

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