@@ -24,26 +24,19 @@ export function createTippy(target, opts = {}) {
2424 return instance ;
2525}
2626
27- function getTippyTooltipContent ( target ) {
28- // prefer to always use the "[data-tooltip-content]" attribute
29- // for backward compatibility, we also support the ".tooltip[data-content]" attribute
30- let content = target . getAttribute ( 'data-tooltip-content' ) ;
31- if ( ! content && target . classList . contains ( 'tooltip' ) ) {
32- content = target . getAttribute ( 'data-content' ) ;
33- }
34- return content ;
35- }
36-
3727/**
38- * Attach a tippy tooltip to the given target element.
39- * If the target element already has a tippy tooltip attached, the tooltip will be updated with the new content.
28+ * Attach a tooltip tippy to the given target element.
29+ * If the target element already has a tooltip tippy attached, the tooltip will be updated with the new content.
4030 * If the target element has no content, then no tooltip will be attached, and it returns null.
31+ *
32+ * Note: "tooltip" doesn't equal to "tippy". "tooltip" means a auto-popup content, it just uses tippy as the implementation.
33+ *
4134 * @param target {HTMLElement}
4235 * @param content {null|string}
4336 * @returns {null|tippy }
4437 */
45- function attachTippyTooltip ( target , content = null ) {
46- content = content ?? getTippyTooltipContent ( target ) ;
38+ function attachTooltip ( target , content = null ) {
39+ content = content ?? getTooltipContent ( target ) ;
4740 if ( ! content ) return null ;
4841
4942 const props = {
@@ -63,27 +56,38 @@ function attachTippyTooltip(target, content = null) {
6356}
6457
6558/**
66- * creating tippy instance is expensive, so we only create it when the user hovers over the element
59+ * creating tooltip tippy instance is expensive, so we only create it when the user hovers over the element
6760 * @param e {Event}
6861 */
69- function lazyTippyOnMouseEnter ( e ) {
70- e . target . removeEventListener ( 'mouseenter' , lazyTippyOnMouseEnter , true ) ;
71- attachTippyTooltip ( this ) ;
62+ function lazyTooltipOnMouseEnter ( e ) {
63+ e . target . removeEventListener ( 'mouseenter' , lazyTooltipOnMouseEnter , true ) ;
64+ attachTooltip ( this ) ;
65+ }
66+
67+ function getTooltipContent ( target ) {
68+ // prefer to always use the "[data-tooltip-content]" attribute
69+ // for backward compatibility, we also support the ".tooltip[data-content]" attribute
70+ // in next PR, refactor all the ".tooltip[data-content]" to "[data-tooltip-content]"
71+ let content = target . getAttribute ( 'data-tooltip-content' ) ;
72+ if ( ! content && target . classList . contains ( 'tooltip' ) ) {
73+ content = target . getAttribute ( 'data-content' ) ;
74+ }
75+ return content ;
7276}
7377
7478/**
75- * Activate the tippy tooltip for all children elements
79+ * Activate the tooltip for all children elements
7680 * And if the element has no aria-label, use the tooltip content as aria-label
7781 * @param target {HTMLElement}
7882 */
79- function attachChildrenLazyTippyTooltip ( target ) {
83+ function attachChildrenLazyTooltip ( target ) {
8084 // the selector must match the logic in getTippyTooltipContent
8185 for ( const el of target . querySelectorAll ( '[data-tooltip-content], .tooltip[data-content]' ) ) {
82- el . addEventListener ( 'mouseenter' , lazyTippyOnMouseEnter , true ) ;
86+ el . addEventListener ( 'mouseenter' , lazyTooltipOnMouseEnter , true ) ;
8387
8488 // meanwhile, if the element has no aria-label, use the tooltip content as aria-label
8589 if ( ! el . hasAttribute ( 'aria-label' ) ) {
86- const content = getTippyTooltipContent ( el ) ;
90+ const content = getTooltipContent ( el ) ;
8791 if ( content ) {
8892 el . setAttribute ( 'aria-label' , content ) ;
8993 }
@@ -97,14 +101,14 @@ export function initGlobalTooltips() {
97101 for ( const mutation of mutationList ) {
98102 if ( mutation . type === 'childList' ) {
99103 for ( const el of mutation . addedNodes ) {
100- // handle all "tooltip" elements in newly added nodes, skip non-related nodes (eg: "#text")
104+ // handle all "tooltip" elements in added nodes which have 'querySelectorAll' method , skip non-related nodes (eg: "#text")
101105 if ( el . querySelectorAll ) {
102- attachChildrenLazyTippyTooltip ( el ) ;
106+ attachChildrenLazyTooltip ( el ) ;
103107 }
104108 }
105109 } else if ( mutation . type === 'attributes' ) {
106110 // sync the tooltip content if the attributes change
107- attachTippyTooltip ( mutation . target ) ;
111+ attachTooltip ( mutation . target ) ;
108112 }
109113 }
110114 } ) ;
@@ -114,17 +118,17 @@ export function initGlobalTooltips() {
114118 attributeFilter : [ 'data-tooltip-content' , 'data-content' ] ,
115119 } ) ;
116120
117- attachChildrenLazyTippyTooltip ( document . documentElement ) ;
121+ attachChildrenLazyTooltip ( document . documentElement ) ;
118122}
119123
120124export function showTemporaryTooltip ( target , content ) {
121- const tippy = target . _tippy ?? attachTippyTooltip ( target , content ) ;
125+ const tippy = target . _tippy ?? attachTooltip ( target , content ) ;
122126 tippy . setContent ( content ) ;
123127 if ( ! tippy . state . isShown ) tippy . show ( ) ;
124128 tippy . setProps ( {
125129 onHidden : ( tippy ) => {
126130 // reset the default tooltip content, if no default, then this temporary tooltip could be destroyed
127- if ( ! attachTippyTooltip ( target ) ) {
131+ if ( ! attachTooltip ( target ) ) {
128132 tippy . destroy ( ) ;
129133 }
130134 } ,
0 commit comments