Skip to content

Fix floating button offsetted wrong in position: absolute parent#231

Merged
tiagozip merged 1 commit intotiagozip:mainfrom
NightFurySL2001:patch-2
Apr 22, 2026
Merged

Fix floating button offsetted wrong in position: absolute parent#231
tiagozip merged 1 commit intotiagozip:mainfrom
NightFurySL2001:patch-2

Conversation

@NightFurySL2001
Copy link
Copy Markdown
Contributor

Offset is wrong when the button is placed inside a parent with position: absolute.
image

After fix:
image
image

Recreate:

<style>
    .offsetted {
        position: absolute;
        top: 100;
        left: 200;
    }
</style>

<form class="offsetted">
    <div>
        <cap-widget id="floating" required onsolve="console.log(`token: ${event.detail.token}`)"
            data-cap-api-endpoint="/wp-json/maoken-social/v1/cap/"></cap-widget>

        <button data-cap-floating="#floating" data-cap-floating-position="bottom">
            Trigger floating mode
        </button>
    </div>
</form>

<script src="https://cdn.jsdelivr.net/npm/@cap.js/widget"></script>
<script src="https://cdn.jsdelivr.net/npm/@cap.js/widget/cap-floating.min.js"></script> 

Copy link
Copy Markdown
Owner

@tiagozip tiagozip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, thanks!

@tiagozip tiagozip merged commit 0d8122b into tiagozip:main Apr 22, 2026
2 checks passed
@NightFurySL2001
Copy link
Copy Markdown
Contributor Author

Btw just noticed the comment is wrong:

    // `position: absolute` is resolved against the nearest positioned ancestor (capWidget.offsetParent),
    // but getBoundingClientRect() returns viewport-relative coords, convert to container-relative coords.

should be

    // `element.offsetParent()` is resolved against the nearest `position: absolute` ancestor,
    // but `getBoundingClientRect()` returns viewport-relative coords, convert to container-relative coords.

@NightFurySL2001 NightFurySL2001 deleted the patch-2 branch April 22, 2026 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants