Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from ali-h-kudeir/feature/dynamic-accordion-max…
…-height adjust accordion's max-height based on dynamic content height
- Loading branch information
Showing
3 changed files
with
43 additions
and
4 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,30 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
const useDynamicMaxHeight = () => { | ||
const ref = useRef<HTMLDivElement>(null); | ||
const [contentHeight, setContentHeight] = useState<number | null>(0); | ||
|
||
useEffect(() => { | ||
const element = ref.current; | ||
if (!element) return; | ||
|
||
const updateMaxHeight = () => { | ||
const height = element.scrollHeight; | ||
setContentHeight(height); | ||
}; | ||
|
||
updateMaxHeight(); | ||
|
||
// Re-calculate max height when the content changes | ||
const observer = new MutationObserver(updateMaxHeight); | ||
observer.observe(element, { childList: true, subtree: true }); | ||
|
||
return () => { | ||
observer.disconnect(); | ||
}; | ||
}, []); | ||
|
||
return { ref, contentHeight }; | ||
}; | ||
|
||
export default useDynamicMaxHeight; |