Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong arrow positioning in transformed container #25

Closed
mojabyte opened this issue Jan 1, 2024 · 2 comments · Fixed by #26
Closed

Wrong arrow positioning in transformed container #25

mojabyte opened this issue Jan 1, 2024 · 2 comments · Fixed by #26
Labels
bug Something isn't working

Comments

@mojabyte
Copy link
Contributor

mojabyte commented Jan 1, 2024

When the reference, popper, and arrow elements are descendants of an element with transform style, the arrow positioning will break, while the popper position is correct. It happens only when the container with the transform style is smaller than the reference size + popper size.

To reproduce, use this test that applies a transform: scale(1); style to a parent with no visual changes:

<div id="container">
    <div id="transform">
        <div id="reference"></div>
        <div id="popper"></div>
        <div id="arrow"></div>
    </div>
</div>

<style>
    @import '_reset.css';

    #container {
        display: flex;
        align-items: center;
        justify-content: center;
    }

    #transform {
        transform: scale(1);
        display: flex;
        align-items: center;
        justify-content: center;
    }

    #reference {
        width: 40px;
        height: 40px;
    }

    #popper {
        width: 100px;
        height: 100px;
    }

    #arrow{
        position: fixed;
        width: 10px;
        height: 10px;
        background: black;
        transform: translate(-50%, -50%) rotate(45deg);
    }
</style>

<script type="module">
    import {reposition} from '/dist/nanopop.mjs';

    const reference = document.querySelector('#reference');
    const popper = document.querySelector('#popper');
    const arrow = document.querySelector('#arrow');

    reposition(reference, popper, {
        position: location.hash.slice(1),
        arrow: arrow
    });
</script>

the result would be:

image

@mojabyte
Copy link
Contributor Author

mojabyte commented Jan 1, 2024

The problem is using the refBox[variantKey] to calculate the arrowVariantVal in:

refBox[variantKey] + refBoxCenterOffset : variantVal + variantSize / 2;

refBox position values are calculated from the viewport. Still, when applied to the arrow that is a descendant of an element with the transform style, it is positioned based on that element instead of the viewport (Elements with transforms act as a containing block for fixed position descendants). This is why the arrow is positioned wrong horizontally in the above example.

The wrong vertical position is because of this line:

if (positionVal < refBox[positionKey]) {

that similarly uses refBox[positionKey].

Using matched position and variant keys to infer the arrow position offsets from the popper position will fix the issue. I've proposed a fix in this PR #26.

@simonwep
Copy link
Owner

simonwep commented Jan 6, 2024

Hey! This is awesome (your issue, not the bug haha) - thank you for this elaborate bug-report and description :D I'll comment on your PR!

@simonwep simonwep added the bug Something isn't working label Jan 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants