use-toggle-expend.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { useEffect, useState } from 'react'
  2. type Params = {
  3. ref: React.RefObject<HTMLDivElement>
  4. hasFooter?: boolean
  5. isInNode?: boolean
  6. }
  7. const useToggleExpend = ({ ref, hasFooter = true, isInNode }: Params) => {
  8. const [isExpand, setIsExpand] = useState(false)
  9. const [wrapHeight, setWrapHeight] = useState(ref.current?.clientHeight)
  10. const editorExpandHeight = isExpand ? wrapHeight! - (hasFooter ? 56 : 29) : 0
  11. useEffect(() => {
  12. setWrapHeight(ref.current?.clientHeight)
  13. // eslint-disable-next-line react-hooks/exhaustive-deps
  14. }, [isExpand])
  15. const wrapClassName = (() => {
  16. if (!isExpand)
  17. return ''
  18. if (isInNode)
  19. return 'fixed z-10 right-[9px] top-[166px] bottom-[8px] w-[419px] p-4 bg-white rounded-xl'
  20. return 'absolute z-10 left-4 right-6 top-[52px] bottom-0 pb-4 bg-white'
  21. })()
  22. const wrapStyle = isExpand ? { boxShadow: '0px 0px 12px -4px rgba(16, 24, 40, 0.05), 0px -3px 6px -2px rgba(16, 24, 40, 0.03)' } : {}
  23. return {
  24. wrapClassName,
  25. wrapStyle,
  26. editorExpandHeight,
  27. isExpand,
  28. setIsExpand,
  29. }
  30. }
  31. export default useToggleExpend