Skip to content

Commit ee68324

Browse files
committed
feat: item container margin support (#404)
1 parent b4742d4 commit ee68324

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

next-release-notes.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<!--
21
### Breaking Changes
32

43
### Features
54

65
### Bug Fixes and Improvements
7-
- Fix problem with expandSubsequently resolving before expanding all items
6+
- Fix problem with expandSubsequently resolving before expanding all items (#403)
7+
- Improve behavior for cases where item containers have margin (#404)
8+
89
### Other Changes
9-
-->

packages/core/src/controlledEnvironment/layoutUtils.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ export const computeItemHeight = (treeId: string) => {
44
const firstItem = getDocument()?.querySelector<HTMLElement>(
55
`[data-rct-tree="${treeId}"] [data-rct-item-container="true"]`
66
);
7-
return firstItem?.offsetHeight ?? 5;
7+
if (firstItem) {
8+
const style = getComputedStyle(firstItem);
9+
// top margin flows into the bottom margin of the previous item, so ignore it
10+
return firstItem.offsetHeight + parseFloat(style.marginBottom);
11+
}
12+
return 5;
813
};
914

1015
export const isOutsideOfContainer = (e: DragEvent, treeBb: DOMRect) =>

packages/core/src/stories/BasicExamples.stories.tsx

+34
Original file line numberDiff line numberDiff line change
@@ -669,3 +669,37 @@ export const RightToLeftRenderers = () => (
669669
<Tree treeId="tree-1" rootItem="root" treeLabel="Tree Example" />
670670
</UncontrolledTreeEnvironment>
671671
);
672+
673+
export const ItemContainersWithMargin = () => (
674+
<UncontrolledTreeEnvironment<string>
675+
canDragAndDrop
676+
canDropOnFolder
677+
canReorderItems
678+
dataProvider={
679+
new StaticTreeDataProvider(longTree.items, (item, data) => ({
680+
...item,
681+
data,
682+
}))
683+
}
684+
getItemTitle={item => item.data}
685+
viewState={{
686+
'tree-1': {
687+
expandedItems: [
688+
'Fruit',
689+
'Meals',
690+
'America',
691+
'Europe',
692+
'Asia',
693+
'Desserts',
694+
],
695+
},
696+
}}
697+
>
698+
<style>
699+
{
700+
'.rct-tree-item-title-container { margin-top: 10px; margin-bottom: 20px;}'
701+
}
702+
</style>
703+
<Tree treeId="tree-1" rootItem="root" treeLabel="Tree Example" />
704+
</UncontrolledTreeEnvironment>
705+
);

0 commit comments

Comments
 (0)