综合办公系统
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

uv-tooltip.vue 10KB

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