Skip to content

Commit ac405c8

Browse files
authored
Merge pull request #30 from svelte-plugins/positioning
🐛 fix(positioning): add support for transform and fix bug with relative
2 parents cc2c9d6 + 577a5ee commit ac405c8

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

docs/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/src/App.svelte

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
<h2>Examples using action</h2>
1818

19-
<div class="example">
19+
<div class="example relative">
2020
<p>This tooltip should appear on the <u title="hello world!" action="click" use:tooltip>top</u> when clicked. The content for the tooltip is provided by the <code>title</code> attribute.</p>
2121

2222
<Prism code={'<u title="hello world!" action="click" use:tooltip>top</u>'} />
2323
</div>
2424

25-
<div class="example">
25+
<div class="example relative">
2626
<p>This tooltip should appear on the <i use:tooltip={{ content: '<b>Tooltip Top</b><p>This is an example of using HTML and content wrapping.</p>', position: 'top', animation: 'slide', arrow: false }}>top</i>.</p>
2727

2828
<Prism code={"<i use:tooltip={{ content: '<b>Tooltip Top</b><p>This is an example of using HTML and content wrapping.</p>', position: 'top', animation: 'slide', arrow: false }}>top</i>"} />
@@ -262,6 +262,10 @@
262262
margin-bottom: 40px;
263263
}
264264
265+
.example.relative {
266+
position: relative;
267+
}
268+
265269
:global(.tooltip.tooltip-theme) {
266270
--tooltip-background-color: hotpink;
267271
--tooltip-box-shadow: 0 1px 8px pink;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@svelte-plugins/tooltips",
3-
"version": "2.1.0",
3+
"version": "2.1.1",
44
"license": "MIT",
55
"description": "A simple tooltip action and component designed for Svelte.",
66
"author": "Kieran Boyle (https://github.com/dysfunc)",

src/helpers.js

+13
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ export const computeTooltipPosition = (containerRef, tooltipRef, position, coord
6969
} else if (elementPosition === 'absolute' || elementPosition === 'relative') {
7070
cumulativeOffsetTop -= parseFloat(computedStyle.top) || 0;
7171
cumulativeOffsetLeft -= parseFloat(computedStyle.left) || 0;
72+
73+
if (elementPosition === 'relative') {
74+
cumulativeOffsetTop -= currentElement.offsetTop;
75+
cumulativeOffsetLeft -= currentElement.offsetLeft;
76+
}
77+
}
78+
79+
const transform = computedStyle.transform;
80+
81+
if (transform && transform !== 'none') {
82+
const transformMatrix = new DOMMatrix(transform);
83+
cumulativeOffsetTop -= transformMatrix.m42;
84+
cumulativeOffsetLeft -= transformMatrix.m41;
7285
}
7386

7487
currentElement = currentElement.parentElement;

0 commit comments

Comments
 (0)