-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make bar graph height dynamic based on viewport height (#886)
Signed-off-by: Raphael Arce <[email protected]>
- Loading branch information
1 parent
05b342b
commit 4e4e5ba
Showing
3 changed files
with
65 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/components/filter/age-range-slider/hooks/use-bar-graph-height.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters