Skip to content

Commit

Permalink
Merge pull request #30 from svelte-plugins/positioning
Browse files Browse the repository at this point in the history
🐛 fix(positioning): add support for transform and fix bug with relative
  • Loading branch information
dysfunc authored Dec 18, 2023
2 parents cc2c9d6 + 577a5ee commit ac405c8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions docs/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

<h2>Examples using action</h2>

<div class="example">
<div class="example relative">
<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>

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

<div class="example">
<div class="example relative">
<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>

<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>"} />
Expand Down Expand Up @@ -262,6 +262,10 @@
margin-bottom: 40px;
}
.example.relative {
position: relative;
}
:global(.tooltip.tooltip-theme) {
--tooltip-background-color: hotpink;
--tooltip-box-shadow: 0 1px 8px pink;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@svelte-plugins/tooltips",
"version": "2.1.0",
"version": "2.1.1",
"license": "MIT",
"description": "A simple tooltip action and component designed for Svelte.",
"author": "Kieran Boyle (https://github.com/dysfunc)",
Expand Down
13 changes: 13 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ export const computeTooltipPosition = (containerRef, tooltipRef, position, coord
} else if (elementPosition === 'absolute' || elementPosition === 'relative') {
cumulativeOffsetTop -= parseFloat(computedStyle.top) || 0;
cumulativeOffsetLeft -= parseFloat(computedStyle.left) || 0;

if (elementPosition === 'relative') {
cumulativeOffsetTop -= currentElement.offsetTop;
cumulativeOffsetLeft -= currentElement.offsetLeft;
}
}

const transform = computedStyle.transform;

if (transform && transform !== 'none') {
const transformMatrix = new DOMMatrix(transform);
cumulativeOffsetTop -= transformMatrix.m42;
cumulativeOffsetLeft -= transformMatrix.m41;
}

currentElement = currentElement.parentElement;
Expand Down

0 comments on commit ac405c8

Please sign in to comment.