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

Drag line position is incorrect when giving item-container a margin. #404

Closed
ethan-vanderheijden opened this issue Jul 26, 2024 · 1 comment
Assignees

Comments

@ethan-vanderheijden
Copy link

Describe the bug
If you give the item-container element a vertical margin, the drag line position is calculated incorrectly.

To Reproduce
Give item-container a vertical margin like so:

.rct-tree-item-title-container {
    margin-top: 2px;
    margin-bottom: 2px;
}
2024-07-25.21-27-25_edited.mp4

Additional context
The issue is that it is calculating the item container's height incorrectly:

export const computeItemHeight = (treeId: string) => {
const firstItem = getDocument()?.querySelector<HTMLElement>(
`[data-rct-tree="${treeId}"] [data-rct-item-container="true"]`
);
return firstItem?.offsetHeight ?? 5;
};

It uses .offsetHeight, which doesn't take into account margins. The following patch worked for me:

const firstItem = getDocument()?.querySelector(
    `[data-rct-tree="${treeId}"] [data-rct-item-container="true"]`
);
if (firstItem) {
    const style = getComputedStyle(firstItem);
    // note: divide total margin by two
    // when two item containers are adjacent and both have margins, only one margin is rendered
    return firstItem.offsetHeight + (parseFloat(style.marginBottom) + parseFloat(style.marginTop)) / 2;
} else {
    return 5;
}
@lukasbach
Copy link
Owner

Thanks for the report, I've fixed it in the latest version.

I've changed the implementation to just use maximum value of full top and full bottom margin instead of the average of top and bottom margin. Your implementation works fine if top and bottom margin are equal, in which case the new implementation will behave identical. But generally, an DOM element with top margin will have its margin flow into the bottom margin of the preceding element, and for differing margins this behaves inconsistent.

Now, if the bottom margin is larger than the top margin, the top margin is ignored completely since it completely flows into the bottom margin of the preceding item. If the top margin is larger, it extends beyond the bottom margin of the preceding element, and the top margin can thus be ignored.

Again, thank you very much for your report and the sample code, that was very helpful.

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

No branches or pull requests

2 participants