u-tooltip.vue 10 KB

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