Skip to content

Commit

Permalink
fix: make bar graph height dynamic based on viewport height (#886)
Browse files Browse the repository at this point in the history
Signed-off-by: Raphael Arce <[email protected]>
  • Loading branch information
raphael-arce authored May 2, 2024
1 parent 05b342b commit 4e4e5ba
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/components/filter/age-range-slider/bar-graph.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useMemo } from "react";
import { BarItem } from "./bar-item";
import { getMaxCount, getTreesGroupByAge } from "./tree-age-grouped";
import { useBarGraphHeight } from "./hooks/use-bar-graph-height.tsx";

const TREES_GROUPED_BY_AGE = getTreesGroupByAge();
const MAX_COUNT = getMaxCount();
Expand All @@ -25,14 +26,18 @@ export const BarGraph: React.FC<BarGraphProps> = ({ min, max }) => {

const yAxisLabelHeight = calculateBarPercentage(100000, MAX_COUNT);

const { height, translateYAxisIndicator } = useBarGraphHeight();

return (
<div className="w-full h-[90px] relative">
<div className="w-full relative" style={{ height: height }}>
<div className="flex flex-row gap-0.5 w-full h-full">
<div
className="w-full border border-[#DDDDDD] z-0 absolute"
style={{ bottom: yAxisLabelHeight }}
></div>
<span className="text-[#DDDDDD] -translate-y-0.5 font-semibold absolute right-0">
<span
className={`${translateYAxisIndicator} text-[#DDDDDD] font-semibold absolute right-0`}
>
100k
</span>
{barItems.map((ageGroup) => (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { useEffect, useState } from "react";

export function useBarGraphHeight() {
const [height, setHeight] = useState(0);
const [translateYAxisIndicator, setTranslateYAxisIndicator] = useState("");
useEffect(() => {
const handleResize = () => {
const newBarGraphHeight = getBarGraphHeight(window.innerHeight);
setHeight(newBarGraphHeight);
setTranslateYAxisIndicator(getTranslateYAxisIndicator(newBarGraphHeight));
};

// Create a new ResizeObserver
const resizeObserver = new ResizeObserver(handleResize);

// Observe when document.body resizes
resizeObserver.observe(document.body);

// Cleanup function to remove the observer when component unmounts
return () => {
resizeObserver.disconnect();
};
}, []);

return { height, translateYAxisIndicator };
}

function getBarGraphHeight(innerHeight: number) {
if (innerHeight < 600) {
return 20;
}

if (innerHeight < 650) {
return 40;
}

if (innerHeight < 700) {
return 60;
}

return 90;
}

function getTranslateYAxisIndicator(height: number) {
switch (height) {
case 20:
return "-translate-y-[1.125rem]";
case 40:
return "-translate-y-3";
case 60:
return "-translate-y-2";
default:
return "-translate-y-0.5";
}
}
4 changes: 3 additions & 1 deletion src/components/router/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export const Router: React.FC = () => {
className={`${isFilterViewVisible && "bg-white rounded-t-lg sm:bg-transparent"}`}
>
<div
className={`${treeId ? "hidden" : "block sm:hidden max-h-[calc(100svh-150px)] overflow-y-auto"}`}
className={
treeId ? "hidden" : "block sm:hidden max-h-[calc(100svh-150px)]"
}
>
{isFilterViewVisible && <Filter />}
</div>
Expand Down

0 comments on commit 4e4e5ba

Please sign in to comment.