Skip to content

Commit 6566baf

Browse files
committed
fix(auto-position): add delay when inverting for animation
1 parent a4a6e85 commit 6566baf

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

src/action-tooltip.svelte

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<script>
22
import { onMount, onDestroy } from 'svelte';
3+
import { formatVariableKey, isInViewport } from './helpers';
4+
import { inverse } from './constants';
35
46
export let content = '';
57
export let align = 'left';
@@ -15,25 +17,9 @@
1517
let minWidth = 0;
1618
let component = null;
1719
18-
const inverse = {
19-
left: 'right',
20-
right: 'left',
21-
top: 'bottom',
22-
bottom: 'top'
23-
};
24-
25-
const isInViewport = () => {
26-
const rect = ref.getBoundingClientRect();
27-
28-
return (
29-
rect.top >= 0 &&
30-
rect.left >= 0 &&
31-
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
32-
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
33-
);
34-
};
35-
3620
onMount(() => {
21+
let delay = 0;
22+
3723
if (ref !== null) {
3824
if (isComponent && !component) {
3925
component = new content.component({ target: ref, props: content.props });
@@ -50,23 +36,20 @@
5036
5137
if (style && typeof style === 'object') {
5238
for (let prop in style) {
39+
const key = formatVariableKey(prop);
5340
const value = style[prop];
54-
const key = prop
55-
.replace(/-_$/g, '')
56-
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
57-
.replace(/([A-Z])([A-Z])(?=[a-z])/g, '$1-$2')
58-
.toLowerCase();
5941
6042
ref.style.setProperty(`--tooltip-${key}`, value);
6143
}
6244
}
6345
}
6446
65-
if (autoPosition && !isInViewport()) {
47+
if (autoPosition && !isInViewport(ref)) {
6648
position = inverse[position];
49+
delay = 200;
6750
}
6851
69-
ref.classList.add('show');
52+
setTimeout(() => ref.classList.add('show'), delay);
7053
});
7154
7255
onDestroy(() => {
@@ -116,11 +99,16 @@
11699
* Tooltip Styling
117100
*--------------------------*/
118101
102+
.tooltip-container {
103+
position: relative;
104+
}
105+
119106
.tooltip {
120107
background-color: var(--tooltip-background-color);
121108
box-shadow: var(--tooltip-box-shadow);
122109
border-radius: var(--tooltip-border-radius);
123110
color: var(--tooltip-color);
111+
opacity: 0;
124112
font-family: var(--tooltip-font-family);
125113
font-size: var(--tooltip-font-size);
126114
font-style: normal;
@@ -129,11 +117,14 @@
129117
padding: var(--tooltip-padding);
130118
position: absolute;
131119
text-align: left;
120+
visibility: hidden;
132121
white-space: nowrap;
133122
z-index: var(--tooltip-z-index);
134123
}
135124
136125
.tooltip.show {
126+
opacity: 1;
127+
visibility: visible;
137128
white-space: normal;
138129
}
139130

src/constants.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const inverse = {
2+
left: 'right',
3+
right: 'left',
4+
top: 'bottom',
5+
bottom: 'top'
6+
};

src/helpers.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const isInViewport = (element) => {
2+
const rect = element.getBoundingClientRect();
3+
4+
return (
5+
rect.top >= 0 &&
6+
rect.left >= 0 &&
7+
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
8+
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
9+
);
10+
};
11+
12+
export const formatVariableKey = (str) => {
13+
return str
14+
.replace(/-_$/g, '')
15+
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
16+
.replace(/([A-Z])([A-Z])(?=[a-z])/g, '$1-$2')
17+
.toLowerCase();
18+
};

src/tooltip.svelte

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<script>
22
import { onMount, onDestroy } from 'svelte';
3+
import { formatVariableKey, isInViewport } from './helpers';
4+
import { inverse } from './constants';
35
46
export let content = '';
57
export let align = 'left';
@@ -15,35 +17,22 @@
1517
let tooltipRef = null;
1618
let minWidth = 0;
1719
let component = null;
18-
19-
const inverse = {
20-
left: 'right',
21-
right: 'left',
22-
top: 'bottom',
23-
bottom: 'top'
24-
};
25-
26-
const isInViewport = () => {
27-
const rect = tooltipRef.getBoundingClientRect();
28-
29-
return (
30-
rect.top >= 0 &&
31-
rect.left >= 0 &&
32-
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
33-
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
34-
);
35-
};
20+
let originalPosition = position;
3621
3722
const onMouseEnter = () => {
38-
if (autoPosition && !isInViewport()) {
23+
let delay = 0;
24+
25+
if (autoPosition && !isInViewport(tooltipRef)) {
3926
position = inverse[position];
27+
delay = 200;
4028
}
4129
42-
tooltipRef.classList.add('show');
30+
setTimeout(() => tooltipRef.classList.add('show'), delay);
4331
};
4432
4533
const onMouseLeave = () => {
4634
tooltipRef.classList.remove('show');
35+
position = originalPosition;
4736
};
4837
4938
onMount(() => {
@@ -71,12 +60,8 @@
7160
7261
if (style && typeof style === 'object') {
7362
for (let prop in style) {
63+
const key = formatVariableKey(prop);
7464
const value = style[prop];
75-
const key = prop
76-
.replace(/-_$/g, '')
77-
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
78-
.replace(/([A-Z])([A-Z])(?=[a-z])/g, '$1-$2')
79-
.toLowerCase();
8065
8166
tooltipRef.style.setProperty(`--tooltip-${key}`, value);
8267
}

0 commit comments

Comments
 (0)