Skip to content

Commit

Permalink
Merge pull request #205 from cosmology-tech/fix/collapse-height-timeline
Browse files Browse the repository at this point in the history
fix(timeline): items container collapses height to zero
  • Loading branch information
yyyyaaa authored Oct 10, 2024
2 parents 238c561 + ce44113 commit 007b918
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/react/stories/marketing/Timeline.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const SecondaryContent = () => {
export const Default: Story = {
render() {
return (
<Box height="4000px" paddingBottom="$24">
<Box paddingBottom="$24">
<Timeline
events={[
{
Expand Down
36 changes: 33 additions & 3 deletions src/ui/timeline/timeline.lite.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Show,
For,
onUpdate,
onMount,
onUnMount,
useMetadata,
Expand All @@ -21,20 +22,47 @@ useMetadata({
});

export default function Timeline(props: TimelineProps) {
const timelineRef = useRef<HTMLDivElement>(null);
let cleanupRef = useRef<() => void>(null);

const state = useStore<{
internalTheme: UIState["theme"];
timelineHeight: number;
updateTimelineHeight: () => void;
}>({
internalTheme: store.getState().theme,
timelineHeight: 0,
updateTimelineHeight: () => {
if (timelineRef) {
const height = timelineRef.scrollHeight;
state.timelineHeight = height;
}
},
});

let cleanupRef = useRef<() => void>(null);

onMount(() => {
cleanupRef = store.subscribe((newState, prevState) => {
const handleResize = () => {
state.updateTimelineHeight();
};

window.addEventListener("resize", handleResize);

const cleanupStore = store.subscribe((newState, prevState) => {
state.internalTheme = newState.theme;
});

state.updateTimelineHeight();

cleanupRef = () => {
window.removeEventListener("resize", handleResize);
cleanupStore();
};
});

onUpdate(() => {
state.updateTimelineHeight();
}, [props.events]);

onUnMount(() => {
if (typeof cleanupRef === "function") {
cleanupRef();
Expand All @@ -43,6 +71,8 @@ export default function Timeline(props: TimelineProps) {

return (
<Box
boxRef={timelineRef}
minHeight={`${state.timelineHeight}px`}
display="flex"
flexDirection="column"
gap="$16"
Expand Down

0 comments on commit 007b918

Please sign in to comment.