main.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import Popper from 'element-ui/src/utils/vue-popper';
  2. import debounce from 'throttle-debounce/debounce';
  3. import { addClass, removeClass, on, off } from 'element-ui/src/utils/dom';
  4. import { generateId } from 'element-ui/src/utils/util';
  5. import Vue from 'vue';
  6. export default {
  7. name: 'ElTooltip',
  8. mixins: [Popper],
  9. props: {
  10. openDelay: {
  11. type: Number,
  12. default: 0
  13. },
  14. disabled: Boolean,
  15. manual: Boolean,
  16. effect: {
  17. type: String,
  18. default: 'dark'
  19. },
  20. arrowOffset: {
  21. type: Number,
  22. default: 0
  23. },
  24. popperClass: String,
  25. content: String,
  26. visibleArrow: {
  27. default: true
  28. },
  29. transition: {
  30. type: String,
  31. default: 'el-fade-in-linear'
  32. },
  33. popperOptions: {
  34. default() {
  35. return {
  36. boundariesPadding: 10,
  37. gpuAcceleration: false
  38. };
  39. }
  40. },
  41. enterable: {
  42. type: Boolean,
  43. default: true
  44. },
  45. hideAfter: {
  46. type: Number,
  47. default: 0
  48. },
  49. tabindex: {
  50. type: Number,
  51. default: 0
  52. }
  53. },
  54. data() {
  55. return {
  56. tooltipId: `el-tooltip-${generateId()}`,
  57. timeoutPending: null,
  58. focusing: false
  59. };
  60. },
  61. beforeCreate() {
  62. if (this.$isServer) return;
  63. this.popperVM = new Vue({
  64. data: { node: '' },
  65. render(h) {
  66. return this.node;
  67. }
  68. }).$mount();
  69. this.debounceClose = debounce(200, () => this.handleClosePopper());
  70. },
  71. render(h) {
  72. if (this.popperVM) {
  73. this.popperVM.node = (
  74. <transition
  75. name={ this.transition }
  76. onAfterLeave={ this.doDestroy }>
  77. <div
  78. onMouseleave={ () => { this.setExpectedState(false); this.debounceClose(); } }
  79. onMouseenter= { () => { this.setExpectedState(true); } }
  80. ref="popper"
  81. role="tooltip"
  82. id={this.tooltipId}
  83. aria-hidden={ (this.disabled || !this.showPopper) ? 'true' : 'false' }
  84. v-show={!this.disabled && this.showPopper}
  85. class={
  86. ['el-tooltip__popper', 'is-' + this.effect, this.popperClass]
  87. }>
  88. { this.$slots.content || this.content }
  89. </div>
  90. </transition>);
  91. }
  92. const firstElement = this.getFirstElement();
  93. if (!firstElement) return null;
  94. const data = firstElement.data = firstElement.data || {};
  95. data.staticClass = this.addTooltipClass(data.staticClass);
  96. return firstElement;
  97. },
  98. mounted() {
  99. this.referenceElm = this.$el;
  100. if (this.$el.nodeType === 1) {
  101. this.$el.setAttribute('aria-describedby', this.tooltipId);
  102. this.$el.setAttribute('tabindex', this.tabindex);
  103. on(this.referenceElm, 'mouseenter', this.show);
  104. on(this.referenceElm, 'mouseleave', this.hide);
  105. on(this.referenceElm, 'focus', () => {
  106. if (!this.$slots.default || !this.$slots.default.length) {
  107. this.handleFocus();
  108. return;
  109. }
  110. const instance = this.$slots.default[0].componentInstance;
  111. if (instance && instance.focus) {
  112. instance.focus();
  113. } else {
  114. this.handleFocus();
  115. }
  116. });
  117. on(this.referenceElm, 'blur', this.handleBlur);
  118. on(this.referenceElm, 'click', this.removeFocusing);
  119. }
  120. // fix issue https://github.com/ElemeFE/element/issues/14424
  121. if (this.value && this.popperVM) {
  122. this.popperVM.$nextTick(() => {
  123. if (this.value) {
  124. this.updatePopper();
  125. }
  126. });
  127. }
  128. },
  129. watch: {
  130. focusing(val) {
  131. if (val) {
  132. addClass(this.referenceElm, 'focusing');
  133. } else {
  134. removeClass(this.referenceElm, 'focusing');
  135. }
  136. }
  137. },
  138. methods: {
  139. show() {
  140. this.setExpectedState(true);
  141. this.handleShowPopper();
  142. },
  143. hide() {
  144. this.setExpectedState(false);
  145. this.debounceClose();
  146. },
  147. handleFocus() {
  148. this.focusing = true;
  149. this.show();
  150. },
  151. handleBlur() {
  152. this.focusing = false;
  153. this.hide();
  154. },
  155. removeFocusing() {
  156. this.focusing = false;
  157. },
  158. addTooltipClass(prev) {
  159. if (!prev) {
  160. return 'el-tooltip';
  161. } else {
  162. return 'el-tooltip ' + prev.replace('el-tooltip', '');
  163. }
  164. },
  165. handleShowPopper() {
  166. if (!this.expectedState || this.manual) return;
  167. clearTimeout(this.timeout);
  168. this.timeout = setTimeout(() => {
  169. this.showPopper = true;
  170. }, this.openDelay);
  171. if (this.hideAfter > 0) {
  172. this.timeoutPending = setTimeout(() => {
  173. this.showPopper = false;
  174. }, this.hideAfter);
  175. }
  176. },
  177. handleClosePopper() {
  178. if (this.enterable && this.expectedState || this.manual) return;
  179. clearTimeout(this.timeout);
  180. if (this.timeoutPending) {
  181. clearTimeout(this.timeoutPending);
  182. }
  183. this.showPopper = false;
  184. if (this.disabled) {
  185. this.doDestroy();
  186. }
  187. },
  188. setExpectedState(expectedState) {
  189. if (expectedState === false) {
  190. clearTimeout(this.timeoutPending);
  191. }
  192. this.expectedState = expectedState;
  193. },
  194. getFirstElement() {
  195. const slots = this.$slots.default;
  196. if (!Array.isArray(slots)) return null;
  197. let element = null;
  198. for (let index = 0; index < slots.length; index++) {
  199. if (slots[index] && slots[index].tag) {
  200. element = slots[index];
  201. };
  202. }
  203. return element;
  204. }
  205. },
  206. beforeDestroy() {
  207. this.popperVM && this.popperVM.$destroy();
  208. },
  209. destroyed() {
  210. const reference = this.referenceElm;
  211. if (reference.nodeType === 1) {
  212. off(reference, 'mouseenter', this.show);
  213. off(reference, 'mouseleave', this.hide);
  214. off(reference, 'focus', this.handleFocus);
  215. off(reference, 'blur', this.handleBlur);
  216. off(reference, 'click', this.removeFocusing);
  217. }
  218. }
  219. };