{"version":3,"file":"use-expandable.mjs","names":[],"sources":["../../../../src/code-block/hooks/use-expandable.ts"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\n\nimport { useEffect, useLayoutEffect, useRef, useState } from 'react';\n\n/**\n * A hook to manage the expandable state of a content container.\n * @param maxHeight - The maximum height of the content container.\n * @param dependency - A dependency to watch for changes.\n * @returns An object containing the container height, whether the content is overflowing, and the expandable state.\n */\nexport const useExpandable = (\n  maxHeight: number | undefined,\n  dependency: unknown,\n) => {\n  const ref = useRef<HTMLDivElement>(null);\n\n  const [containerHeight, setContainerHeight] = useState(`${maxHeight}px`);\n  const [isExpanded, setIsExpanded] = useState(maxHeight === undefined);\n  const [hasOverflowingContent, setHasOverflowingContent] = useState(false);\n\n  useEffect(() => {\n    if ((ref.current?.scrollHeight ?? 0) <= (ref.current?.clientHeight ?? 0)) {\n      setIsExpanded(true);\n    } else {\n      setIsExpanded(false);\n    }\n  }, []);\n\n  useLayoutEffect(() => {\n    if (maxHeight === undefined) {\n      return;\n    }\n\n    if ((ref.current?.clientHeight ?? 0) >= (ref.current?.scrollHeight ?? 0)) {\n      setContainerHeight(`fit-content`);\n      setHasOverflowingContent(false);\n    } else {\n      setContainerHeight(`${maxHeight}px`);\n      setHasOverflowingContent(true);\n    }\n  }, [maxHeight, dependency]);\n\n  const toggleExpand = () => {\n    if (isExpanded) {\n      setContainerHeight(`${maxHeight}px`);\n      setIsExpanded(false);\n    } else {\n      setContainerHeight(`fit-content`);\n      setIsExpanded(true);\n    }\n  };\n\n  return {\n    containerHeight,\n    hasOverflowingContent,\n    isExpanded,\n    ref,\n    toggleExpand,\n  };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAa,iBACX,WACA,eACG;CACH,MAAM,MAAM,OAAuB,IAAI;CAEvC,MAAM,CAAC,iBAAiB,sBAAsB,SAAS,GAAG,UAAU,GAAG;CACvE,MAAM,CAAC,YAAY,iBAAiB,SAAS,cAAc,KAAA,CAAS;CACpE,MAAM,CAAC,uBAAuB,4BAA4B,SAAS,KAAK;CAExE,gBAAgB;;EACd,MAAA,yBAAA,eAAK,IAAI,aAAA,QAAA,iBAAA,KAAA,IAAA,KAAA,IAAA,aAAS,kBAAA,QAAA,0BAAA,KAAA,IAAA,wBAAgB,QAAA,yBAAA,gBAAO,IAAI,aAAA,QAAA,kBAAA,KAAA,IAAA,KAAA,IAAA,cAAS,kBAAA,QAAA,0BAAA,KAAA,IAAA,wBAAgB,IACpE,cAAc,IAAI;OAElB,cAAc,KAAK;CAEvB,GAAG,CAAC,CAAC;CAEL,sBAAsB;;EACpB,IAAI,cAAc,KAAA,GAChB;EAGF,MAAA,0BAAA,gBAAK,IAAI,aAAA,QAAA,kBAAA,KAAA,IAAA,KAAA,IAAA,cAAS,kBAAA,QAAA,2BAAA,KAAA,IAAA,yBAAgB,QAAA,0BAAA,gBAAO,IAAI,aAAA,QAAA,kBAAA,KAAA,IAAA,KAAA,IAAA,cAAS,kBAAA,QAAA,2BAAA,KAAA,IAAA,yBAAgB,IAAI;GACxE,mBAAmB,aAAa;GAChC,yBAAyB,KAAK;EAChC,OAAO;GACL,mBAAmB,GAAG,UAAU,GAAG;GACnC,yBAAyB,IAAI;EAC/B;CACF,GAAG,CAAC,WAAW,UAAU,CAAC;CAE1B,MAAM,qBAAqB;EACzB,IAAI,YAAY;GACd,mBAAmB,GAAG,UAAU,GAAG;GACnC,cAAc,KAAK;EACrB,OAAO;GACL,mBAAmB,aAAa;GAChC,cAAc,IAAI;EACpB;CACF;CAEA,OAAO;EACL;EACA;EACA;EACA;EACA;CACF;AACF"}