u-tooltip.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. emits: ["click"],
  200. methods: {
  201. init() {
  202. this.getElRect()
  203. },
  204. // 长按触发事件
  205. async longpressHandler() {
  206. this.tooltipTop = 0
  207. this.showTooltip = true
  208. },
  209. // 点击透明遮罩
  210. overlayClickHandler() {
  211. this.showTooltip = false
  212. },
  213. // 点击弹出按钮
  214. btnClickHandler(index) {
  215. this.showTooltip = false
  216. // 如果需要展示复制按钮,此处index需要加1,因为复制按钮在第一个位置
  217. this.$emit('click', this.showCopy ? index + 1 : index)
  218. },
  219. // 查询内容高度
  220. queryRect(ref) {
  221. // #ifndef APP-NVUE
  222. // $uGetRect为uView自带的节点查询简化方法,详见文档介绍:https://ijry.github.io/uview-plus/js/getRect.html
  223. // 组件内部一般用this.$uGetRect,对外的为uni.$u.getRect,二者功能一致,名称不同
  224. return new Promise(resolve => {
  225. this.$uGetRect(`#${ref}`).then(size => {
  226. resolve(size)
  227. })
  228. })
  229. // #endif
  230. // #ifdef APP-NVUE
  231. // nvue下,使用dom模块查询元素高度
  232. // 返回一个promise,让调用此方法的主体能使用then回调
  233. return new Promise(resolve => {
  234. dom.getComponentRect(this.$refs[ref], res => {
  235. resolve(res.size)
  236. })
  237. })
  238. // #endif
  239. },
  240. // 元素尺寸
  241. getElRect() {
  242. // 调用之前,先将指示器调整到屏幕外,方便获取尺寸
  243. this.showTooltip = true
  244. this.tooltipTop = -10000
  245. uni.$u.sleep(500).then(() => {
  246. this.queryRect(this.tooltipId).then(size => {
  247. this.tooltipInfo = size
  248. // 获取气泡尺寸之后,将其隐藏,为了让下次切换气泡显示与隐藏时,有淡入淡出的效果
  249. this.showTooltip = false
  250. })
  251. this.queryRect(this.textId).then(size => {
  252. this.textInfo = size
  253. })
  254. })
  255. },
  256. // 复制文本到粘贴板
  257. setClipboardData() {
  258. // 关闭组件
  259. this.showTooltip = false
  260. this.$emit('click', 0)
  261. // #ifndef H5
  262. uni.setClipboardData({
  263. // 优先使用copyText字段,如果没有,则默认使用text字段当做复制的内容
  264. data: this.copyText || this.text,
  265. success: () => {
  266. this.showToast && uni.$u.toast('复制成功')
  267. },
  268. fail: () => {
  269. this.showToast && uni.$u.toast('复制失败')
  270. },
  271. complete: () => {
  272. this.showTooltip = false
  273. }
  274. })
  275. // #endif
  276. // #ifdef H5
  277. let event = window.event || e || {}
  278. let clipboard = new ClipboardJS('', {
  279. text: () => this.copyText || this.text
  280. })
  281. clipboard.on('success', (e) => {
  282. this.showToast && uni.$u.toast('复制成功')
  283. clipboard.off('success')
  284. clipboard.off('error')
  285. // 在单页应用中,需要销毁DOM的监听
  286. clipboard.destroy()
  287. })
  288. clipboard.on('error', (e) => {
  289. this.showToast && uni.$u.toast('复制失败')
  290. clipboard.off('success')
  291. clipboard.off('error')
  292. // 在单页应用中,需要销毁DOM的监听
  293. clipboard.destroy()
  294. })
  295. clipboard.onClick(event)
  296. // #endif
  297. }
  298. }
  299. }
  300. </script>
  301. <style lang="scss" scoped>
  302. @import "../../libs/css/components.scss";
  303. .u-tooltip {
  304. position: relative;
  305. @include flex;
  306. &__wrapper {
  307. @include flex;
  308. justify-content: center;
  309. /* #ifndef APP-NVUE */
  310. white-space: nowrap;
  311. /* #endif */
  312. &__text {
  313. font-size: 14px;
  314. }
  315. &__popup {
  316. @include flex;
  317. justify-content: center;
  318. &__list {
  319. background-color: #060607;
  320. position: relative;
  321. flex: 1;
  322. border-radius: 5px;
  323. padding: 0px 0;
  324. @include flex(row);
  325. align-items: center;
  326. overflow: hidden;
  327. &__btn {
  328. padding: 11px 13px;
  329. &--hover {
  330. background-color: #58595B;
  331. }
  332. &__text {
  333. line-height: 12px;
  334. font-size: 13px;
  335. color: #FFFFFF;
  336. }
  337. }
  338. }
  339. &__indicator {
  340. position: absolute;
  341. background-color: #060607;
  342. width: 14px;
  343. height: 14px;
  344. bottom: -4px;
  345. transform: rotate(45deg);
  346. border-radius: 2px;
  347. z-index: -1;
  348. &--hover {
  349. background-color: #58595B;
  350. }
  351. }
  352. }
  353. }
  354. }
  355. </style>