Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eba67fe74f | ||
|
|
690412dc33 | ||
|
|
db9df24786 | ||
|
|
dde1b4f94e |
@@ -1,3 +1,11 @@
|
||||
## v4.1.1
|
||||
|
||||
- Исправление зависаний (в некоторых случаях) всплывающих подсказок
|
||||
|
||||
## v4.1.0
|
||||
|
||||
- Отключаем события mouseenter/mouseleave для toch-устройств
|
||||
|
||||
## v4.0.0
|
||||
|
||||
- Переработана логика инициализации и обновления тултипа
|
||||
|
||||
61
index.js
61
index.js
@@ -8,6 +8,20 @@ const attributeToOption = (attribute) => {
|
||||
return attribute.charAt(0).toLowerCase() + attribute.slice(1);
|
||||
};
|
||||
|
||||
const isTouchDevice = () => 'ontouchstart' in window || navigator.maxTouchPoints > 0;
|
||||
|
||||
let visibilityListenerRegistered = false;
|
||||
const handleVisibilityChange = () => {
|
||||
if (document.hidden) {
|
||||
// Скрываем все активные всплывающие подсказки
|
||||
for (const $tooltip of document.querySelectorAll('.tooltip')) {
|
||||
if ($tooltip._reference?._tooltip?.isVisible) {
|
||||
$tooltip._reference._tooltip.hide({ immediately: true });
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const createTooltip = ($el, content, options) => {
|
||||
options = {
|
||||
animation: [
|
||||
@@ -150,6 +164,10 @@ export const createTooltip = ($el, content, options) => {
|
||||
return;
|
||||
}
|
||||
|
||||
clearTimeout(showTimeout);
|
||||
clearTimeout(hideTimeout);
|
||||
cancelAnimationFrame(rafId);
|
||||
|
||||
$el._tooltip.$tooltip?.remove();
|
||||
|
||||
// Вызываем autoUpdateCleanup только если всплывающая подсказка была видна (иначе вызывать её не имеет смысла)
|
||||
@@ -280,21 +298,16 @@ export const createTooltip = ($el, content, options) => {
|
||||
|
||||
showTimeout = setTimeout(
|
||||
async () => {
|
||||
if (!$el._tooltip.isVisible) {
|
||||
(options.appendTo === 'parent' ? $el.parentElement : options.appendTo).append(
|
||||
$el._tooltip.$tooltip,
|
||||
);
|
||||
$el._tooltip.isVisible = true;
|
||||
$el._tooltip.autoUpdateCleanup = autoUpdate(
|
||||
$el,
|
||||
$el._tooltip.$tooltip,
|
||||
$el._tooltip.updatePosition,
|
||||
);
|
||||
// Проверяем $el._tooltip на сущестование
|
||||
if (!$el._tooltip || $el._tooltip.isVisible) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
options.hideOnClick &&
|
||||
(options.trigger.includes('click') || options.trigger.includes('manual'))
|
||||
) {
|
||||
(options.appendTo === 'parent' ? $el.parentElement : options.appendTo).append($el._tooltip.$tooltip);
|
||||
$el._tooltip.isVisible = true;
|
||||
$el._tooltip.autoUpdateCleanup = autoUpdate($el, $el._tooltip.$tooltip, $el._tooltip.updatePosition);
|
||||
|
||||
if (options.hideOnClick && (options.trigger.includes('click') || options.trigger.includes('manual'))) {
|
||||
document.body.addEventListener('click', $el._tooltip.hideOnClickListener);
|
||||
listeners.push({
|
||||
el: document.body,
|
||||
@@ -317,7 +330,6 @@ export const createTooltip = ($el, content, options) => {
|
||||
if (options.onShown) {
|
||||
options.onShown($el._tooltip);
|
||||
}
|
||||
}
|
||||
},
|
||||
immediately ? 0 : options.delay[0],
|
||||
);
|
||||
@@ -327,7 +339,11 @@ export const createTooltip = ($el, content, options) => {
|
||||
clearTimeout(showTimeout);
|
||||
hideTimeout = setTimeout(
|
||||
async () => {
|
||||
if ($el._tooltip.isVisible) {
|
||||
// Проверяем $el._tooltip на сущестование
|
||||
if (!$el._tooltip || !$el._tooltip.isVisible) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (options.onHide) {
|
||||
options.onHide($el._tooltip);
|
||||
}
|
||||
@@ -339,6 +355,11 @@ export const createTooltip = ($el, content, options) => {
|
||||
}).finished;
|
||||
} catch {} // eslint-disable-line no-empty
|
||||
|
||||
// Ещё одна проверка на сущестование $el._tooltip после await
|
||||
if (!$el._tooltip) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($el._tooltip.$tooltip) {
|
||||
$el._tooltip.$tooltip.remove();
|
||||
}
|
||||
@@ -348,7 +369,6 @@ export const createTooltip = ($el, content, options) => {
|
||||
if (options.onHidden) {
|
||||
options.onHidden($el._tooltip);
|
||||
}
|
||||
}
|
||||
},
|
||||
immediately ? 0 : options.delay[1],
|
||||
);
|
||||
@@ -414,11 +434,13 @@ export const createTooltip = ($el, content, options) => {
|
||||
for (const trigger of options.trigger.split(' ')) {
|
||||
switch (trigger) {
|
||||
case 'mouseenter': {
|
||||
if (!isTouchDevice()) {
|
||||
$el.addEventListener('mouseenter', $el._tooltip.mouseEnterListener);
|
||||
listeners.push({ el: $el, event: 'mouseenter', listener: $el._tooltip.mouseEnterListener });
|
||||
|
||||
$el.addEventListener('mouseleave', $el._tooltip.mouseLeaveListener);
|
||||
listeners.push({ el: $el, event: 'mouseleave', listener: $el._tooltip.mouseLeaveListener });
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'click': {
|
||||
@@ -446,4 +468,9 @@ export const createTooltip = ($el, content, options) => {
|
||||
};
|
||||
|
||||
registerListeners();
|
||||
|
||||
if (!visibilityListenerRegistered) {
|
||||
document.addEventListener('visibilitychange', handleVisibilityChange);
|
||||
visibilityListenerRegistered = true;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@advdominion/tooltip",
|
||||
"version": "4.0.0",
|
||||
"version": "4.1.1",
|
||||
"type": "module",
|
||||
"packageManager": "yarn@4.9.4",
|
||||
"main": "index.js",
|
||||
|
||||
Reference in New Issue
Block a user