@@ -24,26 +24,19 @@ export function createTippy(target, opts = {}) {
24
24
return instance ;
25
25
}
26
26
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
-
37
27
/**
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.
40
30
* 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
+ *
41
34
* @param target {HTMLElement}
42
35
* @param content {null|string}
43
36
* @returns {null|tippy }
44
37
*/
45
- function attachTippyTooltip ( target , content = null ) {
46
- content = content ?? getTippyTooltipContent ( target ) ;
38
+ function attachTooltip ( target , content = null ) {
39
+ content = content ?? getTooltipContent ( target ) ;
47
40
if ( ! content ) return null ;
48
41
49
42
const props = {
@@ -63,27 +56,38 @@ function attachTippyTooltip(target, content = null) {
63
56
}
64
57
65
58
/**
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
67
60
* @param e {Event}
68
61
*/
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 ;
72
76
}
73
77
74
78
/**
75
- * Activate the tippy tooltip for all children elements
79
+ * Activate the tooltip for all children elements
76
80
* And if the element has no aria-label, use the tooltip content as aria-label
77
81
* @param target {HTMLElement}
78
82
*/
79
- function attachChildrenLazyTippyTooltip ( target ) {
83
+ function attachChildrenLazyTooltip ( target ) {
80
84
// the selector must match the logic in getTippyTooltipContent
81
85
for ( const el of target . querySelectorAll ( '[data-tooltip-content], .tooltip[data-content]' ) ) {
82
- el . addEventListener ( 'mouseenter' , lazyTippyOnMouseEnter , true ) ;
86
+ el . addEventListener ( 'mouseenter' , lazyTooltipOnMouseEnter , true ) ;
83
87
84
88
// meanwhile, if the element has no aria-label, use the tooltip content as aria-label
85
89
if ( ! el . hasAttribute ( 'aria-label' ) ) {
86
- const content = getTippyTooltipContent ( el ) ;
90
+ const content = getTooltipContent ( el ) ;
87
91
if ( content ) {
88
92
el . setAttribute ( 'aria-label' , content ) ;
89
93
}
@@ -97,14 +101,14 @@ export function initGlobalTooltips() {
97
101
for ( const mutation of mutationList ) {
98
102
if ( mutation . type === 'childList' ) {
99
103
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")
101
105
if ( el . querySelectorAll ) {
102
- attachChildrenLazyTippyTooltip ( el ) ;
106
+ attachChildrenLazyTooltip ( el ) ;
103
107
}
104
108
}
105
109
} else if ( mutation . type === 'attributes' ) {
106
110
// sync the tooltip content if the attributes change
107
- attachTippyTooltip ( mutation . target ) ;
111
+ attachTooltip ( mutation . target ) ;
108
112
}
109
113
}
110
114
} ) ;
@@ -114,17 +118,17 @@ export function initGlobalTooltips() {
114
118
attributeFilter : [ 'data-tooltip-content' , 'data-content' ] ,
115
119
} ) ;
116
120
117
- attachChildrenLazyTippyTooltip ( document . documentElement ) ;
121
+ attachChildrenLazyTooltip ( document . documentElement ) ;
118
122
}
119
123
120
124
export function showTemporaryTooltip ( target , content ) {
121
- const tippy = target . _tippy ?? attachTippyTooltip ( target , content ) ;
125
+ const tippy = target . _tippy ?? attachTooltip ( target , content ) ;
122
126
tippy . setContent ( content ) ;
123
127
if ( ! tippy . state . isShown ) tippy . show ( ) ;
124
128
tippy . setProps ( {
125
129
onHidden : ( tippy ) => {
126
130
// reset the default tooltip content, if no default, then this temporary tooltip could be destroyed
127
- if ( ! attachTippyTooltip ( target ) ) {
131
+ if ( ! attachTooltip ( target ) ) {
128
132
tippy . destroy ( ) ;
129
133
}
130
134
} ,
0 commit comments