-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Use a general approach to show tooltip, fix temporary tooltip bug #23574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
d06b47c
45981f5
b0f056f
3e195a2
462257d
b266575
ba8ad89
690ed36
1175c4f
0544cd3
c083b4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ import tippy from 'tippy.js'; | |
export function createTippy(target, opts = {}) { | ||
const instance = tippy(target, { | ||
appendTo: document.body, | ||
placement: target.getAttribute('data-placement') || 'top-start', | ||
placement: 'top-start', | ||
animation: false, | ||
allowHTML: false, | ||
hideOnClick: false, | ||
|
@@ -25,38 +25,108 @@ export function createTippy(target, opts = {}) { | |
return instance; | ||
} | ||
|
||
export function initTooltip(el, props = {}) { | ||
const content = el.getAttribute('data-content') || props.content; | ||
function getTippyTooltipContent(target) { | ||
// prefer to always use the "[data-tooltip-content]" attribute | ||
// for backward compatibility, we also support the ".tooltip[data-content]" attribute | ||
let content = target.getAttribute('data-tooltip-content'); | ||
if (!content && target.classList.contains('tooltip')) { | ||
content = target.getAttribute('data-content'); | ||
} | ||
return content; | ||
} | ||
|
||
/** | ||
* Attach a tippy tooltip to the given target element. | ||
* If the target element already has a tippy tooltip attached, the tooltip will be updated with the new content. | ||
* If the target element has no content, then no tooltip will be attached, and it returns null. | ||
* @param target {HTMLElement} | ||
* @param content {null|string} | ||
* @returns {null|tippy} | ||
*/ | ||
function attachTippyTooltip(target, content = null) { | ||
content = content ?? getTippyTooltipContent(target); | ||
if (!content) return null; | ||
if (!el.hasAttribute('aria-label')) el.setAttribute('aria-label', content); | ||
return createTippy(el, { | ||
|
||
const props = { | ||
content, | ||
delay: 100, | ||
role: 'tooltip', | ||
...(el.getAttribute('data-tooltip-interactive') === 'true' ? {interactive: true} : {}), | ||
...props, | ||
}); | ||
} | ||
placement: target.getAttribute('data-tooltip-placement') || 'top-start', | ||
...(target.getAttribute('data-tooltip-interactive') === 'true' ? {interactive: true} : {}), | ||
}; | ||
|
||
export function showTemporaryTooltip(target, content) { | ||
let tippy, oldContent; | ||
if (target._tippy) { | ||
tippy = target._tippy; | ||
oldContent = tippy.props.content; | ||
if (!target._tippy) { | ||
createTippy(target, props); | ||
} else { | ||
tippy = initTooltip(target, {content}); | ||
target._tippy.setProps(props); | ||
} | ||
return target._tippy; | ||
} | ||
|
||
/** | ||
* creating tippy instance is expensive, so we only create it when the user hovers over the element | ||
* @param e {Event} | ||
*/ | ||
function lazyTippyOnMouseEnter(e) { | ||
e.target.removeEventListener('mouseenter', lazyTippyOnMouseEnter, true); | ||
attachTippyTooltip(this); | ||
} | ||
|
||
/** | ||
* Activate the tippy tooltip for all children elements | ||
* And if the element has no aria-label, use the tooltip content as aria-label | ||
* @param target {HTMLElement} | ||
*/ | ||
function attachChildrenLazyTippyTooltip(target) { | ||
// the selector must match the logic in getTippyTooltipContent | ||
for (const el of target.querySelectorAll('[data-tooltip-content], .tooltip[data-content]')) { | ||
el.addEventListener('mouseenter', lazyTippyOnMouseEnter, true); | ||
|
||
// meanwhile, if the element has no aria-label, use the tooltip content as aria-label | ||
if (!el.hasAttribute('aria-label')) { | ||
const content = getTippyTooltipContent(el); | ||
if (content) { | ||
el.setAttribute('aria-label', content); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export function initGlobalTooltips() { | ||
// use MutationObserver to detect new elements added to the DOM, or attributes changed | ||
const observer = new MutationObserver((mutationList) => { | ||
for (const mutation of mutationList) { | ||
if (mutation.type === 'childList') { | ||
for (const el of mutation.addedNodes) { | ||
// handle all "tooltip" elements in newly added nodes, skip non-related nodes (eg: "#text") | ||
if (el.querySelectorAll) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line looks broken. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not broken, just a quick check to see the "node" has Otherwise some nodes (like I agree that there would be other approaches to check if the node is non-#text, at the moment, this quick check is clear and fast IMO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use node.nodeType please, it's much more clear. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you provide a list for the types? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But that's slower than the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, then There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
attachChildrenLazyTippyTooltip(el); | ||
} | ||
} | ||
} else if (mutation.type === 'attributes') { | ||
// sync the tooltip content if the attributes change | ||
attachTippyTooltip(mutation.target); | ||
} | ||
} | ||
}); | ||
observer.observe(document, { | ||
subtree: true, | ||
childList: true, | ||
attributeFilter: ['data-tooltip-content', 'data-content'], | ||
}); | ||
|
||
attachChildrenLazyTippyTooltip(document.documentElement); | ||
} | ||
|
||
export function showTemporaryTooltip(target, content) { | ||
const tippy = target._tippy ?? attachTippyTooltip(target, content); | ||
tippy.setContent(content); | ||
if (!tippy.state.isShown) tippy.show(); | ||
tippy.setProps({ | ||
onHidden: (tippy) => { | ||
if (oldContent) { | ||
tippy.setContent(oldContent); | ||
tippy.setProps({onHidden: undefined}); | ||
} else { | ||
// reset the default tooltip content, if no default, then this temporary tooltip could be destroyed | ||
if (!attachTippyTooltip(target)) { | ||
tippy.destroy(); | ||
// after destroy, the `_tippy` is detached, it can't do "setProps (etc...)" anymore | ||
} | ||
}, | ||
}); | ||
|
Uh oh!
There was an error while loading. Please reload this page.