u-tooltip.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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, 'px'),
  33. zIndex: zIndex,
  34. ...tooltipStyleCpu
  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. triggerInfo: {
  148. width: 0,
  149. left: 0
  150. },
  151. // 三角形指示器的样式
  152. indicatorStyle: {},
  153. // 气泡在可能超出屏幕边沿范围时,重新定位后,距离屏幕边沿的距离
  154. screenGap: 12,
  155. // 三角形指示器的宽高,由于对元素进行了角度旋转,精确计算指示器位置时,需要用到其尺寸信息
  156. indicatorWidth: 14,
  157. tooltipStyle: {},
  158. calcReacted: false
  159. }
  160. },
  161. watch: {
  162. async propsChange() {
  163. await this.getElRect()
  164. // this.getTooltipStyle();
  165. }
  166. },
  167. computed: {
  168. // 特别处理H5的复制,因为H5浏览器是自带系统复制功能的,在H5环境
  169. // 当一些依赖参数变化时,需要重新计算气泡和指示器的位置信息
  170. propsChange() {
  171. return [this.text, this.buttons]
  172. },
  173. // 计算气泡和指示器的位置信息
  174. tooltipStyleCpu() {
  175. if (!this.calcReacted) {
  176. return {}
  177. }
  178. const style = {},
  179. sysInfo = getWindowInfo()
  180. if (this.direction === 'left') {
  181. // 右侧显示逻辑
  182. style.transform = ``
  183. // 垂直居中对齐
  184. style.top = '-' + addUnit((this.triggerInfo.height - this.tooltipInfo.height) / 2, 'px')
  185. style.right = addUnit(this.triggerInfo.width + this.indicatorWidth, 'px')
  186. this.indicatorStyle = {}
  187. this.indicatorStyle.right = '-4px'
  188. this.indicatorStyle.top = addUnit((this.tooltipInfo.height - this.indicatorWidth) / 2, 'px')
  189. } else if (this.direction === 'right') {
  190. // 右侧显示逻辑
  191. style.transform = ``
  192. // 垂直居中对齐
  193. style.top = '-' + addUnit((this.triggerInfo.height - this.tooltipInfo.height) / 2, 'px')
  194. style.left = addUnit(this.triggerInfo.width + this.indicatorWidth, 'px')
  195. this.indicatorStyle = {}
  196. this.indicatorStyle.left = '-4px'
  197. this.indicatorStyle.top = addUnit((this.triggerInfo.height - this.indicatorWidth) / 2, 'px')
  198. } else if (this.direction === 'top' || this.direction === 'bottom') {
  199. style.transform = `translateY(${this.direction === 'top' ? '-100%' : '100%'})`
  200. if (this.tooltipInfo.width / 2 > this.triggerInfo.left + this.triggerInfo.width / 2 - this.screenGap) {
  201. this.indicatorStyle = {}
  202. style.left = `-${addUnit(this.triggerInfo.left - this.screenGap)}`
  203. this.indicatorStyle.left = addUnit(this.triggerInfo.width / 2 - getPx(style.left) - this.indicatorWidth /
  204. 2, 'px')
  205. } else if (this.tooltipInfo.width / 2 > sysInfo.windowWidth - this.triggerInfo.right + this.triggerInfo.width / 2 -
  206. this.screenGap) {
  207. this.indicatorStyle = {}
  208. style.right = `-${addUnit(sysInfo.windowWidth - this.triggerInfo.right - this.screenGap)}`
  209. this.indicatorStyle.right = addUnit(this.triggerInfo.width / 2 - getPx(style.right) - this
  210. .indicatorWidth / 2)
  211. } else {
  212. const left = Math.abs(this.triggerInfo.width / 2 - this.tooltipInfo.width / 2)
  213. style.left = this.triggerInfo.width > this.tooltipInfo.width ? addUnit(left) : -addUnit(left)
  214. this.indicatorStyle = {}
  215. }
  216. if (this.direction === 'top') {
  217. style.marginTop = '-10px'
  218. this.indicatorStyle.bottom = '-4px'
  219. } else {
  220. style.marginBottom = '-10px'
  221. this.indicatorStyle.top = '-4px'
  222. }
  223. }
  224. let styleMerge = {...style, ...this.forcePosition}
  225. this.tooltipStyle = styleMerge
  226. return styleMerge
  227. }
  228. },
  229. mounted() {
  230. this.init()
  231. },
  232. emits: ["click"],
  233. methods: {
  234. addStyle,
  235. addUnit,
  236. async init() {
  237. await this.getElRect()
  238. // this.getTooltipStyle();
  239. },
  240. // 点击触发事件
  241. async clickHander() {
  242. // this.getTooltipStyle();
  243. if (this.triggerMode == 'click') {
  244. this.tooltipTop = 0
  245. this.showTooltip = true
  246. }
  247. },
  248. // 长按触发事件
  249. async longpressHandler() {
  250. // this.getTooltipStyle();
  251. if (this.triggerMode == 'longpress') {
  252. this.tooltipTop = 0
  253. this.showTooltip = true
  254. }
  255. },
  256. // 点击透明遮罩
  257. overlayClickHandler() {
  258. this.showTooltip = false
  259. },
  260. // 点击弹出按钮
  261. btnClickHandler(index) {
  262. this.showTooltip = false
  263. // 如果需要展示复制按钮,此处index需要加1,因为复制按钮在第一个位置
  264. this.$emit('click', this.showCopy ? index + 1 : index)
  265. },
  266. // 查询内容高度
  267. queryRect(ref) {
  268. // #ifndef APP-NVUE
  269. // $uGetRect为uview-plus自带的节点查询简化方法,详见文档介绍:https://ijry.github.io/uview-plus/js/getRect.html
  270. // 组件内部一般用this.$uGetRect,对外的为uni.$u.getRect,二者功能一致,名称不同
  271. return new Promise(resolve => {
  272. this.$uGetRect(`#${ref}`).then(size => {
  273. resolve(size)
  274. })
  275. })
  276. // #endif
  277. // #ifdef APP-NVUE
  278. // nvue下,使用dom模块查询元素高度
  279. // 返回一个promise,让调用此方法的主体能使用then回调
  280. return new Promise(resolve => {
  281. dom.getComponentRect(this.$refs[ref], res => {
  282. resolve(res.size)
  283. })
  284. })
  285. // #endif
  286. },
  287. // 元素尺寸
  288. getElRect() {
  289. return new Promise(async(resolve) => {
  290. // 调用之前,先将指示器调整到屏幕外,方便获取尺寸
  291. this.showTooltip = true
  292. this.tooltipTop = -10000
  293. sleep(500).then(async () => {
  294. this.tooltipInfo = await this.queryRect(this.tooltipId)
  295. // 获取气泡尺寸之后,将其隐藏,为了让下次切换气泡显示与隐藏时,有淡入淡出的效果
  296. this.showTooltip = false
  297. this.triggerInfo = await this.queryRect(this.textId)
  298. sleep(500).then(() => {
  299. this.calcReacted = true
  300. })
  301. resolve()
  302. })
  303. })
  304. },
  305. // 复制文本到粘贴板
  306. setClipboardData() {
  307. // 关闭组件
  308. this.showTooltip = false
  309. this.$emit('click', 0)
  310. uni.setClipboardData({
  311. // 优先使用copyText字段,如果没有,则默认使用text字段当做复制的内容
  312. data: this.copyText || this.text,
  313. success: () => {
  314. this.showToast && toast('复制成功')
  315. },
  316. fail: () => {
  317. this.showToast && toast('复制失败')
  318. },
  319. complete: () => {
  320. this.showTooltip = false
  321. }
  322. })
  323. }
  324. }
  325. }
  326. </script>
  327. <style lang="scss" scoped>
  328. .u-tooltip {
  329. position: relative;
  330. @include flex;
  331. &__wrapper {
  332. @include flex;
  333. justify-content: center;
  334. /* #ifndef APP-NVUE */
  335. white-space: nowrap;
  336. /* #endif */
  337. &__text {
  338. font-size: 14px;
  339. }
  340. &__popup {
  341. @include flex;
  342. justify-content: center;
  343. &__list {
  344. background-color: #060607;
  345. color: #FFFFFF;
  346. position: relative;
  347. flex: 1;
  348. border-radius: 5px;
  349. padding: 0px 0;
  350. @include flex(row);
  351. align-items: center;
  352. overflow: hidden;
  353. &__btn {
  354. padding: 11px 13px;
  355. &--hover {
  356. background-color: #58595B;
  357. }
  358. &__text {
  359. line-height: 12px;
  360. font-size: 13px;
  361. color: #FFFFFF;
  362. }
  363. }
  364. }
  365. &__indicator {
  366. position: absolute;
  367. background-color: #060607;
  368. width: 14px;
  369. height: 14px;
  370. bottom: -4px;
  371. transform: rotate(45deg);
  372. border-radius: 2px;
  373. z-index: -1;
  374. &--hover {
  375. background-color: #58595B;
  376. }
  377. }
  378. }
  379. }
  380. }
  381. </style>