Skip to content

Commit

Permalink
fix: consider gaps for positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
stampaaaron committed Jun 16, 2023
1 parent 4310e66 commit 380bd57
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/components/Timeline.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
.timeline__items-container {
display: flex;
flex-direction: column;
gap: var(--gap);
flex: 1;
}

Expand All @@ -51,6 +50,10 @@
gap: var(--card-offset);
}

.timeline-item:not(:last-child) {
margin-bottom: var(--gap);
}

.timeline__items-container--left .timeline-item {
flex-direction: row-reverse;
}
Expand Down
13 changes: 10 additions & 3 deletions src/components/Timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,16 @@ export function Timeline(props: TimelineProps) {
let leftHeight = leftContainer.current.offsetTop;
let rightHeight = rightContainer.current.offsetTop;

const defaultMarkerOffset = elements[0].marker?.offsetTop ?? 0;
const [firstElement] = elements;

const defaultMarkerOffset = firstElement.marker?.offsetTop ?? 0;
let nextMarkerOffset = defaultMarkerOffset;

if (!firstElement.item) return;

const { marginBottom } = getComputedStyle(firstElement.item);
const gapHeight = parseFloat(marginBottom);

elements.forEach((refs) => {
const { item, pointer, marker } = refs;
if (!item || !pointer || !marker) return;
Expand All @@ -74,10 +81,10 @@ export function Timeline(props: TimelineProps) {
// defines whether an item should be appended on the left or right side of the timeline
if ((positioning !== 'right' && leftHeight > rightHeight) || positioning === 'left') {
rightContainer.current?.appendChild(item);
rightHeight += item.offsetHeight;
rightHeight += item.offsetHeight + gapHeight;
} else {
leftContainer.current?.appendChild(item);
leftHeight += item.offsetHeight;
leftHeight += item.offsetHeight + gapHeight;
}
});
}
Expand Down

0 comments on commit 380bd57

Please sign in to comment.