Skip to content

Commit

Permalink
fix: remove tip container when destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
zzxming committed Dec 31, 2024
1 parent f4ac845 commit 2062615
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/utils/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,8 @@ export const createTooltip = (target: HTMLElement, options: Partial<TooltipOptio
className = ensureArray(className.split(' '));
}
if (msg || content || onShow) {
if (!tooltipContainer) {
tooltipContainer = document.createElement('div');
document.body.appendChild(tooltipContainer);
}
const tooltip = document.createElement('div');
tooltip.classList.add('toolbar-tip__tooltip', 'hidden', 'transparent', ...className);
tooltipContainer.appendChild(tooltip);

const setTooltipContent = () => {
if (content) {
Expand Down Expand Up @@ -89,9 +84,7 @@ export const createTooltip = (target: HTMLElement, options: Partial<TooltipOptio
};
const transitionendHandler = () => {
tooltip.classList.add('hidden');
if (tooltipContainer.contains(tooltip)) {
tooltipContainer.removeChild(tooltip);
}
tooltip.remove();
if (cleanup) cleanup();
};
function show() {
Expand All @@ -101,6 +94,10 @@ export const createTooltip = (target: HTMLElement, options: Partial<TooltipOptio
const setContentResult = setTooltipContent();
if (!setContentResult) return;

if (!tooltipContainer) {
tooltipContainer = document.createElement('div');
document.body.appendChild(tooltipContainer);
}
tooltipContainer.appendChild(tooltip);
tooltip.removeEventListener('transitionend', transitionendHandler);
tooltip.classList.remove('hidden');
Expand Down Expand Up @@ -132,9 +129,11 @@ export const createTooltip = (target: HTMLElement, options: Partial<TooltipOptio
}
if (cleanup) cleanup();
tooltip.remove();
if (tooltipContainer.children.length <= 0) {
tooltipContainer.remove();
}
};

hide();
return {
instance: tooltip,
destroy,
Expand Down

0 comments on commit 2062615

Please sign in to comment.