-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Dynamic inputs for grade distribution comparison
- Loading branch information
Showing
3 changed files
with
190 additions
and
71 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
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
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,46 @@ | ||
import { useCallback, useState } from "react"; | ||
|
||
const RECHART_CERTESIAN_AXIS_TICK_VALUE_SELECTOR = `.recharts-cartesian-axis-tick-value[orientation="left"], | ||
.recharts-cartesian-axis-tick-value[orientation="right"]`; | ||
|
||
const useDynamicYAxisWidth = (tickMargin = 12) => { | ||
const [width, setWidth] = useState<number>(); | ||
|
||
const handleRef = useCallback( | ||
(ref: unknown | null) => { | ||
// @ts-expect-error - LineChart cannot be typed | ||
if (!ref?.container) return; | ||
|
||
// @ts-expect-error - LineChart cannot be typed | ||
const tickValueElements = ref.container.querySelectorAll( | ||
RECHART_CERTESIAN_AXIS_TICK_VALUE_SELECTOR | ||
); | ||
|
||
const highestWidth = [...tickValueElements] | ||
.map((el) => { | ||
const boundingRect = el.getBoundingClientRect(); | ||
|
||
if (!boundingRect?.width) return 0; | ||
|
||
return boundingRect.width; | ||
}) | ||
.reduce((accumulator, value) => { | ||
if (accumulator < value) { | ||
return value; | ||
} | ||
|
||
return accumulator; | ||
}, 0); | ||
|
||
setWidth(highestWidth + tickMargin); | ||
}, | ||
[tickMargin] | ||
); | ||
|
||
return { | ||
width, | ||
handleRef, | ||
}; | ||
}; | ||
|
||
export default useDynamicYAxisWidth; |