|
| 1 | +import { useCallback, useEffect, useRef, useState } from 'react'; |
| 2 | + |
| 3 | +const DEFAULT_COLLAPSE_TIME = 300; |
| 4 | + |
| 5 | +export interface CollapsibleParameters { |
| 6 | + defaultIsOpen?: boolean; |
| 7 | + onChange?: (isOpen: boolean) => void; |
| 8 | + animateOnCollapseEffect?: boolean; |
| 9 | + collapseTime?: number; |
| 10 | +} |
| 11 | + |
| 12 | +const useCollapse = <Element extends HTMLElement>({ |
| 13 | + defaultIsOpen = true, |
| 14 | + onChange, |
| 15 | + animateOnCollapseEffect = true, |
| 16 | + collapseTime = DEFAULT_COLLAPSE_TIME |
| 17 | +}: CollapsibleParameters) => { |
| 18 | + const [collapsed, setCollapsed] = useState(!defaultIsOpen); |
| 19 | + const [originalHeight, setOriginalHeight] = useState(0); |
| 20 | + const collapsibleRef = useRef<Element>(null); |
| 21 | + |
| 22 | + const setCollapsedStyles = (height: number, isCollapsed: boolean) => { |
| 23 | + const { style } = collapsibleRef.current!; |
| 24 | + const heightString = isCollapsed ? '0' : `${height}px`; |
| 25 | + |
| 26 | + style.height = heightString; |
| 27 | + |
| 28 | + style.paddingTop = isCollapsed ? '0' : ''; |
| 29 | + style.paddingBottom = isCollapsed ? '0' : ''; |
| 30 | + style.marginTop = isCollapsed ? '0' : ''; |
| 31 | + style.marginBottom = isCollapsed ? '0' : ''; |
| 32 | + }; |
| 33 | + |
| 34 | + const calculateHeight = useCallback(() => { |
| 35 | + const { scrollHeight } = collapsibleRef.current!; |
| 36 | + |
| 37 | + const newHeight = scrollHeight ?? 0; |
| 38 | + setOriginalHeight(newHeight); |
| 39 | + }, []); |
| 40 | + |
| 41 | + // Sets initial height values |
| 42 | + useEffect(() => { |
| 43 | + if (collapsibleRef.current) { |
| 44 | + collapsibleRef.current.style.transition = `height ${collapseTime}ms ease-out, padding ${collapseTime}ms ease-out, margin ${collapseTime}ms ease-out`; |
| 45 | + collapsibleRef.current.style.overflow = 'hidden'; |
| 46 | + } |
| 47 | + |
| 48 | + calculateHeight(); |
| 49 | + }, [calculateHeight, collapseTime]); |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + // Note: There's a small bug that causes the resize not to correctly reduce the collapsible's |
| 53 | + // height when the screen is enlargened. The reason is because the scroll height does not |
| 54 | + // change because the height value is already set. The screen does not break though, the only |
| 55 | + // problem is that the collapsible element has more height than it should but it works. |
| 56 | + window.addEventListener('resize', calculateHeight); |
| 57 | + return () => { |
| 58 | + window.removeEventListener('resize', calculateHeight); |
| 59 | + }; |
| 60 | + }, [calculateHeight]); |
| 61 | + |
| 62 | + // Changes height values when height or collapsible state changes |
| 63 | + useEffect(() => { |
| 64 | + setCollapsedStyles(originalHeight, collapsed); |
| 65 | + }, [originalHeight, collapsed]); |
| 66 | + |
| 67 | + const handleCollapse = () => { |
| 68 | + setCollapsed(!collapsed); |
| 69 | + setCollapsedStyles(originalHeight, !collapsed); |
| 70 | + |
| 71 | + if (animateOnCollapseEffect) { |
| 72 | + setTimeout(() => { |
| 73 | + onChange?.(collapsed); |
| 74 | + }, collapseTime); |
| 75 | + } else { |
| 76 | + onChange?.(collapsed); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + return { handleCollapse, collapsibleRef, collapsed }; |
| 81 | +}; |
| 82 | + |
| 83 | +export default useCollapse; |
0 commit comments