Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
49fe8cb238
|
|||
|
04e29c7987
|
|||
|
f1e8f86ce9
|
@@ -1,3 +1,8 @@
|
|||||||
|
## v7.0.0
|
||||||
|
|
||||||
|
- Изменено значение по умолчанию для параметра `trigger`
|
||||||
|
- Рефакторинг кода и устранение ошибок
|
||||||
|
|
||||||
## v6.0.0
|
## v6.0.0
|
||||||
|
|
||||||
- Корректная работа сообытий mouseenter/mouseleave на тач-устройствах
|
- Корректная работа сообытий mouseenter/mouseleave на тач-устройствах
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ createTooltip(document.querySelector('button'), 'Подсказка', {
|
|||||||
placement: 'top',
|
placement: 'top',
|
||||||
shiftPadding: [8, 0],
|
shiftPadding: [8, 0],
|
||||||
theme: 'light',
|
theme: 'light',
|
||||||
trigger: 'mouseenter',
|
trigger: 'mouseenter click',
|
||||||
virtualReference: undefined,
|
virtualReference: undefined,
|
||||||
zIndex: '',
|
zIndex: '',
|
||||||
// Callback-функции, по умолчанию не заданы
|
// Callback-функции, по умолчанию не заданы
|
||||||
|
|||||||
77
index.js
77
index.js
@@ -25,6 +25,15 @@ const handleVisibilityChange = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const templates = {
|
||||||
|
arrow: () => `
|
||||||
|
<div class="tooltip__arrow" style="pointer-events: none; position: absolute; transform: rotate(45deg);"></div>
|
||||||
|
`,
|
||||||
|
interactiveHelper: () => `
|
||||||
|
<div class="tooltip__interactive-helper" style="position: absolute; z-index: -1;"></div>
|
||||||
|
`,
|
||||||
|
};
|
||||||
|
|
||||||
export const createTooltip = ($el, content, options) => {
|
export const createTooltip = ($el, content, options) => {
|
||||||
options = {
|
options = {
|
||||||
animation: [
|
animation: [
|
||||||
@@ -42,7 +51,7 @@ export const createTooltip = ($el, content, options) => {
|
|||||||
placement: 'top',
|
placement: 'top',
|
||||||
shiftPadding: [8, 0],
|
shiftPadding: [8, 0],
|
||||||
theme: 'light',
|
theme: 'light',
|
||||||
trigger: 'mouseenter',
|
trigger: 'mouseenter click',
|
||||||
virtualReference: undefined,
|
virtualReference: undefined,
|
||||||
zIndex: '',
|
zIndex: '',
|
||||||
...options,
|
...options,
|
||||||
@@ -70,7 +79,9 @@ export const createTooltip = ($el, content, options) => {
|
|||||||
$interactive: undefined,
|
$interactive: undefined,
|
||||||
_showTimeout: undefined,
|
_showTimeout: undefined,
|
||||||
_hideTimeout: undefined,
|
_hideTimeout: undefined,
|
||||||
|
_currentAnimation: undefined,
|
||||||
autoUpdateCleanup: () => {},
|
autoUpdateCleanup: () => {},
|
||||||
|
updatePosition: async () => {},
|
||||||
|
|
||||||
setContent(updatedContent) {
|
setContent(updatedContent) {
|
||||||
if (updatedContent !== undefined) {
|
if (updatedContent !== undefined) {
|
||||||
@@ -94,15 +105,7 @@ export const createTooltip = ($el, content, options) => {
|
|||||||
if (updatedOptions.arrow !== undefined && $el._tooltip.$tooltip) {
|
if (updatedOptions.arrow !== undefined && $el._tooltip.$tooltip) {
|
||||||
if (options.arrow) {
|
if (options.arrow) {
|
||||||
if (!$el._tooltip.$arrow) {
|
if (!$el._tooltip.$arrow) {
|
||||||
$el._tooltip.$tooltip.insertAdjacentHTML(
|
$el._tooltip.$tooltip.querySelector('.tooltip__root').insertAdjacentHTML('afterbegin', templates.arrow());
|
||||||
'afterbegin',
|
|
||||||
`
|
|
||||||
<div
|
|
||||||
class="tooltip__arrow"
|
|
||||||
style="pointer-events: none; position: absolute; transform: rotate(45deg);">
|
|
||||||
</div>
|
|
||||||
`,
|
|
||||||
);
|
|
||||||
$el._tooltip.$arrow = $el._tooltip.$tooltip.querySelector('.tooltip__arrow');
|
$el._tooltip.$arrow = $el._tooltip.$tooltip.querySelector('.tooltip__arrow');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -120,15 +123,7 @@ export const createTooltip = ($el, content, options) => {
|
|||||||
if ($el._tooltip.$tooltip) {
|
if ($el._tooltip.$tooltip) {
|
||||||
if (options.interactive) {
|
if (options.interactive) {
|
||||||
if (!$el._tooltip.$interactive) {
|
if (!$el._tooltip.$interactive) {
|
||||||
$el._tooltip.$tooltip.insertAdjacentHTML(
|
$el._tooltip.$tooltip.querySelector('.tooltip__root').insertAdjacentHTML('afterbegin', templates.interactiveHelper());
|
||||||
'afterbegin',
|
|
||||||
`
|
|
||||||
<div
|
|
||||||
class="tooltip__interactive-helper"
|
|
||||||
style="position: absolute; z-index: -1;">
|
|
||||||
</div>
|
|
||||||
`,
|
|
||||||
);
|
|
||||||
$el._tooltip.$interactive =
|
$el._tooltip.$interactive =
|
||||||
$el._tooltip.$tooltip.querySelector('.tooltip__interactive-helper');
|
$el._tooltip.$tooltip.querySelector('.tooltip__interactive-helper');
|
||||||
}
|
}
|
||||||
@@ -169,6 +164,7 @@ export const createTooltip = ($el, content, options) => {
|
|||||||
|
|
||||||
clearTimeout($el._tooltip._showTimeout);
|
clearTimeout($el._tooltip._showTimeout);
|
||||||
clearTimeout($el._tooltip._hideTimeout);
|
clearTimeout($el._tooltip._hideTimeout);
|
||||||
|
$el._tooltip._currentAnimation?.cancel();
|
||||||
cancelAnimationFrame(rafId);
|
cancelAnimationFrame(rafId);
|
||||||
|
|
||||||
$el._tooltip.$tooltip?.remove();
|
$el._tooltip.$tooltip?.remove();
|
||||||
@@ -183,6 +179,8 @@ export const createTooltip = ($el, content, options) => {
|
|||||||
}
|
}
|
||||||
listeners.length = 0;
|
listeners.length = 0;
|
||||||
|
|
||||||
|
document.body.removeEventListener('click', $el._tooltip.hideOnClickListener);
|
||||||
|
|
||||||
delete $el._tooltip;
|
delete $el._tooltip;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -204,26 +202,8 @@ export const createTooltip = ($el, content, options) => {
|
|||||||
|
|
||||||
$el._tooltip.$tooltip.innerHTML = `
|
$el._tooltip.$tooltip.innerHTML = `
|
||||||
<div class="tooltip__root">
|
<div class="tooltip__root">
|
||||||
${
|
${options.interactive ? templates.interactiveHelper() : ''}
|
||||||
options.interactive
|
${options.arrow ? templates.arrow() : ''}
|
||||||
? `
|
|
||||||
<div
|
|
||||||
class="tooltip__interactive-helper"
|
|
||||||
style="position: absolute; z-index: -1;">
|
|
||||||
</div>
|
|
||||||
`
|
|
||||||
: ''
|
|
||||||
}
|
|
||||||
${
|
|
||||||
options.arrow
|
|
||||||
? `
|
|
||||||
<div
|
|
||||||
class="tooltip__arrow"
|
|
||||||
style="pointer-events: none; position: absolute; transform: rotate(45deg);">
|
|
||||||
</div>
|
|
||||||
`
|
|
||||||
: ''
|
|
||||||
}
|
|
||||||
<div class="tooltip__container"></div>
|
<div class="tooltip__container"></div>
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
@@ -332,10 +312,13 @@ export const createTooltip = ($el, content, options) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await $el._tooltip.$tooltip.querySelector('.tooltip__root').animate(options.animation[0], {
|
$el._tooltip._currentAnimation?.cancel();
|
||||||
|
const animation = $el._tooltip.$tooltip.querySelector('.tooltip__root').animate(options.animation[0], {
|
||||||
duration: immediately ? 0 : options.duration[0],
|
duration: immediately ? 0 : options.duration[0],
|
||||||
easing: options.easing[0],
|
easing: options.easing[0],
|
||||||
}).finished;
|
});
|
||||||
|
$el._tooltip._currentAnimation = animation;
|
||||||
|
await animation.finished;
|
||||||
} catch {} // eslint-disable-line no-empty
|
} catch {} // eslint-disable-line no-empty
|
||||||
|
|
||||||
if (options.onShown) {
|
if (options.onShown) {
|
||||||
@@ -359,11 +342,16 @@ export const createTooltip = ($el, content, options) => {
|
|||||||
options.onHide($el._tooltip);
|
options.onHide($el._tooltip);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
document.body.removeEventListener('click', $el._tooltip.hideOnClickListener);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await $el._tooltip.$tooltip.querySelector('.tooltip__root').animate(options.animation[1], {
|
$el._tooltip._currentAnimation?.cancel();
|
||||||
|
const animation = $el._tooltip.$tooltip.querySelector('.tooltip__root').animate(options.animation[1], {
|
||||||
duration: immediately ? 0 : options.duration[1],
|
duration: immediately ? 0 : options.duration[1],
|
||||||
easing: options.easing[1],
|
easing: options.easing[1],
|
||||||
}).finished;
|
});
|
||||||
|
$el._tooltip._currentAnimation = animation;
|
||||||
|
await animation.finished;
|
||||||
} catch {} // eslint-disable-line no-empty
|
} catch {} // eslint-disable-line no-empty
|
||||||
|
|
||||||
// Ещё одна проверка на сущестование $el._tooltip после await
|
// Ещё одна проверка на сущестование $el._tooltip после await
|
||||||
@@ -392,17 +380,14 @@ export const createTooltip = ($el, content, options) => {
|
|||||||
|
|
||||||
if (options.hideOnClick === 'all') {
|
if (options.hideOnClick === 'all') {
|
||||||
$el._tooltip.hide();
|
$el._tooltip.hide();
|
||||||
document.body.removeEventListener('click', $el._tooltip.hideOnClickListener);
|
|
||||||
} else if (options.hideOnClick === 'toggle') {
|
} else if (options.hideOnClick === 'toggle') {
|
||||||
if ($el.contains(target)) {
|
if ($el.contains(target)) {
|
||||||
$el._tooltip.hide();
|
$el._tooltip.hide();
|
||||||
document.body.removeEventListener('click', $el._tooltip.hideOnClickListener);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// options.hideOnClick === true
|
// options.hideOnClick === true
|
||||||
if ($el.contains(target) || !$el._tooltip.$tooltip.contains(target)) {
|
if ($el.contains(target) || !$el._tooltip.$tooltip.contains(target)) {
|
||||||
$el._tooltip.hide();
|
$el._tooltip.hide();
|
||||||
document.body.removeEventListener('click', $el._tooltip.hideOnClickListener);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@advdominion/tooltip",
|
"name": "@advdominion/tooltip",
|
||||||
"version": "6.0.0",
|
"version": "7.0.0",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"packageManager": "yarn@4.9.4",
|
"packageManager": "yarn@4.9.4",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
|
|||||||
Reference in New Issue
Block a user