Skip to content

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

Closed
@ethan-vanderheijden

Description

@ethan-vanderheijden

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;
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions