You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tippy.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import tippy, {followCursor} from 'tippy.js';
  2. const visibleInstances = new Set();
  3. export function createTippy(target, opts = {}) {
  4. // the callback functions should be destructured from opts,
  5. // because we should use our own wrapper functions to handle them, do not let the user override them
  6. const {onHide, onShow, onDestroy, ...other} = opts;
  7. const instance = tippy(target, {
  8. appendTo: document.body,
  9. animation: false,
  10. allowHTML: false,
  11. hideOnClick: false,
  12. interactiveBorder: 20,
  13. ignoreAttributes: true,
  14. maxWidth: 500, // increase over default 350px
  15. onHide: (instance) => {
  16. visibleInstances.delete(instance);
  17. return onHide?.(instance);
  18. },
  19. onDestroy: (instance) => {
  20. visibleInstances.delete(instance);
  21. return onDestroy?.(instance);
  22. },
  23. onShow: (instance) => {
  24. // hide other tooltip instances so only one tooltip shows at a time
  25. for (const visibleInstance of visibleInstances) {
  26. if (visibleInstance.props.role === 'tooltip') {
  27. visibleInstance.hide();
  28. }
  29. }
  30. visibleInstances.add(instance);
  31. return onShow?.(instance);
  32. },
  33. arrow: `<svg width="16" height="7"><path d="m0 7 8-7 8 7Z" class="tippy-svg-arrow-outer"/><path d="m0 8 8-7 8 7Z" class="tippy-svg-arrow-inner"/></svg>`,
  34. role: 'menu', // HTML role attribute, only tooltips should use "tooltip"
  35. theme: other.role || 'menu', // CSS theme, we support either "tooltip" or "menu"
  36. plugins: [followCursor],
  37. ...other,
  38. });
  39. // for popups where content refers to a DOM element, we use the 'tippy-target' class
  40. // to initially hide the content, now we can remove it as the content has been removed
  41. // from the DOM by tippy
  42. if (other.content instanceof Element) {
  43. other.content.classList.remove('tippy-target');
  44. }
  45. return instance;
  46. }
  47. /**
  48. * Attach a tooltip tippy to the given target element.
  49. * If the target element already has a tooltip tippy attached, the tooltip will be updated with the new content.
  50. * If the target element has no content, then no tooltip will be attached, and it returns null.
  51. *
  52. * Note: "tooltip" doesn't equal to "tippy". "tooltip" means a auto-popup content, it just uses tippy as the implementation.
  53. *
  54. * @param target {HTMLElement}
  55. * @param content {null|string}
  56. * @returns {null|tippy}
  57. */
  58. function attachTooltip(target, content = null) {
  59. switchTitleToTooltip(target);
  60. content = content ?? target.getAttribute('data-tooltip-content');
  61. if (!content) return null;
  62. // when element has a clipboard target, we update the tooltip after copy
  63. // in which case it is undesirable to automatically hide it on click as
  64. // it would momentarily flash the tooltip out and in.
  65. const hasClipboardTarget = target.hasAttribute('data-clipboard-target');
  66. const hideOnClick = !hasClipboardTarget;
  67. const props = {
  68. content,
  69. delay: 100,
  70. role: 'tooltip',
  71. theme: 'tooltip',
  72. hideOnClick,
  73. placement: target.getAttribute('data-tooltip-placement') || 'top-start',
  74. followCursor: target.getAttribute('data-tooltip-follow-cursor') || false,
  75. ...(target.getAttribute('data-tooltip-interactive') === 'true' ? {interactive: true, aria: {content: 'describedby', expanded: false}} : {}),
  76. };
  77. if (!target._tippy) {
  78. createTippy(target, props);
  79. } else {
  80. target._tippy.setProps(props);
  81. }
  82. return target._tippy;
  83. }
  84. function switchTitleToTooltip(target) {
  85. const title = target.getAttribute('title');
  86. if (title) {
  87. target.setAttribute('data-tooltip-content', title);
  88. target.setAttribute('aria-label', title);
  89. // keep the attribute, in case there are some other "[title]" selectors
  90. // and to prevent infinite loop with <relative-time> which will re-add
  91. // title if it is absent
  92. target.setAttribute('title', '');
  93. }
  94. }
  95. /**
  96. * Creating tooltip tippy instance is expensive, so we only create it when the user hovers over the element
  97. * According to https://www.w3.org/TR/DOM-Level-3-Events/#events-mouseevent-event-order , mouseover event is fired before mouseenter event
  98. * Some browsers like PaleMoon don't support "addEventListener('mouseenter', capture)"
  99. * The tippy by default uses "mouseenter" event to show, so we use "mouseover" event to switch to tippy
  100. * @param e {Event}
  101. */
  102. function lazyTooltipOnMouseHover(e) {
  103. e.target.removeEventListener('mouseover', lazyTooltipOnMouseHover, true);
  104. attachTooltip(this);
  105. }
  106. // Activate the tooltip for current element.
  107. // If the element has no aria-label, use the tooltip content as aria-label.
  108. function attachLazyTooltip(el) {
  109. el.addEventListener('mouseover', lazyTooltipOnMouseHover, {capture: true});
  110. // meanwhile, if the element has no aria-label, use the tooltip content as aria-label
  111. if (!el.hasAttribute('aria-label')) {
  112. const content = el.getAttribute('data-tooltip-content');
  113. if (content) {
  114. el.setAttribute('aria-label', content);
  115. }
  116. }
  117. }
  118. // Activate the tooltip for all children elements.
  119. function attachChildrenLazyTooltip(target) {
  120. for (const el of target.querySelectorAll('[data-tooltip-content]')) {
  121. attachLazyTooltip(el);
  122. }
  123. }
  124. const elementNodeTypes = new Set([Node.ELEMENT_NODE, Node.DOCUMENT_FRAGMENT_NODE]);
  125. export function initGlobalTooltips() {
  126. // use MutationObserver to detect new "data-tooltip-content" elements added to the DOM, or attributes changed
  127. const observerConnect = (observer) => observer.observe(document, {
  128. subtree: true,
  129. childList: true,
  130. attributeFilter: ['data-tooltip-content', 'title']
  131. });
  132. const observer = new MutationObserver((mutationList, observer) => {
  133. const pending = observer.takeRecords();
  134. observer.disconnect();
  135. for (const mutation of [...mutationList, ...pending]) {
  136. if (mutation.type === 'childList') {
  137. // mainly for Vue components and AJAX rendered elements
  138. for (const el of mutation.addedNodes) {
  139. if (elementNodeTypes.has(el.nodeType)) {
  140. attachChildrenLazyTooltip(el);
  141. if (el.hasAttribute('data-tooltip-content')) {
  142. attachLazyTooltip(el);
  143. }
  144. }
  145. }
  146. } else if (mutation.type === 'attributes') {
  147. attachTooltip(mutation.target);
  148. }
  149. }
  150. observerConnect(observer);
  151. });
  152. observerConnect(observer);
  153. attachChildrenLazyTooltip(document.documentElement);
  154. }
  155. export function showTemporaryTooltip(target, content) {
  156. // if the target is inside a dropdown, don't show the tooltip because when the dropdown
  157. // closes, the tippy would be pushed unsightly to the top-left of the screen like seen
  158. // on the issue comment menu.
  159. if (target.closest('.ui.dropdown > .menu')) return;
  160. const tippy = target._tippy ?? attachTooltip(target, content);
  161. tippy.setContent(content);
  162. if (!tippy.state.isShown) tippy.show();
  163. tippy.setProps({
  164. onHidden: (tippy) => {
  165. // reset the default tooltip content, if no default, then this temporary tooltip could be destroyed
  166. if (!attachTooltip(target)) {
  167. tippy.destroy();
  168. }
  169. },
  170. });
  171. }