u-tooltip.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. backgroundColor: bgColor && showTooltip && tooltipTop !== -10000 ? bgColor : 'transparent'
  21. }"
  22. >{{ text }}
  23. </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. >复制
  61. </text>
  62. </view>
  63. <u-line
  64. direction="column"
  65. color="#8d8e90"
  66. v-if="showCopy && buttons.length > 0"
  67. length="18"
  68. ></u-line>
  69. <template v-for="(item , index) in buttons">
  70. <view
  71. class="u-tooltip__wrapper__popup__list__btn"
  72. hover-class="u-tooltip__wrapper__popup__list__btn--hover"
  73. :key="index"
  74. >
  75. <text
  76. class="u-tooltip__wrapper__popup__list__btn__text"
  77. @tap="btnClickHandler(index)"
  78. >{{ item }}
  79. </text>
  80. </view>
  81. <u-line
  82. direction="column"
  83. color="#8d8e90"
  84. v-if="index < buttons.length - 1"
  85. length="18"
  86. :key="index"
  87. ></u-line>
  88. </template>
  89. </view>
  90. </view>
  91. </u-transition>
  92. </view>
  93. </view>
  94. </template>
  95. <script>
  96. import props from './props.js';
  97. // #endif
  98. // #ifdef H5
  99. import ClipboardJS from "./clipboard.min.js"
  100. // #ifdef APP-NVUE
  101. const dom = uni.requireNativePlugin('dom')
  102. // #endif
  103. /**
  104. * Tooltip
  105. * @description
  106. * @tutorial https://www.uviewui.com/components/tooltip.html
  107. * @property {String | Number} text 需要显示的提示文字
  108. * @property {String | Number} copyText 点击复制按钮时,复制的文本,为空则使用text值
  109. * @property {String | Number} size 文本大小(默认 14 )
  110. * @property {String} color 字体颜色(默认 '#606266' )
  111. * @property {String} bgColor 弹出提示框时,文本的背景色(默认 'transparent' )
  112. * @property {String} direction 弹出提示的方向,top-上方,bottom-下方(默认 'top' )
  113. * @property {String | Number} zIndex 弹出提示的z-index,nvue无效(默认 10071 )
  114. * @property {Boolean} showCopy 是否显示复制按钮(默认 true )
  115. * @property {Array} buttons 扩展的按钮组
  116. * @property {Boolean} overlay 是否显示透明遮罩以防止触摸穿透(默认 true )
  117. * @property {Object} customStyle 定义需要用到的外部样式
  118. *
  119. * @event {Function}
  120. * @example
  121. */
  122. export default {
  123. name: 'u-tooltip',
  124. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  125. data() {
  126. return {
  127. // 是否展示气泡
  128. showTooltip: true,
  129. // 生成唯一id,防止一个页面多个组件,造成干扰
  130. textId: uni.$u.guid(),
  131. tooltipId: uni.$u.guid(),
  132. // 初始时甚至为很大的值,让其移到屏幕外面,为了计算元素的尺寸
  133. tooltipTop: -10000,
  134. // 气泡的位置信息
  135. tooltipInfo: {
  136. width: 0,
  137. left: 0
  138. },
  139. // 文本的位置信息
  140. textInfo: {
  141. width: 0,
  142. left: 0
  143. },
  144. // 三角形指示器的样式
  145. indicatorStyle: {},
  146. // 气泡在可能超出屏幕边沿范围时,重新定位后,距离屏幕边沿的距离
  147. screenGap: 12,
  148. // 三角形指示器的宽高,由于对元素进行了角度旋转,精确计算指示器位置时,需要用到其尺寸信息
  149. indicatorWidth: 14,
  150. }
  151. },
  152. watch: {
  153. propsChange() {
  154. this.getElRect()
  155. }
  156. },
  157. computed: {
  158. // 特别处理H5的复制,因为H5浏览器是自带系统复制功能的,在H5环境
  159. // 当一些依赖参数变化时,需要重新计算气泡和指示器的位置信息
  160. propsChange() {
  161. return [this.text, this.buttons]
  162. },
  163. // 计算气泡和指示器的位置信息
  164. tooltipStyle() {
  165. const style = {
  166. transform: `translateY(${this.direction === 'top' ? '-100%' : '100%'})`,
  167. },
  168. sys = uni.$u.sys(),
  169. getPx = uni.$u.getPx,
  170. addUnit = uni.$u.addUnit
  171. if (this.tooltipInfo.width / 2 > this.textInfo.left + this.textInfo.width / 2 - this.screenGap) {
  172. this.indicatorStyle = {}
  173. style.left = `-${addUnit(this.textInfo.left - this.screenGap)}`
  174. this.indicatorStyle.left = addUnit(this.textInfo.width / 2 - getPx(style.left) - this.indicatorWidth /
  175. 2)
  176. } else if (this.tooltipInfo.width / 2 > sys.windowWidth - this.textInfo.right + this.textInfo.width / 2 -
  177. this.screenGap) {
  178. this.indicatorStyle = {}
  179. style.right = `-${addUnit(sys.windowWidth - this.textInfo.right - this.screenGap)}`
  180. this.indicatorStyle.right = addUnit(this.textInfo.width / 2 - getPx(style.right) - this
  181. .indicatorWidth / 2)
  182. } else {
  183. const left = Math.abs(this.textInfo.width / 2 - this.tooltipInfo.width / 2)
  184. style.left = this.textInfo.width > this.tooltipInfo.width ? addUnit(left) : -addUnit(left)
  185. this.indicatorStyle = {}
  186. }
  187. if (this.direction === 'top') {
  188. style.marginTop = '-10px'
  189. this.indicatorStyle.bottom = '-4px'
  190. } else {
  191. style.marginBottom = '-10px'
  192. this.indicatorStyle.top = '-4px'
  193. }
  194. return style
  195. }
  196. },
  197. mounted() {
  198. this.init()
  199. },
  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://www.uviewui.com/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. color: $u-content-color;
  314. font-size: 14px;
  315. }
  316. &__popup {
  317. @include flex;
  318. justify-content: center;
  319. &__list {
  320. background-color: #060607;
  321. position: relative;
  322. flex: 1;
  323. border-radius: 5px;
  324. padding: 0px 0;
  325. @include flex(row);
  326. align-items: center;
  327. overflow: hidden;
  328. &__btn {
  329. padding: 11px 13px;
  330. &--hover {
  331. background-color: #58595B;
  332. }
  333. &__text {
  334. line-height: 12px;
  335. font-size: 13px;
  336. color: #FFFFFF;
  337. }
  338. }
  339. }
  340. &__indicator {
  341. position: absolute;
  342. background-color: #060607;
  343. width: 14px;
  344. height: 14px;
  345. bottom: -4px;
  346. transform: rotate(45deg);
  347. border-radius: 2px;
  348. z-index: -1;
  349. &--hover {
  350. background-color: #58595B;
  351. }
  352. }
  353. }
  354. }
  355. }
  356. </style>